Add command permissions based on game promote level

This commit is contained in:
John Gross
2017-02-06 16:49:20 -08:00
parent 0f23e2a778
commit 650ab05d74
6 changed files with 32 additions and 24 deletions

View File

@@ -14,26 +14,4 @@ namespace Torch.API.ModAPI.Ingame
{ {
} }
public static class PistonExtensions
{
public static IMyCubeGrid GetConnectedGrid(this IMyPistonBase pistonBase)
{
if (!pistonBase.IsAttached)
return null;
return ((Sandbox.ModAPI.IMyPistonBase)pistonBase).TopGrid;
}
}
public static class RotorExtensions
{
public static IMyCubeGrid GetConnectedGrid(this IMyMotorStator rotorBase)
{
if (!rotorBase.IsAttached)
return null;
return ((Sandbox.ModAPI.IMyMotorStator)rotorBase).RotorGrid;
}
}
} }

View File

@@ -3,12 +3,14 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using Torch.API; using Torch.API;
using Torch.Commands.Permissions;
using VRage.Game.ModAPI; using VRage.Game.ModAPI;
namespace Torch.Commands namespace Torch.Commands
{ {
public class Command public class Command
{ {
public MyPromoteLevel MinimumPromoteLevel { get; }
public string Name { get; } public string Name { get; }
public string Description { get; } public string Description { get; }
public string HelpText { get; } public string HelpText { get; }
@@ -25,6 +27,9 @@ namespace Torch.Commands
if (commandAttribute == null) if (commandAttribute == null)
throw new TypeLoadException($"Method does not have a {nameof(CommandAttribute)}"); throw new TypeLoadException($"Method does not have a {nameof(CommandAttribute)}");
var permissionAttribute = commandMethod.GetCustomAttribute<PermissionAttribute>();
MinimumPromoteLevel = permissionAttribute?.PromoteLevel ?? MyPromoteLevel.None;
if (!commandMethod.DeclaringType.IsSubclassOf(typeof(CommandModule))) if (!commandMethod.DeclaringType.IsSubclassOf(typeof(CommandModule)))
throw new TypeLoadException($"Command {commandMethod.Name}'s declaring type {commandMethod.DeclaringType.FullName} is not a subclass of {nameof(CommandModule)}"); throw new TypeLoadException($"Command {commandMethod.Name}'s declaring type {commandMethod.DeclaringType.FullName} is not a subclass of {nameof(CommandModule)}");

View File

@@ -4,6 +4,7 @@ using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using NLog; using NLog;
using Sandbox.Game.World;
using Torch.API; using Torch.API;
using Torch.Managers; using Torch.Managers;
using VRage.Game.ModAPI; using VRage.Game.ModAPI;
@@ -30,8 +31,8 @@ namespace Torch.Commands
public bool HasPermission(ulong steamId, Command command) public bool HasPermission(ulong steamId, Command command)
{ {
_log.Warn("Command permissions not implemented"); var userLevel = MySession.Static.GetUserPromoteLevel(steamId);
return true; return userLevel >= command.MinimumPromoteLevel;
} }
public bool IsCommand(string command) public bool IsCommand(string command)
@@ -97,6 +98,7 @@ namespace Torch.Commands
if (!HasPermission(msg.Author, command)) if (!HasPermission(msg.Author, command))
{ {
_log.Info($"{player.DisplayName} tried to use command {cmdPath} without permission"); _log.Info($"{player.DisplayName} tried to use command {cmdPath} without permission");
_torch.Multiplayer.SendMessage($"You need to be a {command.MinimumPromoteLevel} or higher to use that command.", playerId: player.IdentityId);
return; return;
} }

View File

@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VRage.Game.ModAPI;
namespace Torch.Commands.Permissions
{
public class PermissionAttribute : Attribute
{
public MyPromoteLevel PromoteLevel { get; }
public PermissionAttribute(MyPromoteLevel promoteLevel)
{
PromoteLevel = promoteLevel;
}
}
}

View File

@@ -3,7 +3,9 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Torch.Commands.Permissions;
using Torch.Managers; using Torch.Managers;
using VRage.Game.ModAPI;
namespace Torch.Commands namespace Torch.Commands
{ {
@@ -65,6 +67,7 @@ namespace Torch.Commands
} }
[Command("stop", "Stops the server.")] [Command("stop", "Stops the server.")]
[Permission(MyPromoteLevel.Admin)]
public void Stop() public void Stop()
{ {
Context.Respond("Stopping server."); Context.Respond("Stopping server.");

View File

@@ -154,6 +154,7 @@
<Compile Include="Commands\CommandManager.cs" /> <Compile Include="Commands\CommandManager.cs" />
<Compile Include="Commands\CommandTree.cs" /> <Compile Include="Commands\CommandTree.cs" />
<Compile Include="Commands\DefaultPermissionAttribute.cs" /> <Compile Include="Commands\DefaultPermissionAttribute.cs" />
<Compile Include="Commands\Permissions\PermissionAttribute.cs" />
<Compile Include="Commands\Permissions\PermissonsSystem.cs" /> <Compile Include="Commands\Permissions\PermissonsSystem.cs" />
<Compile Include="Commands\TorchCommands.cs" /> <Compile Include="Commands\TorchCommands.cs" />
<Compile Include="Managers\ChatManager.cs" /> <Compile Include="Managers\ChatManager.cs" />