Skip to content

Commit 1faae75

Browse files
authored
Added multi-space strings
Can now surround strings with quotes to keep multi spaced strings
1 parent efec1a2 commit 1faae75

File tree

5 files changed

+87
-22
lines changed

5 files changed

+87
-22
lines changed

Commander/Runtime/Core/CommandParser.cs

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,65 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Globalization;
34
using System.Linq;
5+
using System.Text.RegularExpressions;
46

57
public static class CommandParser
6-
{
8+
{
9+
private static readonly Regex _tokeniser = new Regex(
10+
@"[\""].+?[\""]|['].+?[']|[^ ]+",
11+
RegexOptions.Compiled);
12+
13+
/// <summary>
14+
/// Takes the raw input of the user and returns the command name and it's converted parameters
15+
/// </summary>
16+
/// <param name="rawInput">Input sent by user</param>
17+
/// <param name="command">The commands name</param>
18+
/// <param name="args">The command parameters</param>
719
public static void StringToCommand(string rawInput, out string command, out string[] args)
820
{
9-
// TODO: Keep in single string when surrounded in quotes
10-
var parts = rawInput.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
11-
if (parts.Length == 0)
21+
// Skip empty input
22+
if (string.IsNullOrWhiteSpace(rawInput))
23+
{
24+
command = "";
25+
args = Array.Empty<string>();
26+
return;
27+
}
28+
29+
// Keep strings which are surrounded by " or '
30+
var matches = _tokeniser.Matches(rawInput.Trim());
31+
var tokens = new List<string>(matches.Count);
32+
foreach (Match m in matches)
33+
{
34+
var t = m.Value;
35+
36+
// Strip the quotes
37+
if ((t.StartsWith("\"") && t.EndsWith("\"")) || (t.StartsWith("'") && t.EndsWith("'")))
38+
{
39+
t = t.Substring(1, t.Length - 2);
40+
}
41+
tokens.Add(t);
42+
}
43+
44+
// Skip if we found no tokens from past stripping
45+
if (tokens.Count == 0)
1246
{
1347
command = "";
1448
args = Array.Empty<string>();
1549
return;
1650
}
1751

18-
// Return variables
19-
command = parts[0];
20-
args = parts.Length > 1? parts.Skip(1).ToArray() : Array.Empty<string>();
52+
command = tokens[0];
53+
args = tokens.Skip(1).ToArray();
2154
}
2255

23-
// Convert string into it's given type. E.g "1" = int, "STATE_IDLE" = enum, etc...
56+
/// <summary>
57+
/// Convert string into it's given type. E.g "1" = int, "STATE_IDLE" = enum, etc...
58+
/// </summary>
59+
/// <param name="raw">The raw string</param>
60+
/// <param name="targetType">Type of data we will convert to</param>
61+
/// <returns></returns>
62+
/// <exception cref="InvalidOperationException"></exception>
2463
public static object ConvertStringToType(string raw, Type targetType)
2564
{
2665
// String Conversion
@@ -41,6 +80,7 @@ public static object ConvertStringToType(string raw, Type targetType)
4180
return Convert.ChangeType(raw, targetType, CultureInfo.InvariantCulture);
4281
}
4382

44-
throw new InvalidOperationException($"No converter for type {targetType.Name}");
83+
// Data needs
84+
throw new InvalidOperationException($"No converter for type {targetType.Name}! Create conversion in .../Commander/Runtime/Core/CommandParse.cs");
4585
}
4686
}

Commander/Runtime/Core/CommandRegistry.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ public static class CommandRegistry
99
{
1010
#region Properties
1111

12-
/// <summary>
13-
/// Enable or disable debug logs
14-
/// </summary>
15-
public static bool ShowDebugLogs = true;
16-
1712
/// <summary>
1813
/// Name of command and it's tuple of method info and target MonoBehaviour
1914
/// </summary>
2015
private static Dictionary<string, List<(MethodInfo mi, MonoBehaviour target)>> _commands
2116
= new Dictionary<string, List<(MethodInfo, MonoBehaviour)>>
22-
(StringComparer.OrdinalIgnoreCase);
17+
(StringComparer.OrdinalIgnoreCase);
18+
19+
/// <summary>
20+
/// Enable or disable debug logs
21+
/// </summary>
22+
public static bool ShowDebugLogs = true;
2323

2424
#endregion
2525

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using UnityEngine;
2+
3+
public class CommandTest : MonoBehaviour
4+
{
5+
[Command("TestCommand")]
6+
private void TestCommandMethod(int a, string b)
7+
{
8+
Debug.Log($"Test command executed with parameters: a={a}, b={b}");
9+
}
10+
}

Commander/Samples/Scripts/CommandTest.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Commander/Samples/Scripts/ConsoleManager.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ public class ConsoleManager : MonoBehaviour, ConsoleInput.IUIActions
77
{
88
[SerializeField] private GameObject consoleUI;
99
[SerializeField] private TMP_InputField inputField;
10-
private ConsoleInput _consoleInput;
11-
10+
11+
private ConsoleInput _consoleInput;
12+
13+
#region Lifecycle
14+
1215
private void Awake()
1316
{
1417
consoleUI.SetActive(false);
@@ -25,16 +28,26 @@ private void OnDisable()
2528
{
2629
_consoleInput.UI.Disable();
2730
inputField.onSubmit.RemoveAllListeners();
28-
}
29-
31+
}
32+
33+
#endregion
34+
35+
#region Input Events
36+
3037
public void OnConsoleToggle(InputAction.CallbackContext context)
3138
{
3239
bool toggle = !consoleUI.activeSelf;
3340
consoleUI.SetActive(toggle);
34-
}
35-
41+
}
42+
43+
#endregion
44+
45+
#region UI Events
46+
3647
public void InputSent(string input)
3748
{
3849
CommandRegistry.ExecuteCommand(input);
39-
}
50+
}
51+
52+
#endregion
4053
}

0 commit comments

Comments
 (0)