-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathValue.cs
More file actions
232 lines (194 loc) · 7.52 KB
/
Copy pathValue.cs
File metadata and controls
232 lines (194 loc) · 7.52 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
using System.Collections;
using System.Reflection;
using LabApi.Features.Wrappers;
using SER.Code.Exceptions;
using SER.Code.Extensions;
using SER.Code.Helpers;
using SER.Code.Helpers.ResultSystem;
using SER.Code.ValueSystem.Other;
using SER.Code.ValueSystem.PropertySystem;
using UnityEngine;
namespace SER.Code.ValueSystem;
public abstract class Value : IEquatable<Value>
{
public SingleTypeOfValue Type => new(GetType());
public static Type GuessValueType(Type t, uint? depth = null)
{
if (depth is >= 5)
{
Log.Warn($"type {t.AccurateName} is trying to be resolved recursively, aborting");
Log.Warn(Log.GetStackTrace());
return t;
}
if (Sanitize() is { } failedResultType) return failedResultType;
if (typeof(Value).IsAssignableFrom(t)) return t;
if (typeof(Enum).IsAssignableFrom(t))
{
if (t.IsDefined(typeof(FlagsAttribute)))
{
return typeof(EnumFlagValue<>).MakeGenericType(t);
}
return typeof(EnumValue<>).MakeGenericType(t);
}
if (typeof(Color).IsAssignableFrom(t)) return typeof(ColorValue);
if (t == typeof(bool)) return typeof(BoolValue);
if (t == typeof(byte) || t == typeof(sbyte) || t == typeof(short) || t == typeof(ushort) ||
t == typeof(int) || t == typeof(uint) || t == typeof(long) || t == typeof(ulong) ||
t == typeof(float) || t == typeof(double) || t == typeof(decimal))
return typeof(NumberValue);
if (t == typeof(string)) return typeof(TextValue);
if (t == typeof(TimeSpan)) return typeof(DurationValue);
if (typeof(Player).IsAssignableFrom(t) || typeof(IEnumerable<Player>).IsAssignableFrom(t)) return typeof(PlayerValue);
if (typeof(IEnumerable).IsAssignableFrom(t))
{
var itemType = t.GetInterfaces()
.Concat([t])
.FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IEnumerable<>))
?.GetGenericArguments()[0] ?? typeof(object);
if (itemType == t)
{
return typeof(ReferenceValue<>).MakeGenericType(t);
}
return typeof(CollectionValue<>).MakeGenericType(GuessValueType(
itemType,
depth.HasValue ? depth + 1 : 1
));
}
return typeof(ReferenceValue<>).MakeGenericType(t);
// handles things like references to structs
Type? Sanitize()
{
var count = 0;
while (t.IsByRef && count++ < 10)
{
if (t.GetElementType() is { } element)
{
t = element;
}
else
{
return typeof(ReferenceValue);
}
}
return null;
}
}
public static char GetPrefixOfValue(SingleTypeOfValue value)
{
if (value.IsSameOrHigherThan<LiteralValue>()) return '$';
if (value.IsSameOrHigherThan<PlayerValue>()) return '@';
if (value.IsSameOrHigherThan<ReferenceValue>()) return '*';
if (value.IsSameOrHigherThan<CollectionValue>()) return '&';
return '?';
}
public abstract int HashCode { get; }
public abstract TryGet<object> ToCSharpObject(Type? targetType);
public static Value Parse(object obj)
{
// ReSharper disable once ConvertIfStatementToSwitchStatement
if (obj is null) throw new AndrzejFuckedUpException();
if (obj is Value v) return v;
try
{
return obj switch
{
bool b => new BoolValue(b),
byte n => new NumberValue(n),
sbyte n => new NumberValue(n),
short n => new NumberValue(n),
ushort n => new NumberValue(n),
int n => new NumberValue(n),
uint n => new NumberValue(n),
long n => new NumberValue(n),
ulong n => new NumberValue(n),
float n => new NumberValue((decimal)n),
double n => new NumberValue((decimal)n),
decimal n => new NumberValue(n),
string s => new StaticTextValue(s),
Enum => (Value)Activator.CreateInstance(GuessValueType(obj.GetType()), obj),
TimeSpan t => new DurationValue(t),
Player p => new PlayerValue(p),
IEnumerable<Player> ps => new PlayerValue(ps),
IEnumerable => (Value)Activator.CreateInstance(GuessValueType(obj.GetType()), obj),
Color c => new ColorValue(c),
_ => (Value)Activator.CreateInstance(GuessValueType(obj.GetType()), obj),
};
}
catch (Exception e)
{
Log.Warn($"Failed to parse {obj.GetType().AccurateName} to SER value: {e}");
return new StaticTextValue(obj.ToString());
}
}
public static Dictionary<string, IValueWithProperties.PropInfo>? GetPropertiesOfValue(Type t)
{
if (!typeof(IValueWithProperties).IsAssignableFrom(t)) return null;
if (PropertyCache.TryGetValue(t, out var cached)) return cached;
if (t == typeof(TextValue))
{
t = typeof(StaticTextValue);
}
else if (typeof(ReferenceValue).IsAssignableFrom(t) && t.IsGenericType)
{
return ReferencePropertyRegistry.GetProperties(t.GetGenericArguments()[0]);
}
return PropertyCache[t] = t.CreateInstance<IValueWithProperties>().Properties;
}
private static readonly Dictionary<Type, Dictionary<string, IValueWithProperties.PropInfo>> PropertyCache = new();
public string FriendlyName => GetFriendlyName(GetType());
public static string GetFriendlyName(Type t)
{
if (typeof(Value).IsAssignableFrom(t))
{
var property = t.GetProperty("FriendlyName", BindingFlags.Public | BindingFlags.Static);
if (property != null)
{
return (string)property.GetValue(null);
}
if (t.BaseType != null && t.BaseType != typeof(object))
{
var baseName = GetFriendlyName(t.BaseType);
if (baseName != "generic value") return baseName;
}
return "generic value";
}
if (t == typeof(object)) return "generic value";
return t.AccurateName;
}
public override string ToString()
{
return FriendlyName;
}
public override int GetHashCode() => HashCode;
public override bool Equals(object? obj)
{
return this == obj;
}
public static bool operator ==(Value? lhs, Value? rhs)
{
if (lhs is null && rhs is null) return true;
if (lhs is null || rhs is null) return false;
return lhs.Equals(rhs);
}
public static bool operator ==(Value? lhs, object? rhs)
{
return rhs is Value rhsV && lhs == rhsV;
}
public static bool operator ==(object? lhs, Value? rhs)
{
return lhs is Value lhsV && lhsV == rhs;
}
public static bool operator !=(Value? lhs, Value? rhs)
{
return !(lhs == rhs);
}
public static bool operator !=(Value? lhs, object? rhs)
{
return !(lhs == rhs);
}
public static bool operator !=(object? lhs, Value? rhs)
{
return !(lhs == rhs);
}
public abstract bool Equals(Value? other);
}