//
// PetManagerList.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;
///
/// NosTale list of .
///
public class PetManagerList
{
///
/// Create instance.
///
/// The NosTale process browser.
/// The options.
/// The player manager or an error.
public static Result Create(ExternalNosBrowser nosBrowser, PetManagerOptions options)
{
var characterObjectAddress = nosBrowser.Scanner.CompiledFindPattern(options.PetManagerListPattern);
if (!characterObjectAddress.Found)
{
return new BindingNotFoundError(options.PetManagerListPattern, "PetManagerList");
}
if (nosBrowser.Process.MainModule is null)
{
return new NotFoundError("Cannot find the main module of the target process.");
}
int staticManagerAddress = (int)nosBrowser.Process.MainModule.BaseAddress + characterObjectAddress.Offset;
return new PetManagerList(nosBrowser.Memory, staticManagerAddress, options.PetManagerListOffsets);
}
private readonly IMemory _memory;
private readonly int _staticPetManagerListAddress;
private readonly int[] _staticPetManagerOffsets;
///
/// Initializes a new instance of the class.
///
/// The memory.
/// The static pet manager address.
/// The offsets to follow to the pet manager list address.
public PetManagerList(IMemory memory, int staticPetManagerListAddress, int[] staticPetManagerOffsets)
{
_memory = memory;
_staticPetManagerListAddress = staticPetManagerListAddress;
_staticPetManagerOffsets = staticPetManagerOffsets;
}
///
/// Gets the address of the pet manager.
///
/// An address to the pet manager list.
public IntPtr Address => _memory.FollowStaticAddressOffsets(_staticPetManagerListAddress, _staticPetManagerOffsets);
///
/// Gets the length of the array.
///
public int Length
{
get
{
_memory.SafeRead(Address + 0x08, out int length);
return length;
}
}
private IntPtr List
{
get
{
_memory.SafeRead(Address + 0x04, out int listPointer);
return (IntPtr)listPointer;
}
}
///
/// Get the first pet.
///
/// First pet, if exists.
public MapNpcObj? GetFirst()
{
if (Length == 0)
{
return null;
}
_memory.SafeRead(List, out int firstAddress);
return new MapNpcObj(_memory, (IntPtr)firstAddress);
}
///
/// Get the second pet.
///
/// Second pet, if exists.
public MapNpcObj? GetSecond()
{
if (Length < 2)
{
return null;
}
_memory.SafeRead(List + 0x04, out int secondAddress);
return new MapNpcObj(_memory, (IntPtr)secondAddress);
}
}