- Added more public CommandsManager access methods to the COGWHEEL static class
- Ability to add custom parsers to the CommandManager with AddCustomerParser(Type, Func<string, object>) - Added two custom parsers from the COGWHEEL class: `System.Numerics.Vector2` and `System.Numerics.Vector3`
This commit is contained in:
parent
2a1c40a51c
commit
8ffedd6ba9
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
This is an example of how to initialize the Cogwheel system in a console application
|
This is an example of how to initialize the Cogwheel system in a console application
|
||||||
using the default console implementation and adding the assembly containing the built-in commands.
|
using the default console implementation and adding the assembly containing the built-in commands.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Numerics;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
namespace Cogwheel;
|
namespace Cogwheel;
|
||||||
|
|
||||||
@ -23,11 +25,12 @@ public static class COGWHEEL
|
|||||||
_console = console;
|
_console = console;
|
||||||
_commandsManager = commandsManager;
|
_commandsManager = commandsManager;
|
||||||
|
|
||||||
|
InitializeCustomParsers();
|
||||||
|
|
||||||
_commandsManager.Initialize(_console);
|
_commandsManager.Initialize(_console);
|
||||||
_console.Initialize(_commandsManager);
|
_console.Initialize(_commandsManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// == Public static methods for the CommandsManager == //
|
// == Public static methods for the CommandsManager == //
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Runs a command
|
/// Runs a command
|
||||||
@ -177,6 +180,34 @@ public static class COGWHEEL
|
|||||||
_console.Log($"Current context: {_commandsManager.CurrentContext.GetType()} : {_commandsManager.CurrentContextGuid}");
|
_console.Log($"Current context: {_commandsManager.CurrentContext.GetType()} : {_commandsManager.CurrentContextGuid}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds an assembly to the CommandsManager for command discovery
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="assembly"></param>
|
||||||
|
public static void AddAssembly(Assembly assembly)
|
||||||
|
{
|
||||||
|
_commandsManager.AddAssembly(assembly);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Removes an assembly from the CommandsManager
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="assembly"></param>
|
||||||
|
public static void RemoveAssembly(Assembly assembly)
|
||||||
|
{
|
||||||
|
_commandsManager.RemoveAssembly(assembly);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a custom parser for a given type
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="type"></param>
|
||||||
|
/// <param name="parser"></param>
|
||||||
|
public static void AddCustomParser(Type type, Func<string, object> parser)
|
||||||
|
{
|
||||||
|
_commandsManager.AddCustomParser(type, parser);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Logs all registered objects and their GUIDs, optionally filtered by type
|
/// Logs all registered objects and their GUIDs, optionally filtered by type
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -212,4 +243,52 @@ public static class COGWHEEL
|
|||||||
SetContext(guidString);
|
SetContext(guidString);
|
||||||
Log($"Set context to {_commandsManager.CurrentContext?.GetType().FullName} : {_commandsManager.CurrentContextGuid}");
|
Log($"Set context to {_commandsManager.CurrentContext?.GetType().FullName} : {_commandsManager.CurrentContextGuid}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes custom parsers for the Cogwheel system
|
||||||
|
/// </summary>
|
||||||
|
/// <exception cref="FormatException"></exception>
|
||||||
|
private static void InitializeCustomParsers()
|
||||||
|
{
|
||||||
|
// Add custom parser for Vector2
|
||||||
|
_commandsManager.AddCustomParser(typeof(Vector2), s =>
|
||||||
|
{
|
||||||
|
s = s.Trim('(', ')');
|
||||||
|
|
||||||
|
var parts = s.Split(',');
|
||||||
|
if (parts.Length != 2)
|
||||||
|
{
|
||||||
|
throw new FormatException("Input string must have exactly three components separated by commas.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!float.TryParse(parts[0].Trim(), out float x) ||
|
||||||
|
!float.TryParse(parts[1].Trim(), out float y))
|
||||||
|
{
|
||||||
|
throw new FormatException("Input string contains invalid float values.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Vector2(x, y);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Add custom parser for Vector3
|
||||||
|
_commandsManager.AddCustomParser(typeof(Vector3), s =>
|
||||||
|
{
|
||||||
|
s = s.Trim('(', ')');
|
||||||
|
|
||||||
|
var parts = s.Split(',');
|
||||||
|
if (parts.Length != 3)
|
||||||
|
{
|
||||||
|
throw new FormatException("Input string must have exactly three components separated by commas.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!float.TryParse(parts[0].Trim(), out float x) ||
|
||||||
|
!float.TryParse(parts[1].Trim(), out float y) ||
|
||||||
|
!float.TryParse(parts[2].Trim(), out float z))
|
||||||
|
{
|
||||||
|
throw new FormatException("Input string contains invalid float values.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Vector3(x, y, z);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,6 +1,3 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
@ -434,7 +431,7 @@ public class CommandsManager
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Adds the specified assembly to the list of assemblies to search for commands.
|
/// Adds the specified assembly to the list of assemblies used to search for commands.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="assembly"></param>
|
/// <param name="assembly"></param>
|
||||||
public void AddAssembly(Assembly assembly)
|
public void AddAssembly(Assembly assembly)
|
||||||
@ -450,6 +447,49 @@ public class CommandsManager
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Removes the specified assembly from the list of assemblies used to search for commands.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="assembly"></param>
|
||||||
|
public void RemoveAssembly(Assembly assembly)
|
||||||
|
{
|
||||||
|
if (Assemblies.Contains(assembly))
|
||||||
|
{
|
||||||
|
Assemblies.Remove(assembly);
|
||||||
|
RefreshCommandsList();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
CogwheelConsole.LogWarning($"Tried to remove {assembly.FullName} but it is not registered.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Clears the list of assemblies used to search for commands.
|
||||||
|
/// </summary>
|
||||||
|
public void ClearAssemblies()
|
||||||
|
{
|
||||||
|
Assemblies.Clear();
|
||||||
|
RefreshCommandsList();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a custom parser for the specified type.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="type"></param>
|
||||||
|
/// <param name="parser"></param>
|
||||||
|
public void AddCustomParser(Type type, Func<string, object> parser)
|
||||||
|
{
|
||||||
|
if (!CustomParsers.ContainsKey(type))
|
||||||
|
{
|
||||||
|
CustomParsers[type] = parser;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
CogwheelConsole.LogWarning($"Tried to add a custom parser for {type.FullName} but it is already registered.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the hash of the properties of the specified object.
|
/// Gets the hash of the properties of the specified object.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -5,10 +5,23 @@ All notable changes to this project will be documented in this file.
|
|||||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
||||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||||
|
|
||||||
|
## [1.3.0] - 2025-01-02
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- Added more public CommandsManager access methods to the COGWHEEL static class
|
||||||
|
- Ability to add custom parsers to the CommandManager with AddCustomerParser(Type, Func<string, object>)
|
||||||
|
- Added two custom parsers from the COGWHEEL class: `System.Numerics.Vector2` and `System.Numerics.Vector3`
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
### Removed
|
||||||
|
|
||||||
## [1.2.0] - 2025-01-02
|
## [1.2.0] - 2025-01-02
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
- v1.1.1 - Ability to add assemblies to the CommandManager
|
- Ability to add assemblies to the CommandManager
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user