-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathLoadingExtension.cs
202 lines (187 loc) · 7.39 KB
/
LoadingExtension.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
using ColossalFramework;
using ICities;
using MetroOverhaul.Detours;
using MetroOverhaul.Redirection;
using MetroOverhaul.NEXT;
using System;
using System.Collections.Generic;
using Harmony;
using ColossalFramework.UI;
using MetroOverhaul.OptionsFramework;
using MetroOverhaul.UI;
using UnityEngine;
using MetroOverhaul.Extensions;
using System.Reflection;
using System.Linq;
namespace MetroOverhaul
{
public class LoadingExtension : LoadingExtensionBase
{
public static Initializer Container;
private static AssetsUpdater _updater;
private HarmonyInstance _harmony;
public static bool Done { get; private set; } // Only one Assets installation throughout the application
private static readonly Queue<Action> LateBuildUpQueue = new Queue<Action>();
#if DEBUG
private bool indebug = true;
#else
private bool indebug = false;
#endif
private LoadMode _cachedMode;
public override void OnCreated(ILoading loading)
{
base.OnCreated(loading);
if (OptionsWrapper<Options>.Options.improvedMetroTrainAi)
{
Redirector<MetroTrainAIDetour>.Deploy();
}
_updater = null;
LateBuildUpQueue.Clear();
InstallAssets();
if (Container == null)
{
Container = new GameObject("MetroOverhaul").AddComponent<Initializer>();
Container.AppMode = loading.currentMode;
}
_harmony = HarmonyInstance.Create("andreharv.Skylines-MetroOverhaulMod");
if (OptionsWrapper<Options>.Options.ingameTrainMetroConverter)
{
PatchInitializePrefab.Apply(_harmony, ref Container);
}
if (loading.currentMode == AppMode.AssetEditor)
{
Redirector<RoadsGroupPanelDetour>.Deploy();
}
if (loading.currentMode == AppMode.Game)
{
Redirector<DepotAIDetour>.Deploy();
if (OptionsWrapper<Options>.Options.improvedPassengerTrainAi)
{
Redirector<PassengerTrainAIDetour>.Deploy();
}
}
}
public static void EnqueueLateBuildUpAction(Action action)
{
LateBuildUpQueue.Enqueue(action);
}
private static void InstallAssets()
{
if (Done) // Only one Assets installation throughout the application
{
return;
}
var path = Util.AssemblyPath;
foreach (var action in AssetManager.instance.CreateLoadingSequence(path))
{
var localAction = action;
Loading.QueueLoadingAction(() =>
{
try
{
localAction();
}
catch (Exception ex)
{
UnityEngine.Debug.LogException(ex);
}
});
}
Done = true;
}
public override void OnLevelLoaded(LoadMode mode)
{
base.OnLevelLoaded(mode);
bool isVanilla = OptionsWrapper<Options>.Options.ghostMode;
if (!isVanilla)
{
_cachedMode = mode;
while (LateBuildUpQueue.Count > 0)
{
try
{
LateBuildUpQueue.Dequeue().Invoke();
}
catch (Exception e)
{
UIView.library.ShowModal<ExceptionPanel>("ExceptionPanel").SetMessage("Enable asset in Content Manager!", e.Message, false);
}
}
if (_updater == null)
{
_updater = new AssetsUpdater();
_updater.UpdateExistingAssets(mode);
}
}
AssetsUpdater.UpdateBuildingsMetroPaths(mode, isVanilla);
if (!isVanilla)
{
if (mode == LoadMode.NewGame || mode == LoadMode.LoadGame || mode == LoadMode.NewGameFromScenario)
{
var gameObject = new GameObject("MetroOverhaulUISetup");
gameObject.AddComponent<UpgradeSetup>();
//#if DEBUG
// gameObject.AddComponent<StyleSelectionStationUI>();
//#else
// gameObject.AddComponent<StyleSelectionUI>();
//#endif
if (OptionsWrapper<Options>.Options.metroUi)
{
var metroStationCustomizerUI = (MetroStationCustomizerUI)UIView.GetAView().AddUIComponent(typeof(MetroStationCustomizerUI));
UIView.GetAView().AddUIComponent(typeof(MetroTrackCustomizerUI));
UIView.GetAView().AddUIComponent(typeof(MetroAboveGroundStationCustomizerUI));
PatchCreateBuilding.Apply(_harmony, ref metroStationCustomizerUI);
}
}
else
{
var gameObject = new GameObject("MetroOverhaulUISetup");
//gameObject.AddComponent<StyleSelectionStationUI>();
#if DEBUG
UIView.GetAView().AddUIComponent(typeof(MetroStationCustomizerUI));
UIView.GetAView().AddUIComponent(typeof(MetroTrackAssetCustomizerUI));
UIView.GetAView().AddUIComponent(typeof(MetroAboveGroundStationCustomizerUI));
#endif
}
//Container.DoSomething();
}
}
public override void OnLevelUnloading()
{
base.OnLevelUnloading();
if (!OptionsWrapper<Options>.Options.ghostMode)
{
//it appears, the game caches vanilla prefabs even when exiting to main menu, and stations won't load properly on reloading from main menu
//AssetsUpdater.UpdateBuildingsMetroPaths(_cachedMode, true);
var go = GameObject.Find("MetroOverhaulUISetup");
if (go != null)
{
GameObject.Destroy(go);
}
}
GameObject.Destroy(UIView.GetAView().FindUIComponent<MetroStationCustomizerUI>("MetroStationCustomizerUI"));
GameObject.Destroy(UIView.GetAView().FindUIComponent<MetroTrackCustomizerUI>("MetroTrackCustomizerUI"));
GameObject.Destroy(UIView.GetAView().FindUIComponent<MetroAboveGroundStationCustomizerUI>("MetroAboveGroundStationCustomizerUI"));
}
public override void OnReleased()
{
base.OnReleased();
if (OptionsWrapper<Options>.Options.ingameTrainMetroConverter)
PatchInitializePrefab.Revert(_harmony);
PatchCreateBuilding.Revert(_harmony);
if (!OptionsWrapper<Options>.Options.ghostMode)
{
_updater = null;
if (Container == null)
{
return;
}
UnityEngine.Object.Destroy(Container);
Container = null;
Redirector<RoadsGroupPanelDetour>.Revert();
Redirector<DepotAIDetour>.Revert();
Redirector<PassengerTrainAIDetour>.Revert();
}
}
}
}