Skip to content

Commit

Permalink
1.4.0 update
Browse files Browse the repository at this point in the history
+ Added inspector configurable commands
+ Added some documentation
+ Added "hello world" command example
+ Added "NonLoadableCommand" attribute
* Updated Readme
* Fixed namespace/console name convention
  • Loading branch information
austephner committed Dec 24, 2021
1 parent 83cd6a2 commit b8bc660
Show file tree
Hide file tree
Showing 18 changed files with 325 additions and 85 deletions.
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A simple in-game developer console with easy-to-implement commands and scripting
![Example](https://i.imgur.com/hWwjmZl.gif)

#### Features
- Easily code new commands with no additional setup - just implement the command class!
- Easily create new commands **from the inspector** or **C#**!
- Some default/starter commands are included
- Modular and extensible components
- Tons of events to hook into. This includes `UnityEvent`, inheritable class events, and `static Action` events
Expand All @@ -16,9 +16,7 @@ A simple in-game developer console with easy-to-implement commands and scripting
#### Todo
- Some functions/classes need additional documentation
- Scripting for executing a series of commands from a text file or string
- Configurable commands that can be created in the inspector with `UnityEvent` and require no coding.
- Control improvements in the example console.
- Improved README documentation to describe all settings/configurations/options for the console's game object.

# Getting Started
1. Import the package or Github content into your Assets folder
Expand All @@ -41,11 +39,21 @@ Note that this API does come with some default commands, but to embrace the powe
| Previous Command | Assigns the "previous" command sent relative to the current history position into the input buffer. | Up Arrow |
| Next Command | Assigns the "next" command sent relative to the current history position into the input buffer. | Down Arrow |

### Creating New Commands
The system uses C# Reflection to find commands. All you have to do is implement the `DevConsoleCommand` class and start the game.
# Creating New Commands
There's two main ways to create new commands.

### From the Inspector (Basic)
1. Select the `DevConsoleBehaviour` game object in the scene.
2. Under the "Misc" section in the component's inspector, find the "Unity Event Commands" array.
3. Add a new element and configure/customize the command as needed. <br><br>
![Unity Event Example](https://i.imgur.com/be4E1dd.png)

### From Code (C#)
The system uses C# Reflection to find commands. All you have to do is implement the `DevConsoleCommand` class and start the game. This command is included within the examples.
```c#
using System.Collections.Generic;
using DevConsole;
using DevConsole.Commands;

public class HelloWorldCommand : DevConsoleCommand
{
Expand All @@ -58,7 +66,7 @@ public class HelloWorldCommand : DevConsoleCommand
// The action that actually happens when this command is executed.
public override void Execute(List<string> parameters)
{
DevConsole.Print("Hello world!");
Console.Print("Hello world!");
}

// (OPTIONAL) The text displayed when the "help helloworld" or "help hw" command is executed
Expand Down Expand Up @@ -93,7 +101,7 @@ This section describes how to configure a `DevConsoleBehaviour`.
| Enable Dev Mode On Start | When enabled, the `DevConsoleBehaviour` will automatically enter "Dev Mode" if possible when it starts. |
| Allow Cheat Mode | Allows the usage of "Cheat Mode". If disabled, `SetCheatMode(...)` cannot be called and cheat-mode-only commands cannot be executed. |
| Enable Cheat Mode On Start | When enabled, the `DevConsoleBehaviour` will automatically enter "Cheat Mode" if possible when it starts. |
| Max History | The maximum amount of entries the console will remember.
| Max History | The maximum amount of entries the console will remember. |

### Components
| Field | Description |
Expand Down
15 changes: 13 additions & 2 deletions Runtime/Behaviours/DevConsoleBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,18 @@ public static event Action<bool>
private string _startMessage = "";

[Header("Components"), SerializeField,
Tooltip("Determines how input is handled when updating this console.")]
Tooltip("Determines how input is handled when updating this console.")]
protected ConsoleInputBehaviour _consoleInputBehaviour;

[SerializeField,
Tooltip("Determines how printing and clearing text is handled for this console.")]
Tooltip("Determines how printing and clearing text is handled for this console.")]
protected ConsoleDisplayBehaviour _consoleDisplayBehaviour;

[Header("Misc"),
SerializeField,
Tooltip("Commands that can be added to the behaviour through the inspector.")]
protected UnityEventCommand[] _unityEventCommands = new UnityEventCommand[0];

[Header("Events"), SerializeField]
protected UnityEvent _onInitialized;

Expand Down Expand Up @@ -189,9 +194,15 @@ private void OnEnable()

_devConsoleCommands =
TypeUtil.GetNonAbstractSubTypes(typeof(DevConsoleCommand))
.Where(type => Attribute.GetCustomAttribute(type, typeof(NonLoadableCommandAttribute)) == null)
.Select(devConsoleCommand => (DevConsoleCommand) Activator.CreateInstance(devConsoleCommand))
.ToList();

foreach (var unityEventCommand in _unityEventCommands)
{
_devConsoleCommands.Add(unityEventCommand);
}

if (_printUnityConsoleLogs) Application.logMessageReceived += OnUnityLog;
if (_startsOpen) _open = true;
if (_openOnStart && !_open) Open();
Expand Down
24 changes: 12 additions & 12 deletions Runtime/Commands/DefaultCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public override string GetHelp()

public override void Execute(List<string> parameters)
{
DevConsole.Print(string.Join(" ", parameters));
Console.Print(string.Join(" ", parameters));
}
}

Expand All @@ -43,14 +43,14 @@ public override void Execute(List<string> parameters)
{
if (parameters.Count == 0)
{
foreach (var command in DevConsole.GetAllRegisteredCommands())
foreach (var command in Console.GetAllRegisteredCommands())
{
if (command.cheatModeOnly && !DevConsole.isCheatModeEnabled)
if (command.cheatModeOnly && !Console.isCheatModeEnabled)
{
continue;
}

if (command.devModeOnly && !DevConsole.isDevModeEnabled)
if (command.devModeOnly && !Console.isDevModeEnabled)
{
continue;
}
Expand All @@ -62,28 +62,28 @@ public override void Execute(List<string> parameters)
continue;
}

DevConsole.Print($"{string.Join(", ", command.GetNames())} --> <i> {help}</i>");
Console.Print($"{string.Join(", ", command.GetNames())} --> <i> {help}</i>");
}
}
else
{
var command = DevConsole.GetCommandByName(parameters[0]);
var command = Console.GetCommandByName(parameters[0]);

if (command == null ||
command.cheatModeOnly && !DevConsole.isCheatModeEnabled ||
command.devModeOnly && !DevConsole.isDevModeEnabled)
command.cheatModeOnly && !Console.isCheatModeEnabled ||
command.devModeOnly && !Console.isDevModeEnabled)
{
PrintNotAvailable();
return;
}

DevConsole.Print(command.GetHelp());
Console.Print(command.GetHelp());
}
}

private void PrintNotAvailable()
{
DevConsole.PrintError("Help for this command is not available.");
Console.PrintError("Help for this command is not available.");
}
}

Expand All @@ -104,7 +104,7 @@ public override string GetHelp()

public override void Execute(List<string> parameters)
{
DevConsole.Clear();
Console.Clear();
}
}

Expand All @@ -120,7 +120,7 @@ public override string[] GetNames()

public override void Execute(List<string> parameters)
{
DevConsole.Print(DateTime.Now.ToString("g"));
Console.Print(DateTime.Now.ToString("g"));
}
}
}
12 changes: 12 additions & 0 deletions Runtime/Commands/NonLoadableCommandAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using DevConsole.Behaviours;

namespace DevConsole.Commands
{
/// <summary>
/// Put onto <see cref="DevConsoleCommand"/> implementations that shouldn't be loaded initially by the
/// <see cref="DevConsoleBehaviour"/>.
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public class NonLoadableCommandAttribute : Attribute { }
}
3 changes: 3 additions & 0 deletions Runtime/Commands/NonLoadableCommandAttribute.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions Runtime/Commands/UnityEventCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

namespace DevConsole.Commands
{
[Serializable, NonLoadableCommand]
public class UnityEventCommand : DevConsoleCommand
{
[SerializeField,
Tooltip("Names that can be used to invoke this command. All entries are set to lowercase and trimmed before comparison.")]
private string[] _names;

[SerializeField,
Tooltip("The text that appears when a user types \"help [your-command-name]\"")]
private string _helpText;

[SerializeField,
Tooltip("Enable if this command should only be used during \"Dev Mode\".")]
private bool _devModeOnly;

[SerializeField,
Tooltip("Enable if this command should only be used during \"Cheat Mode\".")]
private bool _cheatModeOnly;

[SerializeField,
Tooltip("Invoked when this command is executed. It'll be invoked before \"onParameterInvoke\".")]
private UnityEvent _onInvoke;

[SerializeField,
Tooltip("Invoked when this command is executed. It's invoked with all parameters sent to the command and invoked after \"onInvoke\".")]
private StringParamUnityEvent _onParameterInvoke;

public override string[] GetNames() => _names;

public override string GetHelp() => _helpText;

public override bool devModeOnly => _devModeOnly;

public override bool cheatModeOnly => _cheatModeOnly;

public override void Execute(List<string> parameters)
{
_onInvoke?.Invoke();
_onParameterInvoke?.Invoke(parameters);
}
}

[Serializable]
public class StringParamUnityEvent : UnityEvent<List<string>> { }
}
3 changes: 3 additions & 0 deletions Runtime/Commands/UnityEventCommand.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

109 changes: 109 additions & 0 deletions Runtime/Console.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using System.Collections.Generic;
using DevConsole.Behaviours;
using DevConsole.Commands;
using DevConsole.Enums;
using static DevConsole.Behaviours.DevConsoleBehaviour;

namespace DevConsole
{
/// <summary>
/// Allows quick and easy access to <see cref="DevConsoleBehaviour"/> instance functionality.
/// </summary>
public static class Console
{
/// <summary>
/// Prints a message to the dev console, using the <see cref="DevConsoleBehaviour"/> <see cref="DevConsoleBehaviour.Instance"/>.
/// </summary>
/// <param name="message">The message to print.</param>
public static void Print(string message) => Instance?.Print(message);

/// <summary>
/// Prints a message to the dev console, using the <see cref="DevConsoleBehaviour"/>
/// <see cref="DevConsoleBehaviour.Instance"/> of the specified <see cref="DevConsolePrintType"/>.
/// </summary>
/// <param name="message">The message to print.</param>
/// <param name="printType">he message type.</param>
public static void Print(string message, DevConsolePrintType printType) => Instance?.Print(message, printType);

/// <summary>
/// Prints an error message to the dev console, using the <see cref="DevConsoleBehaviour"/> <see cref="DevConsoleBehaviour.Instance"/>.
/// </summary>
/// <param name="message">The error message to print.</param>
public static void PrintError(string message) => Instance?.Print(message, DevConsolePrintType.Error);

/// <summary>
/// Prints a warning message to the dev console, using the <see cref="DevConsoleBehaviour"/> <see cref="DevConsoleBehaviour.Instance"/>.
/// </summary>
/// <param name="message">The warning message to print.</param>
public static void PrintWarning(string message) => Instance?.Print(message, DevConsolePrintType.Warning);

/// <summary>
/// Prints a success message to the dev console, using the <see cref="DevConsoleBehaviour"/> <see cref="DevConsoleBehaviour.Instance"/>.
/// </summary>
/// <param name="message">The success message to print.</param>
public static void PrintSuccess(string message) => Instance?.Print(message, DevConsolePrintType.Success);

/// <summary>
/// Toggles the <see cref="DevConsoleBehaviour"/> <see cref="DevConsoleBehaviour.Instance"/>.
/// </summary>
public static void Toggle() => Instance?.Toggle();

/// <summary>
/// Opens the <see cref="DevConsoleBehaviour"/> <see cref="DevConsoleBehaviour.Instance"/>.
/// </summary>
public static void Open() => Instance?.Open();

/// <summary>
/// Closes the <see cref="DevConsoleBehaviour"/> <see cref="DevConsoleBehaviour.Instance"/>.
/// </summary>
public static void Close() => Instance?.Close();

/// <summary>
/// Clears the <see cref="DevConsoleBehaviour"/> <see cref="DevConsoleBehaviour.Instance"/>.
/// </summary>
public static void Clear() => Instance?.Clear();

/// <summary>
/// Gets all registered commands from the <see cref="DevConsoleBehaviour"/> <see cref="DevConsoleBehaviour.Instance"/>.
/// </summary>
/// <returns><see cref="List{T}"/> of <see cref="DevConsoleCommand"/> registered in the <see cref="DevConsoleBehaviour"/> <see cref="DevConsoleBehaviour.Instance"/>.</returns>
public static List<DevConsoleCommand> GetAllRegisteredCommands() => Instance?.GetAllRegisteredCommands();

/// <summary>
/// Gets a command matching the given <see cref="name"/>.
/// </summary>
/// <param name="name">The name of the command to get.</param>
/// <returns>A <see cref="DevConsoleCommand"/> if one exists for the given name.</returns>
public static DevConsoleCommand GetCommandByName(string name) => Instance?.GetCommandByName(name);

/// <summary>
/// Checks whether or not the <see cref="DevConsoleBehaviour"/> <see cref="DevConsoleBehaviour.Instance"/> is open.
/// </summary>
public static bool isOpen => Instance?.isOpen ?? false;

/// <summary>
/// Checks whether or not the <see cref="DevConsoleBehaviour"/> <see cref="DevConsoleBehaviour.Instance"/> has dev mode enabled.
/// </summary>
public static bool isDevModeEnabled => Instance?.isDevModeEnabled ?? false;

/// <summary>
/// Checks whether or not the <see cref="DevConsoleBehaviour"/> <see cref="DevConsoleBehaviour.Instance"/> has the cheat mode enabled.
/// </summary>
public static bool isCheatModeEnabled => Instance?.isCheatModeEnabled ?? false;

/// <summary>
/// The current input buffer for the <see cref="DevConsoleBehaviour"/> <see cref="DevConsoleBehaviour.Instance"/>.
/// </summary>
public static string inputBuffer
{
get => Instance?.inputBuffer;
set
{
if (Instance)
{
Instance.inputBuffer = value;
}
}
}
}
}
File renamed without changes.
Loading

0 comments on commit b8bc660

Please sign in to comment.