Skip to content

Commit

Permalink
ConsoleEnabled variable and fixes to console logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Orange-Panda committed Jan 23, 2022
1 parent 12b7588 commit c2733c5
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 19 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ All notable changes to this package are documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [1.0.1] - 2022-01-22

### Added
- Support for ConsoleEnabled variable, preventing user access to the development console.
- Default enable state configuration options for editor and standalone builds.
- Console warns user if there is no event system present.

### Changed
- Help manual usage parameters are now shown in a more user friendly syntax.

### Fixed
- Addressed issue with console recognizing a parameterless method with higher priority than a LongString method.

## [1.0.0] - 2022-01-22

### Added
Expand Down
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ https://github.com/Orange-Panda/VespaIO.git
1. After importing the package into your Unity project create a settings asset by going to `Tools > Vespa IO > Select Console Settings`
1. This file defines all the options you can configure for the Vespa IO Console. If this file is not present the defaults will be used instead.
2. In one of your scripts add the [StaticCommand] attribute to a static method and provide it with a key. This should look something like the following:
```c#
[StaticCommand("keyhere")]
private static void MethodName()
{
// DO SOMETHING
}
```
```c#
[StaticCommand("keyhere")]
private static void MethodName()
{
// DO SOMETHING
}
```
3. Enter play mode and press the `~` key to open the console.
1. If prompted to, import the TextMeshPro essentials. The default console runner requires these assets.
4. Enter the command key defined for the method in step 2.
5. Your method will now be called any time your enter the command.
6. Vespa IO comes with some built in commands, use the `help` command to see a list of all commands (built-in and user defined commands)
Expand Down
28 changes: 26 additions & 2 deletions Runtime/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public Command(StaticCommandAttribute attribute, MethodInfo method)
ParameterInfo[] parameters = method.GetParameters();
for (int i = 0; i < parameters.Length; i++)
{
Guide += $"[{parameters[i].ParameterType}] ";
Guide += $"[{TranslateParameter(parameters[i].ParameterType)}] ";
}
Cheat = attribute.Cheat;
Hidden = attribute.Hidden;
Expand All @@ -152,9 +152,33 @@ public void AddMethod(StaticCommandAttribute attribute, MethodInfo method)
ParameterInfo[] parameters = method.GetParameters();
for (int i = 0; i < parameters.Length; i++)
{
Guide += $"[{parameters[i].ParameterType}] ";
Guide += $"[{TranslateParameter(parameters[i].ParameterType)}] ";
}
Methods.Add(method);
}

private string TranslateParameter(Type type)
{
if (type == typeof(int))
{
return "INTEGER";
}
else if (type == typeof(float))
{
return "FLOAT";
}
else if (type == typeof(string))
{
return "WORD";
}
else if (type == typeof(Longstring))
{
return "PHRASE";
}
else
{
return "INVALID/UNKNOWN";
}
}
}
}
9 changes: 9 additions & 0 deletions Runtime/ConsoleSettingsFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,15 @@ static ConsoleSettings()
[Serializable]
public class ConsoleSettingsConfig
{
[Header("Commands")]
[Tooltip("The default state of the console enabled variable in the unity editor.")]
public bool defaultConsoleEnableEditor = true;
[Tooltip("The default state of the console enabled variable in a standalone build (i.e non-editor)")]
public bool defaultConsoleEnableStandalone = true;
[Tooltip("When should the commands be preloaded into memory? This will take some time depending on the size of your project and will only occur once per play session. Selecting none will load commands upon the first command input.")]
public DevConsole.PreloadType preloadType;

[Header("Console")]
[Tooltip("When true will automatically create an instance of the console when the game starts.")]
public bool instantiateConsoleOnLoad = true;
[Tooltip("Where is the console template stored? Must be a path inside a resources folder.")]
Expand All @@ -51,6 +58,8 @@ public class ConsoleSettingsConfig

public ConsoleSettingsConfig DeepCopy() => new ConsoleSettingsConfig
{
defaultConsoleEnableEditor = defaultConsoleEnableEditor,
defaultConsoleEnableStandalone = defaultConsoleEnableStandalone,
preloadType = preloadType,
instantiateConsoleOnLoad = instantiateConsoleOnLoad,
consoleResourcePath = consoleResourcePath,
Expand Down
23 changes: 17 additions & 6 deletions Runtime/DevConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ public static class DevConsole

public static void ProcessCommand(string submitText)
{
if (!ConsoleEnabled)
{
Log("<color=red>Error:</color> Console is not enabled.");
return;
}

// Parse input into useful variables
if (!TryParseCommand(submitText, out string commandName, out object[] args, out Longstring longstring))
{
Expand Down Expand Up @@ -120,7 +126,11 @@ public static void ProcessCommand(string submitText)
}

// Execute the best method found in the previous step, if one is found
if (bestMethod != null)
if (longstringMethod != null && !string.IsNullOrWhiteSpace(longstring) && (bestMethod == null || bestMethodArgCount == 0))
{
longstringMethod.Invoke(null, new object[] { longstring });
}
else if (bestMethod != null)
{
if (args.Length < bestMethodArgCount)
{
Expand All @@ -144,11 +154,6 @@ public static void ProcessCommand(string submitText)

bestMethod.Invoke(null, args);
}
// If there were no matches for the parameters and a method expects a long string, invoke that one instead.
else if (longstringMethod != null)
{
longstringMethod.Invoke(null, new object[] { longstring });
}
// No methods support the user input parameters.
else
{
Expand Down Expand Up @@ -201,6 +206,12 @@ private static void CreateConsole()
{
PrintWelcome();

#if UNITY_EDITOR
ConsoleEnabled = ConsoleSettings.Config.defaultConsoleEnableEditor;
#else
ConsoleEnabled = ConsoleSettings.Config.defaultConsoleEnableStandalone;
#endif

if (ConsoleSettings.Config.preloadType == PreloadType.RuntimeStart)
{
Commands.PreloadLookup();
Expand Down
25 changes: 21 additions & 4 deletions Runtime/DevConsoleRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class DevConsoleRunner : MonoBehaviour
[SerializeField] private TMP_InputField inputText;

private bool historyDirty;
private bool hasNoEventSystem;

internal static DevConsoleRunner Instance { get; private set; }

Expand Down Expand Up @@ -58,7 +59,7 @@ private void Update()
bool openKey = Input.GetKeyDown(KeyCode.Tilde) || Input.GetKeyDown(KeyCode.BackQuote);
bool exitKey = Input.GetKeyDown(KeyCode.Tilde) || Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.BackQuote) || Input.GetMouseButtonDown(0);

if (DevConsole.ConsoleActive && exitKey)
if ((DevConsole.ConsoleActive || !DevConsole.ConsoleEnabled) && exitKey)
{
SetConsoleState(false);
}
Expand Down Expand Up @@ -88,12 +89,28 @@ public void SetConsoleState(bool value)
if (EventSystem.current != null)
{
EventSystem.current.SetSelectedGameObject(value ? inputText.gameObject : null);
}

if (value)
if (value)
{
inputText.OnPointerClick(new PointerEventData(EventSystem.current));
}

if (hasNoEventSystem)
{
DevConsole.Clear();
DevConsole.PrintWelcome();
hasNoEventSystem = false;
}
}
#if UNITY_EDITOR
else
{
inputText.OnPointerClick(new PointerEventData(EventSystem.current));
DevConsole.Clear();
DevConsole.Log("<color=red>ERROR:</color> No event system present in scene. The developer console cannot function without this.");
DevConsole.Log("Add an event system by right clicking in the scene Hierarchy > UI > Event System.");
hasNoEventSystem = true;
}
#endif
}

private void OnSubmit(string submitText)
Expand Down

0 comments on commit c2733c5

Please sign in to comment.