- 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:
Chris Bell 2025-01-03 19:12:19 -06:00
parent 2a1c40a51c
commit 8ffedd6ba9
4 changed files with 139 additions and 7 deletions

View File

@ -1,5 +1,7 @@
using System;
using System.Linq;
using System.Numerics;
using System.Reflection;
namespace Cogwheel;
@ -23,11 +25,12 @@ public static class COGWHEEL
_console = console;
_commandsManager = commandsManager;
InitializeCustomParsers();
_commandsManager.Initialize(_console);
_console.Initialize(_commandsManager);
}
// == Public static methods for the CommandsManager == //
/// <summary>
/// Runs a command
@ -177,6 +180,34 @@ public static class COGWHEEL
_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>
/// Logs all registered objects and their GUIDs, optionally filtered by type
/// </summary>
@ -212,4 +243,52 @@ public static class COGWHEEL
SetContext(guidString);
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);
});
}
}

View File

@ -1,6 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
@ -434,7 +431,7 @@ public class CommandsManager
}
/// <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>
/// <param name="assembly"></param>
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>
/// Gets the hash of the properties of the specified object.
/// </summary>

View File

@ -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/),
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
### Added
- v1.1.1 - Ability to add assemblies to the CommandManager
- Ability to add assemblies to the CommandManager
### Changed