-
Notifications
You must be signed in to change notification settings - Fork 16
/
Mod.cs
241 lines (212 loc) · 8.64 KB
/
Mod.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
using ColossalFramework.Plugins;
using ICities;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
using EightyOne.Areas;
using EightyOne.Attributes;
using EightyOne.ResourceManagers;
using EightyOne.Terrain;
using EightyOne.Zones;
using System.ComponentModel;
namespace EightyOne
{
public class Mod : IUserMod
{
public string Description
{
get
{
return "Unlocks 81 tiles";
}
}
public string Name
{
get
{
return "81 Tile Unlock";
}
}
}
public class ModLoad : LoadingExtensionBase
{
public static Unlimiter unlimiter;
public static GameObject gm;
public override void OnLevelLoaded(LoadMode mode)
{
gm = new GameObject("unlimiter");
unlimiter = gm.AddComponent<Unlimiter>();
unlimiter.EnableHooks();
}
public override void OnLevelUnloading()
{
unlimiter.DisableHooks();
GameObject.Destroy(gm);
}
}
public class Unlimiter : MonoBehaviour
{
private static Dictionary<MethodInfo, RedirectCallsState> redirects;
public static bool IsEnabled;
private static BindingFlags allFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly;
public void EnableHooks()
{
if (IsEnabled)
{
return;
}
IsEnabled = true;
FakeWaterManager.Init();
FakeDistrictManager.Init();
FakeDistrictTool.Init();
FakeImmaterialResourceManager.Init();
FakeTerrainManager.Init();
FakeZoneManager.Init();
FakeNetManager.Init();
FakeZoneTool.Init();
FakeElectricityManager.Init();
var toReplace = new Type[]
{
typeof(GameAreaManager), typeof(FakeGameAreaManager),
typeof(GameAreaInfoPanel), typeof(FakeGameAreaInfoPanel),
typeof(GameAreaTool), typeof(FakeGameAreaTool),
typeof(NetManager), typeof(FakeNetManager),
typeof(ZoneManager), typeof(FakeZoneManager),
typeof(BuildingTool ), typeof(FakeBuildingTool),
typeof(Building ), typeof(FakeBuilding),
typeof(ZoneTool), typeof(FakeZoneTool),
//typeof(PrivateBuildingAI), typeof(FakePrivateBuildingAI),
typeof(TerrainManager), typeof(FakeTerrainManager),
typeof(ElectricityManager), typeof(FakeElectricityManager),
typeof(WaterManager), typeof(FakeWaterManager),
typeof(ImmaterialResourceManager), typeof(FakeImmaterialResourceManager),
typeof(DistrictManager), typeof(FakeDistrictManager),
typeof(DistrictTool), typeof(FakeDistrictTool),
typeof(NaturalResourceManager), typeof(FakeNatualResourceManager),
};
redirects = new Dictionary<MethodInfo, RedirectCallsState>();
for (int i = 0; i < toReplace.Length; i += 2)
{
var from = toReplace[i];
var to = toReplace[i + 1];
foreach (var method in to.GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic))
{
if (method.GetCustomAttributes(typeof(ReplaceMethodAttribute), false).Length == 1)
{
AddRedirect(from, method);
}
}
}
FakeGameAreaManager.Init();
}
private void AddRedirect(Type type1, MethodInfo method)
{
var parameters = method.GetParameters();
Type[] types;
if (parameters.Length > 0 && parameters[0].ParameterType == type1)
types = parameters.Skip(1).Select(p => p.ParameterType).ToArray();
else
types = parameters.Select(p => p.ParameterType).ToArray();
var originalMethod = type1.GetMethod(method.Name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static, null, types, null);
if (originalMethod == null)
{
Debug.Log("Cannot find " + method.Name);
}
redirects.Add(originalMethod, RedirectionHelper.RedirectCalls(originalMethod, method));
}
public void DisableHooks()
{
if (!IsEnabled)
{
return;
}
IsEnabled = false;
foreach (var kvp in redirects)
{
RedirectionHelper.RevertRedirect(kvp.Key, kvp.Value);
}
FakeImmaterialResourceManager.OnDestroy();
FakeDistrictManager.OnDestroy();
FakeWaterManager.OnDestroy();
FakeElectricityManager.OnDestroy();
FakeGameAreaManager.OnDestroy();
}
public void Update()
{
if ((Input.GetKey(KeyCode.RightShift) || Input.GetKey(KeyCode.LeftShift)) && Input.GetKeyDown(KeyCode.U))
{
FakeGameAreaManager.UnlockAll();
}
}
public static void CopyArray(IList newArray, object em, string propertyName)
{
var oldArray = (IList)em.GetType().GetField(propertyName, allFlags).GetValue(em);
for (var i = 0; i < newArray.Count; i += 1)
{
newArray[i] = oldArray[i];
}
}
public static void CopyArrayBack(IList newArray, object em, string propertyName)
{
var oldArray = (IList)em.GetType().GetField(propertyName, allFlags).GetValue(em);
for (var i = 0; i < newArray.Count; i += 1)
{
oldArray[i] = newArray[i];
}
}
public static void CopyStructArray(IList newArray, object em, string propertyName)
{
var oldArray = (IList)em.GetType().GetField(propertyName, allFlags).GetValue(em);
var fields = GetFieldsFromStruct(newArray[0], oldArray[0]);
for (var i = 0; i < newArray.Count; i += 1)
{
newArray[i] = CopyStruct((object)newArray[0], oldArray[i], fields);
}
}
public static void CopyStructArrayBack(IList newArray, object em, string propertyName)
{
var oldArray = (IList)em.GetType().GetField(propertyName, allFlags).GetValue(em);
var fields = GetFieldsFromStruct( oldArray[0],newArray[0]);
for (var i = 0; i < newArray.Count; i += 1)
{
oldArray[i] = CopyStruct((object)oldArray[i], newArray[i], fields);
}
}
public static Dictionary<FieldInfo, FieldInfo> GetFieldsFromStruct(object newArray, object oldArray)
{
var fields = new Dictionary<FieldInfo, FieldInfo>();
foreach (var f in oldArray.GetType().GetFields(allFlags))
{
fields.Add(newArray.GetType().GetField(f.Name, allFlags), f);
}
return fields;
}
public static void SetPropertyValue<T>(ref T result, object obj, string propName)
{
result = (T)obj.GetType().GetField(propName, allFlags).GetValue(obj);
}
public static void SetPropertyValueBack(object result, object obj, string propName)
{
obj.GetType().GetField(propName, allFlags).SetValue(obj,result);
}
public static object CopyStruct(object newObj, object original, Dictionary<FieldInfo, FieldInfo> fields)
{
foreach (var field in fields)
{
if (field.Key.FieldType != field.Value.FieldType){
if (field.Key.FieldType == typeof(byte))
{
var oo = Mathf.Clamp((ushort)field.Value.GetValue(original),0,255);
field.Key.SetValue(newObj,(byte)oo);
continue;
}
}
field.Key.SetValue(newObj, field.Value.GetValue(original));
}
return newObj;
}
}
}