//
// NosBindingManager.cs
//
// Copyright (c) František Boháček. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Diagnostics;
using Microsoft.Extensions.Options;
using NosSmooth.LocalBinding.Objects;
using NosSmooth.LocalBinding.Options;
using Reloaded.Hooks;
using Reloaded.Hooks.Definitions;
using Reloaded.Memory.Sigscan;
using Reloaded.Memory.Sources;
using Remora.Results;
namespace NosSmooth.LocalBinding;
///
/// Nostale entity binding manager.
///
public class NosBindingManager : IDisposable
{
private readonly CharacterBindingOptions _characterBindingOptions;
private readonly NetworkBindingOptions _networkBindingOptions;
private readonly ExternalNosBrowser _nosBrowser;
private SceneManagerBindingOptions _sceneManagerBindingOptions;
private NetworkBinding? _networkBinding;
private PlayerManagerBinding? _characterBinding;
private SceneManagerBinding? _sceneManagerBinding;
///
/// Initializes a new instance of the class.
///
/// The character binding options.
/// The network binding options.
/// The scene manager binding options.
/// The player manager options.
/// The scene manager options.
public NosBindingManager
(
IOptions characterBindingOptions,
IOptions networkBindingOptions,
IOptions sceneManagerBindingOptions,
IOptions playerManagerOptions,
IOptions sceneManagerOptions
)
{
Hooks = new ReloadedHooks();
Memory = new Memory();
Scanner = new Scanner(Process.GetCurrentProcess(), Process.GetCurrentProcess().MainModule);
_characterBindingOptions = characterBindingOptions.Value;
_networkBindingOptions = networkBindingOptions.Value;
_sceneManagerBindingOptions = sceneManagerBindingOptions.Value;
_nosBrowser = new ExternalNosBrowser
(Process.GetCurrentProcess(), playerManagerOptions.Value, sceneManagerOptions.Value);
}
///
/// Gets the memory scanner.
///
internal Scanner Scanner { get; }
///
/// Gets the reloaded hooks.
///
internal IReloadedHooks Hooks { get; }
///
/// Gets the current process memory.
///
internal IMemory Memory { get; }
///
/// Gets the network binding.
///
/// Thrown if the manager is not initialized yet.
public NetworkBinding Network
{
get
{
if (_networkBinding is null)
{
throw new InvalidOperationException
(
"Could not get network. The binding manager is not initialized. Did you forget to call NosBindingManager.Initialize?"
);
}
return _networkBinding;
}
}
///
/// Gets the character binding.
///
/// Thrown if the manager is not initialized yet.
public PlayerManagerBinding PlayerManager
{
get
{
if (_characterBinding is null)
{
throw new InvalidOperationException
(
"Could not get character. The binding manager is not initialized. Did you forget to call NosBindingManager.Initialize?"
);
}
return _characterBinding;
}
}
///
/// Gets the character binding.
///
/// Thrown if the manager is not initialized yet.
public SceneManagerBinding SceneManager
{
get
{
if (_sceneManagerBinding is null)
{
throw new InvalidOperationException
(
"Could not get scene manager. The binding manager is not initialized. Did you forget to call NosBindingManager.Initialize?"
);
}
return _sceneManagerBinding;
}
}
///
/// Initialize the existing bindings and hook NosTale functions.
///
/// A result that may or may not have succeeded.
public Result Initialize()
{
var network = NetworkBinding.Create(this, _networkBindingOptions);
if (!network.IsSuccess)
{
return Result.FromError(network);
}
_networkBinding = network.Entity;
var playerManager = _nosBrowser.GetPlayerManager();
if (!playerManager.IsSuccess)
{
return Result.FromError(playerManager);
}
var sceneManager = _nosBrowser.GetSceneManager();
if (!sceneManager.IsSuccess)
{
return Result.FromError(sceneManager);
}
var playerManagerBinding = PlayerManagerBinding.Create
(
this,
playerManager.Entity,
_characterBindingOptions
);
if (!playerManagerBinding.IsSuccess)
{
return Result.FromError(playerManagerBinding);
}
_characterBinding = playerManagerBinding.Entity;
var sceneManagerBinding = SceneManagerBinding.Create
(
this,
sceneManager.Entity,
_sceneManagerBindingOptions
);
if (!sceneManagerBinding.IsSuccess)
{
return Result.FromError(sceneManagerBinding);
}
_sceneManagerBinding = sceneManagerBinding.Entity;
return Result.FromSuccess();
}
///
/// Disable the currently enabled nostale hooks.
///
/// A result that may or may not have succeeded.
public Result DisableHooks()
{
if (_networkBinding is not null)
{
var result = _networkBinding.DisableHooks();
if (!result.IsSuccess)
{
return result;
}
}
return Result.FromSuccess();
}
///
public void Dispose()
{
Scanner.Dispose();
DisableHooks();
}
}