Skip to content

Commit 794c89a

Browse files
change RunReason and improve flags
1 parent 9bccca1 commit 794c89a

14 files changed

Lines changed: 56 additions & 59 deletions

File tree

Code/EventSystem/EventHandler.cs

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ private static void OnNonArgumentedEvent(string evName)
145145
continue;
146146
}
147147

148-
script.Run(RunContext.Event);
148+
script.Run(RunReason.Event);
149149
}
150150
}
151151

@@ -179,7 +179,7 @@ private static void OnArgumentedEvent<T>(string evName, T ev) where T : EventArg
179179
}
180180

181181
script.AddLocalVariables(variables);
182-
var isAllowed = script.RunForEvent(RunContext.Event);
182+
var isAllowed = script.RunForEvent(RunReason.Event);
183183
if (isAllowed.HasValue && ev is ICancellableEvent cancellable1)
184184
cancellable1.IsAllowed = isAllowed.Value;
185185
}
@@ -219,37 +219,12 @@ where value is not null
219219
private static Variable[] InternalGetVariablesFromProperties(List<(object value, string name, Type type)> properties)
220220
{
221221
List<Variable> variables = [];
222-
foreach (var (value, name, type) in properties)
222+
foreach (var (value, name, _) in properties)
223223
{
224-
switch (value)
225-
{
226-
case Enum enumValue:
227-
variables.Add(new LiteralVariable<TextValue>(GetName(), new StaticTextValue(enumValue.ToString())));
228-
continue;
229-
case Player player:
230-
variables.Add(new PlayerVariable(GetName(), new(player)));
231-
continue;
232-
case IEnumerable<Player> players:
233-
variables.Add(new PlayerVariable(GetName(), new(players)));
234-
continue;
235-
case null:
236-
if (type == typeof(Player))
237-
{
238-
// todo: wtf is this?
239-
// variables.Add(new PlayerVariable(GetName(), []));
240-
}
241-
continue;
242-
default:
243-
{
244-
variables.Add(Variable.Create(GetName(), Value.Parse(value, null)));
245-
continue;
246-
}
247-
}
248-
249-
string GetName()
250-
{
251-
return $"ev{name.First().ToString().ToUpper()}{name[1..]}";
252-
}
224+
variables.Add(Variable.Create(
225+
$"ev{name.First().ToString().ToUpper()}{name[1..]}",
226+
Value.Parse(value, null))
227+
);
253228
}
254229

255230
return variables.ToArray();

Code/FlagSystem/Flags/CustomCommandFlag.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,12 @@ public override void Unbind()
158158

159159
public override Result OnScriptRunning(Script scr)
160160
{
161-
if (scr.Context == RunContext.CustomCommand)
161+
if (scr.HasFlag<OnEventFlag>())
162+
{
163+
return $"Detected conflicting flag: {nameof(OnEventFlag)}.";
164+
}
165+
166+
if (scr.RunReason == RunReason.CustomCommand)
162167
{
163168
return true;
164169
}
@@ -256,7 +261,7 @@ public static Result RunAttachedScript(CustomCommand cmd, ScriptExecutor sender,
256261
script.AddLocalVariable(new LiteralVariable<TextValue>(name, new StaticTextValue(slice.Value)));
257262
}
258263

259-
script.Run(RunContext.CustomCommand);
264+
script.Run(RunReason.CustomCommand);
260265
return true;
261266
}
262267

Code/FlagSystem/Flags/InteractableToyEventFlag.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static void RunBoundScripts(Player player, InteractableToy interactableTo
5555
}
5656

5757
script.AddLocalVariables(variables);
58-
script.Run(RunContext.Event);
58+
script.Run(RunReason.Event);
5959
}
6060
}
6161
}

Code/FlagSystem/Flags/OnEventFlag.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using SER.Code.Helpers;
33
using SER.Code.Helpers.ResultSystem;
44
using SER.Code.ScriptSystem;
5+
using SER.Code.ScriptSystem.Structures;
56
using EventHandler = SER.Code.EventSystem.EventHandler;
67

78
namespace SER.Code.FlagSystem.Flags;
@@ -41,7 +42,12 @@ public class OnEventFlag : Flag
4142

4243
public override Result OnScriptRunning(Script scr)
4344
{
44-
if (scr.Context == RunContext.Event)
45+
if (scr.HasFlag<CustomCommandFlag>())
46+
{
47+
return $"Detected conflicting flag: {nameof(CustomCommandFlag)}.";
48+
}
49+
50+
if (scr.RunReason == RunReason.Event)
4551
{
4652
return true;
4753
}

Code/MethodSystem/Methods/ScriptMethods/RunFuncMethod.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using SER.Code.MethodSystem.BaseMethods.Synchronous;
99
using SER.Code.MethodSystem.MethodDescriptors;
1010
using SER.Code.ScriptSystem;
11+
using SER.Code.ScriptSystem.Structures;
1112
using SER.Code.ValueSystem;
1213
using SER.Code.VariableSystem.Bases;
1314

@@ -83,6 +84,6 @@ public override void Execute()
8384
scriptToRun.AddLocalVariable(variable);
8485
}
8586

86-
scriptToRun.Run(RunContext.Script, Script);
87+
scriptToRun.Run(RunReason.Script, Script);
8788
}
8889
}

Code/MethodSystem/Methods/ScriptMethods/RunScriptAndWaitMethod.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using SER.Code.ArgumentSystem.BaseArguments;
55
using SER.Code.MethodSystem.BaseMethods.Yielding;
66
using SER.Code.ScriptSystem;
7+
using SER.Code.ScriptSystem.Structures;
78
using SER.Code.VariableSystem.Bases;
89

910
namespace SER.Code.MethodSystem.Methods.ScriptMethods;
@@ -30,7 +31,7 @@ public override IEnumerator<float> Execute()
3031
var variables = Args.GetRemainingArguments<Variable, VariableArgument>("variables to pass");
3132

3233
script.AddLocalVariables(variables);
33-
script.Run(RunContext.Script, Script);
34+
script.Run(RunReason.Script, Script);
3435

3536
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
3637
while (script.IsRunning)

Code/MethodSystem/Methods/ScriptMethods/RunScriptMethod.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using SER.Code.ArgumentSystem.BaseArguments;
44
using SER.Code.MethodSystem.BaseMethods.Synchronous;
55
using SER.Code.ScriptSystem;
6+
using SER.Code.ScriptSystem.Structures;
67
using SER.Code.VariableSystem.Bases;
78

89
namespace SER.Code.MethodSystem.Methods.ScriptMethods;
@@ -29,6 +30,6 @@ public override void Execute()
2930
var variables = Args.GetRemainingArguments<Variable, VariableArgument>("variables to pass");
3031

3132
script.AddLocalVariables(variables);
32-
script.Run(RunContext.Script, Script);
33+
script.Run(RunReason.Script, Script);
3334
}
3435
}

Code/MethodSystem/Methods/ScriptMethods/ThisMethod.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using SER.Code.FlagSystem;
77
using SER.Code.MethodSystem.BaseMethods.Synchronous;
88
using SER.Code.ScriptSystem;
9+
using SER.Code.ScriptSystem.Structures;
910
using SER.Code.ValueSystem;
1011

1112
namespace SER.Code.MethodSystem.Methods.ScriptMethods;
@@ -20,7 +21,7 @@ public class ThisMethod : ReturningMethod
2021
new OptionsArgument("info to receive",
2122
"flags",
2223
new("caller","The name of the script that called this script"),
23-
Option.Enum<RunContext>("context"),
24+
Option.Enum<RunReason>("context"),
2425
new("duration","The amount of time the script's been running for"),
2526
"name",
2627
new("path", "The path to the script on the local directory of the server"),
@@ -40,7 +41,7 @@ public override void Execute()
4041
"flags" => new CollectionValue(ScriptFlagHandler.GetScriptFlags(Script.Name)
4142
.Select(f => f.GetType().Name.Replace("Flag", ""))),
4243
"caller" => new StaticTextValue(Script.Caller?.Name ?? "none"),
43-
"context" => new StaticTextValue(Script.Context.ToString()),
44+
"context" => new StaticTextValue(Script.RunReason.ToString()),
4445
"duration" => new DurationValue(Script.TimeRunning),
4546
"name" => new StaticTextValue(Script.Name),
4647
"path" => new StaticTextValue(FileSystem.FileSystem.GetScriptPath(Script)),

Code/MethodSystem/Methods/ScriptMethods/TriggerMethod.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public override void Execute()
3333
throw new ScriptRuntimeError(this, error);
3434
}
3535

36-
script.Run(RunContext.Script, Script);
36+
script.Run(RunReason.Script, Script);
3737
}
3838
}
3939
}

Code/Plugin/CommandEvents.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,6 @@ public static void CaptureCommand(CommandExecutingEventArgs ev)
7474

7575
ev.Sender.Respond($"Running method '{methodName}'!");
7676
ev.IsAllowed = false;
77-
script.Run(RunContext.BaseCommand);
77+
script.Run(RunReason.BaseCommand);
7878
}
7979
}

0 commit comments

Comments
 (0)