//
// NetworkManager.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 NosSmooth.LocalBinding.Errors;
using NosSmooth.LocalBinding.Extensions;
using NosSmooth.LocalBinding.Options;
using Reloaded.Memory.Sources;
using Remora.Results;
namespace NosSmooth.LocalBinding.Structs;
///
/// An object used for PacketSend and PacketReceive functions.
///
public class NetworkManager : NostaleObject
{
///
/// Create instance.
///
/// The NosTale process browser.
/// The options.
/// The player manager or an error.
public static Result Create(NosBrowserManager nosBrowserManager, NetworkManagerOptions options)
{
var networkObjectAddress = nosBrowserManager.Scanner.FindPattern(options.NetworkObjectPattern);
if (!networkObjectAddress.Found)
{
return new BindingNotFoundError(options.NetworkObjectPattern, "NetworkBinding");
}
if (nosBrowserManager.Process.MainModule is null)
{
return new NotFoundError("Cannot find the main module of the target process.");
}
var staticAddress = (nuint)(nosBrowserManager.Process.MainModule.BaseAddress + networkObjectAddress.Offset
+ options.NetworkObjectOffset);
return new NetworkManager(nosBrowserManager.Memory, staticAddress);
}
private readonly IMemory _memory;
private readonly nuint _staticNetworkManagerAddress;
///
/// Initializes a new instance of the class.
///
/// The memory.
/// The pointer to the beginning of the player manager structure.
public NetworkManager(IMemory memory, nuint staticNetworkManagerAddress)
: base(memory, staticNetworkManagerAddress)
{
_memory = memory;
_staticNetworkManagerAddress = staticNetworkManagerAddress;
}
///
/// Gets the address to the player manager.
///
public override nuint Address => GetManagerAddress(false);
private nuint GetManagerAddress(bool third)
{
nuint networkManager = _staticNetworkManagerAddress;
_memory.Read(networkManager, out networkManager);
_memory.Read(networkManager, out networkManager);
_memory.Read(networkManager, out networkManager);
if (third)
{
_memory.Read(networkManager + 0x34, out networkManager);
}
return networkManager;
}
///
/// Gets an address pointer used in PacketSend function.
///
/// Pointer to the object.
public nuint GetAddressForPacketSend()
=> GetManagerAddress(false);
///
/// Gets an address pointer used in PacketReceive function.
///
/// Pointer to the object.
public nuint GetAddressForPacketReceive()
=> GetManagerAddress(true);
}