From 13b3512885b2793866ad0fe3fe68b907d9496eb2 Mon Sep 17 00:00:00 2001 From: Rutherther Date: Tue, 14 Feb 2023 14:00:04 +0100 Subject: [PATCH] feat(binding): move modules to dictionary for easier management --- .../NosBrowserManager.cs | 287 +++++------------- 1 file changed, 70 insertions(+), 217 deletions(-) diff --git a/src/Core/NosSmooth.LocalBinding/NosBrowserManager.cs b/src/Core/NosSmooth.LocalBinding/NosBrowserManager.cs index 6256113..1a7c9be 100644 --- a/src/Core/NosSmooth.LocalBinding/NosBrowserManager.cs +++ b/src/Core/NosSmooth.LocalBinding/NosBrowserManager.cs @@ -60,12 +60,6 @@ public class NosBrowserManager private readonly NetworkManagerOptions _networkManagerOptions; private readonly UnitManagerOptions _unitManagerOptions; private readonly NtClientOptions _ntClientOptions; - private PlayerManager? _playerManager; - private SceneManager? _sceneManager; - private PetManagerList? _petManagerList; - private NetworkManager? _networkManager; - private UnitManager? _unitManager; - private NtClient? _ntClient; private bool _initialized; /// @@ -153,132 +147,47 @@ public class NosBrowserManager public bool IsNostaleProcess => NosBrowserManager.IsProcessNostaleProcess(Process); /// - /// Gets the network manager. + /// Gets whether the player is currently in game. /// - /// Thrown if the browser is not initialized or there was an error with initialization of network manager. - public Optional NetworkManager - { - get - { - if (_networkManager is null) - { - throw new InvalidOperationException - ( - "Could not get network manager. The browser manager is not initialized. Did you forget to call NosBrowserManager.Initialize?" - ); - } - - return _networkManager; - } - } + /// + /// It may be unsafe to access some data if the player is not in game. + /// + public Optional IsInGame => PlayerManager.Map(manager => manager.Player.Address != nuint.Zero); /// /// Gets the network manager. /// - /// Thrown if the browser is not initialized or there was an error with initialization of unit manager. - public Optional UnitManager - { - get - { - if (_unitManager is null) - { - throw new InvalidOperationException - ( - "Could not get unit manager. The browser manager is not initialized. Did you forget to call NosBrowserManager.Initialize?" - ); - } - - return _unitManager; - } - } + public Optional NetworkManager => GetModule(); /// - /// Gets whether the player is currently in game. + /// Gets the network manager. /// - /// - /// It may be unsafe to access some data if the player is not in game. - /// - public Optional IsInGame => PlayerManager.Map(manager => manager.Player.Address != nuint.Zero); + /// Thrown if the browser is not initialized or there was an error with initialization of unit manager. + public Optional UnitManager => GetModule(); /// /// Gets the nt client. /// /// Thrown if the browser is not initialized or there was an error with initialization of nt client. - public Optional NtClient - { - get - { - if (_ntClient is null) - { - throw new InvalidOperationException - ( - "Could not get nt client. The browser manager is not initialized. Did you forget to call NosBrowserManager.Initialize?" - ); - } - - return _ntClient; - } - } + public Optional NtClient => GetModule(); /// /// Gets the player manager. /// /// Thrown if the browser is not initialized or there was an error with initialization of player manager. - public Optional PlayerManager - { - get - { - if (_playerManager is null) - { - throw new InvalidOperationException - ( - "Could not get player manager. The browser manager is not initialized. Did you forget to call NosBrowserManager.Initialize?" - ); - } - - return _playerManager; - } - } + public Optional PlayerManager => GetModule(); /// /// Gets the scene manager. /// /// Thrown if the browser is not initialized or there was an error with initialization of scene manager. - public Optional SceneManager - { - get - { - if (_sceneManager is null) - { - throw new InvalidOperationException - ( - "Could not get scene manager. The browser manager is not initialized. Did you forget to call NosBrowserManager.Initialize?" - ); - } - - return _sceneManager; - } - } + public Optional SceneManager => GetModule(); /// /// Gets the pet manager list. /// /// Thrown if the browser is not initialized or there was an error with initialization of pet manager list. - public Optional PetManagerList - { - get - { - if (_petManagerList is null) - { - throw new InvalidOperationException - ( - "Could not get pet manager list. The browser manager is not initialized. Did you forget to call NosBrowserManager.Initialize?" - ); - } - - return _petManagerList; - } - } + public Optional PetManagerList => GetModule(); /// /// Initialize the nos browser modules. @@ -294,122 +203,22 @@ public class NosBrowserManager return (Result)new NotNostaleProcessError(Process); } - _initialized = true; - List errorResults = new List(); - if (_unitManager is null) - { - var unitManagerResult = Structs.UnitManager.Create(this, _unitManagerOptions); - if (!unitManagerResult.IsSuccess) - { - errorResults.Add - ( - Result.FromError - ( - new CouldNotInitializeModuleError(typeof(UnitManager), unitManagerResult.Error), - unitManagerResult - ) - ); - } - - _unitManager = unitManagerResult.Entity; - } - - if (_networkManager is null) - { - var networkManagerResult = Structs.NetworkManager.Create(this, _networkManagerOptions); - if (!networkManagerResult.IsSuccess) - { - errorResults.Add - ( - Result.FromError - ( - new CouldNotInitializeModuleError(typeof(NetworkManager), networkManagerResult.Error), - networkManagerResult - ) - ); - } - - _networkManager = networkManagerResult.Entity; - } - - if (_playerManager is null) - { - var playerManagerResult = Structs.PlayerManager.Create(this, _playerManagerOptions); - if (!playerManagerResult.IsSuccess) - { - errorResults.Add - ( - Result.FromError - ( - new CouldNotInitializeModuleError(typeof(PlayerManager), playerManagerResult.Error), - playerManagerResult - ) - ); - } - - _playerManager = playerManagerResult.Entity; - } - - if (_sceneManager is null) - { - var sceneManagerResult = Structs.SceneManager.Create(this, _sceneManagerOptions); - if (!sceneManagerResult.IsSuccess) - { - errorResults.Add - ( - Result.FromError - ( - new CouldNotInitializeModuleError(typeof(SceneManager), sceneManagerResult.Error), - sceneManagerResult - ) - ); - } - - _sceneManager = sceneManagerResult.Entity; - } - - if (_petManagerList is null) + NostaleObject Map(T val) + where T : NostaleObject { - var petManagerResult = Structs.PetManagerList.Create(this, _petManagerOptions); - if (!petManagerResult.IsSuccess) - { - errorResults.Add - ( - Result.FromError - ( - new CouldNotInitializeModuleError(typeof(PetManagerList), petManagerResult.Error), - petManagerResult - ) - ); - } - - _petManagerList = petManagerResult.Entity; + return val; } - if (_ntClient is null) - { - var ntClientResult = Structs.NtClient.Create(this, _ntClientOptions); - if (!ntClientResult.IsSuccess) - { - errorResults.Add - ( - Result.FromError - ( - new CouldNotInitializeModuleError(typeof(NtClient), ntClientResult.Error), - ntClientResult - ) - ); - } - - _ntClient = ntClientResult.Entity; - } - - return errorResults.Count switch - { - 0 => Result.FromSuccess(), - 1 => errorResults[0], - _ => (Result)new AggregateError(errorResults) - }; + _initialized = true; + return HandleResults + ( + (typeof(UnitManager), () => Structs.UnitManager.Create(this, _unitManagerOptions).Map(Map)), + (typeof(NetworkManager), () => Structs.NetworkManager.Create(this, _networkManagerOptions).Map(Map)), + (typeof(PlayerManager), () => Structs.PlayerManager.Create(this, _playerManagerOptions).Map(Map)), + (typeof(SceneManager), () => Structs.SceneManager.Create(this, _sceneManagerOptions).Map(Map)), + (typeof(PetManagerList), () => Structs.PetManagerList.Create(this, _petManagerOptions).Map(Map)), + (typeof(NtClient), () => Structs.NtClient.Create(this, _ntClientOptions).Map(Map)) + ); } /// @@ -481,4 +290,48 @@ public class NosBrowserManager return nosObject; } + + private Result HandleResults(params (Type Type, Func> Builder)[] objects) + { + Result HandleSafe(Func> builder) + { + try + { + return builder(); + } + catch (Exception e) + { + return e; + } + } + + List errorResults = new List(); + foreach (var obj in objects) + { + var createdResult = HandleSafe(obj.Builder); + + if (!createdResult.IsSuccess) + { + errorResults.Add + ( + Result.FromError + ( + new CouldNotInitializeModuleError(obj.Type, createdResult.Error), + createdResult + ) + ); + } + else if (createdResult.IsDefined(out var created)) + { + _modules.Add(obj.Type, created); + } + } + + return errorResults.Count switch + { + 0 => Result.FromSuccess(), + 1 => (Result)errorResults[0], + _ => new AggregateError(errorResults) + }; + } } \ No newline at end of file -- 2.48.1