Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ jobs:
${{ env.WINDOWS_FOLDER }}-with-runtimes.zip

releasing:
if: ${{ github.event_name == 'push' }}
if: ${{ github.event_name == 'push' && (github.ref == 'refs/heads/master' || github.ref == 'refs/heads/beta') }}
needs: [versioning, packaging]
runs-on: ubuntu-latest
env:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public static void ParseFromObject(this ICommandService self, object instance) {
var commandName = commandAttribute.Name;
var commandAlias = commandAliasAttributes.Select(a => a.Alias).ToArray();
var registerRaw = commandAttribute.RegisterRaw;
self.RegisterCommand(commandName, method.CreateDelegate<ICommandService.CommandListener>(instance), registerRaw);
var permission = commandAttribute.Permission;
self.RegisterCommand(commandName, method.CreateDelegate<ICommandService.CommandListener>(instance), registerRaw, permission);
foreach (var alias in commandAlias)
{
self.RegisterCommandAlias(commandName, alias, registerRaw);
Expand Down
19 changes: 16 additions & 3 deletions managed/src/SwiftlyS2.Core/Modules/Commands/CommandCallback.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using System.Runtime.InteropServices;
using SwiftlyS2.Core.Natives;
using SwiftlyS2.Core.Services;
using SwiftlyS2.Shared.Misc;
using SwiftlyS2.Shared.Commands;
using SwiftlyS2.Shared.Permissions;
using SwiftlyS2.Shared.Players;
using Microsoft.Extensions.Logging;
using SwiftlyS2.Shared.Profiler;
using SwiftlyS2.Shared;

namespace SwiftlyS2.Core.Commands;

Expand Down Expand Up @@ -40,17 +44,22 @@ internal class CommandCallback : CommandCallbackBase {

private nint _unmanagedCallbackPtr;
private ulong _nativeListenerId;
private string _permissions;

private ILogger<CommandCallback> _logger;
private readonly IPlayerManagerService _playerManagerService;
private readonly IPermissionManager _permissionManager;

public CommandCallback(string commandName, bool registerRaw, ICommandService.CommandListener handler, ILoggerFactory loggerFactory, IContextedProfilerService profiler)
public CommandCallback(string commandName, bool registerRaw, ICommandService.CommandListener handler, string permission, IPlayerManagerService playerManagerService, IPermissionManager permissionManager, ILoggerFactory loggerFactory, IContextedProfilerService profiler)
: base(loggerFactory, profiler)
{
_logger = LoggerFactory.CreateLogger<CommandCallback>();
_playerManagerService = playerManagerService;
_permissionManager = permissionManager;
Guid = Guid.NewGuid();

CommandName = commandName;

_permissions = permission;
_handler = handler;

_unmanagedCallback = (playerId, argsPtr, commandNamePtr, prefixPtr, slient) => {
Expand All @@ -63,7 +72,11 @@ public CommandCallback(string commandName, bool registerRaw, ICommandService.Com

var args = argsString.Split('\x01');
var context = new CommandContext(playerId, args, commandNameString, prefixString, slient);
_handler(context);
if (string.IsNullOrWhiteSpace(_permissions) || _permissionManager.PlayerHasPermission(_playerManagerService.GetPlayer(playerId).SteamID, _permissions)) {
_handler(context);
} else {
context.Reply("You do not have permission to use this command.");
}
Profiler.StopRecording(category);
} catch (Exception e) {
_logger.LogError(e, "Failed to handle command {0}.", commandName);
Expand Down
17 changes: 10 additions & 7 deletions managed/src/SwiftlyS2.Core/Modules/Commands/CommandService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using SwiftlyS2.Shared.Commands;
using SwiftlyS2.Shared.Plugins;
using SwiftlyS2.Shared.Profiler;
using SwiftlyS2.Shared.Players;
using SwiftlyS2.Shared.Permissions;

namespace SwiftlyS2.Core.Commands;

Expand All @@ -14,21 +16,22 @@ internal class CommandService : ICommandService, IDisposable
private ILogger<CommandService> _Logger { get; init; }
private ILoggerFactory _LoggerFactory { get; init; }
private IContextedProfilerService _Profiler { get; init; }
private IPlayerManagerService _PlayerManagerService { get; init; }
private IPermissionManager _PermissionManager { get; init; }

private object _lock = new();

public CommandService(ILogger<CommandService> logger, ILoggerFactory loggerFactory, IContextedProfilerService profiler)
{
public CommandService(ILogger<CommandService> logger, ILoggerFactory loggerFactory, IContextedProfilerService profiler, IPlayerManagerService playerManagerService, IPermissionManager permissionManager) {
_Logger = logger;
_LoggerFactory = loggerFactory;
_Profiler = profiler;
_PlayerManagerService = playerManagerService;
_PermissionManager = permissionManager;
}

public Guid RegisterCommand(string commandName, ICommandService.CommandListener handler, bool registerRaw = false)
{
var callback = new CommandCallback(commandName, registerRaw, handler, _LoggerFactory, _Profiler);
lock (_lock)
{
public Guid RegisterCommand(string commandName, ICommandService.CommandListener handler, bool registerRaw = false, string permission = "") {
var callback = new CommandCallback(commandName, registerRaw, handler, permission, _PlayerManagerService, _PermissionManager, _LoggerFactory, _Profiler);
lock (_lock) {
_callbacks.Add(callback);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ public class Command : Attribute {

public bool RegisterRaw { get; set; } = false;

public Command(string name, bool registerRaw = false) {
public string Permission { get; set; } = "";

public Command(string name, bool registerRaw = false, string permission = "") {
Name = name;
RegisterRaw = registerRaw;
Permission = permission;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ public interface ICommandService
/// <param name="commandName">The command name.</param>
/// <param name="handler">The handler callback for the command.</param>
/// <param name="registerRaw">If set to false, the command will not starts with a `sw_` prefix.</param>
/// <param name="permission">The permission required to use the command.</param>
/// <returns>The guid of the command.</returns>
Guid RegisterCommand(string commandName, CommandListener handler, bool registerRaw = false);
Guid RegisterCommand(string commandName, CommandListener handler, bool registerRaw = false, string permission = "");

/// <summary>
/// Registers a command alias.
Expand Down
12 changes: 11 additions & 1 deletion managed/src/TestPlugin/TestPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@


public class TestConfig {
public string Name { get; set; }

Check warning on line 29 in managed/src/TestPlugin/TestPlugin.cs

View workflow job for this annotation

GitHub Actions / managed

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public int Age { get; set; }
}

[PluginMetadata(Id = "testplugin", Version = "1.0.0")]
public class TestPlugin : BasePlugin {

public TestPlugin(ISwiftlyCore core) : base(core) {

Check warning on line 36 in managed/src/TestPlugin/TestPlugin.cs

View workflow job for this annotation

GitHub Actions / managed

Non-nullable field '_config' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.
}

public override void ConfigureSharedInterface(IInterfaceManager interfaceManager) {
Expand Down Expand Up @@ -264,7 +264,7 @@

[EventListener<EventDelegates.OnEntityCreated>]
public void OnEntityCreated(IOnEntityCreatedEvent @event) {
@event.Entity.Entity.DesignerName = "abc";
// @event.Entity.Entity.DesignerName = "abc";
Console.WriteLine("TestPlugin OnEntityCreated222 " + @event.Entity.Entity?.DesignerName);
}

Expand Down Expand Up @@ -292,6 +292,16 @@
Console.WriteLine(Core.Permission.PlayerHasPermission(7656, context.Args[0]));
}

[Command("tt5")]
public void TestCommand5(ICommandContext context) {
Console.WriteLine("TestPlugin TestCommand5");
}

[Command("tt6", permission: "tt6")]
public void TestCommand6(ICommandContext context) {
Console.WriteLine("TestPlugin TestCommand6");
}

[GameEventHandler(HookMode.Pre)]
public HookResult TestGameEventHandler(EventPlayerJump @e) {
Console.WriteLine(@e.UserIdController.PlayerName);
Expand Down
Loading