-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTAMod.cs
377 lines (353 loc) · 17.1 KB
/
TAMod.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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
using CitiesHarmony.API;
using ColossalFramework;
using ColossalFramework.Globalization;
using ColossalFramework.Plugins;
using ColossalFramework.UI;
using ICities;
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Threading;
using System.Xml;
using UI;
using UnityEngine;
namespace TreeAnarchy {
public class TAMod : ILoadingExtension, IUserMod {
internal const string m_modVersion = @"1.3.2";
internal const string m_assemblyVersion = m_modVersion + @".*";
private const string m_modName = @"Tree Anarchy";
private const string m_modDesc = @"Lets you plant more trees with tree snapping";
private const string m_debugLogFile = @"00TreeAnarchyDebug.log";
internal const string KeybindingConfigFile = @"TreeAnarchyKeyBindSetting";
/* Some standard constant definition for tree limits */
private static float m_ScaleFactor = 4f;
public const int DefaultTreeLimit = 262144;
public const int DefaultTreeUpdateCount = 4096;
/* Unlimited Trees Related */
internal static int RemoveReplaceOrKeep = 0;
internal static bool OldFormatLoaded = false;
private static bool m_treeEffectOnWind = true;
internal static bool TreeEffectOnWind {
get => m_treeEffectOnWind;
set {
if (m_treeEffectOnWind != value) {
m_treeEffectOnWind = value;
ThreadPool.QueueUserWorkItem(SaveSettings);
}
}
}
internal static int LastMaxTreeLimit = DefaultTreeLimit;
internal static float TreeScaleFactor {
get => m_ScaleFactor;
set {
LastTreeScaleFactor = m_ScaleFactor;
m_ScaleFactor = value;
ThreadPool.QueueUserWorkItem(SaveSettings);
}
}
internal static float LastTreeScaleFactor = m_ScaleFactor;
internal static int MaxTreeLimit => (int)(DefaultTreeLimit * TreeScaleFactor);
internal static int MaxTreeUpdateLimit => (int)(DefaultTreeUpdateCount * TreeScaleFactor);
internal static int CheckLowLimit => MaxTreeLimit - 12144;
internal static int CheckHighLimit => MaxTreeLimit - 5;
/* Tree Snapping */
private static bool m_useTreeSnapping = false;
internal static bool UseTreeSnapping {
get => m_useTreeSnapping;
set {
if (m_useTreeSnapping != value) {
m_useTreeSnapping = value;
if (TAOptionPanel.TreeSnapCB) TAOptionPanel.TreeSnapCB.isChecked = value;
UIIndicator.SnapIndicator?.SetState(value);
MoveIt.MoveItTool.treeSnapping = value;
ThreadPool.QueueUserWorkItem(SaveSettings);
}
}
}
internal static bool UseExperimentalTreeSnapping = false;
internal static bool UseTreeSnapToBuilding = true;
internal static bool UseTreeSnapToNetwork = true;
internal static bool UseTreeSnapToProp = true;
/* Lock Forestry */
private static bool m_useLockForestry = false;
internal static bool UseLockForestry {
get => m_useLockForestry;
set {
if (m_useLockForestry != value) {
m_useLockForestry = value;
if (TAOptionPanel.TreeSnapCB) TAOptionPanel.LockForestryCB.isChecked = value;
UIIndicator.LockForestryIndicator?.SetState(value);
ThreadPool.QueueUserWorkItem(SaveSettings);
}
}
}
private static bool m_persistentLockForestry = true;
internal static bool PersistentLockForestry {
get => m_persistentLockForestry;
set {
if (m_persistentLockForestry != value) {
m_persistentLockForestry = value;
ThreadPool.QueueUserWorkItem(SaveSettings);
}
}
}
/* Tree Movement Related */
private static float m_treeSwayFactor = 1f;
internal static float TreeSwayFactor {
get => m_treeSwayFactor;
set {
if (m_treeSwayFactor != value) {
m_treeSwayFactor = value;
ThreadPool.QueueUserWorkItem(SaveSettings);
}
}
}
/* Tree Anarchy Related */
private static bool m_useTreeAnarchy = true;
internal static bool UseTreeAnarchy {
get => m_useTreeAnarchy;
set {
if (m_useTreeAnarchy != value) {
m_useTreeAnarchy = value;
if (TAOptionPanel.TreeAnarchyCB) TAOptionPanel.TreeAnarchyCB.isChecked = value;
UIIndicator.AnarchyIndicator?.SetState(value);
}
}
}
private static bool m_hideTreeOnLoad = false;
internal static bool HideTreeOnLoad {
get => m_hideTreeOnLoad;
set {
if (m_hideTreeOnLoad != value) {
m_hideTreeOnLoad = value;
ThreadPool.QueueUserWorkItem(SaveSettings);
}
}
}
private static bool m_deleteOnOverlap = false;
internal static bool DeleteOnOverlap {
get => m_deleteOnOverlap;
set {
if (m_deleteOnOverlap != value) {
m_deleteOnOverlap = value;
ThreadPool.QueueUserWorkItem(SaveSettings);
}
}
}
/* Tree LOD Fix Related */
private static bool m_useTreeLODFix = true;
internal static bool UseTreeLODFix {
get => m_useTreeLODFix;
set {
if (m_useTreeLODFix != value) {
m_useTreeLODFix = value;
UseTreeAnarchy = !value;
ThreadPool.QueueUserWorkItem(SaveSettings);
}
}
}
private static TAManager.TreeLODResolution m_treeLODResolution = TAManager.TreeLODResolution.Medium;
internal static TAManager.TreeLODResolution TreeLODSelectedResolution {
get => m_treeLODResolution;
set {
if (m_treeLODResolution != value) {
m_treeLODResolution = value;
ThreadPool.QueueUserWorkItem(SaveSettings);
}
}
}
internal static bool IsInGame { get; private set; } = false;
#region IUserMod
public string Name => m_modName + " " + m_modVersion;
public string Description => m_modDesc;
public TAMod() {
try {
CreateDebugFile();
} catch (Exception e) {
UnityEngine.Debug.LogException(e);
}
}
public void OnEnabled() {
GameSettings.AddSettingsFile(new SettingsFile[] {
new SettingsFile() { fileName = KeybindingConfigFile }
});
TALocale.Init();
TAPatcher.CheckIncompatibleMods();
for (int loadTries = 0; loadTries < 2; loadTries++) {
if (LoadSettings()) break; // Try 2 times, and if still fails, then use default settings
}
if (PersistentLockForestry) UseLockForestry = true;
if (HideTreeOnLoad) UseTreeAnarchy = false;
HarmonyHelper.DoOnHarmonyReady(TAPatcher.EnableCore);
}
public void OnDisabled() {
if (HarmonyHelper.IsHarmonyInstalled) {
TAPatcher.DisableCore();
}
TALocale.Destroy();
SaveSettings();
}
public void OnSettingsUI(UIHelperBase helper) {
TALocale.OnLocaleChanged();
LocaleManager.eventLocaleChanged += TALocale.OnLocaleChanged;
TAOptionPanel.SetupPanel((helper.AddGroup(m_modName + @" -- Version " + m_modVersion) as UIHelper).self as UIPanel);
}
#endregion
#region ILoadingExtension
public void OnCreated(ILoading loading) {
CheckAllPlugins();
TAManager.Initialize();
if (HarmonyHelper.IsHarmonyInstalled) {
TAPatcher.LateEnable();
}
}
public void OnReleased() {
if (HarmonyHelper.IsHarmonyInstalled) {
TAPatcher.DisableLatePatch();
}
}
public void OnLevelLoaded(LoadMode mode) {
IsInGame = true;
if (Singleton<ToolManager>.instance.m_properties.m_mode != ItemClass.Availability.AssetEditor) {
UIIndicator indicatorPanel = UIIndicator.Setup();
if (indicatorPanel) {
UIIndicator.UIIcon treeSnap = default;
treeSnap = indicatorPanel.AddSnappingIcon(TALocale.GetLocale(@"TreeSnapIsOn"), TALocale.GetLocale(@"TreeSnapIsOff"), UseTreeSnapping, (_, p) => {
UseTreeSnapping = !UseTreeSnapping;
}, out bool finalState);
if (finalState != UseTreeSnapping) {
UseTreeSnapping = finalState;
}
UIIndicator.UIIcon treeAnarchy = default;
treeAnarchy = indicatorPanel.AddAnarchyIcon(TALocale.GetLocale(@"TreeAnarchyIsOn"), TALocale.GetLocale(@"TreeAnarchyIsOff"), UseTreeAnarchy, (_, p) => {
UseTreeAnarchy = !UseTreeAnarchy;
}, out finalState);
if (finalState != UseTreeAnarchy) {
UseTreeAnarchy = finalState;
}
UIIndicator.UIIcon lockForestry = default;
lockForestry = indicatorPanel.AddLockForestryIcon(TALocale.GetLocale(@"LockForestryIsOn"), TALocale.GetLocale(@"LockForestryIsOff"), UseLockForestry, (_, p) => {
UseLockForestry = !UseLockForestry;
});
}
}
TAOptionPanel.UpdateState(true);
}
void ILoadingExtension.OnLevelUnloading() {
IsInGame = false;
}
#endregion
private const string SettingsFileName = @"TreeAnarchyConfig.xml";
internal static bool LoadSettings() {
try {
if (!File.Exists(SettingsFileName)) {
SaveSettings();
}
XmlDocument xmlConfig = new XmlDocument {
XmlResolver = null
};
xmlConfig.Load(SettingsFileName);
m_ScaleFactor = float.Parse(xmlConfig.DocumentElement.GetAttribute(@"ScaleFactor"), NumberStyles.Float, CultureInfo.CurrentCulture.NumberFormat);
m_treeEffectOnWind = bool.Parse(xmlConfig.DocumentElement.GetAttribute(@"TreeEffectOnWind"));
m_useTreeSnapping = bool.Parse(xmlConfig.DocumentElement.GetAttribute(@"UseTreeSnapping"));
UseExperimentalTreeSnapping = bool.Parse(xmlConfig.DocumentElement.GetAttribute(@"UseExperimentalTreeSnapping"));
UseTreeSnapToBuilding = bool.Parse(xmlConfig.DocumentElement.GetAttribute(@"UseTreeSnapToBuilding"));
UseTreeSnapToNetwork = bool.Parse(xmlConfig.DocumentElement.GetAttribute(@"UseTreeSnapToNetwork"));
UseTreeSnapToProp = bool.Parse(xmlConfig.DocumentElement.GetAttribute(@"UseTreeSnapToProp"));
m_treeSwayFactor = float.Parse(xmlConfig.DocumentElement.GetAttribute(@"TreeSwayFactor"), NumberStyles.Float, CultureInfo.CurrentCulture.NumberFormat);
m_useLockForestry = bool.Parse(xmlConfig.DocumentElement.GetAttribute(@"LockForestry"));
PersistentLockForestry = bool.Parse(xmlConfig.DocumentElement.GetAttribute(@"PersistentLock"));
m_deleteOnOverlap = bool.Parse(xmlConfig.DocumentElement.GetAttribute(@"DeleteOnOverlap"));
m_useTreeLODFix = bool.Parse(xmlConfig.DocumentElement.GetAttribute(@"UseTreeLODFix"));
m_treeLODResolution = (TAManager.TreeLODResolution)int.Parse(xmlConfig.DocumentElement.GetAttribute(@"TreeLODSelectedResolution"));
try {
m_hideTreeOnLoad = bool.Parse(xmlConfig.DocumentElement.GetAttribute(@"HideTreeOnLoad"));
} catch {
m_hideTreeOnLoad = false;
}
} catch {
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(@"TreeAnarchyConfig");
root.Attributes.Append(AddElement(xmlConfig, @"ScaleFactor", m_ScaleFactor));
root.Attributes.Append(AddElement(xmlConfig, @"TreeEffectOnWind", m_treeEffectOnWind));
root.Attributes.Append(AddElement(xmlConfig, @"UseTreeSnapping", m_useTreeSnapping));
root.Attributes.Append(AddElement(xmlConfig, @"UseExperimentalTreeSnapping", UseExperimentalTreeSnapping));
root.Attributes.Append(AddElement(xmlConfig, @"UseTreeSnapToBuilding", UseTreeSnapToBuilding));
root.Attributes.Append(AddElement(xmlConfig, @"UseTreeSnapToNetwork", UseTreeSnapToNetwork));
root.Attributes.Append(AddElement(xmlConfig, @"UseTreeSnapToProp", UseTreeSnapToProp));
root.Attributes.Append(AddElement(xmlConfig, @"TreeSwayFactor", m_treeSwayFactor));
root.Attributes.Append(AddElement(xmlConfig, @"LockForestry", m_useLockForestry));
root.Attributes.Append(AddElement(xmlConfig, @"PersistentLock", PersistentLockForestry));
root.Attributes.Append(AddElement(xmlConfig, @"DeleteOnOverlap", m_deleteOnOverlap));
root.Attributes.Append(AddElement(xmlConfig, @"UseTreeLODFix", m_useTreeLODFix));
root.Attributes.Append(AddElement(xmlConfig, @"TreeLODSelectedResolution", (int)m_treeLODResolution));
root.Attributes.Append(AddElement(xmlConfig, @"HideTreeOnLoad", m_hideTreeOnLoad));
xmlConfig.AppendChild(root);
xmlConfig.Save(SettingsFileName);
} finally {
Monitor.Exit(settingsLock);
}
}
private 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 CheckAllPlugins() {
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 TALog(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);
}
}
}
}