-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLoader.cs
217 lines (197 loc) · 7.42 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
using CitiesHarmony.API;
using ColossalFramework;
using ColossalFramework.Plugins;
using ColossalFramework.UI;
using ICities;
using RealGasStation.CustomAI;
using RealGasStation.Patch;
using RealGasStation.UI;
using RealGasStation.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using UnityEngine;
namespace RealGasStation
{
public class Loader : LoadingExtensionBase
{
public static LoadMode CurrentLoadMode;
public static bool DetourInited = false;
public static bool HarmonyDetourInited = false;
public static bool HarmonyDetourFailed = true;
public static bool isGuiRunning = false;
public static bool isRealCityRunning = false;
public static bool isRealCityV10 = false;
public static PlayerBuildingButton PBButton;
public static string m_atlasName = "RealGasStation";
public static bool m_atlasLoaded;
public override void OnCreated(ILoading loading)
{
base.OnCreated(loading);
}
public override void OnLevelLoaded(LoadMode mode)
{
base.OnLevelLoaded(mode);
Loader.CurrentLoadMode = mode;
if (RealGasStation.IsEnabled)
{
if (mode == LoadMode.LoadGame || mode == LoadMode.NewGame)
{
DebugLog.LogToFileOnly("OnLevelLoaded");
InitDetour();
HarmonyInitDetour();
SetupGui();
if (mode == LoadMode.NewGame)
{
DebugLog.LogToFileOnly("New Game");
}
}
}
}
public override void OnLevelUnloading()
{
base.OnLevelUnloading();
if (Loader.CurrentLoadMode == LoadMode.LoadGame || Loader.CurrentLoadMode == LoadMode.NewGame)
{
if (RealGasStation.IsEnabled)
{
RevertDetour();
HarmonyRevertDetour();
RealGasStationThreading.isFirstTime = true;
if (Loader.isGuiRunning)
{
RemoveGui();
}
}
}
}
public override void OnReleased()
{
base.OnReleased();
}
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, "Icon/RealGasStation.png"), m_atlasName);
if (m_atlasLoaded)
{
var spriteSuccess = true;
spriteSuccess = SpriteUtilities.AddSpriteToAtlas(new Rect(new Vector2(1, 1), new Vector2(191, 191)), "Pic", m_atlasName)
&& spriteSuccess;
if (!spriteSuccess) DebugLog.LogToFileOnly("Some sprites haven't been loaded. This is abnormal; you should probably report this to the mod creator.");
}
else DebugLog.LogToFileOnly("The texture atlas (provides custom icons) has not loaded. All icons have reverted to text prompts.");
}
public static void SetupGui()
{
LoadSprites();
if (m_atlasLoaded)
{
SetupPlayerBuildingButton();
Loader.isGuiRunning = true;
}
}
public static void RemoveGui()
{
Loader.isGuiRunning = false;
if (PBButton != null)
{
UnityEngine.Object.Destroy(PBButton);
Loader.PBButton = null;
}
}
public static void SetupPlayerBuildingButton()
{
var playerbuildingInfo = UIView.Find<UIPanel>("(Library) CityServiceWorldInfoPanel");
if (PBButton == null)
{
PBButton = (playerbuildingInfo.AddUIComponent(typeof(PlayerBuildingButton)) as PlayerBuildingButton);
}
PBButton.width = 30f;
PBButton.height = 40f;
PBButton.relativePosition = new Vector3(playerbuildingInfo.size.x - PBButton.width - 90, playerbuildingInfo.size.y - PBButton.height);
PBButton.Show();
}
public void InitDetour()
{
isRealCityRunning = CheckRealCityIsLoaded();
if (isRealCityRunning)
{
Version RealCity_Version = Assembly.Load("RealCity").GetName().Version;
if (RealCity_Version > new Version(10, 0, 0, 0))
{
DebugLog.LogToFileOnly($"Detect RealCity V10, version = {RealCity_Version}");
isRealCityV10 = true;
}
else
{
isRealCityV10 = false;
}
}
DetourInited = true;
}
public void RevertDetour()
{
DetourInited = false;
}
public void HarmonyInitDetour()
{
if (HarmonyHelper.IsHarmonyInstalled)
{
if (!HarmonyDetourInited)
{
DebugLog.LogToFileOnly("Init harmony detours");
HarmonyDetours.Apply();
HarmonyDetourInited = true;
}
}
}
public void HarmonyRevertDetour()
{
if (HarmonyHelper.IsHarmonyInstalled)
{
if (HarmonyDetourInited)
{
DebugLog.LogToFileOnly("Revert harmony detours");
HarmonyDetours.DeApply();
HarmonyDetourInited = false;
HarmonyDetourFailed = true;
}
}
}
private bool Check3rdPartyModLoaded(string namespaceStr, bool printAll = false)
{
bool thirdPartyModLoaded = false;
var loadingWrapperLoadingExtensionsField = typeof(LoadingWrapper).GetField("m_LoadingExtensions", BindingFlags.NonPublic | BindingFlags.Instance);
List<ILoadingExtension> loadingExtensions = (List<ILoadingExtension>)loadingWrapperLoadingExtensionsField.GetValue(Singleton<LoadingManager>.instance.m_LoadingWrapper);
if (loadingExtensions != null)
{
foreach (ILoadingExtension extension in loadingExtensions)
{
if (printAll)
DebugLog.LogToFileOnly($"Detected extension: {extension.GetType().Name} in namespace {extension.GetType().Namespace}");
if (extension.GetType().Namespace == null)
continue;
var nsStr = extension.GetType().Namespace.ToString();
if (namespaceStr.Equals(nsStr))
{
DebugLog.LogToFileOnly($"The mod '{namespaceStr}' has been detected.");
thirdPartyModLoaded = true;
break;
}
}
}
else
{
DebugLog.LogToFileOnly("Could not get loading extensions");
}
return thirdPartyModLoaded;
}
private bool CheckRealCityIsLoaded()
{
return this.Check3rdPartyModLoaded("RealCity", false);
}
}
}