forked from bloodypenguin/ImprovedPublicTransport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImprovedPublicTransportMod.cs
226 lines (193 loc) · 9.67 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
225
226
using ColossalFramework;
using ColossalFramework.UI;
using ICities;
using System;
using System.Reflection;
using Harmony;
using ImprovedPublicTransport2.Detour;
using ImprovedPublicTransport2.Detour.Vehicles;
using ImprovedPublicTransport2.HarmonyPatches;
using ImprovedPublicTransport2.OptionsFramework.Extensions;
using ImprovedPublicTransport2.RedirectionFramework;
using UnityEngine;
using Utils = ImprovedPublicTransport2.Util.Utils;
namespace ImprovedPublicTransport2
{
public class ImprovedPublicTransportMod : LoadingExtensionBase, IUserMod
{
public static bool inGame = false;
private GameObject _iptGameObject;
private GameObject _worldInfoPanel;
private readonly string version = "5.0.0";
private HarmonyInstance HarmonyInstance;
public string Name => $"Improved Public Transport 2 [r{version}]";
public string Description => Localization.Get("MOD_DESCRIPTION");
public void OnSettingsUI(UIHelperBase helper)
{
helper.AddOptionsGroup<Settings>(Localization.Get);
}
public override void OnCreated(ILoading loading)
{
base.OnCreated(loading);
HarmonyInstance = HarmonyInstance.Create("github.com/bloodypenguin/ImprovedPublicTransport");
}
public override void OnLevelLoaded(LoadMode mode)
{
base.OnLevelLoaded(mode);
if (mode != LoadMode.LoadGame && mode != LoadMode.NewGame && mode != LoadMode.NewGameFromScenario)
{
return;
}
inGame = true;
try
{
Utils.Log((object) $"Begin init version: {version}");
this.ReleaseUnusedCitizenUnits();
UIView objectOfType = UnityEngine.Object.FindObjectOfType<UIView>();
if ((UnityEngine.Object) objectOfType != (UnityEngine.Object) null)
{
this._iptGameObject = new GameObject("IptGameObject");
this._iptGameObject.transform.parent = objectOfType.transform;
this._iptGameObject.AddComponent<SimHelper>();
this._iptGameObject.AddComponent<LineWatcher>();
this._worldInfoPanel = new GameObject("PublicTransportStopWorldInfoPanel");
this._worldInfoPanel.transform.parent = objectOfType.transform;
this._worldInfoPanel.AddComponent<PublicTransportStopWorldInfoPanel>();
CachedNodeData.Init();
Patch(typeof(DepotAI), nameof(DepotAI.StartTransfer), DepotAIPatch.GetPrefix(), null);
Patch(typeof(NetManager), nameof(NetManager.ReleaseNode), null,
typeof(NetManagerPatch).GetMethod(nameof(NetManagerPatch.ReleaseNode))); //TODO: think, whether it should be pre or post
CachedVehicleData.Init();
Patch(typeof(VehicleManager), "ReleaseWaterSource", null,
typeof(VehicleManagerPatch).GetMethod(nameof(VehicleManagerPatch.ReleaseWaterSource))); //TODO: think, whether it should be pre or post
Redirector<BusAIDetour>.Deploy();
PatchCountPassengers(typeof(BusAI));
Redirector<TrolleybusAIDetour>.Deploy();
PatchCountPassengers(typeof(TrolleybusAI));
Redirector<PassengerTrainAIDetour>.Deploy();
PatchCountPassengers(typeof(PassengerTrainAI));
Redirector<PassengerShipAIDetour>.Deploy();
PatchCountPassengers(typeof(PassengerShipAI));
Redirector<PassengerPlaneAIDetour>.Deploy();
PatchCountPassengers(typeof(PassengerPlaneAI));
Redirector<PassengerFerryAIDetour>.Deploy();
PatchCountPassengers(typeof(PassengerFerryAI));
Redirector<PassengerBlimpAIDetour>.Deploy();
PatchCountPassengers(typeof(PassengerBlimpAI));
Redirector<PassengerHelicopterAIDetour>.Deploy();
PatchCountPassengers(typeof(PassengerHelicopterAI));
Redirector<TramAIDetour>.Deploy();
PatchCountPassengers(typeof(TramAI));
Redirector<CommonBuildingAIReverseDetour>.Deploy();
Redirector<PublicTransportStopButtonDetour>.Deploy();
Redirector<PublicTransportVehicleButtonDetour>.Deploy();
Redirector<PublicTransportWorldInfoPanelDetour>.Deploy();
BuildingExtension.Init();
LineWatcher.instance.Init();
CachedTransportLineData.Init();
Redirector<TransportLineDetour>.Deploy();
VehiclePrefabs.Init();
SerializableDataExtension.instance.Loaded = true;
LocaleModifier.Init();
this._iptGameObject.AddComponent<VehicleEditor>();
this._iptGameObject.AddComponent<PanelExtenderLine>();
this._iptGameObject.AddComponent<PanelExtenderVehicle>();
this._iptGameObject.AddComponent<PanelExtenderCityService>();
Utils.Log((object) "Loading done!");
}
else
Utils.LogError((object) "UIView not found, aborting!");
}
catch (Exception ex)
{
Utils.LogError((object) ("Error during initialization, IPT disables itself." + System.Environment.NewLine + "Please try again without any other mod." + System.Environment.NewLine + "Please upload your log file and post the link here if that didn't help:" + System.Environment.NewLine + "http://steamcommunity.com/workshop/filedetails/discussion/424106600/615086038663282271/" + System.Environment.NewLine + ex.Message + System.Environment.NewLine + (object) ex.InnerException + System.Environment.NewLine + ex.StackTrace));
this.Deinit();
}
}
public override void OnLevelUnloading()
{
base.OnLevelUnloading();
if (!inGame)
return;
inGame = false;
this.Deinit();
Utils.Log((object) ("Unloading done!" + System.Environment.NewLine));
}
private void PatchCountPassengers(Type type)
{
Patch(type, "UnloadPassengers",
typeof(UnloadPassengersPatch).GetMethod(nameof(UnloadPassengersPatch.UnloadPassengersPre), BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static),
typeof(UnloadPassengersPatch).GetMethod(nameof(UnloadPassengersPatch.UnloadPassengersPost), BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static)
);
Patch(type, "LoadPassengers",
typeof(LoadPassengersPatch).GetMethod(nameof(LoadPassengersPatch.LoadPassengersPre), BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static),
typeof(LoadPassengersPatch).GetMethod(nameof(LoadPassengersPatch.LoadPassengersPost), BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static)
);
}
private void Patch(Type type, string methodName, MethodInfo prePatch, MethodInfo postPatch)
{
try
{
var bindingFlags = BindingFlags.NonPublic | BindingFlags.Public;
bindingFlags |= BindingFlags.Instance;
HarmonyInstance.Patch(type.GetMethod(methodName,
bindingFlags),
prefix: prePatch == null ? null : new HarmonyMethod(prePatch),
postfix: postPatch == null ? null : new HarmonyMethod(postPatch));
}
catch (Exception e)
{
UnityEngine.Debug.LogError("Improved Public Tranport 2: Failed to patch method " + methodName);
UnityEngine.Debug.LogException(e);
}
}
private void ReleaseUnusedCitizenUnits()
{
Utils.Log((object) "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 && (int) citizenUnit.m_building == 0 && ((int) citizenUnit.m_vehicle == 0 && (int) citizenUnit.m_goods == 0))
{
++num;
instance.m_units.m_buffer[index] = new CitizenUnit();
instance.m_units.ReleaseItem((uint) index);
Utils.LogToTxt((object) string.Format("CitizenUnit #{0} - Flags: {1} - Citizens: #{2} | #{3} | #{4} | #{5} | #{6}", (object) index, (object) citizenUnit.m_flags, (object) citizenUnit.m_citizen0, (object) citizenUnit.m_citizen1, (object) citizenUnit.m_citizen2, (object) citizenUnit.m_citizen3, (object) citizenUnit.m_citizen4));
}
}
Utils.Log((object) ("Cleared " + (object) num + " unused citizen units."));
}
private void Deinit()
{
HarmonyInstance?.UnpatchAll();
Redirector<TramAIDetour>.Revert();
Redirector<PassengerTrainAIDetour>.Revert();
Redirector<PassengerShipAIDetour>.Revert();
Redirector<PassengerPlaneAIDetour>.Revert();
Redirector<PassengerFerryAIDetour>.Revert();
Redirector<PassengerBlimpAIDetour>.Revert();
Redirector<BusAIDetour>.Revert();
Redirector<CommonBuildingAIReverseDetour>.Revert();
Redirector<PublicTransportStopButtonDetour>.Revert();
Redirector<PublicTransportVehicleButtonDetour>.Revert();
Redirector<PublicTransportWorldInfoPanelDetour>.Revert();
Redirector<TrolleybusAIDetour>.Revert();
Redirector<PassengerHelicopterAIDetour>.Revert();
Redirector<TransportLineDetour>.Revert();
CachedTransportLineData.Deinit();
BuildingExtension.Deinit();
CachedNodeData.Deinit();
CachedVehicleData.Deinit();
VehiclePrefabs.Deinit();
SerializableDataExtension.instance.Loaded = false;
LocaleModifier.Deinit();
if ((UnityEngine.Object) this._iptGameObject != (UnityEngine.Object) null)
UnityEngine.Object.Destroy((UnityEngine.Object) this._iptGameObject);
if (!((UnityEngine.Object) this._worldInfoPanel != (UnityEngine.Object) null))
return;
UnityEngine.Object.Destroy((UnityEngine.Object) this._worldInfoPanel);
}
}
}