Verlet Intergration Soft Body
Loading...
Searching...
No Matches
PhysicsCSAlevlProject.CommandRegistry Class Reference

Registry for console commands that can be invoked from the debug console. This class allows for registering methods as console commands using the ConsoleCommandAttribute, and provides functionality to invoke these commands based on user input. The commands are stored in a dictionary for efficient lookup, and the registry supports invoking commands with parameters as well as listing all registered commands. This system enables a flexible and extensible way to add debug functionality to the application without hardcoding command handling logic in the main game loop. More...

Public Member Functions

 CommandRegistry (ImGuiLogger logger)
void RegisterType (object instance, Type type, bool logRegistration=true)
 Registers all methods of the given instance that are marked with the ConsoleCommandAttribute as console commands. The method uses reflection to find all methods in the specified type that have the ConsoleCommandAttribute, and adds them to the _commands dictionary using the command path as the key. This allows for dynamic registration of commands based on the attributes applied to methods, making it easy to add new commands by simply marking methods with the appropriate attribute. The logRegistration parameter can be set to false to suppress logging of each registered command, which can be useful when registering a large number of commands or when registering commands in a context where logging is not desired.
IEnumerable< string > GetRegisteredCommandPaths ()
 Returns a list of all registered command paths in the registry. This method extracts the command paths from the _commands dictionary, ensuring that they are distinct and sorted alphabetically for easier readability. The returned list can be used to display available commands to the user or for debugging purposes to verify which commands have been registered in the system.
bool TryInvokeCommand (ImGuiLogger.Command command)
 Attempts to invoke a console command based on the provided command information. The method constructs the full command key from the command path and command name, and looks it up in the _commands dictionary. If a matching command is found, it invokes the associated method with the provided parameters. If the invocation is successful, it returns true. If an error occurs during invocation, it logs the error message and also returns true to indicate that the command was recognized but failed to execute. If no matching command is found, it returns false to indicate that the command is unknown. This method allows for dynamic execution of commands based on user input from the debug console, providing a flexible way to interact with the application for debugging and testing purposes.

Private Attributes

readonly ImGuiLogger _logger
readonly Dictionary< string,(object instance, MethodInfo method, string commandPath) > _commands = new()

Detailed Description

Registry for console commands that can be invoked from the debug console. This class allows for registering methods as console commands using the ConsoleCommandAttribute, and provides functionality to invoke these commands based on user input. The commands are stored in a dictionary for efficient lookup, and the registry supports invoking commands with parameters as well as listing all registered commands. This system enables a flexible and extensible way to add debug functionality to the application without hardcoding command handling logic in the main game loop.

Member Function Documentation

◆ GetRegisteredCommandPaths()

IEnumerable< string > PhysicsCSAlevlProject.CommandRegistry.GetRegisteredCommandPaths ( )
inline

Returns a list of all registered command paths in the registry. This method extracts the command paths from the _commands dictionary, ensuring that they are distinct and sorted alphabetically for easier readability. The returned list can be used to display available commands to the user or for debugging purposes to verify which commands have been registered in the system.

Returns

◆ RegisterType()

void PhysicsCSAlevlProject.CommandRegistry.RegisterType ( object instance,
Type type,
bool logRegistration = true )
inline

Registers all methods of the given instance that are marked with the ConsoleCommandAttribute as console commands. The method uses reflection to find all methods in the specified type that have the ConsoleCommandAttribute, and adds them to the _commands dictionary using the command path as the key. This allows for dynamic registration of commands based on the attributes applied to methods, making it easy to add new commands by simply marking methods with the appropriate attribute. The logRegistration parameter can be set to false to suppress logging of each registered command, which can be useful when registering a large number of commands or when registering commands in a context where logging is not desired.

Parameters
instance
type
logRegistration

◆ TryInvokeCommand()

bool PhysicsCSAlevlProject.CommandRegistry.TryInvokeCommand ( ImGuiLogger.Command command)
inline

Attempts to invoke a console command based on the provided command information. The method constructs the full command key from the command path and command name, and looks it up in the _commands dictionary. If a matching command is found, it invokes the associated method with the provided parameters. If the invocation is successful, it returns true. If an error occurs during invocation, it logs the error message and also returns true to indicate that the command was recognized but failed to execute. If no matching command is found, it returns false to indicate that the command is unknown. This method allows for dynamic execution of commands based on user input from the debug console, providing a flexible way to interact with the application for debugging and testing purposes.

Parameters
command
Returns

The documentation for this class was generated from the following file:
  • PhysicsCSAlevlProject/Game1.DebugCommands.cs