From 384af49ba2d68ecc90b67278c98d296facffa76b Mon Sep 17 00:00:00 2001 From: Tosoks67 Date: Fri, 21 Nov 2025 09:14:42 +0100 Subject: [PATCH 1/3] Value Equals fix; switch style fix {insert trollface emoji here} --- ValueSystem/CollectionValue.cs | 4 ++- ValueSystem/LiteralValue.cs | 2 ++ ValueSystem/PlayerValue.cs | 2 ++ ValueSystem/ReferenceValue.cs | 2 ++ ValueSystem/Value.cs | 52 +++++++++++++++++----------------- 5 files changed, 35 insertions(+), 27 deletions(-) diff --git a/ValueSystem/CollectionValue.cs b/ValueSystem/CollectionValue.cs index 149ba03..7b9048a 100644 --- a/ValueSystem/CollectionValue.cs +++ b/ValueSystem/CollectionValue.cs @@ -49,7 +49,9 @@ public Type Type if (CastedValues == Array.Empty()) field = value; } } = null!; - + + public override bool EqualCondition(Value other) => other is CollectionValue otherP && CastedValues.SequenceEqual(otherP.CastedValues) && Type == otherP.Type; + public TryGet GetAt(int index) { if (index < 1) return $"Provided index {index}, but index cannot be less than 1"; diff --git a/ValueSystem/LiteralValue.cs b/ValueSystem/LiteralValue.cs index 750d7e1..d85883d 100644 --- a/ValueSystem/LiteralValue.cs +++ b/ValueSystem/LiteralValue.cs @@ -9,6 +9,8 @@ public abstract class LiteralValue(object value) : Value public object Value => value; + public override bool EqualCondition(Value other) => other is LiteralValue otherP && Value.Equals(otherP.Value); + public override string ToString() { return StringRep; diff --git a/ValueSystem/PlayerValue.cs b/ValueSystem/PlayerValue.cs index 662aa13..f61750a 100644 --- a/ValueSystem/PlayerValue.cs +++ b/ValueSystem/PlayerValue.cs @@ -15,4 +15,6 @@ public PlayerValue(IEnumerable players) } public Player[] Players { get; } + + public override bool EqualCondition(Value other) => other is PlayerValue otherP && Players.SequenceEqual(otherP.Players); } \ No newline at end of file diff --git a/ValueSystem/ReferenceValue.cs b/ValueSystem/ReferenceValue.cs index 97d9df1..d295df7 100644 --- a/ValueSystem/ReferenceValue.cs +++ b/ValueSystem/ReferenceValue.cs @@ -8,6 +8,8 @@ public class ReferenceValue(object? value) : Value public bool IsValid => value is not null; public object Value => value ?? throw new ScriptRuntimeError("Value of reference is invalid."); + public override bool EqualCondition(Value other) => other is ReferenceValue otherP && Value.Equals(otherP.Value); + public override string ToString() { return $"<{Value.GetType().GetAccurateName()} reference | {Value.GetHashCode()}>"; diff --git a/ValueSystem/Value.cs b/ValueSystem/Value.cs index 2be5b9a..4497336 100644 --- a/ValueSystem/Value.cs +++ b/ValueSystem/Value.cs @@ -6,6 +6,8 @@ namespace SER.ValueSystem; public abstract class Value { + public abstract bool EqualCondition(Value other); + public static Value Parse(object obj) { if (obj is null) throw new AndrzejFuckedUpException(); @@ -13,24 +15,24 @@ public static Value Parse(object obj) 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 TextValue(s), - TimeSpan t => new DurationValue(t), - Player p => new PlayerValue(p), - IEnumerable ps => new PlayerValue(ps), - IEnumerable e => new CollectionValue(e), - _ => new ReferenceValue(obj), + 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 TextValue(s), + TimeSpan t => new DurationValue(t), + Player p => new PlayerValue(p), + IEnumerable ps => new PlayerValue(ps), + IEnumerable e => new CollectionValue(e), + _ => new ReferenceValue(obj), }; } @@ -41,11 +43,11 @@ public override int GetHashCode() { return this switch { - LiteralValue => ((LiteralValue)this).Value.GetHashCode(), - PlayerValue => ((PlayerValue)this).Players.GetHashCode(), // Returns the hash code of the reference, not the value + LiteralValue => ((LiteralValue)this).Value.GetHashCode(), + PlayerValue => ((PlayerValue)this).Players.GetHashCode(), // Returns the hash code of the reference, not the value CollectionValue => ((CollectionValue)this).CastedValues.GetHashCode(), // Returns the hash code of the reference, not the value - ReferenceValue => ((ReferenceValue)this).Value.GetHashCode(), // Might return the hash code of the reference, not the value - _ => throw new TosoksFuckedUpException("undefined value type") + ReferenceValue => ((ReferenceValue)this).Value.GetHashCode(), // Might return the hash code of the reference, not the value + _ => throw new TosoksFuckedUpException("undefined value type") }; } @@ -56,11 +58,9 @@ public override bool Equals(object 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 is LiteralValue && ((LiteralValue)lhs).Value.Equals(((LiteralValue)rhs).Value)) || - (lhs is PlayerValue && ((PlayerValue)lhs).Players.SequenceEqual(((PlayerValue)rhs).Players)) || - (lhs is CollectionValue && ((CollectionValue)lhs).CastedValues.SequenceEqual(((CollectionValue)rhs).CastedValues)) || - (lhs is ReferenceValue && ((ReferenceValue)lhs).Value.Equals(((ReferenceValue)lhs).Value)); + return lhs.EqualCondition(rhs); } public static bool operator ==(Value? lhs, object? rhs) From cfd50e38148a2074cb823e16f149a1a804e842dd Mon Sep 17 00:00:00 2001 From: Tosoks67 Date: Fri, 21 Nov 2025 10:13:56 +0100 Subject: [PATCH 2/3] ReferenceValue EqualCondition fix --- ValueSystem/ReferenceValue.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ValueSystem/ReferenceValue.cs b/ValueSystem/ReferenceValue.cs index d295df7..76dae6a 100644 --- a/ValueSystem/ReferenceValue.cs +++ b/ValueSystem/ReferenceValue.cs @@ -8,7 +8,11 @@ public class ReferenceValue(object? value) : Value public bool IsValid => value is not null; public object Value => value ?? throw new ScriptRuntimeError("Value of reference is invalid."); - public override bool EqualCondition(Value other) => other is ReferenceValue otherP && Value.Equals(otherP.Value); + public override bool EqualCondition(Value other) + { + if (other is not ReferenceValue otherP || !IsValid || !otherP.IsValid) return false; + return Value.Equals(otherP.Value); + } public override string ToString() { From d544ae8887ef30f392b952a6d0d0597424fc9a0a Mon Sep 17 00:00:00 2001 From: Tosoks67 Date: Fri, 21 Nov 2025 14:43:12 +0100 Subject: [PATCH 3/3] CollectionValue EqualCondition fix --- ValueSystem/CollectionValue.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/ValueSystem/CollectionValue.cs b/ValueSystem/CollectionValue.cs index 7b9048a..b6b4e58 100644 --- a/ValueSystem/CollectionValue.cs +++ b/ValueSystem/CollectionValue.cs @@ -50,7 +50,15 @@ public Type Type } } = null!; - public override bool EqualCondition(Value other) => other is CollectionValue otherP && CastedValues.SequenceEqual(otherP.CastedValues) && Type == otherP.Type; + public override bool EqualCondition(Value other) + { + if (other is not CollectionValue otherP || otherP.CastedValues.Length != CastedValues.Length) return false; + for (int i = 0; i < CastedValues.Length; i++) + { + if (!CastedValues[i].EqualCondition(otherP.CastedValues[i])) return false; + } + return true; + } public TryGet GetAt(int index) {