-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoader.cs
More file actions
160 lines (143 loc) · 5.15 KB
/
Loader.cs
File metadata and controls
160 lines (143 loc) · 5.15 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
using ICities;
using ColossalFramework;
using System.Reflection;
using System.Collections.Generic;
using MoreOutsideInteraction.Util;
using MoreOutsideInteraction.CustomAI;
using CitiesHarmony.API;
using System;
namespace MoreOutsideInteraction
{
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 isRealCityRunning = false;
public static bool isRealCityV10 = false;
public override void OnCreated(ILoading loading)
{
base.OnCreated(loading);
}
public override void OnLevelLoaded(LoadMode mode)
{
base.OnLevelLoaded(mode);
Loader.CurrentLoadMode = mode;
if (MoreOutsideInteraction.IsEnabled)
{
if (mode == LoadMode.LoadGame || mode == LoadMode.NewGame)
{
DebugLog.LogToFileOnly("OnLevelLoaded");
InitDetour();
for (int i = 0; i < 65536; i ++)
{
CustomPlayerBuildingAI.canReturn[i] = false;
}
HarmonyInitDetour();
MoreOutsideInteraction.LoadSetting();
if (mode == LoadMode.NewGame)
{
DebugLog.LogToFileOnly("New Game");
}
}
}
}
public override void OnLevelUnloading()
{
base.OnLevelUnloading();
if (MoreOutsideInteraction.IsEnabled)
{
if (CurrentLoadMode == LoadMode.LoadGame || CurrentLoadMode == LoadMode.NewGame)
{
RevertDetour();
HarmonyRevertDetour();
}
}
MoreOutsideInteraction.SaveSetting();
}
public override void OnReleased()
{
base.OnReleased();
}
private bool CheckRealCityIsLoaded()
{
return this.Check3rdPartyModLoaded("RealCity", 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;
}
}
}
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;
}
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;
}
}
}