Skip to content

Commit

Permalink
Added ConsoleCustomTypeParserAttribute (#97)
Browse files Browse the repository at this point in the history
This attribute is an alternative to DebugLogConsole.AddCustomParameterType.
  • Loading branch information
Eivlisskiv authored Oct 27, 2024
1 parent 51e0996 commit fe646cf
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 6 deletions.
8 changes: 8 additions & 0 deletions Plugins/IngameDebugConsole/Scripts/Attributes.meta

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

21 changes: 21 additions & 0 deletions Plugins/IngameDebugConsole/Scripts/Attributes/ConsoleAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Reflection;

namespace IngameDebugConsole
{
public abstract class ConsoleAttribute : Attribute
{
public MethodInfo Method { get; private set; }
public abstract int Order { get; }

public void SetMethod(MethodInfo method)
{
if (Method != null)
throw new Exception("Method was already initialized.");

Method = method;
}

public abstract void Load();
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;

namespace IngameDebugConsole
{
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
public class ConsoleCustomTypeParserAttribute : ConsoleAttribute
{
public readonly Type type;
public readonly string readableName;

public override int Order { get { return 0; } }

public ConsoleCustomTypeParserAttribute(Type type, string readableName)
{
this.type = type;
this.readableName = readableName;
}

public override void Load()
{
DebugLogConsole.AddCustomParameterType(Method, type, readableName);
}
}
}

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

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace IngameDebugConsole
{
[AttributeUsage( AttributeTargets.Method, Inherited = false, AllowMultiple = true )]
public class ConsoleMethodAttribute : Attribute
public class ConsoleMethodAttribute : ConsoleAttribute
{
private string m_command;
private string m_description;
Expand All @@ -13,11 +13,18 @@ public class ConsoleMethodAttribute : Attribute
public string Description { get { return m_description; } }
public string[] ParameterNames { get { return m_parameterNames; } }

public override int Order { get { return 1; } }

public ConsoleMethodAttribute( string command, string description, params string[] parameterNames )
{
m_command = command;
m_description = description;
m_parameterNames = parameterNames;
}

public override void Load()
{
DebugLogConsole.AddCommand(Command, Description, Method, null, ParameterNames);
}
}
}
23 changes: 18 additions & 5 deletions Plugins/IngameDebugConsole/Scripts/DebugLogConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,25 @@ public static void SearchAssemblyForConsoleMethods( Assembly assembly )
{
try
{
List<ConsoleAttribute> methods = new List<ConsoleAttribute>();
foreach( Type type in assembly.GetExportedTypes() )
{
foreach( MethodInfo method in type.GetMethods( BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly ) )
{
foreach( object attribute in method.GetCustomAttributes( typeof( ConsoleMethodAttribute ), false ) )
foreach( object attribute in method.GetCustomAttributes( typeof(ConsoleAttribute), true ) )
{
ConsoleMethodAttribute consoleMethod = attribute as ConsoleMethodAttribute;
if( consoleMethod != null )
AddCommand( consoleMethod.Command, consoleMethod.Description, method, null, consoleMethod.ParameterNames );
ConsoleAttribute consoleAttribute = (ConsoleAttribute)attribute;
consoleAttribute.SetMethod(method);
methods.Add(consoleAttribute);
}
}
}

methods.Sort((a, b) => a.Order.CompareTo(b.Order));
for (int i = 0; i < methods.Count; i++)
{
methods[i].Load();
}
}
catch( NotSupportedException ) { }
catch( System.IO.FileNotFoundException ) { }
Expand Down Expand Up @@ -376,6 +383,12 @@ public static void AddCustomParameterType( Type type, ParseFunction parseFunctio
typeReadableNames[type] = typeReadableName;
}

internal static void AddCustomParameterType(MethodInfo method, Type type, string readableName)
{
ParseFunction function = (ParseFunction)Delegate.CreateDelegate(typeof(ParseFunction), method);
AddCustomParameterType(type, function, readableName);
}

// Remove a custom Type from the list of recognized command parameter Types
public static void RemoveCustomParameterType( Type type )
{
Expand Down Expand Up @@ -439,7 +452,7 @@ private static void AddCommand( string command, string description, string metho
AddCommand( command, description, method, instance, parameterNames );
}

private static void AddCommand( string command, string description, MethodInfo method, object instance, string[] parameterNames )
internal static void AddCommand( string command, string description, MethodInfo method, object instance, string[] parameterNames )
{
if( string.IsNullOrEmpty( command ) )
{
Expand Down

0 comments on commit fe646cf

Please sign in to comment.