Skip to content

Commit 7f78542

Browse files
authored
Added Package.json
1 parent 1faae75 commit 7f78542

File tree

10 files changed

+258
-78
lines changed

10 files changed

+258
-78
lines changed

Commander/Runtime/Core/CommandParser.cs

Lines changed: 68 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,83 +4,86 @@
44
using System.Linq;
55
using System.Text.RegularExpressions;
66

7-
public static class CommandParser
7+
namespace Commander
88
{
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>
19-
public static void StringToCommand(string rawInput, out string command, out string[] args)
9+
public static class CommandParser
2010
{
21-
// Skip empty input
22-
if (string.IsNullOrWhiteSpace(rawInput))
23-
{
24-
command = "";
25-
args = Array.Empty<string>();
26-
return;
27-
}
11+
private static readonly Regex _tokeniser = new Regex(
12+
@"[\""].+?[\""]|['].+?[']|[^ ]+",
13+
RegexOptions.Compiled);
2814

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)
15+
/// <summary>
16+
/// Takes the raw input of the user and returns the command name and it's converted parameters
17+
/// </summary>
18+
/// <param name="rawInput">Input sent by user</param>
19+
/// <param name="command">The commands name</param>
20+
/// <param name="args">The command parameters</param>
21+
public static void StringToCommand(string rawInput, out string command, out string[] args)
3322
{
34-
var t = m.Value;
35-
36-
// Strip the quotes
37-
if ((t.StartsWith("\"") && t.EndsWith("\"")) || (t.StartsWith("'") && t.EndsWith("'")))
23+
// Skip empty input
24+
if (string.IsNullOrWhiteSpace(rawInput))
3825
{
39-
t = t.Substring(1, t.Length - 2);
26+
command = "";
27+
args = Array.Empty<string>();
28+
return;
4029
}
41-
tokens.Add(t);
42-
}
4330

44-
// Skip if we found no tokens from past stripping
45-
if (tokens.Count == 0)
46-
{
47-
command = "";
48-
args = Array.Empty<string>();
49-
return;
50-
}
31+
// Keep strings which are surrounded by " or '
32+
var matches = _tokeniser.Matches(rawInput.Trim());
33+
var tokens = new List<string>(matches.Count);
34+
foreach (Match m in matches)
35+
{
36+
var t = m.Value;
5137

52-
command = tokens[0];
53-
args = tokens.Skip(1).ToArray();
54-
}
38+
// Strip the quotes
39+
if ((t.StartsWith("\"") && t.EndsWith("\"")) || (t.StartsWith("'") && t.EndsWith("'")))
40+
{
41+
t = t.Substring(1, t.Length - 2);
42+
}
43+
tokens.Add(t);
44+
}
5545

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>
63-
public static object ConvertStringToType(string raw, Type targetType)
64-
{
65-
// String Conversion
66-
if (targetType == typeof(string))
67-
{
68-
return raw;
69-
}
46+
// Skip if we found no tokens from past stripping
47+
if (tokens.Count == 0)
48+
{
49+
command = "";
50+
args = Array.Empty<string>();
51+
return;
52+
}
7053

71-
// Enum Conversion
72-
if (targetType.IsEnum)
73-
{
74-
return Enum.Parse(targetType, raw, ignoreCase: true);
54+
command = tokens[0];
55+
args = tokens.Skip(1).ToArray();
7556
}
7657

77-
// IConvertible Primitive Converison
78-
if (typeof(IConvertible).IsAssignableFrom(targetType))
58+
/// <summary>
59+
/// Convert string into it's given type. E.g "1" = int, "STATE_IDLE" = enum, etc...
60+
/// </summary>
61+
/// <param name="raw">The raw string</param>
62+
/// <param name="targetType">Type of data we will convert to</param>
63+
/// <returns></returns>
64+
/// <exception cref="InvalidOperationException"></exception>
65+
public static object ConvertStringToType(string raw, Type targetType)
7966
{
80-
return Convert.ChangeType(raw, targetType, CultureInfo.InvariantCulture);
81-
}
67+
// String Conversion
68+
if (targetType == typeof(string))
69+
{
70+
return raw;
71+
}
8272

83-
// Data needs
84-
throw new InvalidOperationException($"No converter for type {targetType.Name}! Create conversion in .../Commander/Runtime/Core/CommandParse.cs");
85-
}
73+
// Enum Conversion
74+
if (targetType.IsEnum)
75+
{
76+
return Enum.Parse(targetType, raw, ignoreCase: true);
77+
}
78+
79+
// IConvertible Primitive Converison
80+
if (typeof(IConvertible).IsAssignableFrom(targetType))
81+
{
82+
return Convert.ChangeType(raw, targetType, CultureInfo.InvariantCulture);
83+
}
84+
85+
// Data needs
86+
throw new InvalidOperationException($"No converter for type {targetType.Name}! Create conversion in .../Commander/Runtime/Core/CommandParse.cs");
87+
}
88+
}
8689
}

Commander/Samples/CommanderDemo.unity

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,50 @@ NavMeshSettings:
119119
debug:
120120
m_Flags: 0
121121
m_NavMeshData: {fileID: 0}
122+
--- !u!1 &228865648
123+
GameObject:
124+
m_ObjectHideFlags: 0
125+
m_CorrespondingSourceObject: {fileID: 0}
126+
m_PrefabInstance: {fileID: 0}
127+
m_PrefabAsset: {fileID: 0}
128+
serializedVersion: 6
129+
m_Component:
130+
- component: {fileID: 228865650}
131+
- component: {fileID: 228865649}
132+
m_Layer: 0
133+
m_Name: Examples
134+
m_TagString: Untagged
135+
m_Icon: {fileID: 0}
136+
m_NavMeshLayer: 0
137+
m_StaticEditorFlags: 0
138+
m_IsActive: 1
139+
--- !u!114 &228865649
140+
MonoBehaviour:
141+
m_ObjectHideFlags: 0
142+
m_CorrespondingSourceObject: {fileID: 0}
143+
m_PrefabInstance: {fileID: 0}
144+
m_PrefabAsset: {fileID: 0}
145+
m_GameObject: {fileID: 228865648}
146+
m_Enabled: 1
147+
m_EditorHideFlags: 0
148+
m_Script: {fileID: 11500000, guid: 62f7043f3454cc64aba9c60080aca3f7, type: 3}
149+
m_Name:
150+
m_EditorClassIdentifier:
151+
--- !u!4 &228865650
152+
Transform:
153+
m_ObjectHideFlags: 0
154+
m_CorrespondingSourceObject: {fileID: 0}
155+
m_PrefabInstance: {fileID: 0}
156+
m_PrefabAsset: {fileID: 0}
157+
m_GameObject: {fileID: 228865648}
158+
serializedVersion: 2
159+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
160+
m_LocalPosition: {x: -2.26895, y: -3.69307, z: 9.09619}
161+
m_LocalScale: {x: 1, y: 1, z: 1}
162+
m_ConstrainProportionsScale: 0
163+
m_Children: []
164+
m_Father: {fileID: 0}
165+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
122166
--- !u!1 &1809284737
123167
GameObject:
124168
m_ObjectHideFlags: 0
@@ -413,7 +457,6 @@ GameObject:
413457
m_Component:
414458
- component: {fileID: 1388674661701873443}
415459
- component: {fileID: 8335800068458371921}
416-
- component: {fileID: 3677935594322043925}
417460
- component: {fileID: 8863549574397942605}
418461
m_Layer: 0
419462
m_Name: Console
@@ -604,18 +647,6 @@ GameObject:
604647
m_NavMeshLayer: 0
605648
m_StaticEditorFlags: 0
606649
m_IsActive: 1
607-
--- !u!114 &3677935594322043925
608-
MonoBehaviour:
609-
m_ObjectHideFlags: 0
610-
m_CorrespondingSourceObject: {fileID: 0}
611-
m_PrefabInstance: {fileID: 0}
612-
m_PrefabAsset: {fileID: 0}
613-
m_GameObject: {fileID: 2669327676241856885}
614-
m_Enabled: 1
615-
m_EditorHideFlags: 0
616-
m_Script: {fileID: 11500000, guid: 4ff157bed68984085bc734d6b04a2c43, type: 3}
617-
m_Name:
618-
m_EditorClassIdentifier:
619650
--- !u!114 &3689311857669994412
620651
MonoBehaviour:
621652
m_ObjectHideFlags: 0
@@ -1239,3 +1270,4 @@ SceneRoots:
12391270
m_Roots:
12401271
- {fileID: 3216225477048014940}
12411272
- {fileID: 1809284740}
1273+
- {fileID: 228865650}

Commander/Samples/Scripts/Command Examples.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using UnityEngine;
2+
3+
namespace Commander.Examples
4+
{
5+
public class HealthManager : MonoBehaviour
6+
{
7+
private int _currentHealth = 100;
8+
9+
private void HealthChange(int health)
10+
{
11+
_currentHealth += health;
12+
Debug.Log("Player health is: " + _currentHealth);
13+
}
14+
15+
#region Runtime Commands
16+
17+
[Command("give_health", "Gives x amount of health to player")]
18+
public void AddHealthCommand(int healthToGive)
19+
{
20+
// Flip value if user inputted negative value
21+
if (healthToGive < 0)
22+
{
23+
healthToGive = -healthToGive;
24+
}
25+
HealthChange(healthToGive);
26+
}
27+
28+
[Command("take_health", "Takes x amount of health from the player")]
29+
public void RemoveHealthCommand(int healthToTake)
30+
{
31+
// Flip value if user inputted negative value
32+
if (healthToTake < 0)
33+
{
34+
healthToTake = -healthToTake;
35+
}
36+
HealthChange(-healthToTake);
37+
}
38+
39+
#endregion
40+
}
41+
}

Commander/Samples/Scripts/Command Examples/HealthManager.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/Console.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using Commander;
2+
using TMPro;
3+
using UnityEngine;
4+
using UnityEngine.InputSystem;
5+
6+
namespace Commander.Examples
7+
{
8+
public class ConsoleManager : MonoBehaviour, ConsoleInput.IUIActions
9+
{
10+
[SerializeField] private GameObject consoleUI;
11+
[SerializeField] private TMP_InputField inputField;
12+
13+
private ConsoleInput _consoleInput;
14+
15+
#region Lifecycle
16+
17+
private void Awake()
18+
{
19+
consoleUI.SetActive(false);
20+
if (_consoleInput == null)
21+
{
22+
_consoleInput = new ConsoleInput();
23+
_consoleInput.UI.SetCallbacks(this);
24+
}
25+
_consoleInput.UI.Enable();
26+
inputField.onSubmit.AddListener(InputSent);
27+
}
28+
29+
private void OnDisable()
30+
{
31+
_consoleInput.UI.Disable();
32+
inputField.onSubmit.RemoveAllListeners();
33+
}
34+
35+
#endregion
36+
37+
#region Input Events
38+
39+
public void OnConsoleToggle(InputAction.CallbackContext context)
40+
{
41+
bool toggle = !consoleUI.activeSelf;
42+
consoleUI.SetActive(toggle);
43+
}
44+
45+
#endregion
46+
47+
#region UI Events
48+
49+
public void InputSent(string input)
50+
{
51+
CommandRegistry.ExecuteCommand(input);
52+
}
53+
54+
#endregion
55+
}
56+
}

Commander/Samples/Scripts/Console/ConsoleManager.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/package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "com.maxfleetdev.commander",
3+
"version": "1.1.0",
4+
"displayName": "Command Tool",
5+
"description": "Collection of classes for allowing functions to be called at runtime.",
6+
"unity": "2019.1",
7+
"unityRelease": "0b5",
8+
"documentationUrl": "https://github.com/maxfleetdev/unity-command-tool",
9+
"changelogUrl": "https://github.com/maxfleetdev/unity-command-tool/commits/main",
10+
"licensesUrl": "https://github.com/maxfleetdev/unity-command-tool/blob/main/LICENSE",
11+
"keywords": [
12+
"command line tool",
13+
"command line",
14+
"console tools"
15+
],
16+
"author": {
17+
"name": "Max Fleet",
18+
"email": "maxfleetdev@gmail.com",
19+
"url": "https://github.com/maxfleetdev"
20+
}
21+
}

Commander/package.json.meta

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

0 commit comments

Comments
 (0)