mirror of
https://github.com/GTA-ASM/SanAndreasUnity
synced 2025-02-16 12:58:27 +00:00
implement command processing
This commit is contained in:
parent
485ba93c8d
commit
f563597d96
3 changed files with 175 additions and 0 deletions
8
Assets/Scripts/Commands.meta
Normal file
8
Assets/Scripts/Commands.meta
Normal file
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: ec4efa1f3f99f6544b9b89261f715b15
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
156
Assets/Scripts/Commands/CommandManager.cs
Normal file
156
Assets/Scripts/Commands/CommandManager.cs
Normal file
|
@ -0,0 +1,156 @@
|
|||
using System.Collections.Generic;
|
||||
using SanAndreasUnity.Net;
|
||||
using UnityEngine;
|
||||
|
||||
namespace SanAndreasUnity.Commands
|
||||
{
|
||||
public class CommandManager : MonoBehaviour
|
||||
{
|
||||
public static CommandManager Singleton { get; private set; }
|
||||
|
||||
readonly Dictionary<string, CommandInfo> m_registeredCommands = new Dictionary<string, CommandInfo>();
|
||||
|
||||
public IEnumerable<string> RegisteredCommands => m_registeredCommands.Keys;
|
||||
|
||||
public static string invalidSyntaxText => "Invalid syntax";
|
||||
|
||||
[SerializeField] private List<string> m_forbiddenCommands = new List<string>();
|
||||
|
||||
/// <summary> Forbidden commands can not be registered. </summary>
|
||||
public List<string> ForbiddenCommands => m_forbiddenCommands;
|
||||
|
||||
[SerializeField] private bool m_registerHelpCommand = true;
|
||||
|
||||
public struct CommandInfo
|
||||
{
|
||||
public string command;
|
||||
public System.Func<ProcessCommandContext, ProcessCommandResult> commandHandler;
|
||||
public bool allowToRunWithoutServerPermissions;
|
||||
|
||||
public CommandInfo(string command, bool allowToRunWithoutServerPermissions)
|
||||
{
|
||||
this.command = command;
|
||||
this.allowToRunWithoutServerPermissions = allowToRunWithoutServerPermissions;
|
||||
this.commandHandler = null;
|
||||
}
|
||||
}
|
||||
|
||||
public class ProcessCommandResult
|
||||
{
|
||||
public string response;
|
||||
|
||||
public static ProcessCommandResult UnknownCommand => new ProcessCommandResult {response = "Unknown command"};
|
||||
public static ProcessCommandResult InvalidCommand => new ProcessCommandResult {response = "Invalid command"};
|
||||
public static ProcessCommandResult NoPermissions => new ProcessCommandResult {response = "You don't have permissions to run this command"};
|
||||
}
|
||||
|
||||
public class ProcessCommandContext
|
||||
{
|
||||
public string command;
|
||||
public bool hasServerPermissions;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (null == Singleton)
|
||||
Singleton = this;
|
||||
|
||||
if (m_registerHelpCommand)
|
||||
RegisterCommand(new CommandInfo { command = "help", commandHandler = ProcessHelpCommand, allowToRunWithoutServerPermissions = true });
|
||||
}
|
||||
|
||||
public bool RegisterCommand(CommandInfo commandInfo)
|
||||
{
|
||||
commandInfo.command = commandInfo.command.Trim();
|
||||
|
||||
if (this.ForbiddenCommands.Contains(commandInfo.command))
|
||||
return false;
|
||||
|
||||
if (m_registeredCommands.ContainsKey(commandInfo.command))
|
||||
return false;
|
||||
|
||||
m_registeredCommands.Add(commandInfo.command, commandInfo);
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool RemoveCommand(string command)
|
||||
{
|
||||
return m_registeredCommands.Remove(command);
|
||||
}
|
||||
|
||||
public static string[] SplitCommandIntoArguments(string command)
|
||||
{
|
||||
// TODO: add support for arguments that have spaces, i.e. those enclosed with quotes
|
||||
|
||||
return command.Split(new string[] {" ", "\t"}, System.StringSplitOptions.RemoveEmptyEntries);
|
||||
}
|
||||
|
||||
public static string GetRestOfTheCommand(string command, int argumentIndex)
|
||||
{
|
||||
if (argumentIndex < 0)
|
||||
return "";
|
||||
|
||||
string[] args = SplitCommandIntoArguments(command);
|
||||
|
||||
if (argumentIndex > args.Length - 2)
|
||||
return "";
|
||||
|
||||
return string.Join(" ", args, argumentIndex + 1, args.Length - argumentIndex - 1);
|
||||
}
|
||||
|
||||
ProcessCommandResult ProcessCommand(string command, bool hasServerPermissions)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(command))
|
||||
return ProcessCommandResult.UnknownCommand;
|
||||
|
||||
string[] arguments = SplitCommandIntoArguments(command);
|
||||
if (0 == arguments.Length)
|
||||
return ProcessCommandResult.InvalidCommand;
|
||||
|
||||
if (!m_registeredCommands.TryGetValue(arguments[0], out CommandInfo commandInfo))
|
||||
return ProcessCommandResult.UnknownCommand;
|
||||
|
||||
if (!hasServerPermissions && !commandInfo.allowToRunWithoutServerPermissions)
|
||||
return ProcessCommandResult.NoPermissions;
|
||||
|
||||
var context = new ProcessCommandContext {command = command, hasServerPermissions = hasServerPermissions};
|
||||
|
||||
return commandInfo.commandHandler(context);
|
||||
}
|
||||
|
||||
public ProcessCommandResult ProcessCommandAsServer(string command)
|
||||
{
|
||||
return ProcessCommand(command, true);
|
||||
}
|
||||
|
||||
public ProcessCommandResult ProcessCommandForPlayer(Player player, string command)
|
||||
{
|
||||
bool hasServerPermissions = player == Player.Local;
|
||||
return ProcessCommand(command, hasServerPermissions);
|
||||
}
|
||||
|
||||
ProcessCommandResult ProcessHelpCommand(ProcessCommandContext context)
|
||||
{
|
||||
string response = "List of available commands:\n";
|
||||
|
||||
foreach (var pair in m_registeredCommands)
|
||||
{
|
||||
if (!context.hasServerPermissions && !pair.Value.allowToRunWithoutServerPermissions)
|
||||
continue;
|
||||
|
||||
response += pair.Key + "\n";
|
||||
}
|
||||
|
||||
response += "\n";
|
||||
|
||||
return new ProcessCommandResult {response = response};
|
||||
}
|
||||
|
||||
public bool HasCommand(string command)
|
||||
{
|
||||
return m_registeredCommands.ContainsKey(command);
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Scripts/Commands/CommandManager.cs.meta
Normal file
11
Assets/Scripts/Commands/CommandManager.cs.meta
Normal file
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 8cf3900b4a6c9524a82881a6650d9a36
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Add table
Reference in a new issue