From 24f396f971487fee671dad63848a1862a6bcd0a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Sun, 23 Jan 2022 09:31:58 +0100 Subject: [PATCH] feat(localbinding): add pet manager mapping --- .../ExternalNosBrowser.cs | 27 +++- .../NosBindingManager.cs | 11 +- .../Options/PetManagerOptions.cs | 27 ++++ .../Structs/MapNpcObj.cs | 25 ++++ .../Structs/PetManager.cs | 49 ++++++++ .../Structs/PetManagerList.cs | 116 ++++++++++++++++++ .../Structs/SceneManager.cs | 2 +- 7 files changed, 253 insertions(+), 4 deletions(-) create mode 100644 Local/NosSmooth.LocalBinding/Options/PetManagerOptions.cs create mode 100644 Local/NosSmooth.LocalBinding/Structs/MapNpcObj.cs create mode 100644 Local/NosSmooth.LocalBinding/Structs/PetManager.cs create mode 100644 Local/NosSmooth.LocalBinding/Structs/PetManagerList.cs diff --git a/Local/NosSmooth.LocalBinding/ExternalNosBrowser.cs b/Local/NosSmooth.LocalBinding/ExternalNosBrowser.cs index a3a7285..0e6de49 100644 --- a/Local/NosSmooth.LocalBinding/ExternalNosBrowser.cs +++ b/Local/NosSmooth.LocalBinding/ExternalNosBrowser.cs @@ -21,8 +21,10 @@ public class ExternalNosBrowser { private readonly PlayerManagerOptions _playerManagerOptions; private readonly SceneManagerOptions _sceneManagerOptions; + private readonly PetManagerOptions _petManagerOptions; private PlayerManager? _playerManager; private SceneManager? _sceneManager; + private PetManagerList? _petManagerList; /// /// Initializes a new instance of the class. @@ -30,15 +32,18 @@ public class ExternalNosBrowser /// The process to browse. /// The options for obtaining player manager. /// The scene manager options. + /// The pet manager options. public ExternalNosBrowser ( Process process, PlayerManagerOptions playerManagerOptions, - SceneManagerOptions sceneManagerOptions + SceneManagerOptions sceneManagerOptions, + PetManagerOptions petManagerOptions ) { _playerManagerOptions = playerManagerOptions; _sceneManagerOptions = sceneManagerOptions; + _petManagerOptions = petManagerOptions; Process = process; Memory = new ExternalMemory(process); Scanner = new Scanner(process, process.MainModule); @@ -98,4 +103,24 @@ public class ExternalNosBrowser return _sceneManager; } + + /// + /// Get the pet manager list. + /// + /// The player manager or an error. + public Result GetPetManagerList() + { + if (_petManagerList is null) + { + var petManagerResult = PetManagerList.Create(this, _petManagerOptions); + if (!petManagerResult.IsSuccess) + { + return petManagerResult; + } + + _petManagerList = petManagerResult.Entity; + } + + return _petManagerList; + } } \ No newline at end of file diff --git a/Local/NosSmooth.LocalBinding/NosBindingManager.cs b/Local/NosSmooth.LocalBinding/NosBindingManager.cs index aee0c75..a76337b 100644 --- a/Local/NosSmooth.LocalBinding/NosBindingManager.cs +++ b/Local/NosSmooth.LocalBinding/NosBindingManager.cs @@ -38,13 +38,15 @@ public class NosBindingManager : IDisposable /// The scene manager binding options. /// The player manager options. /// The scene manager options. + /// The pet manager options. public NosBindingManager ( IOptions characterBindingOptions, IOptions networkBindingOptions, IOptions sceneManagerBindingOptions, IOptions playerManagerOptions, - IOptions sceneManagerOptions + IOptions sceneManagerOptions, + IOptions petManagerOptions ) { Hooks = new ReloadedHooks(); @@ -54,7 +56,12 @@ public class NosBindingManager : IDisposable _networkBindingOptions = networkBindingOptions.Value; _sceneManagerBindingOptions = sceneManagerBindingOptions.Value; _nosBrowser = new ExternalNosBrowser - (Process.GetCurrentProcess(), playerManagerOptions.Value, sceneManagerOptions.Value); + ( + Process.GetCurrentProcess(), + playerManagerOptions.Value, + sceneManagerOptions.Value, + petManagerOptions.Value + ); } /// diff --git a/Local/NosSmooth.LocalBinding/Options/PetManagerOptions.cs b/Local/NosSmooth.LocalBinding/Options/PetManagerOptions.cs new file mode 100644 index 0000000..e693f11 --- /dev/null +++ b/Local/NosSmooth.LocalBinding/Options/PetManagerOptions.cs @@ -0,0 +1,27 @@ +// +// PetManagerOptions.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.Objects; + +namespace NosSmooth.LocalBinding.Options; + +/// +/// Options for . +/// +public class PetManagerOptions +{ + /// + /// Gets or sets the pattern to find static pet manager list address at. + /// + public string PetManagerListPattern { get; set; } + = "00 ff 70 00 ?? ?? ?? ?? 10 90"; + + /// + /// Gets or sets the offsets to find the pet manager list at from the static address. + /// + public int[] PetManagerListOffsets { get; set; } + = { 4, 8, 0x180, 0x18, 0x1C, 0x54 }; +} \ No newline at end of file diff --git a/Local/NosSmooth.LocalBinding/Structs/MapNpcObj.cs b/Local/NosSmooth.LocalBinding/Structs/MapNpcObj.cs new file mode 100644 index 0000000..80dd847 --- /dev/null +++ b/Local/NosSmooth.LocalBinding/Structs/MapNpcObj.cs @@ -0,0 +1,25 @@ +// +// MapNpcObj.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 Reloaded.Memory.Sources; + +namespace NosSmooth.LocalBinding.Structs; + +/// +/// Npc NosTale object. +/// +public class MapNpcObj : MapBaseObj +{ + /// + /// Initializes a new instance of the class. + /// + /// The memory. + /// The pointer to the object. + public MapNpcObj(IMemory memory, IntPtr mapObjPointer) + : base(memory, mapObjPointer) + { + } +} \ No newline at end of file diff --git a/Local/NosSmooth.LocalBinding/Structs/PetManager.cs b/Local/NosSmooth.LocalBinding/Structs/PetManager.cs new file mode 100644 index 0000000..a929ba2 --- /dev/null +++ b/Local/NosSmooth.LocalBinding/Structs/PetManager.cs @@ -0,0 +1,49 @@ +// +// PetManager.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.Options; +using Reloaded.Memory.Sources; +using Remora.Results; + +namespace NosSmooth.LocalBinding.Structs; + +/// +/// NosTale pet manager. +/// +public class PetManager : ControlManager +{ + private readonly IMemory _memory; + + /// + /// Initializes a new instance of the class. + /// + /// The memory. + /// The pet manager address. + public PetManager(IMemory memory, IntPtr petManagerAddress) + : base(memory) + { + _memory = memory; + Address = petManagerAddress; + } + + /// + /// Gets the address of the pet manager. + /// + public override IntPtr Address { get; } + + /// + /// Gets the player object. + /// + public MapNpcObj Pet + { + get + { + _memory.SafeRead(Address + 0x20, out int playerAddress); + return new MapNpcObj(_memory, (IntPtr)playerAddress); + } + } +} \ No newline at end of file diff --git a/Local/NosSmooth.LocalBinding/Structs/PetManagerList.cs b/Local/NosSmooth.LocalBinding/Structs/PetManagerList.cs new file mode 100644 index 0000000..774cae9 --- /dev/null +++ b/Local/NosSmooth.LocalBinding/Structs/PetManagerList.cs @@ -0,0 +1,116 @@ +// +// 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); + } +} \ No newline at end of file diff --git a/Local/NosSmooth.LocalBinding/Structs/SceneManager.cs b/Local/NosSmooth.LocalBinding/Structs/SceneManager.cs index d426f36..0292855 100644 --- a/Local/NosSmooth.LocalBinding/Structs/SceneManager.cs +++ b/Local/NosSmooth.LocalBinding/Structs/SceneManager.cs @@ -36,7 +36,7 @@ public class SceneManager return new NotFoundError("Cannot find the main module of the target process."); } - int staticManagerAddress = (int)nosBrowser.Process.MainModule.BaseAddress + characterObjectAddress.Offset + 1; + int staticManagerAddress = (int)nosBrowser.Process.MainModule.BaseAddress + characterObjectAddress.Offset; return new SceneManager(nosBrowser.Memory, staticManagerAddress, options.SceneManagerOffsets); } -- 2.48.1