Skip to content

Commit 086715f

Browse files
add a way for ser methods to be ran using a prefix
1 parent 7a6d9b1 commit 086715f

File tree

6 files changed

+102
-29
lines changed

6 files changed

+102
-29
lines changed

Plugin/CommandEvents.cs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using CommandSystem;
4+
using LabApi.Events.Arguments.ServerEvents;
5+
using LabApi.Events.Handlers;
6+
using LabApi.Features.Enums;
7+
using LabApi.Features.Permissions;
8+
using LabApi.Features.Wrappers;
9+
using SER.Helpers.Extensions;
10+
using SER.Plugin.Commands;
11+
using SER.ScriptSystem;
12+
using SER.ScriptSystem.Structures;
13+
using SER.TokenSystem;
14+
using SER.TokenSystem.Tokens;
15+
16+
namespace SER.Plugin;
17+
18+
public static class CommandEvents
19+
{
20+
public static readonly Dictionary<ICommandSender, CommandType> UsedCommandTypes = [];
21+
22+
public static void Initialize()
23+
{
24+
UsedCommandTypes.Clear();
25+
ServerEvents.CommandExecuting += CaptureComamnd;
26+
}
27+
28+
public static void CaptureComamnd(CommandExecutingEventArgs ev)
29+
{
30+
UsedCommandTypes[ev.Sender] = ev.CommandType;
31+
32+
if (MainPlugin.Instance.Config?.SerMethodsAsCommands != true)
33+
{
34+
return;
35+
}
36+
37+
if (Player.Get(ev.Sender) is { } player && player.HasPermissions(MethodCommand.RunPermission))
38+
{
39+
return ;
40+
}
41+
42+
if (!ev.CommandName.StartsWith(">"))
43+
{
44+
return;
45+
}
46+
47+
var methodName = ev.CommandName.Substring(1);
48+
49+
if (!methodName.Any())
50+
{
51+
return;
52+
}
53+
54+
var script = new Script
55+
{
56+
Name = methodName,
57+
Content = $"{methodName} {ev.Arguments.JoinStrings(" ")}",
58+
Executor = ScriptExecutor.Get(ev.Sender, ev.CommandType)
59+
};
60+
61+
if (Tokenizer.SliceLine(methodName).HasErrored(out _, out var slices))
62+
{
63+
return;
64+
}
65+
66+
// check if the a method like this exists
67+
var instance = new MethodToken();
68+
var res = (BaseToken.IParseResult) typeof(MethodToken)
69+
.GetMethod(nameof(MethodToken.TryInit))!
70+
.Invoke(instance, [slices.First(), script, null]);
71+
72+
if (res is not BaseToken.Success)
73+
{
74+
return;
75+
}
76+
77+
ev.IsAllowed = false;
78+
script.Run();
79+
}
80+
}

Plugin/Commands/MethodCommand.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ namespace SER.Plugin.Commands;
1313
[CommandHandler(typeof(RemoteAdminCommandHandler))]
1414
public class MethodCommand : ICommand, IUsePermissions
1515
{
16+
public static string RunPermission => "ser.run";
17+
1618
public bool Execute(ArraySegment<string> arguments, ICommandSender sender, out string response)
1719
{
1820
var player = Player.Get(sender);
19-
if (player is not null && player.HasPermissions(Permission))
21+
if (player is not null && player.HasPermissions(RunPermission))
2022
{
2123
response = "You do not have permission to run scripts.";
2224
return false;
@@ -37,5 +39,5 @@ public bool Execute(ArraySegment<string> arguments, ICommandSender sender, out s
3739
public string Command => "sermethod";
3840
public string[] Aliases => [];
3941
public string Description => "Runs the provied arguments at it was a line in a script.";
40-
public string Permission => "ser.run";
42+
public string Permission => RunPermission;
4143
}

Plugin/Config.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace SER.Plugin;
2+
3+
public class Config
4+
{
5+
public bool SerMethodsAsCommands { get; set; } = false;
6+
}

Plugin/MainPlugin.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@
99
using SER.MethodSystem;
1010
using SER.MethodSystem.Methods.LiteralVariableMethods;
1111
using SER.ScriptSystem;
12-
using SER.ScriptSystem.Structures;
1312
using SER.VariableSystem;
1413
using EventHandler = SER.EventSystem.EventHandler;
1514
using Events = LabApi.Events.Handlers;
1615

1716
namespace SER.Plugin;
1817

1918
[UsedImplicitly]
20-
public class MainPlugin : LabApi.Loader.Features.Plugins.Plugin
19+
public class MainPlugin : LabApi.Loader.Features.Plugins.Plugin<Config>
2120
{
2221
public override string Name => "SER";
2322
public override string Description => "The scripting language for SCP:SL.";
@@ -62,7 +61,7 @@ public override void Enable()
6261
MethodIndex.Initialize();
6362
VariableIndex.Initialize();
6463
Flag.RegisterFlags();
65-
ScriptExecutor.Initialize();
64+
CommandEvents.Initialize();
6665
SendLogo();
6766

6867
Events.ServerEvents.WaitingForPlayers += OnServerFullyInit;

SER.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,11 @@
305305
<Compile Include="Plugin\Commands\RunCommand.cs" />
306306
<Compile Include="Plugin\Commands\StopAllCommand.cs" />
307307
<Compile Include="Plugin\Commands\StopCommand.cs" />
308+
<Compile Include="Plugin\Config.cs" />
308309
<Compile Include="Plugin\FileSystem.cs" />
309310
<Compile Include="Plugin\MainPlugin.cs" />
310311
<Compile Include="Helpers\Log.cs" />
312+
<Compile Include="Plugin\CommandEvents.cs" />
311313
<Compile Include="ScriptSystem\Script.cs" />
312314
<Compile Include="ScriptSystem\Structures\PlayerConsoleExecutor.cs" />
313315
<Compile Include="ScriptSystem\Structures\RemoteAdminExecutor.cs" />
Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
using System.Collections.Generic;
2-
using CommandSystem;
3-
using LabApi.Events.Arguments.ServerEvents;
4-
using LabApi.Events.Handlers;
1+
using CommandSystem;
52
using LabApi.Features.Enums;
63
using RemoteAdmin;
74
using SER.Helpers.Exceptions;
5+
using SER.Plugin;
86
using Logger = LabApi.Features.Console.Logger;
97

108
namespace SER.ScriptSystem.Structures;
@@ -14,19 +12,22 @@ public abstract class ScriptExecutor
1412
public abstract void Reply(string content, Script scr);
1513
public abstract void Warn(string content, Script scr);
1614
public abstract void Error(string content, Script scr);
17-
18-
public static readonly Dictionary<ICommandSender, CommandType> UsedCommandTypes = [];
1915

2016
public static ScriptExecutor Get() => ServerConsoleExecutor.Instance;
2117

2218
public static ScriptExecutor Get(ICommandSender sender)
2319
{
24-
if (!UsedCommandTypes.TryGetValue(sender, out var type))
20+
if (!CommandEvents.UsedCommandTypes.TryGetValue(sender, out var type))
2521
{
2622
Logger.Warn("A command was sent, but cannot infer the command type used. Switching to server.");
2723
return ServerConsoleExecutor.Instance;
2824
}
2925

26+
return Get(sender, type);
27+
}
28+
29+
public static ScriptExecutor Get(ICommandSender sender, CommandType type)
30+
{
3031
if (type == CommandType.Console) return ServerConsoleExecutor.Instance;
3132

3233
if (sender is not PlayerCommandSender playerSender)
@@ -43,21 +44,4 @@ public static ScriptExecutor Get(ICommandSender sender)
4344
_ => throw new AndrzejFuckedUpException()
4445
};
4546
}
46-
47-
public static void Initialize()
48-
{
49-
ServerEvents.CommandExecuting += OnCommandExecuting;
50-
UsedCommandTypes.Clear();
51-
}
52-
53-
public static void Disable()
54-
{
55-
ServerEvents.CommandExecuting -= OnCommandExecuting;
56-
UsedCommandTypes.Clear();
57-
}
58-
59-
public static void OnCommandExecuting(CommandExecutingEventArgs ev)
60-
{
61-
UsedCommandTypes[ev.Sender] = ev.CommandType;
62-
}
6347
}

0 commit comments

Comments
 (0)