forked from pcfantasy/RealConstruction
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoader.cs
More file actions
261 lines (237 loc) · 9.09 KB
/
Loader.cs
File metadata and controls
261 lines (237 loc) · 9.09 KB
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
using ColossalFramework;
using ColossalFramework.UI;
using ICities;
using RealConstruction.CustomAI;
using RealConstruction.UI;
using RealConstruction.Util;
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using System.IO;
using ColossalFramework.Plugins;
using CitiesHarmony.API;
using ColossalFramework.Globalization;
using UnityEngine.SocialPlatforms;
namespace RealConstruction
{
public class Loader : LoadingExtensionBase
{
public static LoadMode CurrentLoadMode;
public static bool HarmonyDetourInited = false;
public static bool HarmonyDetourFailed = true;
public static bool isGuiRunning = false;
public static bool isRealCityRunning = false;
public static PlayerBuildingButton PBMenuPanel;
public static UniqueFactoryButton UBMenuPanel;
public static WarehouseButton WBMenuPanel;
public static string m_atlasName = "RealConstruction";
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 (RealConstruction.IsEnabled)
{
if (mode == LoadMode.LoadGame || mode == LoadMode.NewGame)
{
DebugLog.LogToFileOnly("OnLevelLoaded");
HarmonyInitDetour();
SetupGui();
SetupLocalization();
isRealCityRunning = CheckRealCityIsLoaded();
RealConstruction.LoadSetting();
if (mode == LoadMode.NewGame)
{
DebugLog.LogToFileOnly("New Game");
}
}
}
}
public void DataInit()
{
for (int i = 0; i < 49152; i++)
{
MainDataStore.refreshCanNotConnectedBuildingIDCount[i] = 0;
MainDataStore.canNotConnectedBuildingIDCount[i] = 0;
for (int j = 0; j < 8; j++)
{
MainDataStore.canNotConnectedBuildingID[i, j] = 0;
}
}
}
public override void OnLevelUnloading()
{
base.OnLevelUnloading();
if (Loader.CurrentLoadMode == LoadMode.LoadGame || Loader.CurrentLoadMode == LoadMode.NewGame)
{
if (RealConstruction.IsEnabled)
{
HarmonyRevertDetour();
RealConstructionThreading.isFirstTime = true;
if (Loader.isGuiRunning)
{
RemoveGui();
}
}
}
//RealConstruction.SaveSetting();
}
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/RealConstruction.png"), m_atlasName);
if (m_atlasLoaded)
{
var spriteSuccess = true;
spriteSuccess = SpriteUtilities.AddSpriteToAtlas(new Rect(new Vector2(1, 1), new Vector2(151, 151)), "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();
SetupWareHouseButton();
SetupUniqueFactoryButton();
Loader.isGuiRunning = true;
}
}
public static void RemoveGui()
{
Loader.isGuiRunning = false;
if (PBMenuPanel != null)
{
UnityEngine.Object.Destroy(PBMenuPanel);
Loader.PBMenuPanel = null;
}
if (UBMenuPanel != null)
{
UnityEngine.Object.Destroy(UBMenuPanel);
Loader.UBMenuPanel = null;
}
if (WBMenuPanel != null)
{
UnityEngine.Object.Destroy(WBMenuPanel);
Loader.WBMenuPanel = null;
}
}
public static void SetupPlayerBuildingButton()
{
var playerBuildingInfo = UIView.Find<UIPanel>("(Library) CityServiceWorldInfoPanel");
if (PBMenuPanel == null)
{
PBMenuPanel = (playerBuildingInfo.AddUIComponent(typeof(PlayerBuildingButton)) as PlayerBuildingButton);
}
PBMenuPanel.Show();
}
public static void SetupUniqueFactoryButton()
{
var uniqueFactoryInfo = UIView.Find<UIPanel>("(Library) UniqueFactoryWorldInfoPanel");
if (UBMenuPanel == null)
{
UBMenuPanel = (uniqueFactoryInfo.AddUIComponent(typeof(UniqueFactoryButton)) as UniqueFactoryButton);
}
UBMenuPanel.Show();
}
public static void SetupWareHouseButton()
{
var wareHouseInfo = UIView.Find<UIPanel>("(Library) WarehouseWorldInfoPanel");
if (WBMenuPanel == null)
{
WBMenuPanel = (wareHouseInfo.AddUIComponent(typeof(WarehouseButton)) as WarehouseButton);
}
WBMenuPanel.Show();
}
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();
HarmonyDetourFailed = true;
HarmonyDetourInited = false;
}
}
}
private void SetupLocalization()
{
var delivery_construction_key = new Locale.Key
{
m_Identifier = "VEHICLE_STATUS_CARGOTRUCK_DELIVER",
m_Key = "124",
m_Index = 0
};
var delivery_operation_key = new Locale.Key
{
m_Identifier = "VEHICLE_STATUS_CARGOTRUCK_DELIVER",
m_Key = "125",
m_Index = 0
};
var loc = new Locale();
loc.AddLocalizedString(delivery_construction_key, Localization.Get("TRANSFER_CONSTRUCTION_RESOURCE_TO"));
loc.AddLocalizedString(delivery_operation_key, Localization.Get("TRANSFER_OPERATION_RESOURCE_TO"));
loc.appendOverride = true;
Locale.LocaleOverride = loc;
}
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);
}
}
}