Skip to content

Commit c0d1c9e

Browse files
Revert "add invalid value handling system"
This reverts commit df3518e.
1 parent c93af3a commit c0d1c9e

29 files changed

Lines changed: 104 additions & 249 deletions

Code/Extensions/SerExtensions.cs

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using SER.Code.TokenSystem.Tokens;
77
using SER.Code.TokenSystem.Tokens.Interfaces;
88
using SER.Code.ValueSystem;
9-
using SER.Code.ValueSystem.Other;
109

1110
namespace SER.Code.Extensions;
1211

@@ -26,19 +25,12 @@ public static TryGet<TOut> SuccessTryCast<TOut>(this TryGet<Value> value) where
2625

2726
public static TryGet<TOut> TryCast<TOut>(this object value, string rawRep = "")
2827
{
29-
if (value is null) throw new AndrzejFuckedUpException();
30-
if (value is TOut outValue) return outValue;
31-
32-
if (value is IInvalidable inv && inv.SafeValue is TOut outValue2)
33-
return outValue2;
34-
35-
if (typeof(TOut).IsGenericType && typeof(TOut).GetGenericTypeDefinition() == typeof(Invalidable<>))
28+
switch (value)
3629
{
37-
var innerType = typeof(TOut).GetGenericArguments()[0];
38-
if (innerType.IsInstanceOfType(value))
39-
{
40-
return (TOut)Activator.CreateInstance(typeof(TOut), value);
41-
}
30+
case null:
31+
throw new AndrzejFuckedUpException();
32+
case TOut outValue:
33+
return outValue;
4234
}
4335

4436
string valueRep = "";
@@ -50,11 +42,6 @@ public static TryGet<TOut> TryCast<TOut>(this object value, string rawRep = "")
5042
return $"{valueRep}{value.FriendlyTypeName()} is not a {typeof(TOut).FriendlyTypeName()}";
5143
}
5244

53-
private static Type Unwrap(Type type) =>
54-
(type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Invalidable<>))
55-
? type.GetGenericArguments()[0]
56-
: type;
57-
5845
extension(BaseToken token)
5946
{
6047
public bool CanReturn<T>([NotNullWhen(true)] out Func<TryGet<T>>? get) where T : Value
@@ -106,15 +93,7 @@ public bool CapableOf<T>([NotNullWhen(true)] out Func<TryGet<T>>? get) where T :
10693
if (!valToken.PossibleValues.AreKnown(out var knownReturnTypes)) return true;
10794

10895
// if any of known types is assignable to T, or T to type, then it may return T
109-
return knownReturnTypes.Any(type =>
110-
{
111-
if (typeof(T).IsAssignableFrom(type) || type.IsAssignableFrom(typeof(T))) return true;
112-
113-
var unwrappedTarget = Unwrap(typeof(T));
114-
var unwrappedSource = Unwrap(type);
115-
116-
return unwrappedTarget.IsAssignableFrom(unwrappedSource) || unwrappedSource.IsAssignableFrom(unwrappedTarget);
117-
});
96+
return knownReturnTypes.Any(type => typeof(T).IsAssignableFrom(type) || type.IsAssignableFrom(typeof(T)));
11897
}
11998

12099
public TryGet<T> TryGet<T>() where T : Value

Code/FlagSystem/Flags/OnCRoleFlag.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using SER.Code.ArgumentSystem.Arguments;
2-
using SER.Code.ValueSystem;
32
using SER.Code.Extensions;
43
using SER.Code.FlagSystem.Structures;
54
using SER.Code.MethodSystem.Methods.CustomRoleMethods.Structures;
@@ -73,8 +72,8 @@ public override void OnParsingComplete()
7372
script.CompileWithAutomaticThrow();
7473

7574
script.AddLocalVariables(
76-
new PlayerVariable("evPlayer", new PlayerValue(plr)),
77-
new ReferenceVariable("evCRole", new ReferenceValue(role))
75+
new PlayerVariable("evPlayer", new(plr)),
76+
new ReferenceVariable("evCRole", new(role))
7877
);
7978

8079
script.Run(RunReason.Event);

Code/Helpers/NumericExpressionReslover.cs

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -109,44 +109,35 @@ private static Result ParseToken(
109109
{
110110
switch (token)
111111
{
112-
case IValueToken valueToken:
112+
case ReferenceVariableToken referenceVariable:
113113
{
114-
valueToken.CapableOf<ReferenceValue>(out var referencefGet);
115-
valueToken.CapableOf<LiteralValue>(out var literalGet);
116-
117-
if (referencefGet is null && literalGet is null)
118-
{
119-
goto default;
120-
}
121-
122114
var tmp = MakeTempName();
123-
124-
if (referencefGet is not null && literalGet is not null)
115+
variables[tmp] = new(() =>
125116
{
126-
variables[tmp] = new(() =>
117+
if (referenceVariable.ExactValue.HasErrored(out var error, out var value))
127118
{
128-
var literalVal = literalGet();
129-
if (literalVal.WasSuccessful(out var literalValue))
130-
{
131-
return literalValue.Value;
132-
}
133-
134-
var refVal = referencefGet();
135-
if (refVal.WasSuccessful(out var refValue))
136-
{
137-
return refValue.ToString();
138-
}
139-
140-
return TryGet<object>.Error($"{valueToken} did not return a reference value nor a literal value.");
141-
});
142-
}
143-
else if (referencefGet is not null)
119+
return mainErr + error;
120+
}
121+
122+
return value.IsValid
123+
? value.ToString()
124+
: "invalid";
125+
});
126+
127+
AppendRaw(tmp);
128+
return true;
129+
}
130+
case IValueToken valueToken when valueToken.CapableOf<LiteralValue>(out var get):
131+
{
132+
var tmp = MakeTempName();
133+
134+
if (valueToken.IsConstant)
144135
{
145-
variables[tmp] = new(() => referencefGet.Invoke().OnSuccess(v => v.ToString()));
136+
variables[tmp] = get().OnSuccess(s => s.Value, mainErr);
146137
}
147-
else if (literalGet is not null)
138+
else
148139
{
149-
variables[tmp] = new(() => literalGet.Invoke().OnSuccess(v => v.Value));
140+
variables[tmp] = new(() => get().OnSuccess(s => s.Value, mainErr));
150141
}
151142

152143
AppendRaw(tmp);

Code/MethodSystem/BaseMethods/Method.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,6 @@ public override string ToString()
7373
? $"{Name} method in line {LineNum}"
7474
: $"{Name} method";
7575
}
76-
77-
protected static string NameOfMethod(Type type) => type.Name[..^"Method".Length].Replace("_", ".");
76+
77+
public static string NameOfMethod(Type type) => type.Name[..^"Method".Length].Replace("_", ".");
7878
}

Code/MethodSystem/Methods/CollectionVariableMethods/Coll_RemoveMethod.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public override void Execute()
3232
Script.AddLocalVariable(
3333
new CollectionVariable(
3434
collectionVar.Name,
35-
CollectionValue.Remove(collectionVar, value, amountToRemove)
35+
CollectionValue.Remove(collectionVar.Value, value, amountToRemove)
3636
)
3737
);
3838
}
Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
using SER.Code.ArgumentSystem.Arguments;
22
using SER.Code.ArgumentSystem.BaseArguments;
3+
using SER.Code.Exceptions;
34
using SER.Code.MethodSystem.BaseMethods.Synchronous;
45
using SER.Code.MethodSystem.MethodDescriptors;
5-
using SER.Code.ValueSystem;
66
using SER.Code.ValueSystem.Other;
77

88
namespace SER.Code.MethodSystem.Methods.PlayerDataMethods;
99

1010
[UsedImplicitly]
11-
public class GetPlayerDataMethod : ReturningMethod, IAdditionalDescription
11+
public class GetPlayerDataMethod : ReturningMethod, IAdditionalDescription, ICanError
1212
{
1313
public override string Description => "Gets player data from the key.";
1414

15-
public string AdditionalDescription => "If the key does not exist, invalid value will be returned.";
15+
public string AdditionalDescription =>
16+
"WARNING: This method will error if the key doesn't exist. " +
17+
$"Use {NameOfMethod(typeof(HasPlayerDataMethod))} to verify if a key exists before calling this method.";
1618

1719
public override TypeOfValue Returns => new UnknownTypeOfValue();
18-
20+
21+
public string[] ErrorReasons { get; } =
22+
[
23+
"Key was not found for the player."
24+
];
25+
1926
public override Argument[] ExpectedArguments { get; } =
2027
[
2128
new PlayerArgument("player"),
@@ -27,14 +34,12 @@ public override void Execute()
2734
var player = Args.GetPlayer("player");
2835
var key = Args.GetText("key");
2936

30-
if (SetPlayerDataMethod.PlayerData.TryGetValue(player, out var dict) &&
31-
dict.TryGetValue(key, out var value))
32-
{
33-
ReturnValue = value;
34-
}
35-
else
37+
if (!SetPlayerDataMethod.PlayerData.TryGetValue(player, out var dict) ||
38+
!dict.TryGetValue(key, out var value))
3639
{
37-
ReturnValue = new InvalidValue();
40+
throw new ScriptRuntimeError(this, ErrorReasons[0]);
3841
}
42+
43+
ReturnValue = value;
3944
}
4045
}

Code/MethodSystem/Methods/VariableMethods/LogVarMethod.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class LogVarMethod : ReturningMethod<TextValue>
2020
public override void Execute()
2121
{
2222
var variable = Args.GetVariable("variable");
23-
ReturnValue = $"{variable} = {(variable is LiteralVariable lv ? lv.ExactValue : variable.BaseValue)}"
23+
ReturnValue = $"{variable} = {(variable is LiteralVariable lv ? lv.Value : variable.BaseValue)}"
2424
.ToStaticTextValue();
2525
}
2626
}

Code/Plugin/Commands/HelpSystem/HelpInfoStorage.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using Interactables.Interobjects;
22
using LabApi.Features.Enums;
33
using MapGeneration;
4-
using PlayerRoles;
54
using SER.Code.FlagSystem.Flags;
65

76
namespace SER.Code.Plugin.Commands.HelpSystem;
@@ -15,7 +14,6 @@ public static class HelpInfoStorage
1514
typeof(DoorName),
1615
typeof(ItemType),
1716
typeof(ElevatorGroup),
18-
typeof(CustomCommandFlag.ConsoleType),
19-
typeof(Team)
17+
typeof(CustomCommandFlag.ConsoleType)
2018
];
2119
}

Code/ScriptSystem/Script.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
using SER.Code.TokenSystem.Structures;
1717
using SER.Code.TokenSystem.Tokens;
1818
using SER.Code.TokenSystem.Tokens.VariableTokens;
19-
using SER.Code.ValueSystem;
2019
using SER.Code.VariableSystem;
2120
using SER.Code.VariableSystem.Bases;
2221
using SER.Code.VariableSystem.Structures;
@@ -43,12 +42,12 @@ public required ScriptExecutor Executor
4342
{
4443
case RemoteAdminExecutor { Sender: { } sender } when Player.Get(sender) is { } player:
4544
{
46-
AddLocalVariable(new PlayerVariable("sender", new PlayerValue(player)));
45+
AddLocalVariable(new PlayerVariable("sender", new(player)));
4746
break;
4847
}
4948
case PlayerConsoleExecutor { Sender: { } hub } when Player.Get(hub) is { } player:
5049
{
51-
AddLocalVariable(new PlayerVariable("sender", new PlayerValue(player)));
50+
AddLocalVariable(new PlayerVariable("sender", new(player)));
5251
break;
5352
}
5453
}

Code/TokenSystem/Tokenizer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ public static class Tokenizer
2929
typeof(CollectionVariableToken),
3030
typeof(ReferenceVariableToken),
3131
typeof(DurationToken),
32-
typeof(RunFunctionToken),
33-
typeof(InvalidToken)
32+
typeof(RunFunctionToken)
3433
];
3534

3635
public static readonly Type[] OrderedImportanceTokensFromCollectionSlices =

0 commit comments

Comments
 (0)