-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathShortcuts.cs
118 lines (110 loc) · 5.64 KB
/
Shortcuts.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using SALT.Extensions;
using System;
using System.IO;
using System.Reflection;
using UnityEngine;
namespace SALT
{
internal static partial class Shortcuts
{
public static AssetBundle LoadAssetbundle(Mod mod, string folder, string name)
{
folder = folder.EndsWith(".") ? folder : $"{folder}.";
string path = folder + name;
Assembly assembly = mod.Assembly;
Console.Console.LogWarning("Attempting to load asset bundle at path: " + path);
Stream stream = assembly.GetManifestResourceStream(mod.EntryType, path);
if (stream == null)
throw new Exception("AssetBundle " + name + " was not found");
AssetBundle assetBundle = AssetBundle.LoadFromStream(stream);
if (assetBundle == null)
Console.Console.LogError("AssetBundle " + name + " was loaded incorrectly!");
return assetBundle;
}
public static AssetBundle LoadAssetbundle(Mod mod, string name)
{
string path = name;
Assembly assembly = mod.Assembly;
Console.Console.LogWarning("Attempting to load asset bundle at path: " + path);
Stream stream = assembly.GetManifestResourceStream(mod.EntryType, path);
if (stream == null)
throw new Exception("AssetBundle " + name + " was not found");
AssetBundle assetBundle = AssetBundle.LoadFromStream(stream);
if (assetBundle == null)
Console.Console.LogError("AssetBundle " + name + " was loaded incorrectly!");
return assetBundle;
}
public static Texture2D CreateTexture2DFromImage(Mod mod, string folder, string name)
{
folder = folder.EndsWith(".") ? folder : $"{folder}.";
Assembly assembly = mod.Assembly;
string manifestResourceName = folder + name;
string realName = name.RemoveExtension();
Stream manifestResourceStream = assembly.GetManifestResourceStream(mod.EntryType, manifestResourceName);
Texture2D texture2D = new Texture2D(4, 4);
byte[] numArray = new byte[manifestResourceStream.Length];
manifestResourceStream.Read(numArray, 0, (int)manifestResourceStream.Length);
texture2D.LoadImage(numArray);
Console.Console.Log(realName + " vs " + manifestResourceName);
texture2D.name = realName;
manifestResourceStream.Close();
return texture2D;
}
public static Texture2D CreateTexture2DFromImage(Mod mod, string name)
{
Assembly assembly = mod.Assembly;
string manifestResourceName = name;
string realName = name.RemoveExtension();
Stream manifestResourceStream = assembly.GetManifestResourceStream(mod.EntryType, manifestResourceName);
Texture2D texture2D = new Texture2D(4, 4);
byte[] numArray = new byte[manifestResourceStream.Length];
manifestResourceStream.Read(numArray, 0, (int)manifestResourceStream.Length);
texture2D.LoadImage(numArray);
texture2D.name = realName;
manifestResourceStream.Close();
return texture2D;
}
public static Sprite CreateSpriteFromImage(Mod mod, string folder, string name)
{
folder = folder.EndsWith(".") ? folder : $"{folder}.";
string realName = name.RemoveExtension();
Texture2D texture2D = CreateTexture2DFromImage(mod, folder, name);
Sprite sprite = Sprite.Create(texture2D, new Rect(0.0f, 0.0f, (float)texture2D.width, (float)texture2D.height), new Vector2(0.5f, 0.5f));
sprite.name = realName;
return sprite;
}
public static Sprite CreateSpriteFromImage(Mod mod, string name)
{
string realName = name.RemoveExtension();
Texture2D texture2D = CreateTexture2DFromImage(mod, name);
Sprite sprite = Sprite.Create(texture2D, new Rect(0.0f, 0.0f, (float)texture2D.width, (float)texture2D.height), new Vector2(0.5f, 0.5f));
sprite.name = realName;
return sprite;
}
public static SpriteTexturePack CreateTexture2DAndSpriteFromImage(Mod mod, string folder, string name)
{
folder = folder.EndsWith(".") ? folder : $"{folder}.";
string realName = name.RemoveExtension();
Texture2D texture2D = CreateTexture2DFromImage(mod, folder, name);
Sprite sprite = Sprite.Create(texture2D, new Rect(0.0f, 0.0f, (float)texture2D.width, (float)texture2D.height), new Vector2(0.5f, 0.5f));
sprite.name = realName;
SpriteTexturePack spriteTexturePack = ScriptableObject.CreateInstance<SpriteTexturePack>();
spriteTexturePack.name = realName;
spriteTexturePack.SetTexture(texture2D);
spriteTexturePack.SetSprite(sprite);
return spriteTexturePack;
}
public static SpriteTexturePack CreateTexture2DAndSpriteFromImage(Mod mod, string name)
{
string realName = name.RemoveExtension();
Texture2D texture2D = CreateTexture2DFromImage(mod, name);
Sprite sprite = Sprite.Create(texture2D, new Rect(0.0f, 0.0f, (float)texture2D.width, (float)texture2D.height), new Vector2(0.5f, 0.5f));
sprite.name = realName;
SpriteTexturePack spriteTexturePack = ScriptableObject.CreateInstance<SpriteTexturePack>();
spriteTexturePack.name = realName;
spriteTexturePack.SetTexture(texture2D);
spriteTexturePack.SetSprite(sprite);
return spriteTexturePack;
}
}
}