forked from pcfantasy/AdvancedRoadTools
/
Loader.cs
299 lines (280 loc) · 12.1 KB
/
Loader.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
using AdvancedRoadTools.NewData;
using AdvancedRoadTools.Tools;
using AdvancedRoadTools.UI;
using AdvancedRoadTools.Util;
using ColossalFramework.Plugins;
using ColossalFramework.UI;
using ICities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using UnityEngine;
using Object = UnityEngine.Object;
namespace AdvancedRoadTools
{
public class Loader : LoadingExtensionBase
{
//public static bool DetourInited = false;
public static UIView parentGuiView;
public static LoadMode CurrentLoadMode;
public static bool isGuiRunning = false;
public static ThreeRoundButton threeRoundButton;
public static OneRoundButton oneRoundButton;
public static YRoadButton yRoadButton;
public static SmoothButton smoothButton;
public static FixButton fixButton;
public static string m_atlasName = "AdvancedRoadTools";
public static bool m_atlasLoaded;
public static bool HarmonyDetourInited = false;
public static bool HarmonyDetourFailed = true;
public class Detour
{
public MethodInfo OriginalMethod;
public MethodInfo CustomMethod;
public RedirectCallsState Redirect;
public Detour(MethodInfo originalMethod, MethodInfo customMethod)
{
this.OriginalMethod = originalMethod;
this.CustomMethod = customMethod;
this.Redirect = RedirectionHelper.RedirectCalls(originalMethod, customMethod);
}
}
public static List<Detour> Detours { get; set; }
public static bool DetourInited = false;
public override void OnCreated(ILoading loading)
{
Detours = new List<Detour>();
base.OnCreated(loading);
}
public override void OnLevelLoaded(LoadMode mode)
{
base.OnLevelLoaded(mode);
CurrentLoadMode = mode;
if (AdvancedRoadTools.IsEnabled)
{
if (mode == LoadMode.LoadGame || mode == LoadMode.NewGame || mode == LoadMode.NewMap || mode == LoadMode.LoadMap || mode == LoadMode.NewAsset || mode == LoadMode.LoadAsset)
{
OptionUI.LoadSetting();
SetupGui();
SetupTools();
#if DEBUG
HarmonyInitDetour();
InitDetour();
#endif
DebugLog.LogToFileOnly("OnLevelLoaded");
}
}
}
public override void OnLevelUnloading()
{
base.OnLevelUnloading();
if (CurrentLoadMode == LoadMode.LoadGame || CurrentLoadMode == LoadMode.NewGame || CurrentLoadMode == LoadMode.LoadMap || CurrentLoadMode == LoadMode.NewMap || CurrentLoadMode == LoadMode.LoadAsset || CurrentLoadMode == LoadMode.NewAsset)
{
if (AdvancedRoadTools.IsEnabled)
{
HarmonyRevertDetour();
RevertDetour();
Threading.isFirstTime = true;
if (isGuiRunning)
{
RemoveGui();
}
}
}
}
public static void DataInit()
{
for (int i = 0; i < 262144; i++)
{
MainDataStore.segmentModifiedMinOffset[i] = 0f;
}
MainDataStore.SaveData = new byte[1048576];
}
private static void LoadSprites()
{
if (SpriteUtilities.GetAtlas(m_atlasName) != null) return;
var modPath = PluginManager.instance.FindPluginInfo(Assembly.GetExecutingAssembly()).modPath;
m_atlasLoaded = SpriteUtilities.InitialiseAtlas(Path.Combine(modPath, "Resources/Icon.png"), m_atlasName);
if (m_atlasLoaded)
{
var spriteSuccess = true;
spriteSuccess = SpriteUtilities.AddSpriteToAtlas(new Rect(new Vector2(2, 2), new Vector2(30, 30)), "3Round", m_atlasName)
&& SpriteUtilities.AddSpriteToAtlas(new Rect(new Vector2(34, 2), new Vector2(30, 30)), "3Round_S", m_atlasName)
&& SpriteUtilities.AddSpriteToAtlas(new Rect(new Vector2(66, 2), new Vector2(30, 30)), "1Round", m_atlasName)
&& SpriteUtilities.AddSpriteToAtlas(new Rect(new Vector2(98, 2), new Vector2(30, 30)), "1Round_S", m_atlasName)
&& SpriteUtilities.AddSpriteToAtlas(new Rect(new Vector2(130, 2), new Vector2(30, 30)), "Smooth", m_atlasName)
&& SpriteUtilities.AddSpriteToAtlas(new Rect(new Vector2(162, 2), new Vector2(30, 30)), "Smooth_S", m_atlasName)
&& SpriteUtilities.AddSpriteToAtlas(new Rect(new Vector2(194, 2), new Vector2(30, 30)), "Fix", m_atlasName)
&& SpriteUtilities.AddSpriteToAtlas(new Rect(new Vector2(226, 2), new Vector2(30, 30)), "Fix_S", m_atlasName)
&& SpriteUtilities.AddSpriteToAtlas(new Rect(new Vector2(258, 2), new Vector2(30, 30)), "YRoad", m_atlasName)
&& SpriteUtilities.AddSpriteToAtlas(new Rect(new Vector2(290, 2), new Vector2(30, 30)), "YRoad_S", m_atlasName);
if (!spriteSuccess) DebugLog.LogToFileOnly("Error: Some sprites haven't been loaded. This is abnormal; you should probably report this to the mod creator.");
}
else DebugLog.LogToFileOnly("Error: The texture atlas (provides custom icons) has not loaded. All icons have reverted to text prompts.");
}
public static void SetupTools()
{
if (AdvancedTools.instance == null)
{
ToolController toolController = GameObject.FindObjectOfType<ToolController>();
AdvancedTools.instance = toolController.gameObject.AddComponent<AdvancedTools>();
AdvancedTools.instance.enabled = false;
}
AdvancedTools.InitData();
}
public void HarmonyInitDetour()
{
if (!HarmonyDetourInited)
{
DebugLog.LogToFileOnly("Init harmony detours");
HarmonyDetours.Apply();
HarmonyDetourInited = true;
}
}
public void HarmonyRevertDetour()
{
if (HarmonyDetourInited)
{
DebugLog.LogToFileOnly("Revert harmony detours");
HarmonyDetours.DeApply();
HarmonyDetourInited = false;
HarmonyDetourFailed = true;
}
}
public void InitDetour()
{
if (!DetourInited)
{
DebugLog.LogToFileOnly("Init detours");
bool detourFailed = false;
//1
//private void RefreshJunctionData(ushort nodeID, NetInfo info, uint instanceIndex)
/*DebugLog.LogToFileOnly("Detour NetNode::RefreshJunctionData calls");
try
{
Detours.Add(new Detour(typeof(NetNode).GetMethod("RefreshJunctionData", BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(ushort), typeof(NetInfo), typeof(uint) }, null),
typeof(CustomNetNode).GetMethod("RefreshJunctionData", BindingFlags.Public | BindingFlags.Static, null, new Type[] { typeof(NetNode).MakeByRefType(), typeof(ushort), typeof(NetInfo), typeof(uint) }, null)));
}
catch (Exception)
{
DebugLog.LogToFileOnly("Could not detour NetNode::RefreshJunctionData");
//detourFailed = true;
}*/
//2
//public static void CalculateCorner(ref NetSegment segment, ushort segmentID, bool heightOffset, bool start, bool leftSide, out Vector3 cornerPos, out Vector3 cornerDirection, out bool smooth)
DebugLog.LogToFileOnly("Detour NetSegment::CalculateCorner calls");
try
{
Detours.Add(new Detour(typeof(NetSegment).GetMethod("CalculateCorner", BindingFlags.Public | BindingFlags.Instance, null, new Type[] { typeof(ushort), typeof(bool), typeof(bool), typeof(bool), typeof(Vector3).MakeByRefType(), typeof(Vector3).MakeByRefType(), typeof(bool).MakeByRefType() }, null),
typeof(CustomNetSegment).GetMethod("CalculateCorner", BindingFlags.Public | BindingFlags.Static, null, new Type[] { typeof(NetSegment).MakeByRefType(), typeof(ushort), typeof(bool), typeof(bool), typeof(bool), typeof(Vector3).MakeByRefType(), typeof(Vector3).MakeByRefType(), typeof(bool).MakeByRefType() }, null)));
}
catch (Exception)
{
DebugLog.LogToFileOnly("Could not detour NetSegment::CalculateCorner");
//detourFailed = true;
}
if (detourFailed)
{
DebugLog.LogToFileOnly("Detours failed");
}
else
{
DebugLog.LogToFileOnly("Detours successful");
}
DetourInited = true;
}
}
public void RevertDetour()
{
if (DetourInited)
{
DebugLog.LogToFileOnly("Revert detours");
Detours.Reverse();
foreach (Detour d in Detours)
{
RedirectionHelper.RevertRedirect(d.OriginalMethod, d.Redirect);
}
DetourInited = false;
Detours.Clear();
DebugLog.LogToFileOnly("Reverting detours finished.");
}
}
public static void SetupGui()
{
LoadSprites();
if (m_atlasLoaded)
{
parentGuiView = null;
parentGuiView = UIView.GetAView();
SetupThreeRoundButton();
SetupOneRoundButton();
SetupYRoadButton();
SetupSmoothButton();
#if DEBUG
SetupFixButton();
#endif
isGuiRunning = true;
}
}
public static void SetupThreeRoundButton()
{
if (threeRoundButton == null)
{
threeRoundButton = (parentGuiView.AddUIComponent(typeof(ThreeRoundButton)) as ThreeRoundButton);
}
}
public static void SetupFixButton()
{
if (fixButton == null)
{
fixButton = (parentGuiView.AddUIComponent(typeof(FixButton)) as FixButton);
}
}
public static void SetupOneRoundButton()
{
if (oneRoundButton == null)
{
oneRoundButton = (parentGuiView.AddUIComponent(typeof(OneRoundButton)) as OneRoundButton);
}
}
public static void SetupYRoadButton()
{
if (yRoadButton == null)
{
yRoadButton = (parentGuiView.AddUIComponent(typeof(YRoadButton)) as YRoadButton);
}
}
public static void SetupSmoothButton()
{
if (smoothButton == null)
{
smoothButton = (parentGuiView.AddUIComponent(typeof(SmoothButton)) as SmoothButton);
}
}
public static void RemoveGui()
{
isGuiRunning = false;
if (parentGuiView != null)
{
parentGuiView = null;
UnityEngine.Object.Destroy(threeRoundButton);
UnityEngine.Object.Destroy(oneRoundButton);
UnityEngine.Object.Destroy(yRoadButton);
UnityEngine.Object.Destroy(smoothButton);
#if DEBUG
UnityEngine.Object.Destroy(fixButton);
#endif
threeRoundButton = null;
oneRoundButton = null;
yRoadButton = null;
smoothButton = null;
#if DEBUG
fixButton = null;
#endif
}
}
}
}