forked from klyte45/VehicleMasterControl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceVehiclesManager.cs
More file actions
345 lines (307 loc) · 14.2 KB
/
ServiceVehiclesManager.cs
File metadata and controls
345 lines (307 loc) · 14.2 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
using ColossalFramework;
using ColossalFramework.DataBinding;
using ColossalFramework.Globalization;
using ColossalFramework.UI;
using ICities;
using Klyte.Commons.Extensors;
using Klyte.ServiceVehiclesManager.i18n;
using Klyte.ServiceVehiclesManager.Utils;
using System;
using System.Linq;
using System.Reflection;
using UnityEngine;
[assembly: AssemblyVersion("1.2.1.*")]
namespace Klyte.ServiceVehiclesManager
{
public class ServiceVehiclesManagerMod : MonoBehaviour, IUserMod, ILoadingExtension
{
public static string minorVersion => majorVersion + "." + typeof(ServiceVehiclesManagerMod).Assembly.GetName().Version.Build;
public static string majorVersion => typeof(ServiceVehiclesManagerMod).Assembly.GetName().Version.Major + "." + typeof(ServiceVehiclesManagerMod).Assembly.GetName().Version.Minor;
public static string fullVersion => minorVersion + " r" + typeof(ServiceVehiclesManagerMod).Assembly.GetName().Version.Revision;
public static string version
{
get {
if (typeof(ServiceVehiclesManagerMod).Assembly.GetName().Version.Minor == 0 && typeof(ServiceVehiclesManagerMod).Assembly.GetName().Version.Build == 0)
{
return typeof(ServiceVehiclesManagerMod).Assembly.GetName().Version.Major.ToString();
}
if (typeof(ServiceVehiclesManagerMod).Assembly.GetName().Version.Build > 0)
{
return minorVersion;
}
else
{
return majorVersion;
}
}
}
public static ServiceVehiclesManagerMod instance;
private SavedBool m_debugMode;
public bool needShowPopup;
private static bool isLocaleLoaded = false;
public static bool LocaleLoaded => isLocaleLoaded;
private string currentSelectedConfigEditor => currentCityId;
private static bool m_isKlyteCommonsLoaded = false;
public static bool IsKlyteCommonsEnabled()
{
if (!m_isKlyteCommonsLoaded)
{
try
{
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
var assembly = (from a in assemblies
where a.GetType("Klyte.Commons.KlyteCommonsMod") != null
select a).SingleOrDefault();
if (assembly != null)
{
m_isKlyteCommonsLoaded = true;
}
}
catch { }
}
return m_isKlyteCommonsLoaded;
}
public static bool debugMode => instance.m_debugMode.value;
private SavedString currentSaveVersion => new SavedString("SVMSaveVersion", Settings.gameSettingsFile, "null", true);
private SavedInt currentLanguageId => new SavedInt("SVMLanguage", Settings.gameSettingsFile, 0, true);
internal SVMConfigWarehouse currentLoadedCityConfig => SVMConfigWarehouse.getConfig(currentCityId, currentCityName);
public static bool isCityLoaded => Singleton<SimulationManager>.instance.m_metaData != null;
private string currentCityId => isCityLoaded ? Singleton<SimulationManager>.instance.m_metaData.m_gameInstanceIdentifier : SVMConfigWarehouse.GLOBAL_CONFIG_INDEX;
private string currentCityName => isCityLoaded ? Singleton<SimulationManager>.instance.m_metaData.m_CityName : SVMConfigWarehouse.GLOBAL_CONFIG_INDEX;
private SVMConfigWarehouse currentConfigWarehouseEditor => SVMConfigWarehouse.getConfig(currentSelectedConfigEditor, currentCityName);
private string[] getOptionsForLoadConfig() => currentCityId == SVMConfigWarehouse.GLOBAL_CONFIG_INDEX ? new string[] { SVMConfigWarehouse.GLOBAL_CONFIG_INDEX } : new string[] { currentCityName, SVMConfigWarehouse.GLOBAL_CONFIG_INDEX };
public string Name => "Services Vehicles Manager " + version;
public string Description => "Extension for managing the service vehicles. Requires Klyte Commons.";
public void OnCreated(ILoading loading)
{
}
public ServiceVehiclesManagerMod()
{
Debug.LogWarningFormat("SVMv" + majorVersion + " LOADING SVM ");
SettingsFile svmSettings = new SettingsFile
{
fileName = SVMConfigWarehouse.CONFIG_FILENAME
};
Debug.LogWarningFormat("SVMv" + majorVersion + " SETTING FILES");
try
{
GameSettings.AddSettingsFile(svmSettings);
}
catch (Exception e)
{
SettingsFile tryLoad = GameSettings.FindSettingsFileByName(SVMConfigWarehouse.CONFIG_FILENAME);
if (tryLoad == null)
{
Debug.LogErrorFormat("SVMv" + majorVersion + " SETTING FILES FAIL!!! ");
Debug.LogError(e.Message);
Debug.LogError(e.StackTrace);
}
else
{
svmSettings = tryLoad;
}
}
m_debugMode = new SavedBool("SVMdebugMode", Settings.gameSettingsFile, typeof(ServiceVehiclesManagerMod).Assembly.GetName().Version.Major == 0, true);
if (m_debugMode.value)
Debug.LogWarningFormat("currentSaveVersion.value = {0}, fullVersion = {1}", currentSaveVersion.value, fullVersion);
if (currentSaveVersion.value != fullVersion)
{
needShowPopup = true;
}
LocaleManager.eventLocaleChanged += new LocaleManager.LocaleChangedHandler(autoLoadSVMLocale);
if (instance != null) { Destroy(instance); }
instance = this;
}
public void OnSettingsUI(UIHelperBase helperDefault)
{
UIHelperExtension helper = new UIHelperExtension((UIHelper)helperDefault);
void ev()
{
foreach (Transform child in helper.self.transform)
{
GameObject.Destroy(child?.gameObject);
}
helper.self.eventVisibilityChanged += delegate (UIComponent component, bool b)
{
if (b)
{
showVersionInfoPopup();
}
};
UIHelperExtension group9 = helper.AddGroupExtended(Locale.Get("SVM_BETAS_EXTRA_INFO"));
group9.AddDropdownLocalized("SVM_MOD_LANG", SVMLocaleUtils.getLanguageIndex(), currentLanguageId.value, delegate (int idx)
{
currentLanguageId.value = idx;
loadSVMLocale(true);
});
group9.AddCheckbox(Locale.Get("SVM_DEBUG_MODE"), m_debugMode.value, delegate (bool val) { m_debugMode.value = val; });
group9.AddLabel("Version: " + fullVersion);
group9.AddLabel(Locale.Get("SVM_ORIGINAL_TLM_VERSION") + " " + string.Join(".", ResourceLoader.loadResourceString("TLMVersion.txt").Split(".".ToCharArray()).Take(3).ToArray()));
group9.AddButton(Locale.Get("SVM_RELEASE_NOTES"), delegate ()
{
showVersionInfoPopup(true);
});
//UILabel lblResult = null;
//group9.AddButton("TEST SUBTYPE", delegate ()
// {
// var subclasses = SVMUtils.GetSubtypesRecursive(typeof(BasicBuildingAIOverrides<,>), typeof(BasicBuildingAIOverrides<,>));
// lblResult.text = string.Format("GetOverride pré - subclasses:\r\n\t{0}", string.Join("\r\n\t", subclasses?.Select(x => x.ToString())?.ToArray() ?? new string[0]));
// });
//lblResult = group9.AddLabel("XXX1");
//string testString = "";
//for (int i = 0; i < 360; i++)
//{
// var angle = Vector2.zero.GetAngleToPoint(new Vector2(Mathf.Sin(i * Mathf.Deg2Rad), Mathf.Cos(i * Mathf.Deg2Rad)));
// testString += $"{i:n0}° => {angle:n1} ({Mathf.Sin(i * Mathf.Deg2Rad):n3}, {Mathf.Cos(i * Mathf.Deg2Rad):n3})\n";
//}
//group9.AddLabel("TST:\n" + testString);
SVMUtils.doLog("End Loading Options");
}
if (IsKlyteCommonsEnabled())
{
loadSVMLocale(false);
ev();
}
else
{
eventOnLoadLocaleEnd = null;
eventOnLoadLocaleEnd += ev;
}
}
public bool showVersionInfoPopup(bool force = false)
{
if (needShowPopup || force)
{
try
{
UIComponent uIComponent = UIView.library.ShowModal("ExceptionPanel");
if (uIComponent != null)
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
BindPropertyByKey component = uIComponent.GetComponent<BindPropertyByKey>();
if (component != null)
{
string title = "Service Vehicles Manager v" + version;
string notes = ResourceLoader.loadResourceString("UI.VersionNotes.txt");
string text = "Service Vehicles Manager was updated! Release notes:\r\n\r\n" + notes;
string img = "IconMessage";
component.SetProperties(TooltipHelper.Format(new string[]
{
"title",
title,
"message",
text,
"img",
img
}));
needShowPopup = false;
currentSaveVersion.value = fullVersion;
return true;
}
return false;
}
else
{
SVMUtils.doLog("PANEL NOT FOUND!!!!");
return false;
}
}
catch (Exception e)
{
SVMUtils.doErrorLog("showVersionInfoPopup ERROR {0} {1}", e.GetType(), e.Message);
}
}
return false;
}
public void autoLoadSVMLocale()
{
if (currentLanguageId.value == 0)
{
loadSVMLocale(false);
}
}
public void loadSVMLocale(bool force)
{
if (SingletonLite<LocaleManager>.exists && IsKlyteCommonsEnabled())
{
SVMLocaleUtils.loadLocale(currentLanguageId.value == 0 ? SingletonLite<LocaleManager>.instance.language : SVMLocaleUtils.getSelectedLocaleByIndex(currentLanguageId.value), force);
if (!isLocaleLoaded)
{
isLocaleLoaded = true;
eventOnLoadLocaleEnd?.Invoke();
}
}
}
private delegate void OnLocaleLoadedFirstTime();
private event OnLocaleLoadedFirstTime eventOnLoadLocaleEnd;
private bool m_loaded = false;
public void OnLevelLoaded(LoadMode mode)
{
SVMUtils.doLog("LEVEL LOAD");
if (mode != LoadMode.LoadGame && mode != LoadMode.NewGame && mode != LoadMode.NewGameFromScenario)
{
m_loaded = false;
SVMUtils.doLog("NOT GAME ({0})", mode);
return;
}
if (!IsKlyteCommonsEnabled())
{
throw new Exception("SVM requires Klyte Commons active!");
}
if (SVMController.taSVM == null)
{
SVMController.taSVM = CreateTextureAtlas("UI.Images.sprites.png", "ServiceVehicleManagerSprites", GameObject.FindObjectOfType<UIView>().FindUIComponent<UIPanel>("InfoPanel").atlas.material, 64, 64, new string[] {
"ServiceVehiclesManagerIcon","ServiceVehiclesManagerIconSmall","ToolbarIconGroup6Hovered","ToolbarIconGroup6Focused","HelicopterIndicator","RemoveUnwantedIcon","CargoIndicator", "OutsideIndicator", "BioIndicator"
});
}
loadSVMLocale(false);
SVMController.instance.Awake();
m_loaded = true;
}
public void OnLevelUnloading()
{
if (!m_loaded) { return; }
if (SVMController.instance != null)
{
Destroy(SVMController.instance);
}
// Log.debug ("LEVELUNLOAD");
m_loaded = false;
}
public void OnReleased()
{
}
UITextureAtlas CreateTextureAtlas(string textureFile, string atlasName, Material baseMaterial, int spriteWidth, int spriteHeight, string[] spriteNames)
{
Texture2D tex = new Texture2D(spriteWidth * spriteNames.Length, spriteHeight, TextureFormat.ARGB32, false)
{
filterMode = FilterMode.Bilinear
};
{ // LoadTexture
tex.LoadImage(ResourceLoader.loadResourceData(textureFile));
tex.Apply(true, true);
}
UITextureAtlas atlas = ScriptableObject.CreateInstance<UITextureAtlas>();
{ // Setup atlas
Material material = (Material)Material.Instantiate(baseMaterial);
material.mainTexture = tex;
atlas.material = material;
atlas.name = atlasName;
}
// Add sprites
for (int i = 0; i < spriteNames.Length; ++i)
{
float uw = 1.0f / spriteNames.Length;
var spriteInfo = new UITextureAtlas.SpriteInfo()
{
name = spriteNames[i],
texture = tex,
region = new Rect(i * uw, 0, uw, 1),
};
atlas.AddSprite(spriteInfo);
}
return atlas;
}
}
}