forked from lxteo/cities-skylines-unlimiter-1
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathUtil.cs
136 lines (121 loc) · 4.8 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using ColossalFramework.Plugins;
using ICities;
using UnityEngine;
namespace EightyOne
{
internal static class Util
{
private const BindingFlags AllFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly;
public static void CopyArray(IList newArray, object em, string propertyName)
{
var oldArray = (IList)em.GetType().GetField(propertyName, AllFlags).GetValue(em);
for (var i = 0; i < newArray.Count; i += 1)
{
newArray[i] = oldArray[i];
}
}
public static void CopyArrayBack(IList newArray, object em, string propertyName)
{
var oldArray = (IList)em.GetType().GetField(propertyName, AllFlags).GetValue(em);
for (var i = 0; i < newArray.Count; i += 1)
{
oldArray[i] = newArray[i];
}
}
public static void CopyStructArray(IList newArray, object em, string propertyName)
{
var oldArray = (IList)em.GetType().GetField(propertyName, AllFlags).GetValue(em);
var fields = GetFieldsFromStruct(newArray[0], oldArray[0]);
for (var i = 0; i < newArray.Count; i += 1)
{
newArray[i] = CopyStruct((object)newArray[0], oldArray[i], fields);
}
}
public static void CopyStructArrayBack(IList newArray, object em, string propertyName)
{
var oldArray = (IList)em.GetType().GetField(propertyName, AllFlags).GetValue(em);
var fields = GetFieldsFromStruct(oldArray[0], newArray[0]);
for (var i = 0; i < newArray.Count; i += 1)
{
oldArray[i] = CopyStruct((object)oldArray[i], newArray[i], fields);
}
}
public static Dictionary<FieldInfo, FieldInfo> GetFieldsFromStruct(object newArray, object oldArray)
{
var fields = new Dictionary<FieldInfo, FieldInfo>();
foreach (var f in oldArray.GetType().GetFields(AllFlags))
{
fields.Add(newArray.GetType().GetField(f.Name, AllFlags), f);
}
return fields;
}
public static void SetPropertyValue<T>(ref T result, object obj, string propName)
{
result = (T)obj.GetType().GetField(propName, AllFlags).GetValue(obj);
}
public static void SetPropertyValueBack(object result, object obj, string propName)
{
obj.GetType().GetField(propName, AllFlags).SetValue(obj, result);
}
public static object CopyStruct(object newObj, object original, Dictionary<FieldInfo, FieldInfo> fields)
{
foreach (var field in fields)
{
if (field.Key.FieldType != field.Value.FieldType)
{
if (field.Key.FieldType == typeof(byte))
{
var oo = Mathf.Clamp((ushort)field.Value.GetValue(original), 0, 255);
field.Key.SetValue(newObj, (byte)oo);
continue;
}
}
field.Key.SetValue(newObj, field.Value.GetValue(original));
}
return newObj;
}
public static bool IsModActive(ulong modId)
{
var plugins = PluginManager.instance.GetPluginsInfo();
return plugins.Any(p => p != null && p.isEnabled && p.publishedFileID.AsUInt64 == modId);
}
public static bool IsModActive(string modName)
{
var plugins = PluginManager.instance.GetPluginsInfo();
return (from plugin in plugins.Where(p => p != null && 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 Type FindType(string className)
{
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
try
{
var types = assembly.GetExportedTypes();
foreach (var type in types.Where(type => type.Name == className))
{
return type;
}
}
catch
{
// ignored
}
}
return null;
}
public static bool IsGameMode()
{
return ToolManager.instance.m_properties.m_mode == ItemClass.Availability.Game;
}
}
}