-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathImprovedPublicTransportMod.cs
224 lines (198 loc) · 9.21 KB
/
ImprovedPublicTransportMod.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
using System;
using CitiesHarmony.API;
using ColossalFramework;
using ColossalFramework.UI;
using ICities;
using ImprovedPublicTransport2.HarmonyPatches.BuildingManagerPatches;
using ImprovedPublicTransport2.HarmonyPatches.DepotAIPatches;
using ImprovedPublicTransport2.HarmonyPatches.NetManagerPatches;
using ImprovedPublicTransport2.HarmonyPatches.PublicTransportLineVehicleSelectorPatches;
using ImprovedPublicTransport2.HarmonyPatches.TransportLinePatches;
using ImprovedPublicTransport2.HarmonyPatches.TransportManagerPatches;
using ImprovedPublicTransport2.HarmonyPatches.VehicleManagerPatches;
using ImprovedPublicTransport2.HarmonyPatches.XYZVehicleAIPatches;
using ImprovedPublicTransport2.OptionsFramework.Extensions;
using ImprovedPublicTransport2.RedirectionFramework;
using ImprovedPublicTransport2.Data;
using ImprovedPublicTransport2.HarmonyPatches.PublicTransportVehicleButtonPatches;
using ImprovedPublicTransport2.HarmonyPatches.PublicTransportWorldInfoPanelPatches;
using ImprovedPublicTransport2.ReverseDetours;
using ImprovedPublicTransport2.UI;
using ImprovedPublicTransport2.UI.PanelExtenders;
using UnityEngine;
using Object = UnityEngine.Object;
using Utils = ImprovedPublicTransport2.Util.Utils;
namespace ImprovedPublicTransport2
{
public class ImprovedPublicTransportMod : LoadingExtensionBase, IUserMod
{
public const string BaseModName = "Improved Public Transport 2";
public const string ShortModName = "IPT2";
public static bool InGame;
public static GameObject IptGameObject;
private GameObject _worldInfoPanel;
private const string Version = "7.0.2";
public string Name => $"{BaseModName} [r{Version}]";
public string Description => Localization.Get("MOD_DESCRIPTION");
public void OnSettingsUI(UIHelperBase helper)
{
helper.AddOptionsGroup<Settings.Settings>(Localization.Get);
}
public override void OnLevelLoaded(LoadMode mode)
{
base.OnLevelLoaded(mode);
if (!HarmonyHelper.IsHarmonyInstalled)
{
return;
}
if (mode != LoadMode.LoadGame && mode != LoadMode.NewGame && mode != LoadMode.NewGameFromScenario)
{
return;
}
InGame = true;
try
{
Utils.Log($"{ShortModName}: Begin init version: {Version}");
ReleaseUnusedCitizenUnits();
UIView objectOfType = Object.FindObjectOfType<UIView>();
if (objectOfType != null)
{
IptGameObject = new GameObject("IptGameObject");
IptGameObject.transform.parent = objectOfType.transform;
IptGameObject.AddComponent<SimHelper>();
IptGameObject.AddComponent<LineWatcher>();
_worldInfoPanel = new GameObject("PublicTransportStopWorldInfoPanel");
_worldInfoPanel.transform.parent = objectOfType.transform;
_worldInfoPanel.AddComponent<PublicTransportStopWorldInfoPanel>();
CachedNodeData.Init();
int maxVehicleCount;
if (Utils.IsModActive(1764208250)) // More Vehicles
{
Debug.LogWarning(
$"{ShortModName}: More Vehicles is enabled, applying compatibility workaround");
maxVehicleCount = ushort.MaxValue + 1;
}
else
{
Debug.Log($"{ShortModName}: More Vehicles is not enabled");
maxVehicleCount = VehicleManager.MAX_VEHICLE_COUNT;
}
CachedVehicleData.Init(maxVehicleCount);
LoadPassengersPatch.Apply();
UnloadPassengersPatch.Apply();
StartTransferPatch.Apply();
ReleaseNodePatch.Apply();
ReleaseWaterSourcePatch.Apply();
GetVehicleInfoPatch.Apply();
CheckTransportLineVehiclesPatch.Apply();
ClassMatchesPatch.Apply();
CanLeavePatch.Apply();
Redirector<CommonBuildingAIReverseDetour>.Deploy();
HarmonyPatches.PublicTransportStopButtonPatches.OnMouseDownPatch.Apply();
HarmonyPatches.PublicTransportVehicleButtonPatches.OnMouseDownPatch.Apply();
RefreshVehicleButtonsPatch.Apply();
UpdateStopButtonsPatch.Apply();
BuildingExtension.Init();
LineWatcher.instance.Init();
CachedTransportLineData.Init();
Redirector<TransportLineReverseDetour>.Deploy();
SimulationStepPatch.Apply();
GetLineVehiclePatch.Apply();
CanLeaveStopPatch.Apply();
VehiclePrefabs.Init();
SerializableDataExtension.instance.Loaded = true;
LocaleModifier.Init();
IptGameObject.AddComponent<VehicleEditor>();
IptGameObject.AddComponent<PanelExtenderLine>();
IptGameObject.AddComponent<PanelExtenderVehicle>();
IptGameObject.AddComponent<PanelExtenderCityService>();
Utils.Log("Loading done!");
}
else
Utils.LogError("UIView not found, aborting!");
}
catch (Exception ex)
{
Utils.LogError(
$"{ShortModName}: Error during initialization, IPT disables itself.{Environment.NewLine}" +
$"Please try again without any other mod.{Environment.NewLine}" +
$"Please upload your log file and post the link here if that didn't help:{Environment.NewLine}" +
$"http://steamcommunity.com/workshop/filedetails/discussion/424106600/615086038663282271/{Environment.NewLine}" +
$"{ex.Message}{Environment.NewLine}" +
$"{ex.InnerException}{Environment.NewLine}" +
$"{ex.StackTrace}");
Deinit();
}
}
public override void OnLevelUnloading()
{
base.OnLevelUnloading();
if (!HarmonyHelper.IsHarmonyInstalled)
{
return;
}
if (!InGame)
return;
InGame = false;
Deinit();
Utils.Log("Unloading done!" + Environment.NewLine);
}
private void ReleaseUnusedCitizenUnits()
{
Utils.Log("Find and clear unused citizen units.");
CitizenManager instance = Singleton<CitizenManager>.instance;
int num = 0;
for (int index = 0; index < instance.m_units.m_buffer.Length; ++index)
{
CitizenUnit citizenUnit = instance.m_units.m_buffer[index];
if (citizenUnit.m_flags != CitizenUnit.Flags.None && citizenUnit.m_building == 0 &&
(citizenUnit.m_vehicle == 0 && citizenUnit.m_goods == 0))
{
++num;
instance.m_units.m_buffer[index] = new CitizenUnit();
instance.m_units.ReleaseItem((uint)index);
Utils.LogToTxt(
$"CitizenUnit #{index} - Flags: {citizenUnit.m_flags} - Citizens: #{citizenUnit.m_citizen0} | #{citizenUnit.m_citizen1} | #{citizenUnit.m_citizen2} | #{citizenUnit.m_citizen3} | #{citizenUnit.m_citizen4}");
}
}
Utils.Log("Cleared " + num + " unused citizen units.");
}
private void Deinit()
{
LoadPassengersPatch.Undo();
UnloadPassengersPatch.Undo();
StartTransferPatch.Undo();
ReleaseNodePatch.Undo();
ReleaseWaterSourcePatch.Undo();
GetVehicleInfoPatch.Undo();
ClassMatchesPatch.Undo();
CheckTransportLineVehiclesPatch.Undo();
GetDepotLevelsPatch.Undo();
CanLeavePatch.Undo();
Redirector<CommonBuildingAIReverseDetour>.Revert();
HarmonyPatches.PublicTransportStopButtonPatches.OnMouseDownPatch.Undo();
HarmonyPatches.PublicTransportVehicleButtonPatches.OnMouseDownPatch.Undo();
RefreshVehicleButtonsPatch.Undo();
UpdateStopButtonsPatch.Undo();
Redirector<TransportLineReverseDetour>.Revert();
SimulationStepPatch.Undo();
GetLineVehiclePatch.Undo();
CanLeaveStopPatch.Undo();
CachedTransportLineData.Deinit();
BuildingExtension.Deinit();
CachedNodeData.Deinit();
CachedVehicleData.Deinit();
SerializableDataExtension.instance.Loaded = false;
LocaleModifier.Deinit();
if (IptGameObject != null)
Object.Destroy(IptGameObject);
if (!(_worldInfoPanel != null))
return;
Object.Destroy(_worldInfoPanel);
}
public void OnEnabled()
{
HarmonyHelper.EnsureHarmonyInstalled();
}
}
}