Skip to content
Open
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
19 changes: 19 additions & 0 deletions Torch.Server/Commands/TestCommands.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Torch.Commands;
using Torch.Commands.Permissions;
using VRage.Game.ModAPI;

namespace Torch.Server.Commands
{
public class TestCommands : CommandModule
{
private TorchConfig Config => (TorchConfig)Context.Torch.Config;

[Command("test")]
[Alias("ThisISAnAlias")]
[Permission(MyPromoteLevel.None)]
public void Test()
{
Context.Respond("Hello World!");
}
}
}
1 change: 1 addition & 0 deletions Torch.Server/Torch.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@
<Compile Include="..\Versioning\AssemblyVersion.cs">
<Link>Properties\AssemblyVersion.cs</Link>
</Compile>
<Compile Include="Commands\TestCommands.cs" />
<Compile Include="Commands\WhitelistCommands.cs" />
<Compile Include="FlowDocumentTarget.cs" />
<Compile Include="ListBoxExtensions.cs" />
Expand Down
1 change: 1 addition & 0 deletions Torch.Server/TorchServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ private void OnSessionStateChanged(ITorchSession session, TorchSessionState newS
{
_multiplayerManagerDedicated = CurrentSession.Managers.GetManager<MultiplayerManagerDedicated>();
CurrentSession.Managers.GetManager<CommandManager>().RegisterCommandModule(typeof(WhitelistCommands));
CurrentSession.Managers.GetManager<CommandManager>().RegisterCommandModule(typeof(TestCommands));
ModCommunication.Register();
}
}
Expand Down
14 changes: 14 additions & 0 deletions Torch/Commands/AliasAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace Torch.Commands.Permissions
{
public class AliasAttribute : Attribute
{
public string Alias { get; }

public AliasAttribute(string alias)
{
Alias = alias;
}
}
}
54 changes: 17 additions & 37 deletions Torch/Commands/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class Command
public string Name { get; }
public string Description { get; }
public string HelpText { get; }
public string Alias { get; }
public CommandAction Action { get; }
public Type Module { get; }
public List<string> Path { get; } = new List<string>();
Expand All @@ -31,44 +32,17 @@ public class Command
private ParameterInfo[] _parameters;
private int? _requiredParamCount;
private static readonly Logger Log = LogManager.GetCurrentClassLogger();

public Command(string name, string description, CommandAction action, ITorchPlugin plugin, MyPromoteLevel? minimumPromoteLevel = null, string helpText = null, int requiredParamCount = 0)
{
HelpText = helpText;
Action = action;
Plugin = plugin;
MinimumPromoteLevel = minimumPromoteLevel ?? MyPromoteLevel.Admin;

var split = name.Split(' ');
Name = split.Last();
Description = description;
HelpText = helpText ?? description;

Path.AddRange(split);

var sb = new StringBuilder();
sb.Append($"!{string.Join(" ", Path)} ");

_requiredParamCount = requiredParamCount;
Log.Debug($"Params: ({_requiredParamCount} required)");
SyntaxHelp = sb.ToString();
}

public Command(ITorchPlugin plugin, MethodInfo commandMethod)

public Command(ITorchPlugin plugin, MethodInfo commandMethod, CommandAttribute attributeData)
{
Plugin = plugin;

var commandAttribute = commandMethod.GetCustomAttribute<CommandAttribute>();
if (commandAttribute == null)
throw new TypeLoadException($"Method does not have a {nameof(CommandAttribute)}");


var permissionAttribute = commandMethod.GetCustomAttribute<PermissionAttribute>();
MinimumPromoteLevel = permissionAttribute?.PromoteLevel ?? MyPromoteLevel.Admin;

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)}");

var moduleAttribute = commandMethod.DeclaringType.GetCustomAttribute<CategoryAttribute>();

var aliasAttribute = commandMethod.GetCustomAttribute<AliasAttribute>();

_method = commandMethod;
Module = commandMethod.DeclaringType;
Expand All @@ -77,12 +51,18 @@ public Command(ITorchPlugin plugin, MethodInfo commandMethod)
{
Path.AddRange(moduleAttribute.Path);
}
Path.AddRange(commandAttribute.Path);

Name = commandAttribute.Name;
Description = commandAttribute.Description;
HelpText = commandAttribute.HelpText;

if(aliasAttribute != null)
{
Alias = aliasAttribute.Alias;
}

Path.AddRange(attributeData.Path);

Name = attributeData.Name;
Description = attributeData.Description;
HelpText = attributeData.HelpText;

//parameters
_parameters = commandMethod.GetParameters();

Expand Down
4 changes: 2 additions & 2 deletions Torch/Commands/CommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ public void RegisterCommandModule(Type moduleType, ITorchPlugin plugin = null)
if (commandAttrib == null)
continue;

var command = new Command(plugin, method);
var command = new Command(plugin, method, commandAttrib);
var cmdPath = string.Join(".", command.Path);
_log.Info($"Registering command '{cmdPath}'");
_log.Info($"Registering command '{cmdPath}'" + (!string.IsNullOrEmpty(command.Alias) ? $" (alias: {string.Join(", ", command.Alias)})" : ""));

if (!Commands.AddCommand(command))
_log.Error($"Command path {cmdPath} is already registered.");
Expand Down
18 changes: 9 additions & 9 deletions Torch/Commands/CommandTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@ public CommandTree()

public bool AddCommand(Command command)
{
bool hasAlias = !string.IsNullOrEmpty(command.Alias);
var root = command.Path.First();

if (!_root.ContainsKey(root))
{
_root.Add(root, new CommandNode(root));

if (hasAlias)
{
_root.Add(command.Alias, _root[root]);
}
}

var node = _root[root];
Expand Down Expand Up @@ -65,7 +71,9 @@ public int GetNode(List<string> path, out CommandNode commandNode)
return -1;

if (!_root.ContainsKey(root))
{
return -1;
}

var node = _root[root];
var i = 1;
Expand All @@ -85,15 +93,7 @@ public int GetNode(List<string> path, out CommandNode commandNode)
commandNode = node;
return i;
}

public Command GetCommand(List<string> path, out List<string> args)
{
args = new List<string>();
var skip = GetNode(path, out CommandNode node);
args.AddRange(path.Skip(skip));
return node.Command;
}


public Command GetCommand(string commandText, out string argText)
{
var split = commandText.Split(new []{' '}, StringSplitOptions.RemoveEmptyEntries).ToList();
Expand Down
1 change: 1 addition & 0 deletions Torch/Torch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@
<Compile Include="Collections\SystemSortedView.cs" />
<Compile Include="Collections\TransformComparer.cs" />
<Compile Include="Collections\TransformEnumerator.cs" />
<Compile Include="Commands\AliasAttribute.cs" />
<Compile Include="Commands\ConsoleCommandContext.cs" />
<Compile Include="Event\EventShimAttribute.cs" />
<Compile Include="Extensions\LinqExtensions.cs" />
Expand Down