-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathValue.cs
More file actions
89 lines (74 loc) · 2.82 KB
/
Copy pathValue.cs
File metadata and controls
89 lines (74 loc) · 2.82 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
using System.Collections;
using LabApi.Features.Wrappers;
using SER.Code.Exceptions;
using SER.Code.ScriptSystem;
namespace SER.Code.ValueSystem;
public abstract class Value
{
public abstract bool EqualCondition(Value other);
public abstract int HashCode { get; }
public static Value Parse(object obj, Script? script)
{
if (obj is null) throw new AndrzejFuckedUpException();
if (obj is Value v) return v;
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 when script is not null => new DynamicTextValue(s, script),
string s => new StaticTextValue(s),
TimeSpan t => new DurationValue(t),
Player p => new PlayerValue(p),
IEnumerable<Player> ps => new PlayerValue(ps),
IEnumerable e => new CollectionValue(e),
_ => new ReferenceValue(obj),
};
}
public static string FriendlyName(Type type) => type.Name.Replace("Value", "").ToLower();
public string FriendlyName() => FriendlyName(GetType());
public override string ToString()
{
return $"value of type {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 || lhs.GetType() != rhs.GetType()) return false;
return lhs.EqualCondition(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);
}
}