-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPAModule.cs
More file actions
346 lines (330 loc) · 20.8 KB
/
PAModule.cs
File metadata and controls
346 lines (330 loc) · 20.8 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
346
using CitiesHarmony.API;
using ColossalFramework;
using ColossalFramework.Globalization;
using ColossalFramework.Plugins;
using ColossalFramework.UI;
using EManagersLib;
using ICities;
using PropAnarchy.AdditiveShader;
using PropAnarchy.TransparencyLODFix;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Xml;
using UI;
using UnityEngine;
namespace PropAnarchy {
public sealed class PAModule : ILoadingExtension, IUserMod {
private const string m_modName = @"Prop Anarchy (temp fix by algernon)";
private const string m_modDesc = @"Extends the Prop Framework";
internal const string m_modVersion = @"0.7.5.2";
internal const string m_AssemblyVersion = m_modVersion + @".*";
private const string m_debugLogFile = @"00PropAnarchyDebug.log";
internal const string KeybindingConfigFile = @"PropAnarchyKeyBindSetting";
internal static bool IsInGame = false;
public static float PropLimitScale {
get => EPropManager.PROP_LIMIT_SCALE;
set {
if (EPropManager.PROP_LIMIT_SCALE != value) {
EPropManager.PROP_LIMIT_SCALE = value;
ThreadPool.QueueUserWorkItem(SaveSettings);
}
}
}
public static bool UsePropAnarchy {
get => EPropManager.UsePropAnarchy;
set {
if (EPropManager.UsePropAnarchy != value) {
EPropManager.UsePropAnarchy = value;
UIIndicator.AnarchyIndicator?.SetState(value);
if (PAOptionPanel.m_propAnarchyCB) PAOptionPanel.m_propAnarchyCB.isChecked = value;
}
}
}
/* Prop Snapping related */
public static bool UsePropSnapping {
get => EPropManager.UsePropSnapping;
set {
if (EPropManager.UsePropSnapping != value) {
EPropManager.UsePropSnapping = value;
UIIndicator.SnapIndicator?.SetState(value);
if (PAOptionPanel.m_propSnappingCB) PAOptionPanel.m_propSnappingCB.isChecked = value;
ThreadPool.QueueUserWorkItem(SaveSettings);
}
}
}
#region UserMod
public string Name => m_modName + ' ' + m_modVersion;
public string Description => m_modDesc;
public void OnEnabled() {
try {
CreateDebugFile();
} catch (Exception e) {
UnityEngine.Debug.LogException(e);
}
try {
if (GameSettings.FindSettingsFileByName(KeybindingConfigFile) is null) {
GameSettings.AddSettingsFile(new SettingsFile[] {
new SettingsFile() { fileName = KeybindingConfigFile }
});
}
} catch (Exception e) {
UnityEngine.Debug.LogException(e);
}
PALocale.Init();
for (int loadTries = 0; loadTries < 2; loadTries++) {
if (LoadSettings()) break; // Try 2 times, and if still fails, then use default settings
}
HarmonyHelper.DoOnHarmonyReady(PAPatcher.EnablePatches);
}
public void OnDisabled() {
SaveSettings();
if (HarmonyHelper.IsHarmonyInstalled) PAPatcher.DisablePatches();
}
public void OnSettingsUI(UIHelperBase helper) {
PLT.PropLineTool.InitializeTextures();
PALocale.OnLocaleChanged();
LocaleManager.eventLocaleChanged += PALocale.OnLocaleChanged;
PAOptionPanel.SetupPanel((helper.AddGroup(m_modName + @" -- Version " + m_modVersion) as UIHelper).self as UIPanel);
}
#endregion UserMod
#region LoadingExtension
public void OnCreated(ILoading loading) {
OutputPluginsList();
PAPatcher.AttachMoveItPostProcess();
}
public void OnReleased() {
}
public void UpdateCustomPrefabs() {
List<ManagedAsset> assets = new List<ManagedAsset>();
List<PrefabInfo> rotorShaderPrefabs = new List<PrefabInfo>();
try {
PrefabInfo[] prefabs = Resources.FindObjectsOfTypeAll<PrefabInfo>();
int prefabLen = prefabs.Length;
for (int i = 0; i < prefabLen; i++) {
PrefabInfo prefab = prefabs[i];
if (prefab is PropInfo prop) {
/* Decal prop fix routine added here to prevent re-allocating new enumerator for all prefabinfo types */
DecalPropFix.AssignFix(prop); /* DECAL PROP FIX ROUTINE */
if (prop.m_mesh && prop.m_mesh.name is string propData && AdditiveShaderManager.HasValidData(propData)) {
assets.Add(new ManagedAsset(prop));
PALog(@"[AdditiveShader] : Loaded a prop - " + prop.name + @" marked as having the AdditiveShader");
}
prop.CheckRotorSignature(rotorShaderPrefabs); // Transparency LOD Fix Routine
} else if (prefab is BuildingInfo building) {
if (building.m_mesh && AdditiveShaderManager.HasValidData(building.m_mesh.name)) {
assets.Add(new ManagedAsset(building));
PALog(@"[AdditiveShader] : Loaded a building - " + building.name + @" marked as having the AdditiveShader");
}
if (!(building.m_props is null) && AdditiveShaderManager.ContainsShaderProps(building)) {
assets.Add(new ManagedAsset(building, true));
}
building.CheckRotorSignature(rotorShaderPrefabs); // Transparency LOD Fix Routine
} else if (prefab is BuildingInfoSub buildingSub && buildingSub.m_mesh &&
buildingSub.m_mesh.name is string buildingSubData && AdditiveShaderManager.HasValidData(buildingSubData)) {
assets.Add(new ManagedAsset(buildingSub));
PALog(@"[AdditiveShader] : Loaded a building sub - " + buildingSub.name + @" marked as having the AdditiveShader");
} else if (prefab is VehicleInfoSub vehicleSub && vehicleSub.m_mesh &&
vehicleSub.m_mesh.name is string vehicleSubData && AdditiveShaderManager.HasValidData(vehicleSubData)) {
assets.Add(new ManagedAsset(vehicleSub));
PALog(@"[AdditiveShader] : Loaded a vehicle sub - " + vehicleSub.name + @" mesh marked as having the AdditiveShader");
}
}
} catch (Exception e) {
UnityEngine.Debug.LogException(e);
} finally {
if (assets.Count > 0) {
ManagedAsset[] managedAssets = assets.ToArray();
int assetsLen = managedAssets.Length;
AdditiveShaderManager.m_managedAssets = managedAssets;
for (int i = 0; i < assetsLen; i++) {
if (assets[i].IsContainer || assets[i].Profile.IsStatic) {
assets[i].SetVisible(true);
} else {
assets[i].SetVisible(false);
}
}
// Adding Additive Shader thread as a coroutine into UIView. This saves valuable resources
// instead of creating another gameobject to do mundane work
UIView.GetAView().StartCoroutine(AdditiveShaderManager.AdditiveShaderThread());
// Now process Transparency LOD fix
TransparencyLODFix.TransparencyLODFix.m_rotorShaderPrefabs = rotorShaderPrefabs.ToArray();
}
}
}
public void OnLevelLoaded(LoadMode mode) {
IsInGame = true;
PAOptionPanel.UpdateState(true);
if (Singleton<ToolManager>.instance.m_properties.m_mode != ItemClass.Availability.AssetEditor) {
UIIndicator indicatorPanel = UIIndicator.Setup();
if (indicatorPanel) {
UIIndicator.UIIcon propSnap = default;
propSnap = indicatorPanel.AddSnappingIcon(PALocale.GetLocale(@"PropSnapIsOn"), PALocale.GetLocale(@"PropSnapIsOff"), UsePropSnapping, (_, p) => {
UsePropSnapping = !UsePropSnapping;
}, out bool finalState);
if (finalState != UsePropSnapping) {
UsePropSnapping = finalState;
propSnap.State = finalState;
}
UIIndicator.UIIcon propAnarchy = default;
propAnarchy = indicatorPanel.AddAnarchyIcon(PALocale.GetLocale(@"PropAnarchyIsOn"), PALocale.GetLocale(@"PropAnarchyIsOff"), UsePropAnarchy, (_, p) => {
UsePropAnarchy = !UsePropAnarchy;
}, out finalState);
if (finalState != UsePropAnarchy) {
UsePropAnarchy = finalState;
propAnarchy.State = finalState;
}
}
}
// Fix for prop snapping issues if buffer is at the standard size.
// Since we can't recreate the original height, we clear the fixed height flag instead to at least restore them to terrain height (from 0).
EPropInstance[] propBuffer = EPropManager.m_props.m_buffer;
for (int i = 0; i < propBuffer.Length; i++) {
if (propBuffer[i].m_posY == 0) {
propBuffer[i].m_flags &= ~(ushort)EPropInstance.Flags.FixedHeight & 0xFFFF;
}
}
// The original mods created a new GameObject for running additive shader routines. I'm opting
// to just use existing GameObject and add a coroutine so it doesn't stress Update()
UpdateCustomPrefabs(); // This thread handles initialization of Additive Shader asset and Decal Prop Fix
UIDropDown levelOfDetail = UIView.GetAView().FindUIComponent<UIDropDown>("LevelOfDetail");
levelOfDetail.eventSelectedIndexChanged += (c, val) => TransparencyLODFix.TransparencyLODFix.Update();
UIComponent optionsPanel = UIView.library.Get<UIPanel>("OptionsPanel");
optionsPanel.eventVisibilityChanged += (c, isVisible) => TransparencyLODFix.TransparencyLODFix.Update();
SimulationManager smInstance = Singleton<SimulationManager>.instance;
PLT.PropLineTool.InitializedPLT(mode);
PAPainter.Initialize(smInstance);
}
public void OnLevelUnloading() {
IsInGame = false;
PAOptionPanel.UpdateState(false);
PLT.PropLineTool.UnloadPLT();
UIView.GetAView().StopCoroutine(AdditiveShaderManager.AdditiveShaderThread());
AdditiveShaderManager.m_managedAssets = null;
}
#endregion LoadingExtension
private const string SettingsFileName = @"PropAnarchyConfig.xml";
internal static bool LoadSettings() {
try {
if (!File.Exists(SettingsFileName)) {
SaveSettings();
}
XmlDocument xmlConfig = new XmlDocument {
XmlResolver = null
};
xmlConfig.Load(SettingsFileName);
EPropManager.PROP_LIMIT_SCALE = float.Parse(xmlConfig.DocumentElement.GetAttribute(@"PropLimitScale"), System.Globalization.NumberStyles.Float);
EPropManager.UsePropSnapping = bool.Parse(xmlConfig.DocumentElement.GetAttribute(@"UsePropSnapping"));
PLT.Settings.m_controlMode = (PLT.PropLineTool.ControlMode)int.Parse(xmlConfig.DocumentElement.GetAttribute(@"ControlMode"));
PLT.Settings.m_angleMode = (PLT.PropLineTool.AngleMode)int.Parse(xmlConfig.DocumentElement.GetAttribute(@"AngleMode"));
PLT.Settings.m_angleFlip180 = bool.Parse(xmlConfig.DocumentElement.GetAttribute(@"AngleFlip180"));
PLT.Settings.m_showUndoPreview = bool.Parse(xmlConfig.DocumentElement.GetAttribute(@"ShowUndoPreviews"));
PLT.Settings.m_perfectCircles = bool.Parse(xmlConfig.DocumentElement.GetAttribute(@"PerfectCircles"));
PLT.Settings.m_linearFenceFill = bool.Parse(xmlConfig.DocumentElement.GetAttribute(@"LinearFenceFill"));
PLT.Settings.m_useMeshCenterCorrection = bool.Parse(xmlConfig.DocumentElement.GetAttribute(@"UseMeshCenterCorrection"));
PLT.Settings.m_verticalLayout = bool.Parse(xmlConfig.DocumentElement.GetAttribute(@"VerticalLayout"));
PLT.Settings.m_optionXPos = float.Parse(xmlConfig.DocumentElement.GetAttribute(@"PLTOptionXPos"), System.Globalization.NumberStyles.Float);
PLT.Settings.m_optionYPos = float.Parse(xmlConfig.DocumentElement.GetAttribute(@"PLTOptionYPos"), System.Globalization.NumberStyles.Float);
TransparencyLODFix.Settings.m_hideClouds = bool.Parse(xmlConfig.DocumentElement.GetAttribute(@"HideClouds"));
TransparencyLODFix.Settings.m_lodFactorMultiplierProps = float.Parse(xmlConfig.DocumentElement.GetAttribute(@"LodFactorMultiplierProps"), System.Globalization.NumberStyles.Float);
TransparencyLODFix.Settings.m_distanceOffsetProps = float.Parse(xmlConfig.DocumentElement.GetAttribute(@"DistanceOffsetProps"), System.Globalization.NumberStyles.Float);
TransparencyLODFix.Settings.m_lodDistanceMultiplierProps = float.Parse(xmlConfig.DocumentElement.GetAttribute(@"LodDistanceMultiplierProps"), System.Globalization.NumberStyles.Float);
TransparencyLODFix.Settings.m_fallbackRenderDistanceProps = float.Parse(xmlConfig.DocumentElement.GetAttribute(@"FallbackRenderDistanceProps"), System.Globalization.NumberStyles.Float);
TransparencyLODFix.Settings.m_lodFactorMultiplierBuildings = float.Parse(xmlConfig.DocumentElement.GetAttribute(@"LodFactorMultiplierBuildings"), System.Globalization.NumberStyles.Float);
TransparencyLODFix.Settings.m_distanceOffsetBuildings = float.Parse(xmlConfig.DocumentElement.GetAttribute(@"DistanceOffsetBuildings"), System.Globalization.NumberStyles.Float);
TransparencyLODFix.Settings.m_lodDistanceMultiplierBuildings = float.Parse(xmlConfig.DocumentElement.GetAttribute(@"LodDistanceMultiplierBuildings"), System.Globalization.NumberStyles.Float);
TransparencyLODFix.Settings.m_fallbackRenderDistanceBuildings = float.Parse(xmlConfig.DocumentElement.GetAttribute(@"FallbackRenderDistanceBuildings"), System.Globalization.NumberStyles.Float);
} catch (Exception e) {
UnityEngine.Debug.LogException(e);
SaveSettings(); // Most likely a corrupted file if we enter here. Recreate the file
return false;
}
return true;
}
private static readonly object settingsLock = new object();
internal static void SaveSettings(object _ = null) {
Monitor.Enter(settingsLock);
try {
XmlDocument xmlConfig = new XmlDocument {
XmlResolver = null
};
XmlElement root = xmlConfig.CreateElement(@"PropAnarchyConfig");
root.Attributes.Append(AddElement(xmlConfig, @"PropLimitScale", PropLimitScale));
root.Attributes.Append(AddElement(xmlConfig, @"UsePropSnapping", UsePropSnapping));
root.Attributes.Append(AddElement(xmlConfig, @"ControlMode", (int)PLT.Settings.m_controlMode));
root.Attributes.Append(AddElement(xmlConfig, @"AngleMode", (int)PLT.Settings.m_angleMode));
root.Attributes.Append(AddElement(xmlConfig, @"AngleFlip180", PLT.Settings.m_angleFlip180));
root.Attributes.Append(AddElement(xmlConfig, @"ShowUndoPreviews", PLT.Settings.m_showUndoPreview));
root.Attributes.Append(AddElement(xmlConfig, @"PerfectCircles", PLT.Settings.m_perfectCircles));
root.Attributes.Append(AddElement(xmlConfig, @"LinearFenceFill", PLT.Settings.m_linearFenceFill));
root.Attributes.Append(AddElement(xmlConfig, @"UseMeshCenterCorrection", PLT.Settings.m_useMeshCenterCorrection));
root.Attributes.Append(AddElement(xmlConfig, @"VerticalLayout", PLT.Settings.m_verticalLayout));
root.Attributes.Append(AddElement(xmlConfig, @"PLTOptionXPos", PLT.Settings.m_optionXPos));
root.Attributes.Append(AddElement(xmlConfig, @"PLTOptionYPos", PLT.Settings.m_optionYPos));
root.Attributes.Append(AddElement(xmlConfig, @"HideClouds", TransparencyLODFix.Settings.m_hideClouds));
root.Attributes.Append(AddElement(xmlConfig, @"LodFactorMultiplierProps", TransparencyLODFix.Settings.m_lodFactorMultiplierProps));
root.Attributes.Append(AddElement(xmlConfig, @"DistanceOffsetProps", TransparencyLODFix.Settings.m_distanceOffsetProps));
root.Attributes.Append(AddElement(xmlConfig, @"LodDistanceMultiplierProps", TransparencyLODFix.Settings.m_lodDistanceMultiplierProps));
root.Attributes.Append(AddElement(xmlConfig, @"FallbackRenderDistanceProps", TransparencyLODFix.Settings.m_fallbackRenderDistanceProps));
root.Attributes.Append(AddElement(xmlConfig, @"LodFactorMultiplierBuildings", TransparencyLODFix.Settings.m_lodFactorMultiplierBuildings));
root.Attributes.Append(AddElement(xmlConfig, @"DistanceOffsetBuildings", TransparencyLODFix.Settings.m_distanceOffsetBuildings));
root.Attributes.Append(AddElement(xmlConfig, @"LodDistanceMultiplierBuildings", TransparencyLODFix.Settings.m_lodDistanceMultiplierBuildings));
root.Attributes.Append(AddElement(xmlConfig, @"FallbackRenderDistanceBuildings", TransparencyLODFix.Settings.m_fallbackRenderDistanceBuildings));
xmlConfig.AppendChild(root);
xmlConfig.Save(SettingsFileName);
} finally {
Monitor.Exit(settingsLock);
}
}
internal static XmlAttribute AddElement<T>(XmlDocument doc, string name, T t) {
XmlAttribute attr = doc.CreateAttribute(name);
attr.Value = t.ToString();
return attr;
}
private static readonly Stopwatch profiler = new Stopwatch();
private static readonly object fileLock = new object();
private void CreateDebugFile() {
profiler.Start();
/* Create Debug Log File */
string path = Path.Combine(Application.dataPath, m_debugLogFile);
using (FileStream debugFile = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
using (StreamWriter sw = new StreamWriter(debugFile)) {
sw.WriteLine(@"--- " + m_modName + ' ' + m_modVersion + @" Debug File ---");
sw.WriteLine(Environment.OSVersion);
sw.WriteLine(@"C# CLR Version " + Environment.Version);
sw.WriteLine(@"Unity Version " + Application.unityVersion);
sw.WriteLine(@"-------------------------------------");
}
}
private void OutputPluginsList() {
Monitor.Enter(fileLock);
try {
using (FileStream debugFile = new FileStream(Path.Combine(Application.dataPath, m_debugLogFile), FileMode.Append, FileAccess.Write, FileShare.None))
using (StreamWriter sw = new StreamWriter(debugFile)) {
sw.WriteLine(@"Mods Installed are:");
foreach (PluginManager.PluginInfo info in Singleton<PluginManager>.instance.GetPluginsInfo()) {
if (!(info is null) && info.userModInstance is IUserMod modInstance)
sw.WriteLine(@"=> " + info.name + '-' + modInstance.Name + ' ' + (info.isEnabled ? @"** Enabled **" : @"** Disabled **"));
}
sw.WriteLine(@"-------------------------------------");
}
} finally {
Monitor.Exit(fileLock);
}
}
internal static void PALog(string msg) {
var ticks = profiler.ElapsedTicks;
Monitor.Enter(fileLock);
try {
using (FileStream debugFile = new FileStream(Path.Combine(Application.dataPath, m_debugLogFile), FileMode.Append))
using (StreamWriter sw = new StreamWriter(debugFile)) {
sw.WriteLine($"{(ticks / Stopwatch.Frequency):n0}:{(ticks % Stopwatch.Frequency):D7}-{new StackFrame(1, true).GetMethod().Name} ==> {msg}");
}
} finally {
Monitor.Exit(fileLock);
}
}
}
}