//
// 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 NetworkBinding? _networkBinding;
private CharacterBinding? _characterBinding;
///
/// Initializes a new instance of the class.
///
/// The character binding options.
/// The network binding options.
public NosBindingManager
(
IOptions characterBindingOptions,
IOptions networkBindingOptions
)
{
Hooks = new ReloadedHooks();
Memory = new Memory();
Scanner = new Scanner(Process.GetCurrentProcess(), Process.GetCurrentProcess().MainModule);
_characterBindingOptions = characterBindingOptions.Value;
_networkBindingOptions = networkBindingOptions.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 CharacterBinding Character
{
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;
}
}
///
/// 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 character = CharacterBinding.Create(this, _characterBindingOptions);
if (!character.IsSuccess)
{
return Result.FromError(character);
}
_characterBinding = character.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();
}
}