//
// 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.Options;
using Reloaded.Memory.Sources;
using Remora.Results;
namespace NosSmooth.LocalBinding.Structs;
///
/// NosTale player manager.
///
public class PlayerManager
{
///
/// Create instance.
///
/// The NosTale process browser.
/// The options.
/// The player manager or an error.
public static Result Create(ExternalNosBrowser nosBrowser, CharacterBindingOptions options)
{
var characterObjectAddress = nosBrowser.Scanner.CompiledFindPattern(options.CharacterObjectPattern);
if (!characterObjectAddress.Found)
{
return new BindingNotFoundError(options.CharacterObjectPattern, "PlayerManager");
}
if (nosBrowser.Process.MainModule is null)
{
return new NotFoundError("Cannot find the main module of the target process.");
}
var ptrAddress = nosBrowser.Process.MainModule.BaseAddress + characterObjectAddress.Offset + 0x06;
nosBrowser.Memory.SafeRead(ptrAddress, out int address);
nosBrowser.Memory.SafeRead((IntPtr)address, out address);
return new PlayerManager(nosBrowser.Memory, (IntPtr)address);
}
private readonly IMemory _memory;
///
/// Initializes a new instance of the class.
///
/// The memory.
/// The pointer to the beginning of the player manager structure.
public PlayerManager(IMemory memory, IntPtr playerManager)
{
_memory = memory;
Address = playerManager;
}
///
/// Gets the address to the player manager.
///
public IntPtr Address { get; }
///
/// Gets the current player position x coordinate.
///
public int X
{
get
{
_memory.SafeRead(Address + 0x4, out short x);
return x;
}
}
///
/// Gets the current player position x coordinate.
///
public int Y
{
get
{
_memory.SafeRead(Address + 0x6, out short y);
return y;
}
}
///
/// Gets the target x coordinate the player is moving to.
///
public int TargetX
{
get
{
_memory.SafeRead(Address + 0x8, out short targetX);
return targetX;
}
}
///
/// Gets the target y coordinate the player is moving to.
///
public int TargetY
{
get
{
_memory.SafeRead(Address + 0xA, out short targetX);
return targetX;
}
}
///
/// Gets the player object.
///
public MapPlayerObj Player
{
get
{
_memory.SafeRead(Address + 0x20, out int playerAddress);
return new MapPlayerObj(_memory, (IntPtr)playerAddress);
}
}
///
/// Gets the player id.
///
public int PlayerId
{
get
{
_memory.SafeRead(Address + 0x24, out int playerId);
return playerId;
}
}
}