Skip to content

Commit a239762

Browse files
Merge pull request #13 from Tosoks67/main
Value Equals fix; switch style fix
2 parents 89ced83 + d544ae8 commit a239762

File tree

5 files changed

+47
-27
lines changed

5 files changed

+47
-27
lines changed

ValueSystem/CollectionValue.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,17 @@ public Type Type
4949
if (CastedValues == Array.Empty<Value>()) field = value;
5050
}
5151
} = null!;
52-
52+
53+
public override bool EqualCondition(Value other)
54+
{
55+
if (other is not CollectionValue otherP || otherP.CastedValues.Length != CastedValues.Length) return false;
56+
for (int i = 0; i < CastedValues.Length; i++)
57+
{
58+
if (!CastedValues[i].EqualCondition(otherP.CastedValues[i])) return false;
59+
}
60+
return true;
61+
}
62+
5363
public TryGet<Value> GetAt(int index)
5464
{
5565
if (index < 1) return $"Provided index {index}, but index cannot be less than 1";

ValueSystem/LiteralValue.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ public abstract class LiteralValue(object value) : Value
99

1010
public object Value => value;
1111

12+
public override bool EqualCondition(Value other) => other is LiteralValue otherP && Value.Equals(otherP.Value);
13+
1214
public override string ToString()
1315
{
1416
return StringRep;

ValueSystem/PlayerValue.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ public PlayerValue(IEnumerable<Player> players)
1515
}
1616

1717
public Player[] Players { get; }
18+
19+
public override bool EqualCondition(Value other) => other is PlayerValue otherP && Players.SequenceEqual(otherP.Players);
1820
}

ValueSystem/ReferenceValue.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ public class ReferenceValue(object? value) : Value
88
public bool IsValid => value is not null;
99
public object Value => value ?? throw new ScriptRuntimeError("Value of reference is invalid.");
1010

11+
public override bool EqualCondition(Value other)
12+
{
13+
if (other is not ReferenceValue otherP || !IsValid || !otherP.IsValid) return false;
14+
return Value.Equals(otherP.Value);
15+
}
16+
1117
public override string ToString()
1218
{
1319
return $"<{Value.GetType().GetAccurateName()} reference | {Value.GetHashCode()}>";

ValueSystem/Value.cs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,33 @@ namespace SER.ValueSystem;
66

77
public abstract class Value
88
{
9+
public abstract bool EqualCondition(Value other);
10+
911
public static Value Parse(object obj)
1012
{
1113
if (obj is null) throw new AndrzejFuckedUpException();
1214
if (obj is Value v) return v;
1315

1416
return obj switch
1517
{
16-
bool b => new BoolValue(b),
17-
byte n => new NumberValue(n),
18-
sbyte n => new NumberValue(n),
19-
short n => new NumberValue(n),
20-
ushort n => new NumberValue(n),
21-
int n => new NumberValue(n),
22-
uint n => new NumberValue(n),
23-
long n => new NumberValue(n),
24-
ulong n => new NumberValue(n),
25-
float n => new NumberValue((decimal)n),
26-
double n => new NumberValue((decimal)n),
27-
decimal n => new NumberValue(n),
28-
string s => new TextValue(s),
29-
TimeSpan t => new DurationValue(t),
30-
Player p => new PlayerValue(p),
31-
IEnumerable<Player> ps => new PlayerValue(ps),
32-
IEnumerable e => new CollectionValue(e),
33-
_ => new ReferenceValue(obj),
18+
bool b => new BoolValue(b),
19+
byte n => new NumberValue(n),
20+
sbyte n => new NumberValue(n),
21+
short n => new NumberValue(n),
22+
ushort n => new NumberValue(n),
23+
int n => new NumberValue(n),
24+
uint n => new NumberValue(n),
25+
long n => new NumberValue(n),
26+
ulong n => new NumberValue(n),
27+
float n => new NumberValue((decimal)n),
28+
double n => new NumberValue((decimal)n),
29+
decimal n => new NumberValue(n),
30+
string s => new TextValue(s),
31+
TimeSpan t => new DurationValue(t),
32+
Player p => new PlayerValue(p),
33+
IEnumerable<Player> ps => new PlayerValue(ps),
34+
IEnumerable e => new CollectionValue(e),
35+
_ => new ReferenceValue(obj),
3436
};
3537
}
3638

@@ -41,11 +43,11 @@ public override int GetHashCode()
4143
{
4244
return this switch
4345
{
44-
LiteralValue => ((LiteralValue)this).Value.GetHashCode(),
45-
PlayerValue => ((PlayerValue)this).Players.GetHashCode(), // Returns the hash code of the reference, not the value
46+
LiteralValue => ((LiteralValue)this).Value.GetHashCode(),
47+
PlayerValue => ((PlayerValue)this).Players.GetHashCode(), // Returns the hash code of the reference, not the value
4648
CollectionValue => ((CollectionValue)this).CastedValues.GetHashCode(), // Returns the hash code of the reference, not the value
47-
ReferenceValue => ((ReferenceValue)this).Value.GetHashCode(), // Might return the hash code of the reference, not the value
48-
_ => throw new TosoksFuckedUpException("undefined value type")
49+
ReferenceValue => ((ReferenceValue)this).Value.GetHashCode(), // Might return the hash code of the reference, not the value
50+
_ => throw new TosoksFuckedUpException("undefined value type")
4951
};
5052
}
5153

@@ -56,11 +58,9 @@ public override bool Equals(object obj)
5658

5759
public static bool operator ==(Value? lhs, Value? rhs)
5860
{
61+
if (lhs is null && rhs is null) return true;
5962
if (lhs is null || rhs is null || lhs.GetType() != rhs.GetType()) return false;
60-
return (lhs is LiteralValue && ((LiteralValue)lhs).Value.Equals(((LiteralValue)rhs).Value)) ||
61-
(lhs is PlayerValue && ((PlayerValue)lhs).Players.SequenceEqual(((PlayerValue)rhs).Players)) ||
62-
(lhs is CollectionValue && ((CollectionValue)lhs).CastedValues.SequenceEqual(((CollectionValue)rhs).CastedValues)) ||
63-
(lhs is ReferenceValue && ((ReferenceValue)lhs).Value.Equals(((ReferenceValue)lhs).Value));
63+
return lhs.EqualCondition(rhs);
6464
}
6565

6666
public static bool operator ==(Value? lhs, object? rhs)

0 commit comments

Comments
 (0)