Skip to content

Commit fe646cf

Browse files
authored
Added ConsoleCustomTypeParserAttribute (#97)
This attribute is an alternative to DebugLogConsole.AddCustomParameterType.
1 parent 51e0996 commit fe646cf

File tree

8 files changed

+83
-6
lines changed

8 files changed

+83
-6
lines changed

Plugins/IngameDebugConsole/Scripts/Attributes.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Reflection;
3+
4+
namespace IngameDebugConsole
5+
{
6+
public abstract class ConsoleAttribute : Attribute
7+
{
8+
public MethodInfo Method { get; private set; }
9+
public abstract int Order { get; }
10+
11+
public void SetMethod(MethodInfo method)
12+
{
13+
if (Method != null)
14+
throw new Exception("Method was already initialized.");
15+
16+
Method = method;
17+
}
18+
19+
public abstract void Load();
20+
}
21+
}

Plugins/IngameDebugConsole/Scripts/Attributes/ConsoleAttribute.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
3+
namespace IngameDebugConsole
4+
{
5+
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
6+
public class ConsoleCustomTypeParserAttribute : ConsoleAttribute
7+
{
8+
public readonly Type type;
9+
public readonly string readableName;
10+
11+
public override int Order { get { return 0; } }
12+
13+
public ConsoleCustomTypeParserAttribute(Type type, string readableName)
14+
{
15+
this.type = type;
16+
this.readableName = readableName;
17+
}
18+
19+
public override void Load()
20+
{
21+
DebugLogConsole.AddCustomParameterType(Method, type, readableName);
22+
}
23+
}
24+
}

Plugins/IngameDebugConsole/Scripts/Attributes/ConsoleCustomTypeParserAttribute.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Plugins/IngameDebugConsole/Scripts/ConsoleMethodAttribute.cs renamed to Plugins/IngameDebugConsole/Scripts/Attributes/ConsoleMethodAttribute.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace IngameDebugConsole
44
{
55
[AttributeUsage( AttributeTargets.Method, Inherited = false, AllowMultiple = true )]
6-
public class ConsoleMethodAttribute : Attribute
6+
public class ConsoleMethodAttribute : ConsoleAttribute
77
{
88
private string m_command;
99
private string m_description;
@@ -13,11 +13,18 @@ public class ConsoleMethodAttribute : Attribute
1313
public string Description { get { return m_description; } }
1414
public string[] ParameterNames { get { return m_parameterNames; } }
1515

16+
public override int Order { get { return 1; } }
17+
1618
public ConsoleMethodAttribute( string command, string description, params string[] parameterNames )
1719
{
1820
m_command = command;
1921
m_description = description;
2022
m_parameterNames = parameterNames;
2123
}
24+
25+
public override void Load()
26+
{
27+
DebugLogConsole.AddCommand(Command, Description, Method, null, ParameterNames);
28+
}
2229
}
2330
}

Plugins/IngameDebugConsole/Scripts/ConsoleMethodAttribute.cs.meta renamed to Plugins/IngameDebugConsole/Scripts/Attributes/ConsoleMethodAttribute.cs.meta

File renamed without changes.

Plugins/IngameDebugConsole/Scripts/DebugLogConsole.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,18 +197,25 @@ public static void SearchAssemblyForConsoleMethods( Assembly assembly )
197197
{
198198
try
199199
{
200+
List<ConsoleAttribute> methods = new List<ConsoleAttribute>();
200201
foreach( Type type in assembly.GetExportedTypes() )
201202
{
202203
foreach( MethodInfo method in type.GetMethods( BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly ) )
203204
{
204-
foreach( object attribute in method.GetCustomAttributes( typeof( ConsoleMethodAttribute ), false ) )
205+
foreach( object attribute in method.GetCustomAttributes( typeof(ConsoleAttribute), true ) )
205206
{
206-
ConsoleMethodAttribute consoleMethod = attribute as ConsoleMethodAttribute;
207-
if( consoleMethod != null )
208-
AddCommand( consoleMethod.Command, consoleMethod.Description, method, null, consoleMethod.ParameterNames );
207+
ConsoleAttribute consoleAttribute = (ConsoleAttribute)attribute;
208+
consoleAttribute.SetMethod(method);
209+
methods.Add(consoleAttribute);
209210
}
210211
}
211212
}
213+
214+
methods.Sort((a, b) => a.Order.CompareTo(b.Order));
215+
for (int i = 0; i < methods.Count; i++)
216+
{
217+
methods[i].Load();
218+
}
212219
}
213220
catch( NotSupportedException ) { }
214221
catch( System.IO.FileNotFoundException ) { }
@@ -376,6 +383,12 @@ public static void AddCustomParameterType( Type type, ParseFunction parseFunctio
376383
typeReadableNames[type] = typeReadableName;
377384
}
378385

386+
internal static void AddCustomParameterType(MethodInfo method, Type type, string readableName)
387+
{
388+
ParseFunction function = (ParseFunction)Delegate.CreateDelegate(typeof(ParseFunction), method);
389+
AddCustomParameterType(type, function, readableName);
390+
}
391+
379392
// Remove a custom Type from the list of recognized command parameter Types
380393
public static void RemoveCustomParameterType( Type type )
381394
{
@@ -439,7 +452,7 @@ private static void AddCommand( string command, string description, string metho
439452
AddCommand( command, description, method, instance, parameterNames );
440453
}
441454

442-
private static void AddCommand( string command, string description, MethodInfo method, object instance, string[] parameterNames )
455+
internal static void AddCommand( string command, string description, MethodInfo method, object instance, string[] parameterNames )
443456
{
444457
if( string.IsNullOrEmpty( command ) )
445458
{

0 commit comments

Comments
 (0)