forked from Katalyst6/CSL.NetworkExtensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModInitializer.cs
More file actions
240 lines (200 loc) · 6.92 KB
/
ModInitializer.cs
File metadata and controls
240 lines (200 loc) · 6.92 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using ColossalFramework;
using ColossalFramework.Globalization;
using NetworkExtensions.Framework;
using UnityEngine;
#if DEBUG
using Debug = NetworkExtensions.Framework.Debug;
#endif
namespace NetworkExtensions
{
public partial class ModInitializer : MonoBehaviour
{
private bool _doneWithInit = false;
private bool _initializedNetworkInfo = false;
private static bool s_initializedLocalization = false; //Only one localization throughout the application
public NetCollection NewRoads { get; set; }
public delegate void InitializationCompletedEventHandler(object sender, EventArgs e);
public event InitializationCompletedEventHandler InitializationCompleted;
void Start()
{
Loading.QueueAction(TextureManager.instance.FindAndLoadAllTextures);
}
void Awake()
{
DontDestroyOnLoad(this);
}
void Update()
{
if (!_doneWithInit)
{
Initialize();
}
}
#if DEBUG
private int _frameNb = 0;
private bool _versionShown = false;
#endif
private void Initialize()
{
#if DEBUG
if (_frameNb++ < 20) // Giving some time for the UI to refresh **NB. Putting this constant higher than 100 causes wierd behavior**
{
return;
}
if (!_versionShown)
{
var version = typeof(ModInitializer).Assembly.GetName().Version;
Debug.Log(string.Format("NExt: Version {0}", version));
_versionShown = true;
}
#endif
if (!s_initializedLocalization)
{
if (ValidateLocalizationPrerequisites())
{
InitializeLocalization();
s_initializedLocalization = true;
}
}
if (!_initializedNetworkInfo)
{
if (ValidateNetworkPrerequisites(NewRoads))
{
InitializeNewRoads(NewRoads);
_initializedNetworkInfo = true;
}
}
_doneWithInit =
_initializedNetworkInfo &&
s_initializedLocalization;
if (_doneWithInit)
{
if (InitializationCompleted != null)
{
InitializationCompleted(this, EventArgs.Empty);
}
}
}
private static bool ValidateLocalizationPrerequisites()
{
var localeManager = SingletonLite<LocaleManager>.instance;
if (localeManager == null)
{
return false;
}
var localeField = typeof(LocaleManager).GetFieldByName("m_Locale");
if (localeField == null)
{
return false;
}
var locale = (Locale)localeField.GetValue(localeManager);
if (locale == null)
{
return false;
}
return true;
}
private static bool ValidateNetworkPrerequisites(NetCollection newRoads)
{
var roadObject = GameObject.Find(Mod.ROAD_NETCOLLECTION);
if (roadObject == null)
{
return false;
}
var netColl = FindObjectsOfType<NetCollection>();
if (netColl == null || !netColl.Any())
{
return false;
}
var roadCollFound = false;
foreach (var col in netColl)
{
if (col.name == Mod.ROAD_NETCOLLECTION)
{
roadCollFound = true;
}
}
if (!roadCollFound)
{
return false;
}
if (newRoads == null)
{
return false;
}
return true;
}
private static void InitializeLocalization()
{
Loading.QueueAction(() =>
{
try
{
Debug.Log("NExt: Localization");
var localeManager = SingletonLite<LocaleManager>.instance;
var localeField = typeof(LocaleManager).GetFieldByName("m_Locale");
var locale = (Locale)localeField.GetValue(localeManager);
// For future extensions
//locale.AddCategoryLocalizedString();
foreach (var builder in Mod.NetInfoBuilders)
{
builder.DefineLocalization(locale);
}
}
catch (Exception ex)
{
Debug.Log("NExt: Crashed-Localization");
Debug.Log("NExt: " + ex.Message);
Debug.Log("NExt: " + ex.ToString());
}
});
}
private static void InitializeNewRoads(NetCollection newRoads)
{
Loading.QueueAction(() =>
{
Debug.Log("NExt: Build NetworkExtensions");
// Builders -----------------------------------------------------------------------
var newInfos = new List<NetInfo>();
foreach (var builder in Mod.NetInfoBuilders)
{
try
{
newInfos.AddRange(builder.Build());
}
catch (Exception ex)
{
Debug.Log(string.Format("NExt: Crashed-Network builders {0}", builder));
Debug.Log("NExt: " + ex.Message);
Debug.Log("NExt: " + ex.ToString());
}
}
if (newInfos.Count > 0)
{
newRoads.m_prefabs = newInfos.ToArray();
PrefabCollection<NetInfo>.InitializePrefabs(newRoads.name, newRoads.m_prefabs, new string[] { });
PrefabCollection<NetInfo>.BindPrefabs();
}
// Modifiers ----------------------------------------------------------------------
foreach (var modifier in Mod.NetInfoModifiers)
{
try
{
modifier.ModifyExistingNetInfo();
}
catch (Exception ex)
{
Debug.Log(string.Format("NExt: Crashed-Network modifiers {0}", modifier));
Debug.Log("NExt: " + ex.Message);
Debug.Log("NExt: " + ex.ToString());
}
}
Debug.Log("NExt: Finished installing components");
});
}
}
}