diff --git a/Program.cs b/Program.cs index 26e7a72..7de592d 100644 --- a/Program.cs +++ b/Program.cs @@ -1,6 +1,6 @@ /* 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; diff --git a/src/COGWHEEL.cs b/src/COGWHEEL.cs index 65e0026..29c4071 100644 --- a/src/COGWHEEL.cs +++ b/src/COGWHEEL.cs @@ -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 == // /// /// Runs a command @@ -177,6 +180,34 @@ public static class COGWHEEL _console.Log($"Current context: {_commandsManager.CurrentContext.GetType()} : {_commandsManager.CurrentContextGuid}"); } + /// + /// Adds an assembly to the CommandsManager for command discovery + /// + /// + public static void AddAssembly(Assembly assembly) + { + _commandsManager.AddAssembly(assembly); + } + + /// + /// Removes an assembly from the CommandsManager + /// + /// + public static void RemoveAssembly(Assembly assembly) + { + _commandsManager.RemoveAssembly(assembly); + } + + /// + /// Adds a custom parser for a given type + /// + /// + /// + public static void AddCustomParser(Type type, Func parser) + { + _commandsManager.AddCustomParser(type, parser); + } + /// /// Logs all registered objects and their GUIDs, optionally filtered by type /// @@ -212,4 +243,52 @@ public static class COGWHEEL SetContext(guidString); Log($"Set context to {_commandsManager.CurrentContext?.GetType().FullName} : {_commandsManager.CurrentContextGuid}"); } + + /// + /// Initializes custom parsers for the Cogwheel system + /// + /// + 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); + }); + } } \ No newline at end of file diff --git a/src/CommandsManager.cs b/src/CommandsManager.cs index 355627a..3cfabad 100644 --- a/src/CommandsManager.cs +++ b/src/CommandsManager.cs @@ -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 } /// - /// 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. /// /// public void AddAssembly(Assembly assembly) @@ -450,6 +447,49 @@ public class CommandsManager } } + /// + /// Removes the specified assembly from the list of assemblies used to search for commands. + /// + /// + 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."); + } + } + + /// + /// Clears the list of assemblies used to search for commands. + /// + public void ClearAssemblies() + { + Assemblies.Clear(); + RefreshCommandsList(); + } + + /// + /// Adds a custom parser for the specified type. + /// + /// + /// + public void AddCustomParser(Type type, Func 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."); + } + } + /// /// Gets the hash of the properties of the specified object. /// diff --git a/src/changelog.md b/src/changelog.md index 5618a9f..db3cbe3 100644 --- a/src/changelog.md +++ b/src/changelog.md @@ -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) +- 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