From f6a5b5809481898dc9d99f17c67b4a74f06c672e Mon Sep 17 00:00:00 2001 From: Rutherther Date: Thu, 27 Jan 2022 14:34:59 +0100 Subject: [PATCH] refactor!(localbinding): abstract nostale list and nostale objects --- .../Structs/ControlManager.cs | 22 ++--- .../Structs/MapBaseObj.cs | 20 +++-- .../{MapObjBaseList.cs => NostaleList.cs} | 48 ++++++----- .../Structs/NostaleObject.cs | 43 ++++++++++ .../Structs/PetManager.cs | 21 +++-- .../Structs/PetManagerList.cs | 80 +++---------------- .../Structs/PlayerManager.cs | 14 ++-- .../Structs/SceneManager.cs | 20 ++--- 8 files changed, 125 insertions(+), 143 deletions(-) rename Local/NosSmooth.LocalBinding/Structs/{MapObjBaseList.cs => NostaleList.cs} (61%) create mode 100644 Local/NosSmooth.LocalBinding/Structs/NostaleObject.cs diff --git a/Local/NosSmooth.LocalBinding/Structs/ControlManager.cs b/Local/NosSmooth.LocalBinding/Structs/ControlManager.cs index 3b666617fdb9b7bb7f97b86be9195058e34d3c57..9870d7e0828c86e409ca928ee6d56a5156ca508f 100644 --- a/Local/NosSmooth.LocalBinding/Structs/ControlManager.cs +++ b/Local/NosSmooth.LocalBinding/Structs/ControlManager.cs @@ -11,24 +11,18 @@ namespace NosSmooth.LocalBinding.Structs; /// /// Base for player and pet managers. /// -public abstract class ControlManager +public abstract class ControlManager : NostaleObject { - private readonly IMemory _memory; - /// /// Initializes a new instance of the class. /// /// The memory. - public ControlManager(IMemory memory) + /// The address of the manager. + protected ControlManager(IMemory memory, IntPtr address) + : base(memory, address) { - _memory = memory; } - /// - /// Gets the address of the manager. - /// - public abstract IntPtr Address { get; } - /// /// Gets the current player position x coordinate. /// @@ -36,7 +30,7 @@ public abstract class ControlManager { get { - _memory.SafeRead(Address + 0x4, out short x); + Memory.SafeRead(Address + 0x4, out short x); return x; } } @@ -48,7 +42,7 @@ public abstract class ControlManager { get { - _memory.SafeRead(Address + 0x6, out short y); + Memory.SafeRead(Address + 0x6, out short y); return y; } } @@ -60,7 +54,7 @@ public abstract class ControlManager { get { - _memory.SafeRead(Address + 0x8, out short targetX); + Memory.SafeRead(Address + 0x8, out short targetX); return targetX; } } @@ -72,7 +66,7 @@ public abstract class ControlManager { get { - _memory.SafeRead(Address + 0xA, out short targetX); + Memory.SafeRead(Address + 0xA, out short targetX); return targetX; } } diff --git a/Local/NosSmooth.LocalBinding/Structs/MapBaseObj.cs b/Local/NosSmooth.LocalBinding/Structs/MapBaseObj.cs index 4ac3a2a2e739d64414874b65675fd31f534d6e2d..9e77a840f30e59b30bdea6e0aa82135325e5c4ab 100644 --- a/Local/NosSmooth.LocalBinding/Structs/MapBaseObj.cs +++ b/Local/NosSmooth.LocalBinding/Structs/MapBaseObj.cs @@ -11,10 +11,14 @@ namespace NosSmooth.LocalBinding.Structs; /// /// Base map object. Common for players, monsters, npcs. /// -public class MapBaseObj +public class MapBaseObj : NostaleObject { - private readonly IMemory _memory; - private readonly IntPtr _mapObjPointer; + /// + /// Initializes a new instance of the class. + /// + public MapBaseObj() + { + } /// /// Initializes a new instance of the class. @@ -22,16 +26,10 @@ public class MapBaseObj /// The memory. /// The map object pointer. public MapBaseObj(IMemory memory, IntPtr mapObjPointer) + : base(memory, mapObjPointer) { - _memory = memory; - _mapObjPointer = mapObjPointer; } - /// - /// Gets the pointer to the object. - /// - public IntPtr Address => _mapObjPointer; - /// /// Gets the id of the entity. /// @@ -39,7 +37,7 @@ public class MapBaseObj { get { - _memory.SafeRead(_mapObjPointer + 0x08, out int id); + Memory.SafeRead(Address + 0x08, out int id); return id; } } diff --git a/Local/NosSmooth.LocalBinding/Structs/MapObjBaseList.cs b/Local/NosSmooth.LocalBinding/Structs/NostaleList.cs similarity index 61% rename from Local/NosSmooth.LocalBinding/Structs/MapObjBaseList.cs rename to Local/NosSmooth.LocalBinding/Structs/NostaleList.cs index 13585b08bbb195c722b6af9cfb50e85c7b4b9bd8..4e914f8cad3fe9a867260f7645c333b8a4d4cc70 100644 --- a/Local/NosSmooth.LocalBinding/Structs/MapObjBaseList.cs +++ b/Local/NosSmooth.LocalBinding/Structs/NostaleList.cs @@ -1,5 +1,5 @@ // -// MapObjBaseList.cs +// NostaleList.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. @@ -11,33 +11,36 @@ using Reloaded.Memory.Sources; namespace NosSmooth.LocalBinding.Structs; /// -/// List of map objects. +/// A class representing a list from nostale. /// -public class MapObjBaseList : IEnumerable +/// The type. +public class NostaleList : IEnumerable + where T : NostaleObject, new() { private readonly IMemory _memory; - private readonly IntPtr _objListPointer; - private readonly ArrayPtr _objList; /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// The memory. /// The object list pointer. - public MapObjBaseList(IMemory memory, IntPtr objListPointer) + public NostaleList(IMemory memory, IntPtr objListPointer) { - memory.Read(objListPointer + 0x04, out uint arrayFirst); - _objList = new ArrayPtr(arrayFirst, source: memory); _memory = memory; - _objListPointer = objListPointer; + Address = objListPointer; } + /// + /// Gets the address. + /// + protected IntPtr Address { get; } + /// /// Gets the element at the given index. /// /// The index of the element. /// Thrown if the index is not in the bounds of the array. - public MapBaseObj this[int index] + public T this[int index] { get { @@ -46,7 +49,14 @@ public class MapObjBaseList : IEnumerable throw new IndexOutOfRangeException(); } - return new MapBaseObj(_memory, (IntPtr)_objList[index]); + _memory.SafeRead(Address + 0x04, out int arrayAddress); + _memory.SafeRead((IntPtr)arrayAddress + (0x04 * index), out int objectAddress); + + return new T + { + Memory = _memory, + Address = (IntPtr)objectAddress + }; } } @@ -57,15 +67,15 @@ public class MapObjBaseList : IEnumerable { get { - _memory.SafeRead(_objListPointer + 0x08, out int length); + _memory.SafeRead(Address + 0x08, out int length); return length; } } /// - public IEnumerator GetEnumerator() + public IEnumerator GetEnumerator() { - return new MapObjBaseEnumerator(this); + return new NostaleListEnumerator(this); } /// @@ -74,12 +84,12 @@ public class MapObjBaseList : IEnumerable return GetEnumerator(); } - private class MapObjBaseEnumerator : IEnumerator + private class NostaleListEnumerator : IEnumerator { - private readonly MapObjBaseList _list; + private readonly NostaleList _list; private int _index; - public MapObjBaseEnumerator(MapObjBaseList list) + public NostaleListEnumerator(NostaleList list) { _index = -1; _list = list; @@ -101,7 +111,7 @@ public class MapObjBaseList : IEnumerable _index = -1; } - public MapBaseObj Current => _list[_index]; + public T Current => _list[_index]; object IEnumerator.Current => Current; diff --git a/Local/NosSmooth.LocalBinding/Structs/NostaleObject.cs b/Local/NosSmooth.LocalBinding/Structs/NostaleObject.cs new file mode 100644 index 0000000000000000000000000000000000000000..6f53b8d0f3e6cafa439d1fe0ef857f3a3fc122ce --- /dev/null +++ b/Local/NosSmooth.LocalBinding/Structs/NostaleObject.cs @@ -0,0 +1,43 @@ +// +// NostaleObject.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; + +/// +/// A NosTale object base. +/// +public abstract class NostaleObject +{ + /// + /// Initializes a new instance of the class. + /// + internal NostaleObject() + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The memory. + /// The address in the memory. + protected NostaleObject(IMemory memory, IntPtr address) + { + Memory = memory; + Address = address; + } + + /// + /// Gets the memory the object is stored in. + /// + internal virtual IMemory Memory { get; set; } = null!; + + /// + /// Gets the address of the object. + /// + public virtual IntPtr Address { get; internal set; } = IntPtr.Zero; +} \ No newline at end of file diff --git a/Local/NosSmooth.LocalBinding/Structs/PetManager.cs b/Local/NosSmooth.LocalBinding/Structs/PetManager.cs index 8ef429a390109ff924186af19e863aa91dd36997..84cca5154cf4da22804290bce3ce71559ca4eac4 100644 --- a/Local/NosSmooth.LocalBinding/Structs/PetManager.cs +++ b/Local/NosSmooth.LocalBinding/Structs/PetManager.cs @@ -16,7 +16,13 @@ namespace NosSmooth.LocalBinding.Structs; /// public class PetManager : ControlManager { - private readonly IMemory _memory; + /// + /// Initializes a new instance of the class. + /// + public PetManager() + : base(null!, IntPtr.Zero) + { + } /// /// Initializes a new instance of the class. @@ -24,17 +30,10 @@ public class PetManager : ControlManager /// The memory. /// The pet manager address. public PetManager(IMemory memory, IntPtr petManagerAddress) - : base(memory) + : base(memory, petManagerAddress) { - _memory = memory; - Address = petManagerAddress; } - /// - /// Gets the address of the pet manager. - /// - public override IntPtr Address { get; } - /// /// Gets the player object. /// @@ -42,8 +41,8 @@ public class PetManager : ControlManager { get { - _memory.SafeRead(Address + 0x7C, out int playerAddress); - return new MapNpcObj(_memory, (IntPtr)playerAddress); + Memory.SafeRead(Address + 0x7C, 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 index bb042c4e2a3bcf71182252daa5b8ff74d4385c17..37e99f3637a90004317b04f18b1675a1b8d5df8c 100644 --- a/Local/NosSmooth.LocalBinding/Structs/PetManagerList.cs +++ b/Local/NosSmooth.LocalBinding/Structs/PetManagerList.cs @@ -9,41 +9,38 @@ using NosSmooth.LocalBinding.Extensions; using NosSmooth.LocalBinding.Options; using Reloaded.Memory.Sources; using Remora.Results; +using SharpDisasm.Udis86; namespace NosSmooth.LocalBinding.Structs; /// /// NosTale list of . /// -public class PetManagerList +public class PetManagerList : NostaleList { /// /// Create instance. /// - /// The NosTale process browser. + /// The NosTale process browser. /// The options. /// The player manager or an error. - public static Result Create(ExternalNosBrowser nosBrowser, PetManagerOptions options) + public static Result Create(NosBrowserManager nosBrowserManager, PetManagerOptions options) { - var characterObjectAddress = nosBrowser.Scanner.CompiledFindPattern(options.PetManagerListPattern); + var characterObjectAddress = nosBrowserManager.Scanner.CompiledFindPattern(options.PetManagerListPattern); if (!characterObjectAddress.Found) { return new BindingNotFoundError(options.PetManagerListPattern, "PetManagerList"); } - if (nosBrowser.Process.MainModule is null) + if (nosBrowserManager.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); + int staticManagerAddress = (int)nosBrowserManager.Process.MainModule.BaseAddress + characterObjectAddress.Offset; + return new PetManagerList(nosBrowserManager.Memory, staticManagerAddress, options.PetManagerListOffsets); } - private readonly IMemory _memory; - private readonly int _staticPetManagerListAddress; - private readonly int[] _staticPetManagerOffsets; - /// /// Initializes a new instance of the class. /// @@ -51,66 +48,7 @@ public class PetManagerList /// The static pet manager address. /// The offsets to follow to the pet manager list address. public PetManagerList(IMemory memory, int staticPetManagerListAddress, int[] staticPetManagerOffsets) + : base(memory, memory.FollowStaticAddressOffsets(staticPetManagerListAddress, 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 PetManager? GetFirst() - { - if (Length == 0) - { - return null; - } - - _memory.SafeRead(List, out int firstAddress); - return new PetManager(_memory, (IntPtr)firstAddress); - } - - /// - /// Get the second pet. - /// - /// Second pet, if exists. - public PetManager? GetSecond() - { - if (Length < 2) - { - return null; - } - - _memory.SafeRead(List + 0x04, out int secondAddress); - return new PetManager(_memory, (IntPtr)secondAddress); } } \ No newline at end of file diff --git a/Local/NosSmooth.LocalBinding/Structs/PlayerManager.cs b/Local/NosSmooth.LocalBinding/Structs/PlayerManager.cs index 1e3519c4da3f63813f8e95211769497102ba4396..8594628d53ae93f934cf31f476425e7569439c25 100644 --- a/Local/NosSmooth.LocalBinding/Structs/PlayerManager.cs +++ b/Local/NosSmooth.LocalBinding/Structs/PlayerManager.cs @@ -21,24 +21,24 @@ public class PlayerManager : ControlManager /// /// Create instance. /// - /// The NosTale process browser. + /// The NosTale process browser. /// The options. /// The player manager or an error. - public static Result Create(ExternalNosBrowser nosBrowser, PlayerManagerOptions options) + public static Result Create(NosBrowserManager nosBrowserManager, PlayerManagerOptions options) { - var characterObjectAddress = nosBrowser.Scanner.CompiledFindPattern(options.PlayerManagerPattern); + var characterObjectAddress = nosBrowserManager.Scanner.CompiledFindPattern(options.PlayerManagerPattern); if (!characterObjectAddress.Found) { return new BindingNotFoundError(options.PlayerManagerPattern, "PlayerManager"); } - if (nosBrowser.Process.MainModule is null) + if (nosBrowserManager.Process.MainModule is null) { return new NotFoundError("Cannot find the main module of the target process."); } - var staticAddress = (int)nosBrowser.Process.MainModule.BaseAddress + characterObjectAddress.Offset; - return new PlayerManager(nosBrowser.Memory, staticAddress, options.PlayerManagerOffsets); + var staticAddress = (int)nosBrowserManager.Process.MainModule.BaseAddress + characterObjectAddress.Offset; + return new PlayerManager(nosBrowserManager.Memory, staticAddress, options.PlayerManagerOffsets); } private readonly IMemory _memory; @@ -52,7 +52,7 @@ public class PlayerManager : ControlManager /// 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) + : base(memory, IntPtr.Zero) { _memory = memory; _staticPlayerManagerAddress = staticPlayerManagerAddress; diff --git a/Local/NosSmooth.LocalBinding/Structs/SceneManager.cs b/Local/NosSmooth.LocalBinding/Structs/SceneManager.cs index e2df15af5b53aa5dc4a03bc587bc7935ded138cc..6e334290bcb6b95c0dae5decf5d78bc4cb0439ea 100644 --- a/Local/NosSmooth.LocalBinding/Structs/SceneManager.cs +++ b/Local/NosSmooth.LocalBinding/Structs/SceneManager.cs @@ -20,24 +20,24 @@ public class SceneManager /// /// Create instance. /// - /// The NosTale process browser. + /// The NosTale process browser. /// The options. /// The player manager or an error. - public static Result Create(ExternalNosBrowser nosBrowser, SceneManagerOptions options) + public static Result Create(NosBrowserManager nosBrowserManager, SceneManagerOptions options) { - var characterObjectAddress = nosBrowser.Scanner.CompiledFindPattern(options.SceneManagerObjectPattern); + var characterObjectAddress = nosBrowserManager.Scanner.CompiledFindPattern(options.SceneManagerObjectPattern); if (!characterObjectAddress.Found) { return new BindingNotFoundError(options.SceneManagerObjectPattern, "SceneManager"); } - if (nosBrowser.Process.MainModule is null) + if (nosBrowserManager.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 SceneManager(nosBrowser.Memory, staticManagerAddress, options.SceneManagerOffsets); + int staticManagerAddress = (int)nosBrowserManager.Process.MainModule.BaseAddress + characterObjectAddress.Offset; + return new SceneManager(nosBrowserManager.Memory, staticManagerAddress, options.SceneManagerOffsets); } private readonly int[] _sceneManagerOffsets; @@ -65,22 +65,22 @@ public class SceneManager /// /// Gets the player list. /// - public MapObjBaseList PlayerList => new MapObjBaseList(_memory, ReadPtr(Address + 0xC)); + public NostaleList PlayerList => new NostaleList(_memory, ReadPtr(Address + 0xC)); /// /// Gets the monster list. /// - public MapObjBaseList MonsterList => new MapObjBaseList(_memory, ReadPtr(Address + 0x10)); + public NostaleList MonsterList => new NostaleList(_memory, ReadPtr(Address + 0x10)); /// /// Gets the npc list. /// - public MapObjBaseList NpcList => new MapObjBaseList(_memory, ReadPtr(Address + 0x14)); + public NostaleList NpcList => new NostaleList(_memory, ReadPtr(Address + 0x14)); /// /// Gets the item list. /// - public MapObjBaseList ItemList => new MapObjBaseList(_memory, ReadPtr(Address + 0x18)); + public NostaleList ItemList => new NostaleList(_memory, ReadPtr(Address + 0x18)); /// /// Gets the entity that is currently being followed by the player.