From 07aa0c3ff886a320f3755df66bd172214c972371 Mon Sep 17 00:00:00 2001 From: Rutherther Date: Sun, 23 Jan 2022 21:15:17 +0100 Subject: [PATCH] fix: move focus function to unit manager --- .../Extensions/ServiceCollectionExtensions.cs | 3 +- .../NosBindingManager.cs | 41 +++--- ...anagerBinding.cs => UnitManagerBinding.cs} | 118 ++++++------------ ...ptions.cs => UnitManagerBindingOptions.cs} | 18 ++- .../Structs/SceneManager.cs | 53 ++++++++ 5 files changed, 129 insertions(+), 104 deletions(-) rename Local/NosSmooth.LocalBinding/Objects/{SceneManagerBinding.cs => UnitManagerBinding.cs} (60%) rename Local/NosSmooth.LocalBinding/Options/{SceneManagerBindingOptions.cs => UnitManagerBindingOptions.cs} (57%) diff --git a/Local/NosSmooth.LocalBinding/Extensions/ServiceCollectionExtensions.cs b/Local/NosSmooth.LocalBinding/Extensions/ServiceCollectionExtensions.cs index 395efb3..c51172b 100644 --- a/Local/NosSmooth.LocalBinding/Extensions/ServiceCollectionExtensions.cs +++ b/Local/NosSmooth.LocalBinding/Extensions/ServiceCollectionExtensions.cs @@ -29,8 +29,9 @@ public static class ServiceCollectionExtensions { return serviceCollection .AddSingleton() + .AddSingleton(p => p.GetRequiredService().NosBrowser) .AddSingleton(p => p.GetRequiredService().PlayerManager) - .AddSingleton(p => p.GetRequiredService().SceneManager) + .AddSingleton(p => p.GetRequiredService().UnitManager) .AddSingleton(p => p.GetRequiredService().Network); } } \ No newline at end of file diff --git a/Local/NosSmooth.LocalBinding/NosBindingManager.cs b/Local/NosSmooth.LocalBinding/NosBindingManager.cs index a76337b..d755f46 100644 --- a/Local/NosSmooth.LocalBinding/NosBindingManager.cs +++ b/Local/NosSmooth.LocalBinding/NosBindingManager.cs @@ -23,12 +23,11 @@ public class NosBindingManager : IDisposable { private readonly CharacterBindingOptions _characterBindingOptions; private readonly NetworkBindingOptions _networkBindingOptions; - private readonly ExternalNosBrowser _nosBrowser; - private SceneManagerBindingOptions _sceneManagerBindingOptions; + private UnitManagerBindingOptions _unitManagerBindingOptions; private NetworkBinding? _networkBinding; private PlayerManagerBinding? _characterBinding; - private SceneManagerBinding? _sceneManagerBinding; + private UnitManagerBinding? _unitManagerBinding; /// /// Initializes a new instance of the class. @@ -43,7 +42,7 @@ public class NosBindingManager : IDisposable ( IOptions characterBindingOptions, IOptions networkBindingOptions, - IOptions sceneManagerBindingOptions, + IOptions sceneManagerBindingOptions, IOptions playerManagerOptions, IOptions sceneManagerOptions, IOptions petManagerOptions @@ -54,8 +53,8 @@ public class NosBindingManager : IDisposable Scanner = new Scanner(Process.GetCurrentProcess(), Process.GetCurrentProcess().MainModule); _characterBindingOptions = characterBindingOptions.Value; _networkBindingOptions = networkBindingOptions.Value; - _sceneManagerBindingOptions = sceneManagerBindingOptions.Value; - _nosBrowser = new ExternalNosBrowser + _unitManagerBindingOptions = sceneManagerBindingOptions.Value; + NosBrowser = new ExternalNosBrowser ( Process.GetCurrentProcess(), playerManagerOptions.Value, @@ -64,6 +63,11 @@ public class NosBindingManager : IDisposable ); } + /// + /// Gets the nos browser. + /// + public ExternalNosBrowser NosBrowser { get; } + /// /// Gets the memory scanner. /// @@ -123,11 +127,11 @@ public class NosBindingManager : IDisposable /// Gets the character binding. /// /// Thrown if the manager is not initialized yet. - public SceneManagerBinding SceneManager + public UnitManagerBinding UnitManager { get { - if (_sceneManagerBinding is null) + if (_unitManagerBinding is null) { throw new InvalidOperationException ( @@ -135,7 +139,7 @@ public class NosBindingManager : IDisposable ); } - return _sceneManagerBinding; + return _unitManagerBinding; } } @@ -152,18 +156,12 @@ public class NosBindingManager : IDisposable } _networkBinding = network.Entity; - var playerManager = _nosBrowser.GetPlayerManager(); + var playerManager = NosBrowser.GetPlayerManager(); if (!playerManager.IsSuccess) { return Result.FromError(playerManager); } - var sceneManager = _nosBrowser.GetSceneManager(); - if (!sceneManager.IsSuccess) - { - return Result.FromError(sceneManager); - } - var playerManagerBinding = PlayerManagerBinding.Create ( this, @@ -176,17 +174,16 @@ public class NosBindingManager : IDisposable } _characterBinding = playerManagerBinding.Entity; - var sceneManagerBinding = SceneManagerBinding.Create + var unitManagerBinding = UnitManagerBinding.Create ( this, - sceneManager.Entity, - _sceneManagerBindingOptions + _unitManagerBindingOptions ); - if (!sceneManagerBinding.IsSuccess) + if (!unitManagerBinding.IsSuccess) { - return Result.FromError(sceneManagerBinding); + return Result.FromError(unitManagerBinding); } - _sceneManagerBinding = sceneManagerBinding.Entity; + _unitManagerBinding = unitManagerBinding.Entity; return Result.FromSuccess(); } diff --git a/Local/NosSmooth.LocalBinding/Objects/SceneManagerBinding.cs b/Local/NosSmooth.LocalBinding/Objects/UnitManagerBinding.cs similarity index 60% rename from Local/NosSmooth.LocalBinding/Objects/SceneManagerBinding.cs rename to Local/NosSmooth.LocalBinding/Objects/UnitManagerBinding.cs index 3ce7946..f1a8fc6 100644 --- a/Local/NosSmooth.LocalBinding/Objects/SceneManagerBinding.cs +++ b/Local/NosSmooth.LocalBinding/Objects/UnitManagerBinding.cs @@ -1,11 +1,12 @@ // -// SceneManagerBinding.cs +// UnitManagerBinding.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 System.Diagnostics; using NosSmooth.LocalBinding.Errors; +using NosSmooth.LocalBinding.Extensions; using NosSmooth.LocalBinding.Options; using NosSmooth.LocalBinding.Structs; using Reloaded.Hooks; @@ -13,6 +14,7 @@ using Reloaded.Hooks.Definitions; using Reloaded.Hooks.Definitions.X86; using Reloaded.Memory.Buffers.Internal.Kernel32; using Remora.Results; +using SharpDisasm.Udis86; namespace NosSmooth.LocalBinding.Objects; @@ -22,42 +24,48 @@ namespace NosSmooth.LocalBinding.Objects; /// /// The scene manager holds addresses to entities, mouse position, .... /// -public class SceneManagerBinding +public class UnitManagerBinding { [Function ( new[] { FunctionAttribute.Register.eax, FunctionAttribute.Register.edx }, - FunctionAttribute.Register.ecx, + FunctionAttribute.Register.eax, FunctionAttribute.StackCleanup.Callee )] - private delegate void FocusEntityDelegate(IntPtr outputStringPtr, IntPtr entityPtr); + private delegate int FocusEntityDelegate(IntPtr unitManagerPtr, IntPtr entityPtr); /// /// Create the scene manager binding. /// /// The binding manager. - /// The scene manager. /// The options for the binding. /// A network binding or an error. - public static Result Create - (NosBindingManager bindingManager, SceneManager sceneManager, SceneManagerBindingOptions bindingOptions) + public static Result Create + (NosBindingManager bindingManager, UnitManagerBindingOptions bindingOptions) { var process = Process.GetCurrentProcess(); + var unitManagerStaticAddress = bindingManager.Scanner.CompiledFindPattern(bindingOptions.UnitManagerPattern); + if (!unitManagerStaticAddress.Found) + { + return new BindingNotFoundError(bindingOptions.UnitManagerPattern, "UnitManagerBinding.UnitManager"); + } + var focusEntityAddress = bindingManager.Scanner.CompiledFindPattern(bindingOptions.FocusEntityPattern); if (!focusEntityAddress.Found) { - return new BindingNotFoundError(bindingOptions.FocusEntityPattern, "SceneManagerBinding.FocusEntity"); + return new BindingNotFoundError(bindingOptions.FocusEntityPattern, "UnitManagerBinding.FocusEntity"); } var focusEntityFunction = bindingManager.Hooks.CreateFunction (focusEntityAddress.Offset + (int)process.MainModule!.BaseAddress + 0x04); var focusEntityWrapper = focusEntityFunction.GetWrapper(); - var binding = new SceneManagerBinding + var binding = new UnitManagerBinding ( bindingManager, - sceneManager, + (int)process.MainModule!.BaseAddress + unitManagerStaticAddress.Offset, + bindingOptions.UnitManagerOffsets, focusEntityWrapper ); @@ -71,23 +79,34 @@ public class SceneManagerBinding return binding; } + private readonly int _staticUnitManagerAddress; + private readonly int[] _unitManagerOffsets; + private readonly NosBindingManager _bindingManager; private FocusEntityDelegate _originalFocusEntity; private IHook? _focusHook; - private SceneManagerBinding + private UnitManagerBinding ( NosBindingManager bindingManager, - SceneManager sceneManager, + int staticUnitManagerAddress, + int[] unitManagerOffsets, FocusEntityDelegate originalFocusEntity ) { _originalFocusEntity = originalFocusEntity; _bindingManager = bindingManager; - SceneManager = sceneManager; + _staticUnitManagerAddress = staticUnitManagerAddress; + _unitManagerOffsets = unitManagerOffsets; } + /// + /// Gets the address of unit manager. + /// + public IntPtr Address => _bindingManager.Memory.FollowStaticAddressOffsets + (_staticUnitManagerAddress, _unitManagerOffsets); + /// /// Event that is called when entity focus was called by NosTale. /// @@ -96,27 +115,6 @@ public class SceneManagerBinding /// public event Func? EntityFocus; - /// - /// Gets the scene manager object. - /// - public SceneManager SceneManager { get; } - - /// - /// Focus the entity with the given id. - /// - /// The id of the entity. - /// A result that may or may not have succeeded. - public Result FocusEntity(int id) - { - var entityResult = FindEntity(id); - if (!entityResult.IsSuccess) - { - return Result.FromError(entityResult); - } - - return FocusEntity(entityResult.Entity); - } - /// /// Focus the entity. /// @@ -134,7 +132,8 @@ public class SceneManagerBinding { try { - _originalFocusEntity(IntPtr.Zero, entityAddress); + var res = _originalFocusEntity(Address, entityAddress); + Console.WriteLine(res); } catch (Exception e) { @@ -144,48 +143,7 @@ public class SceneManagerBinding return Result.FromSuccess(); } - /// - /// Find the given entity address. - /// - /// The id of the entity. - /// The pointer to the entity or an error. - public Result FindEntity(int id) - { - if (id == 0) - { - return Result.FromSuccess(null); - } - - var manager = SceneManager; - - var item = manager.ItemList.FirstOrDefault(x => x.Id == id); - if (item is not null) - { - return item; - } - - var monster = manager.MonsterList.FirstOrDefault(x => x.Id == id); - if (monster is not null) - { - return monster; - } - - var npc = manager.NpcList.FirstOrDefault(x => x.Id == id); - if (npc is not null) - { - return npc; - } - - var player = manager.PlayerList.FirstOrDefault(x => x.Id == id); - if (player is not null) - { - return player; - } - - return new NotFoundError($"Could not find entity with id {id}"); - } - - private void FocusEntityDetour(IntPtr outputStringPtr, IntPtr entityId) + private int FocusEntityDetour(IntPtr unitManagerPtr, IntPtr entityId) { MapBaseObj? obj = null; if (entityId != IntPtr.Zero) @@ -196,7 +154,11 @@ public class SceneManagerBinding var result = EntityFocus?.Invoke(obj); if (result ?? true) { - _originalFocusEntity(outputStringPtr, entityId); + var res = _originalFocusEntity(unitManagerPtr, entityId); + Console.WriteLine(res); + return res; } + + return 0; } } \ No newline at end of file diff --git a/Local/NosSmooth.LocalBinding/Options/SceneManagerBindingOptions.cs b/Local/NosSmooth.LocalBinding/Options/UnitManagerBindingOptions.cs similarity index 57% rename from Local/NosSmooth.LocalBinding/Options/SceneManagerBindingOptions.cs rename to Local/NosSmooth.LocalBinding/Options/UnitManagerBindingOptions.cs index 18d669c..e5de2c1 100644 --- a/Local/NosSmooth.LocalBinding/Options/SceneManagerBindingOptions.cs +++ b/Local/NosSmooth.LocalBinding/Options/UnitManagerBindingOptions.cs @@ -1,5 +1,5 @@ // -// SceneManagerBindingOptions.cs +// UnitManagerBindingOptions.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. @@ -9,10 +9,22 @@ using NosSmooth.LocalBinding.Objects; namespace NosSmooth.LocalBinding.Options; /// -/// Options for . +/// Options for . /// -public class SceneManagerBindingOptions +public class UnitManagerBindingOptions { + /// + /// Gets or sets the pattern to static address of unit manager. + /// + public string UnitManagerPattern { get; set; } + = "TODO"; + + /// + /// Gets or sets the pointer offsets from the unit manager static address. + /// + public int[] UnitManagerOffsets { get; set; } + = { 15 }; + /// /// Gets or sets the pattern to find the focus entity method at. /// diff --git a/Local/NosSmooth.LocalBinding/Structs/SceneManager.cs b/Local/NosSmooth.LocalBinding/Structs/SceneManager.cs index 0292855..e2df15a 100644 --- a/Local/NosSmooth.LocalBinding/Structs/SceneManager.cs +++ b/Local/NosSmooth.LocalBinding/Structs/SceneManager.cs @@ -99,9 +99,62 @@ public class SceneManager } } + /// + /// Gets the lock on target marked address. + /// + public IntPtr LockOnTargetMarkedAddress + { + get + { + var ptr = ReadPtr(Address + 0x1C); + ptr = ReadPtr(ptr + 0x04); + ptr = ReadPtr(ptr + 0x00); + return ptr; + } + } + private IntPtr ReadPtr(IntPtr ptr) { _memory.Read(ptr, out int read); return (IntPtr)read; } + + /// + /// Find the given entity address. + /// + /// The id of the entity. + /// The pointer to the entity or an error. + public Result FindEntity(int id) + { + if (id == 0) + { + return Result.FromSuccess(null); + } + + var item = ItemList.FirstOrDefault(x => x.Id == id); + if (item is not null) + { + return item; + } + + var monster = MonsterList.FirstOrDefault(x => x.Id == id); + if (monster is not null) + { + return monster; + } + + var npc = NpcList.FirstOrDefault(x => x.Id == id); + if (npc is not null) + { + return npc; + } + + var player = PlayerList.FirstOrDefault(x => x.Id == id); + if (player is not null) + { + return player; + } + + return new NotFoundError($"Could not find entity with id {id}"); + } } \ No newline at end of file -- 2.48.1