//
// PlayerManager.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 Microsoft.Extensions.Options;
using NosSmooth.LocalBinding.Errors;
using NosSmooth.LocalBinding.Extensions;
using NosSmooth.LocalBinding.Options;
using Reloaded.Memory.Sources;
using Remora.Results;
namespace NosSmooth.LocalBinding.Structs;
///
/// NosTale player manager.
///
public class PlayerManager : ControlManager
{
///
/// Create instance.
///
/// The NosTale process browser.
/// The options.
/// The player manager or an error.
public static Result Create(NosBrowserManager nosBrowserManager, PlayerManagerOptions options)
{
var characterObjectAddress = nosBrowserManager.Scanner.FindPattern(options.PlayerManagerPattern);
if (!characterObjectAddress.Found)
{
return new BindingNotFoundError(options.PlayerManagerPattern, "PlayerManager");
}
if (nosBrowserManager.Process.MainModule is null)
{
return new NotFoundError("Cannot find the main module of the target process.");
}
var staticAddress = (int)nosBrowserManager.Process.MainModule.BaseAddress + characterObjectAddress.Offset;
return new PlayerManager(nosBrowserManager.Memory, staticAddress, options.PlayerManagerOffsets);
}
private readonly IMemory _memory;
private readonly int _staticPlayerManagerAddress;
private readonly int[] _playerManagerOffsets;
///
/// Initializes a new instance of the class.
///
/// The memory.
/// The pointer to the beginning of the player manager structure.
/// The offsets to get the player manager address from the static one.
public PlayerManager(IMemory memory, int staticPlayerManagerAddress, int[] playerManagerOffsets)
: base(memory, nuint.Zero)
{
_memory = memory;
_staticPlayerManagerAddress = staticPlayerManagerAddress;
_playerManagerOffsets = playerManagerOffsets;
}
///
/// Gets the address to the player manager.
///
public override nuint Address => _memory.FollowStaticAddressOffsets
(_staticPlayerManagerAddress, _playerManagerOffsets);
///
/// Gets the player object.
///
public MapPlayerObj Player
{
get
{
_memory.SafeRead(Address + 0x20, out int playerAddress);
return new MapPlayerObj(_memory, (nuint)playerAddress);
}
}
///
/// Gets the player id.
///
public int PlayerId
{
get
{
_memory.SafeRead(Address + 0x24, out int playerId);
return playerId;
}
}
///
public override MapBaseObj Entity => Player;
}