-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPAUtils.cs
98 lines (92 loc) · 4.27 KB
/
PAUtils.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
using ColossalFramework.UI;
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
using UnityEngine;
namespace PropAnarchy {
public static class PAUtils {
public static Func<S, T> CreateGetter<S, T>(FieldInfo field) {
string methodName = field.ReflectedType.FullName + ".get_" + field.Name;
DynamicMethod setterMethod = new DynamicMethod(methodName, typeof(T), new Type[1] { typeof(S) }, true);
ILGenerator gen = setterMethod.GetILGenerator();
if (field.IsStatic) {
gen.Emit(OpCodes.Ldsfld, field);
} else {
gen.Emit(OpCodes.Ldarg_0);
gen.Emit(OpCodes.Ldfld, field);
}
gen.Emit(OpCodes.Ret);
return (Func<S, T>)setterMethod.CreateDelegate(typeof(Func<S, T>));
}
public static Action<S, T> CreateSetter<S, T>(FieldInfo field) {
string methodName = field.ReflectedType.FullName + ".set_" + field.Name;
DynamicMethod setterMethod = new DynamicMethod(methodName, null, new Type[2] { typeof(S), typeof(T) }, true);
ILGenerator gen = setterMethod.GetILGenerator();
if (field.IsStatic) {
gen.Emit(OpCodes.Ldarg_1);
gen.Emit(OpCodes.Stsfld, field);
} else {
gen.Emit(OpCodes.Ldarg_0);
gen.Emit(OpCodes.Ldarg_1);
gen.Emit(OpCodes.Stfld, field);
}
gen.Emit(OpCodes.Ret);
return (Action<S, T>)setterMethod.CreateDelegate(typeof(Action<S, T>));
}
internal static UITextureAtlas CreateTextureAtlas(string atlasName, string path, string[] spriteNames, int maxSpriteSize) {
Texture2D texture2D = new Texture2D(maxSpriteSize, maxSpriteSize, TextureFormat.ARGB32, false);
Texture2D[] textures = new Texture2D[spriteNames.Length];
for (int i = 0; i < spriteNames.Length; i++) {
textures[i] = LoadTextureFromAssembly(path + spriteNames[i] + ".png");
}
Rect[] regions = texture2D.PackTextures(textures, 2, maxSpriteSize);
UITextureAtlas uITextureAtlas = ScriptableObject.CreateInstance<UITextureAtlas>();
Material material = UnityEngine.Object.Instantiate(UIView.GetAView().defaultAtlas.material);
material.mainTexture = texture2D;
uITextureAtlas.material = material;
uITextureAtlas.name = atlasName;
for (int j = 0; j < spriteNames.Length; j++) {
UITextureAtlas.SpriteInfo item = new UITextureAtlas.SpriteInfo() {
name = spriteNames[j],
texture = textures[j],
region = regions[j]
};
uITextureAtlas.AddSprite(item);
}
return uITextureAtlas;
}
internal static Texture2D LoadTextureFromAssembly(string filename) {
Texture2D texture = null;
try {
Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream(filename);
byte[] array = new byte[s.Length];
s.Read(array, 0, array.Length);
texture = new Texture2D(2, 2);
texture.LoadImage(array);
} catch {
PAModule.PALog($"Error loading: {filename}");
throw;
}
return texture;
}
internal static UITextureAtlas GetAtlas(string name) {
UITextureAtlas[] atlases = Resources.FindObjectsOfTypeAll(typeof(UITextureAtlas)) as UITextureAtlas[];
for (int i = 0; i < atlases.Length; i++) {
if (atlases[i].name == name)
return atlases[i];
}
return UIView.GetAView().defaultAtlas;
}
internal static IEnumerable<CodeInstruction> DebugPatchOutput(IEnumerable<CodeInstruction> instructions, MethodBase method) {
PAModule.PALog("---- " + method.Name + " ----");
foreach (var code in instructions) {
PAModule.PALog(code.ToString());
yield return code;
}
PAModule.PALog("------------------------------------------");
}
}
}