This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
forked from bloodypenguin/Skylines-MetroOverhaulMod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Util.cs
247 lines (229 loc) · 8.87 KB
/
Util.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using ColossalFramework.Plugins;
using ICities;
using UnityEngine;
using Object = UnityEngine.Object;
using static ColossalFramework.Plugins.PluginManager;
using ColossalFramework.PlatformServices;
using System.IO;
using ColossalFramework.UI;
using MetroOverhaul.UI;
using MetroOverhaul.NEXT;
using static ToolBase;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace MetroOverhaul {
public static class Util {
private static int count = 0;
public static IEnumerable<FieldInfo> GetAllFieldsFromType(this Type type)
{
if (type == null)
return Enumerable.Empty<FieldInfo>();
BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Static | BindingFlags.Instance |
BindingFlags.DeclaredOnly;
if (type.BaseType != null)
return type.GetFields(flags).Concat(type.BaseType.GetAllFieldsFromType());
else
return type.GetFields(flags);
}
public static FieldInfo GetFieldByName(this Type type, string name)
{
return type.GetAllFieldsFromType().Where(p => p.Name == name).FirstOrDefault();
}
public static string PackageName(string assetName)
{
var publishedFileID = PluginInfo.publishedFileID.ToString();
if (publishedFileID.Equals(PublishedFileId.invalid.ToString()))
{
return assetName;
}
return publishedFileID;
}
public static bool IsMetroTrackCustomizerUIVisible()
{
var ui = UIView.GetAView().FindUIComponent(typeof(MetroTrackCustomizerUI).ToString());
if (ui != null)
{
Debug.Log("The UI is not null!");
Debug.Log("The UI is visible? " + ((MetroTrackCustomizerUI)ui).isVisible + ". Count: " + count++);
//Debug.Log("The UI is active? " + ((MetroTrackCustomizerUI)ui).IsActivated());
//Debug.Log("The UI is active & enabled? " + ((MetroTrackCustomizerUI)ui).isActiveAndEnabled);
return ((MetroTrackCustomizerUI)ui).isVisible;
}
else
{
Debug.Log("The UI is null!");
}
return false;
}
public static T ClonePrefab<T>(T originalPrefab, string newName, Transform parentTransform) where T : PrefabInfo
{
var instance = Object.Instantiate(originalPrefab.gameObject);
instance.name = newName;
instance.transform.SetParent(parentTransform);
instance.transform.localPosition = new Vector3(-7500, -7500, -7500);
var newPrefab = instance.GetComponent<T>();
instance.SetActive(false);
newPrefab.m_prefabInitialized = false;
return newPrefab;
}
public static NetTool GetNetTool()
{
return Object.FindObjectOfType<NetTool>();
}
public static string AssemblyPath => PluginInfo.modPath;
private static PluginInfo PluginInfo {
get
{
var pluginManager = PluginManager.instance;
var plugins = pluginManager.GetPluginsInfo();
foreach (var item in plugins)
{
try
{
var instances = item.GetInstances<IUserMod>();
if (!(instances.FirstOrDefault() is Mod))
{
continue;
}
return item;
}
catch
{
}
}
throw new Exception("Failed to find MetroOverhaul assembly!");
}
}
public static string AssemblyDirectory {
get
{
var pluginManager = PluginManager.instance;
var plugins = pluginManager.GetPluginsInfo();
foreach (var item in plugins)
{
try
{
var instances = item.GetInstances<IUserMod>();
if (!(instances.FirstOrDefault() is Mod))
{
continue;
}
return item.modPath;
}
catch
{
}
}
throw new Exception("Failed to find MetroOverhaul assembly!");
}
}
public static string GetMetroTrackName(NetInfoVersion version, TrackStyle style, TrackType type = TrackType.TwowayTwoLane, bool isNoBar = false)
{
var trackName = "Metro Track";
if (version == NetInfoVersion.Ground)
{
switch (style)
{
case TrackStyle.Vanilla:
trackName += " Ground 01";
break;
case TrackStyle.Modern:
trackName = string.Format("{0} Ground", trackName);
break;
case TrackStyle.Classic:
trackName = string.Format("Steel {0} Ground", trackName);
break;
}
}
else if (version != NetInfoVersion.Tunnel || style != TrackStyle.Vanilla)
{
switch (style)
{
case TrackStyle.Vanilla:
trackName = string.Format("{0} {1} 01", trackName, version.ToString());
break;
case TrackStyle.Modern:
trackName = string.Format("{0} {1}",trackName, version.ToString());
break;
case TrackStyle.Classic:
trackName = string.Format("Steel {0} {1}", trackName, version.ToString());
break;
}
}
switch (type)
{
case TrackType.TwowayOneLane:
trackName += " Small Two-Way";
break;
case TrackType.OnewayTwoLane:
trackName += " Two-Lane One-Way";
break;
case TrackType.OnewayOneLane:
trackName += " Small";
break;
case TrackType.Quad:
trackName += " Large";
break;
}
if (style != TrackStyle.Vanilla)
{
if (isNoBar)
{
return trackName + " NoBar";
}
}
return trackName;
}
public static string GetMetroStationTrackName(NetInfoVersion version, TrackStyle style, StationTrackType stationType = StationTrackType.SidePlatform)
{
string trackName = GetMetroTrackName(version, style).Replace("Metro Track", "Metro Station Track");
switch (stationType)
{
case StationTrackType.SidePlatform:
return trackName;
case StationTrackType.SinglePlatform:
return trackName + " Small";
case StationTrackType.IslandPlatform:
return trackName + " Island";
case StationTrackType.ExpressSidePlatform:
return trackName + " Large";
case StationTrackType.DualIslandPlatform:
return trackName + " Large Dual Island";
}
return trackName;
}
public static bool IsModActive(string modName)
{
var plugins = PluginManager.instance.GetPluginsInfo();
return (from plugin in plugins.Where(p => p.isEnabled)
select plugin.GetInstances<IUserMod>() into instances
where instances.Any()
select instances[0].Name into name
where name == modName
select name).Any();
}
public static bool TryGetWorkshopId(PrefabInfo info, out long workshopId)
{
workshopId = -1;
if (info?.name == null)
{
return false;
}
if (!info.name.Contains(".")) //only for custom prefabs
{
return false;
}
var idStr = info.name.Split('.')[0];
return long.TryParse(idStr, out workshopId);
}
public static bool IsGameMode()
{
return ToolManager.instance.m_properties.m_mode == ItemClass.Availability.Game;
}
}
}