Skip to content

Commit

Permalink
- Console methods are now case insensitive (closed #38)
Browse files Browse the repository at this point in the history
- Assemblies other than Assembly-CSharp are also searched for [ConsoleMethod] attributes (closed #39)
  • Loading branch information
yasirkula committed Dec 28, 2020
1 parent 70e72f9 commit 4cbe93c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ If all the parameters of a function are of supported types, you can register the

- **ConsoleMethod Attribute** *(not supported on UWP platform)*

Simply add **IngameDebugConsole.ConsoleMethod** attribute to your functions. These functions must be *public static* and must reside in a *public* class. These constraints do not apply to the other two methods.
Simply add **IngameDebugConsole.ConsoleMethod** attribute to your functions. These functions must be *public static* and must reside in a *public* class. These constraints do not apply to the other 3 methods.

```csharp
using UnityEngine;
Expand Down
89 changes: 62 additions & 27 deletions Plugins/IngameDebugConsole/Scripts/DebugLogConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using System.Text;
using Object = UnityEngine.Object;
Expand Down Expand Up @@ -109,24 +110,58 @@ public static class DebugLogConsole
// Command parameter delimeter groups
private static readonly string[] inputDelimiters = new string[] { "\"\"", "''", "{}", "()", "[]" };

// CompareInfo used for case-insensitive command name comparison
private static readonly CompareInfo caseInsensitiveComparer = new CultureInfo( "en-US" ).CompareInfo;

static DebugLogConsole()
{
#if UNITY_EDITOR || !NETFX_CORE
// Load commands in most common Unity assemblies
HashSet<Assembly> assemblies = new HashSet<Assembly> { Assembly.GetAssembly( typeof( DebugLogConsole ) ) };
try
// Find all [ConsoleMethod] functions
// Don't search built-in assemblies for console methods since they can't have any
string[] ignoredAssemblies = new string[]
{
assemblies.Add( Assembly.Load( "Assembly-CSharp" ) );
}
catch { }
"Unity",
"System",
"Mono.",
"mscorlib",
"netstandard",
"Assembly-CSharp-Editor",
"TextMeshPro",
"Microsoft.GeneratedCode",
"I18N",
"Boo.",
"UnityScript.",
"Assembly-UnityScript-Editor",
"ICSharpCode.",
"ExCSS.Unity",
#if UNITY_EDITOR
"nunit.",
"SyntaxTree.",
"AssetStoreTools",
#endif
};

foreach( var assembly in assemblies )
foreach( Assembly assembly in AppDomain.CurrentDomain.GetAssemblies() )
{
foreach( var type in assembly.GetExportedTypes() )
string assemblyName = assembly.GetName().Name;
bool ignoreAssembly = false;
for( int i = 0; i < ignoredAssemblies.Length; i++ )
{
if( caseInsensitiveComparer.IsPrefix( assemblyName, ignoredAssemblies[i], CompareOptions.IgnoreCase ) )
{
ignoreAssembly = true;
break;
}
}

if( ignoreAssembly )
continue;

foreach( Type type in assembly.GetExportedTypes() )
{
foreach( var method in type.GetMethods( BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly ) )
foreach( MethodInfo method in type.GetMethods( BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly ) )
{
foreach( var attribute in method.GetCustomAttributes( typeof( ConsoleMethodAttribute ), false ) )
foreach( object attribute in method.GetCustomAttributes( typeof( ConsoleMethodAttribute ), false ) )
{
ConsoleMethodAttribute consoleMethod = attribute as ConsoleMethodAttribute;
if( consoleMethod != null )
Expand Down Expand Up @@ -361,9 +396,9 @@ private static void AddCommand( string command, string description, MethodInfo m
int commandFirstIndex = commandIndex;
int commandLastIndex = commandIndex;

while( commandFirstIndex > 0 && methods[commandFirstIndex - 1].command == command )
while( commandFirstIndex > 0 && caseInsensitiveComparer.Compare( methods[commandFirstIndex - 1].command, command, CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace ) == 0 )
commandFirstIndex--;
while( commandLastIndex < methods.Count - 1 && methods[commandLastIndex + 1].command == command )
while( commandLastIndex < methods.Count - 1 && caseInsensitiveComparer.Compare( methods[commandLastIndex + 1].command, command, CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace ) == 0 )
commandLastIndex++;

commandIndex = commandFirstIndex;
Expand Down Expand Up @@ -406,7 +441,7 @@ private static void AddCommand( string command, string description, MethodInfo m
continue;
}

if( methods[i].command == command && methods[i].parameterTypes.Length == parameterTypes.Length )
if( caseInsensitiveComparer.Compare( methods[i].command, command, CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace ) == 0 && methods[i].parameterTypes.Length == parameterTypes.Length )
{
int j = 0;
while( j < parameterTypes.Length && parameterTypes[j] == methods[i].parameterTypes[j] )
Expand Down Expand Up @@ -458,7 +493,7 @@ public static void RemoveCommand( string command )
{
for( int i = methods.Count - 1; i >= 0; i-- )
{
if( methods[i].command == command )
if( caseInsensitiveComparer.Compare( methods[i].command, command, CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace ) == 0 )
methods.RemoveAt( i );
}
}
Expand Down Expand Up @@ -497,12 +532,12 @@ public static string GetAutoCompleteCommand( string commandStart )
commandIndex = ~commandIndex;

string result = null;
for( int i = commandIndex; i >= 0 && methods[i].command.StartsWith( commandStart ); i-- )
for( int i = commandIndex; i >= 0 && caseInsensitiveComparer.IsPrefix( methods[i].command, commandStart, CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace ); i-- )
result = methods[i].command;

if( result == null )
{
for( int i = commandIndex + 1; i < methods.Count && methods[i].command.StartsWith( commandStart ); i++ )
for( int i = commandIndex + 1; i < methods.Count && caseInsensitiveComparer.IsPrefix( methods[i].command, commandStart, CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace ); i++ )
result = methods[i].command;
}

Expand Down Expand Up @@ -533,9 +568,9 @@ public static void ExecuteCommand( string command )
string _command = commandArguments[0];

int commandLastIndex = commandIndex;
while( commandIndex > 0 && methods[commandIndex - 1].command == _command )
while( commandIndex > 0 && caseInsensitiveComparer.Compare( methods[commandIndex - 1].command, _command, CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace ) == 0 )
commandIndex--;
while( commandLastIndex < methods.Count - 1 && methods[commandLastIndex + 1].command == _command )
while( commandLastIndex < methods.Count - 1 && caseInsensitiveComparer.Compare( methods[commandLastIndex + 1].command, _command, CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace ) == 0 )
commandLastIndex++;

while( commandIndex <= commandLastIndex )
Expand Down Expand Up @@ -670,7 +705,7 @@ internal static void GetCommandSuggestions( string command, List<ConsoleMethodIn
commandNameFullyTyped = command.Length > endIndex;

int commandNameLength = endIndex - i - 1;
if( commandName == null || commandNameLength == 0 || commandName.Length != commandNameLength || command.IndexOf( commandName, i + 1, commandNameLength ) != i + 1 )
if( commandName == null || commandNameLength == 0 || commandName.Length != commandNameLength || caseInsensitiveComparer.IndexOf( command, commandName, i + 1, commandNameLength, CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace ) != i + 1 )
commandName = command.Substring( i + 1, commandNameLength );
}

Expand All @@ -686,7 +721,7 @@ internal static void GetCommandSuggestions( string command, List<ConsoleMethodIn
commandNameFullyTyped = command.Length > endIndex;

int commandNameLength = command[endIndex - 1] == ',' ? endIndex - 1 - i : endIndex - i;
if( commandName == null || commandNameLength == 0 || commandName.Length != commandNameLength || command.IndexOf( commandName, i, commandNameLength ) != i )
if( commandName == null || commandNameLength == 0 || commandName.Length != commandNameLength || caseInsensitiveComparer.IndexOf( command, commandName, i, commandNameLength, CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace ) != i )
commandName = command.Substring( i, commandNameLength );
}

Expand All @@ -710,11 +745,11 @@ internal static void GetCommandSuggestions( string command, List<ConsoleMethodIn
if( !commandNameFullyTyped )
{
// Match all commands that start with commandName
if( commandIndex < methods.Count && methods[commandIndex].command.StartsWith( commandName ) )
if( commandIndex < methods.Count && caseInsensitiveComparer.IsPrefix( methods[commandIndex].command, commandName, CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace ) )
{
while( commandIndex > 0 && methods[commandIndex - 1].command.StartsWith( commandName ) )
while( commandIndex > 0 && caseInsensitiveComparer.IsPrefix( methods[commandIndex - 1].command, commandName, CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace ) )
commandIndex--;
while( commandLastIndex < methods.Count - 1 && methods[commandLastIndex + 1].command.StartsWith( commandName ) )
while( commandLastIndex < methods.Count - 1 && caseInsensitiveComparer.IsPrefix( methods[commandLastIndex + 1].command, commandName, CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace ) )
commandLastIndex++;
}
else
Expand All @@ -723,11 +758,11 @@ internal static void GetCommandSuggestions( string command, List<ConsoleMethodIn
else
{
// Match only the commands that are equal to commandName
if( commandIndex < methods.Count && methods[commandIndex].command == commandName )
if( commandIndex < methods.Count && caseInsensitiveComparer.Compare( methods[commandIndex].command, commandName, CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace ) == 0 )
{
while( commandIndex > 0 && methods[commandIndex - 1].command == commandName )
while( commandIndex > 0 && caseInsensitiveComparer.Compare( methods[commandIndex - 1].command, commandName, CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace ) == 0 )
commandIndex--;
while( commandLastIndex < methods.Count - 1 && methods[commandLastIndex + 1].command == commandName )
while( commandLastIndex < methods.Count - 1 && caseInsensitiveComparer.Compare( methods[commandLastIndex + 1].command, commandName, CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace ) == 0 )
commandLastIndex++;
}
else
Expand Down Expand Up @@ -792,7 +827,7 @@ private static int FindCommandIndex( string command )
while( min <= max )
{
int mid = ( min + max ) / 2;
int comparison = command.CompareTo( methods[mid].command );
int comparison = caseInsensitiveComparer.Compare( command, methods[mid].command, CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace );
if( comparison == 0 )
return mid;
else if( comparison < 0 )
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.yasirkula.ingamedebugconsole",
"displayName": "In-game Debug Console",
"version": "1.4.0",
"version": "1.4.1",
"description": "This asset helps you see debug messages (logs, warnings, errors, exceptions) runtime in a build (also assertions in editor) and execute commands using its built-in console. It also supports logging logcat messages to the console on Android platform.",
"hideInEditor": false
}

0 comments on commit 4cbe93c

Please sign in to comment.