Skip to content

Commit ae9d668

Browse files
committed
Added Help Command
1 parent 369061b commit ae9d668

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed

README.md.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.

Runtime/Core/CommandData.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public struct CommandData
99
#region Properties
1010

1111
public string CommandName { get; }
12+
public string CommandDesc { get; }
1213
public MethodInfo MethodData { get; }
1314
public object TargetClass { get; }
1415
public CommandFlags Flags { get; } // Set roles and flags?
@@ -17,10 +18,11 @@ public struct CommandData
1718

1819
#region Constructors
1920

20-
public CommandData(string commandName, MethodInfo methodData,
21+
public CommandData(string commandName, string commandDesc, MethodInfo methodData,
2122
object targetClass, CommandFlags flags = CommandFlags.None)
2223
{
2324
CommandName = commandName;
25+
CommandDesc = commandDesc;
2426
MethodData = methodData;
2527
TargetClass = targetClass;
2628
Flags = flags;

Runtime/Core/CommandRegistry.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
using System.Collections.Generic;
33
using System.Reflection;
44
using UnityEngine;
5+
using Commander.Core;
56

6-
namespace Commander.Core
7+
namespace Commander
78
{
89
public static class CommandRegistry
910
{
@@ -83,7 +84,14 @@ public static void RegisterInstanceCommands(object targetClass = null)
8384
}
8485

8586
// Assign value of dictionary as new CommandData
86-
_registeredCommands[cmdName] = new CommandData(cmdName, methodInfo, targetClass);
87+
_registeredCommands[cmdName] = new CommandData(
88+
cmdName,
89+
cmdAttribute.Descrption,
90+
methodInfo,
91+
targetClass
92+
// add flags here
93+
);
94+
8795
Debug.Log($"[CommandRegistry] Registered Command '{cmdName}'");
8896
}
8997
}
@@ -106,7 +114,7 @@ public static void RegisterStaticCommands()
106114
continue;
107115
}
108116
string cmdName = string.IsNullOrWhiteSpace(attr.Name) ? method.Name : attr.Name;
109-
_registeredCommands[cmdName] = new CommandData(cmdName, method, null);
117+
_registeredCommands[cmdName] = new CommandData(cmdName, attr.Descrption, method, null);
110118
Debug.Log($"[CommandRegistry] Registered Static Command '{cmdName}'");
111119
}
112120
}
@@ -148,5 +156,18 @@ public static ParameterInfo[] GetParameters(string commandName)
148156
}
149157

150158
#endregion
159+
160+
[Command("help", "Shows all commands and their functions")]
161+
public static void ShowAllCommands()
162+
{
163+
foreach (KeyValuePair<string, CommandData> kvp in _registeredCommands)
164+
{
165+
if (kvp.Value.Flags == CommandFlags.Hidden)
166+
{
167+
continue;
168+
}
169+
Debug.Log(kvp.Key + " - " + kvp.Value.CommandDesc);
170+
}
171+
}
151172
}
152173
}
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
using UnityEngine;
22
using Commander;
3-
using Commander.Core;
43

54
public class TestCommand : MonoBehaviour
65
{
76
void Awake()
87
{
98
CommandRegistry.RegisterInstanceCommands(this);
10-
CommandExecutor.ExecuteCommand("test enum 'hello world'");
9+
CommandExecutor.ExecuteCommand("help");
1110
}
1211

13-
[Command("test", CommandRole.Any)]
12+
[Command("test", "This is a test", CommandRole.Any)]
1413
public void PublicMyCommand(int a, string b)
1514
{
1615
Debug.Log($"Command executed with parameters: a = {a}, b = {b}");
1716
}
17+
18+
[Command("AnotherTest", "This is yet another test", CommandRole.Any)]
19+
public void AnotherCommand(int a)
20+
{
21+
Debug.Log($"Command executed with parameters: a = {a}");
22+
}
1823
}

0 commit comments

Comments
 (0)