Skip to content

Commit b86c1b9

Browse files
use ScriptName type instead of string
1 parent 003c0fd commit b86c1b9

File tree

6 files changed

+82
-41
lines changed

6 files changed

+82
-41
lines changed

EventSystem/EventHandler.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace SER.EventSystem;
2323
public static class EventHandler
2424
{
2525
private static readonly Dictionary<string, Action> UnsubscribeActions = [];
26-
private static readonly Dictionary<string, List<string>> ScriptsUsingEvent = [];
26+
private static readonly Dictionary<string, List<ScriptName>> ScriptsUsingEvent = [];
2727
private static readonly HashSet<string> DisabledEvents = [];
2828
public static List<EventInfo> AvailableEvents = [];
2929

@@ -44,7 +44,7 @@ internal static void EventClear()
4444
DisabledEvents.Clear();
4545
}
4646

47-
internal static void DisableEvent(string evName, string scriptName)
47+
internal static void DisableEvent(string evName, ScriptName scriptName)
4848
{
4949
DisabledEvents.Add(evName);
5050
ConnectEvent(evName, scriptName, false);
@@ -59,7 +59,7 @@ internal static void EnableEvent(string evName, bool unsubscribe = false)
5959
}
6060
}
6161

62-
internal static Result ConnectEvent(string evName, string scriptName, bool allowNonArg = true)
62+
internal static Result ConnectEvent(string evName, ScriptName scriptName, bool allowNonArg = true)
6363
{
6464
if (ScriptsUsingEvent.TryGetValue(evName, out var scriptsConnected))
6565
{
@@ -144,7 +144,7 @@ private static void OnNonArgumentedEvent(string evName)
144144
Result rs = $"Failed to run script '{scrName}' connected to event '{evName}'";
145145
if (Script.CreateByScriptName(scrName, ScriptExecutor.Get()).HasErrored(out var error, out var script))
146146
{
147-
Log.Error(scrName, rs + error);
147+
Log.CompileError(scrName, rs + error);
148148
continue;
149149
}
150150

@@ -176,7 +176,7 @@ private static void OnArgumentedEvent<T>(string evName, T ev) where T : EventArg
176176
Log.Debug($"Running script '{scrName}' for event '{evName}'");
177177
if (Script.CreateByScriptName(scrName, ScriptExecutor.Get()).HasErrored(out var error, out var script))
178178
{
179-
Log.Error(scrName, rs + error);
179+
Log.CompileError(scrName, rs + error);
180180
continue;
181181
}
182182

Plugin/CommandEvents.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static void CaptureComamnd(CommandExecutingEventArgs ev)
5353

5454
var script = new Script
5555
{
56-
Name = methodName,
56+
Name = ScriptName.InitUnchecked(methodName),
5757
Content = $"{methodName} {ev.Arguments.JoinStrings(" ")}",
5858
Executor = ScriptExecutor.Get(ev.Sender, ev.CommandType)
5959
};

Plugin/Commands/MethodCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public bool Execute(ArraySegment<string> arguments, ICommandSender sender, out s
2626

2727
var script = new Script
2828
{
29-
Name = "Command",
29+
Name = ScriptName.InitUnchecked("Command"),
3030
Content = string.Join(" ", arguments.ToArray()),
3131
Executor = ScriptExecutor.Get(sender)
3232
};

Plugin/FileSystem.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
using System.IO;
1+
using System;
2+
using System.IO;
23
using System.Linq;
34
using System.Reflection;
45
using LabApi.Features.Console;
56
using LabApi.Loader.Features.Paths;
67
using SER.Examples;
78
using SER.FlagSystem;
9+
using SER.Helpers;
810
using SER.Helpers.Extensions;
911
using SER.ScriptSystem;
1012
using SER.ScriptSystem.Structures;
@@ -51,22 +53,29 @@ public static void Initialize()
5153
Directory.CreateDirectory(DirPath);
5254
return;
5355
}
54-
56+
5557
UpdateScriptPathCollection();
5658
ScriptFlagHandler.Clear();
5759

5860
foreach (var scriptPath in RegisteredScriptPaths)
5961
{
60-
var scriptName = Path.GetFileNameWithoutExtension(scriptPath);
62+
var scriptName = ScriptName.InitUnchecked(Path.GetFileNameWithoutExtension(scriptPath));
6163

6264
var lines = Script.CreateByVerifiedPath(scriptPath, ServerConsoleExecutor.Instance).GetFlagLines();
6365
if (lines.IsEmpty())
6466
{
6567
continue;
6668
}
67-
69+
6870
ScriptFlagHandler.RegisterScript(lines, scriptName);
6971
}
72+
73+
}
74+
75+
public static string GetScriptPath(ScriptName scriptName)
76+
{
77+
UpdateScriptPathCollection();
78+
return RegisteredScriptPaths.First(p => Path.GetFileNameWithoutExtension(p) == scriptName.Value);
7079
}
7180

7281
public static bool DoesScriptExist(string scriptName, out string path)

ScriptSystem/Script.cs

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace SER.ScriptSystem;
2828

2929
public class Script
3030
{
31-
public required string Name { get; init; }
31+
public required ScriptName Name { get; init; }
3232

3333
public required string Content { get; init; }
3434

@@ -58,10 +58,9 @@ public required ScriptExecutor Executor
5858

5959
public bool IsRunning => RunningScripts.Contains(this);
6060

61-
private static readonly List<Script> _runningScripts = [];
62-
public static readonly ReadOnlyCollection<Script> RunningScripts = _runningScripts.AsReadOnly();
63-
64-
61+
private static readonly List<Script> RunningScriptsList = [];
62+
public static readonly ReadOnlyCollection<Script> RunningScripts = RunningScriptsList.AsReadOnly();
63+
6564
private readonly HashSet<Variable> _variables = [];
6665
public ReadOnlyCollection<Variable> Variables => _variables.ToList().AsReadOnly();
6766

@@ -87,39 +86,22 @@ public void Error(string message)
8786
public static TryGet<Script> CreateByScriptName(string dirtyName, ScriptExecutor? executor)
8887
{
8988
var name = Path.GetFileNameWithoutExtension(dirtyName);
90-
if (!FileSystem.DoesScriptExist(name, out var path))
91-
{
92-
return $"Script '{name}' does not exist in the SER folder or is inaccessible.";
93-
}
94-
95-
return new Script
96-
{
97-
Name = name,
98-
Content = File.ReadAllText(path),
99-
Executor = executor ?? ScriptExecutor.Get()
100-
};
101-
}
102-
103-
public static TryGet<Script> CreateByPath(string path, ScriptExecutor? executor)
104-
{
105-
var name = Path.GetFileNameWithoutExtension(path);
106-
107-
if (!FileSystem.DoesScriptExist(path))
89+
if (ScriptName.TryInit(name).HasErrored(out var initError, out var scriptName))
10890
{
109-
return $"Script '{name}' does not exist in the SER folder or is inaccessible.";
91+
return initError;
11092
}
11193

11294
return new Script
11395
{
114-
Name = name,
115-
Content = File.ReadAllText(path),
96+
Name = scriptName,
97+
Content = File.ReadAllText(FileSystem.GetScriptPath(scriptName)),
11698
Executor = executor ?? ScriptExecutor.Get()
11799
};
118100
}
119101

120102
public static Script CreateByVerifiedPath(string path, ScriptExecutor? executor) => new()
121103
{
122-
Name = Path.GetFileNameWithoutExtension(path),
104+
Name = ScriptName.InitUnchecked(Path.GetFileNameWithoutExtension(path)),
123105
Content = File.ReadAllText(path),
124106
Executor = executor ?? ScriptExecutor.Get()
125107
};
@@ -178,14 +160,14 @@ public void Run()
178160
return null;
179161
}
180162

181-
_runningScripts.Add(this);
163+
RunningScriptsList.Add(this);
182164
_scriptCoroutine = InternalExecute().Run(this, _ => _scriptCoroutine.Kill());
183165
return _isEventAllowed;
184166
}
185167

186168
public void Stop(bool silent = false)
187169
{
188-
_runningScripts.Remove(this);
170+
RunningScriptsList.Remove(this);
189171
_scriptCoroutine.Kill();
190172
if (!silent) Logger.Info($"Script {Name} was stopped");
191173
}
@@ -281,7 +263,7 @@ private IEnumerator<float> InternalExecute()
281263
}
282264
}
283265

284-
_runningScripts.Remove(this);
266+
RunningScriptsList.Remove(this);
285267
}
286268

287269
public TryGet<T> TryGetVariable<T>(VariableToken variable) where T : Variable
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using SER.Helpers.ResultSystem;
2+
using SER.Plugin;
3+
4+
namespace SER.ScriptSystem.Structures;
5+
6+
public readonly record struct ScriptName
7+
{
8+
public readonly string Value;
9+
10+
private ScriptName(string value)
11+
{
12+
Value = value;
13+
}
14+
15+
public TryGet<Script> GetScript(ScriptExecutor? executor)
16+
{
17+
executor ??= ScriptExecutor.Get();
18+
return Script.CreateByScriptName(Value, executor);
19+
}
20+
21+
public static ScriptName InitUnchecked(string name)
22+
{
23+
return new(name);
24+
}
25+
26+
public static TryGet<ScriptName> TryInit(string name)
27+
{
28+
if (FileSystem.DoesScriptExist(name))
29+
{
30+
return $"Script '{name}' does not exist in the SER folder or is inaccessible.";
31+
}
32+
33+
return new ScriptName(name);
34+
}
35+
36+
public static implicit operator string(ScriptName scriptName)
37+
{
38+
return scriptName.Value;
39+
}
40+
41+
public static implicit operator ScriptName(Script script)
42+
{
43+
return script.Name;
44+
}
45+
46+
public override string ToString()
47+
{
48+
return Value;
49+
}
50+
}

0 commit comments

Comments
 (0)