Skip to content

Commit

Permalink
Upgrade to Version 0.2 (#4)
Browse files Browse the repository at this point in the history
* Added the option to ctrl + click a https link to open in the url in default browser

* Figured out a way to set default command string that the users can override if needed

* Added colors to console log + option to configure default color

* Added a feature to automatically scan and populate the developer console

* Added interface and background code to support admin access

* Add support for Input System (#3)

* Added support for users using the Unity Input System package

* A small update that adds identical functionality for both the old input system and the new one

* Updating package info preparing for a merge into main
  • Loading branch information
sliptrixx committed Jun 30, 2021
1 parent 8048a19 commit d697a3d
Show file tree
Hide file tree
Showing 19 changed files with 372 additions and 23 deletions.
18 changes: 18 additions & 0 deletions Commands/AdminPasswordCmd.asset
@@ -0,0 +1,18 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: d3207b6ef524bbf4ab60aaff0685ac30, type: 3}
m_Name: AdminPasswordCmd
m_EditorClassIdentifier:
CommandWord: admin
RequiresAdminAccess: 0
IncludeInScan: 1
password: default
8 changes: 8 additions & 0 deletions Commands/AdminPasswordCmd.asset.meta

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

1 change: 1 addition & 0 deletions Commands/ClearCmd.asset
Expand Up @@ -13,3 +13,4 @@ MonoBehaviour:
m_Name: ClearCmd
m_EditorClassIdentifier:
CommandWord: Clear
IncludeInScan: 1
4 changes: 3 additions & 1 deletion Commands/ConsoleHelpCmd.asset
Expand Up @@ -12,4 +12,6 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: fcb07d569c9537847a1a5c4157a6dda0, type: 3}
m_Name: ConsoleHelpCmd
m_EditorClassIdentifier:
CommandWord: help.console
CommandWord: console.help
RequiresAdminAccess: 1
IncludeInScan: 1
1 change: 1 addition & 0 deletions Commands/VersionCmd.asset
Expand Up @@ -13,3 +13,4 @@ MonoBehaviour:
m_Name: VersionCmd
m_EditorClassIdentifier:
CommandWord: version
IncludeInScan: 1
19 changes: 17 additions & 2 deletions Prefabs/DeveloperConsole.prefab
Expand Up @@ -180,14 +180,16 @@ MonoBehaviour:
m_EditorClassIdentifier:
prefix: /
commands:
- {fileID: 11400000, guid: 4d8fc261eb820ce49886d68aa0614237, type: 2}
- {fileID: 11400000, guid: 59f2102d7131cfb44a22b8e4464783a2, type: 2}
- {fileID: 11400000, guid: be8a611cc43be5744b79767982cea0ac, type: 2}
- {fileID: 11400000, guid: 20ec1ca2bbd44e94faca660ddbecfd80, type: 2}
- {fileID: 11400000, guid: 4d8fc261eb820ce49886d68aa0614237, type: 2}
uiCanvas: {fileID: 855559481631051708}
inputField: {fileID: 855559482485143244}
logUI: {fileID: 855559482330210214}
UIPanel: {fileID: 855559481754126820}
activationKeyCode: 47
defaultColor: {r: 1, g: 1, b: 1, a: 1}
--- !u!1 &855559481445711384
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -598,6 +600,7 @@ GameObject:
- component: {fileID: 855559482330210215}
- component: {fileID: 855559482330210209}
- component: {fileID: 855559482330210214}
- component: {fileID: 7293943294563688969}
m_Layer: 0
m_Name: LogTexts
m_TagString: Untagged
Expand Down Expand Up @@ -672,7 +675,7 @@ MonoBehaviour:
m_fontColor32:
serializedVersion: 2
rgba: 4290361785
m_fontColor: {r: 0.7254902, g: 0.7254902, b: 0.7254902, a: 1}
m_fontColor: {r: 1, g: 1, b: 1, a: 1}
m_enableVertexGradient: 0
m_colorMode: 3
m_fontColorGradient:
Expand Down Expand Up @@ -731,6 +734,18 @@ MonoBehaviour:
m_hasFontAssetChanged: 0
m_baseMaterial: {fileID: 0}
m_maskOffset: {x: 0, y: 0, z: 0, w: 0}
--- !u!114 &7293943294563688969
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 855559482330210212}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: a1931b4d0e739dd4581f9dc652b20ad2, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &855559482485143282
GameObject:
m_ObjectHideFlags: 0
Expand Down
7 changes: 7 additions & 0 deletions Scripts/ConsoleCommand.cs
Expand Up @@ -9,8 +9,15 @@ namespace Hibzz.Console
{
public abstract class ConsoleCommand : ScriptableObject
{
[Tooltip("The keyword to execute this command")]
[SerializeField] public string CommandWord = string.Empty;

[Tooltip("Does the command requires admin access")]
[SerializeField] public bool RequiresAdminAccess = false;

[Tooltip("This property provides the option to exclude a command from scan")]
[SerializeField] public bool IncludeInScan = true;

// abstract class that children must implement
public abstract bool Process(string[] args);
}
Expand Down
54 changes: 50 additions & 4 deletions Scripts/DeveloperConsole.cs
Expand Up @@ -6,6 +6,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEngine;

namespace Hibzz.Console
Expand All @@ -16,7 +17,9 @@ public class DeveloperConsole
private readonly IEnumerable<ConsoleCommand> commands; // registered list of commands

private CyclicQueue<Log> logs; // data structure used to store the logs
private int scrollPos = 0; // variable that keeps track of current scroll position
private int scrollPos = 0; // variable that keeps track of current scroll position

private bool AdminAccess = false; // does the console have admin access at the moment?

/// <summary>
/// constructor that takes in a prefix and list of commands
Expand Down Expand Up @@ -71,6 +74,9 @@ public void ProcessCommand(string commandInput, string[] args)
if(!commandInput.Equals(command.CommandWord, StringComparison.OrdinalIgnoreCase))
{ continue; }

// if the command requires admin acceess and if the console currently has no admin access, then don't execute the command
if(command.RequiresAdminAccess && !AdminAccess) { return; }

if(!command.Process(args))
{
// TODO: Print "invalid args"
Expand Down Expand Up @@ -103,10 +109,11 @@ public string GetLogs()
}

/// <summary>
/// Add a log with the given message
/// Add a log with the given message and color
/// </summary>
/// <param name="message"> The message to print </param>
public void AddLog(string message)
/// <param name="color"> The color of the message </param>
public void AddLog(string message, Color color)
{
// if not all the way to the bottom,
// then increment the scroll position so that the user sees the latest log
Expand All @@ -115,7 +122,10 @@ public void AddLog(string message)
++scrollPos;
}

logs.Enqueue(new Log(message));
// detect the links
message = DetectLinks(message);

logs.Enqueue(new Log(message, color));
}

/// <summary>
Expand Down Expand Up @@ -147,5 +157,41 @@ public void ScrollDown()
++scrollPos;
scrollPos = Mathf.Clamp(scrollPos, 0, logs.Count);
}

/// <summary>
/// Detects strings and replace them with link tags
/// </summary>
/// <param name="message"> The message to look for links in </param>
/// <returns> A string where the urls are replaced with links </returns>
private string DetectLinks(string message)
{
Regex regex = new Regex("https://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&amp;\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", RegexOptions.IgnoreCase);
MatchCollection matches = regex.Matches(message);

foreach (Match match in matches)
{
message = message.Replace(match.Value, "<link=\"" + match.Value + "\">" + match.Value + "</link>");
}

return message;
}

/// <summary>
/// Gives console admin access
/// </summary>
public void RequestAdminAccess()
{
AdminAccess = true;
Debug.Log("Granted admin access to the console");
}

/// <summary>
/// Revokes console admin access
/// </summary>
public void RewokeAdminAccesss()
{
AdminAccess = false;
Debug.Log("Revoked admin acceess to the console");
}
}
}

0 comments on commit d697a3d

Please sign in to comment.