From 5fde9c36cda22eb740f71ff6a7e2614b81efa8a2 Mon Sep 17 00:00:00 2001 From: Rutherther Date: Thu, 27 Jan 2022 14:35:11 +0100 Subject: [PATCH] feat(localbinding): add pet manager binding --- .../Objects/PetManagerBinding.cs | 129 ++++++++++++++++++ .../Options/PetManagerBindingOptions.cs | 26 ++++ 2 files changed, 155 insertions(+) create mode 100644 Local/NosSmooth.LocalBinding/Objects/PetManagerBinding.cs create mode 100644 Local/NosSmooth.LocalBinding/Options/PetManagerBindingOptions.cs diff --git a/Local/NosSmooth.LocalBinding/Objects/PetManagerBinding.cs b/Local/NosSmooth.LocalBinding/Objects/PetManagerBinding.cs new file mode 100644 index 0000000..e3a435e --- /dev/null +++ b/Local/NosSmooth.LocalBinding/Objects/PetManagerBinding.cs @@ -0,0 +1,129 @@ +// +// PetManagerBinding.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.Options; +using NosSmooth.LocalBinding.Structs; +using Reloaded.Hooks.Definitions; +using Reloaded.Hooks.Definitions.X86; +using Remora.Results; + +namespace NosSmooth.LocalBinding.Objects; + +/// +/// The binding to NosTale pet manager. +/// +public class PetManagerBinding +{ + /// + /// Create nostale pet manager binding. + /// + /// The binding manager. + /// The list of the pet managers. + /// The options. + /// A pet manager binding or and error. + public static Result Create + (NosBindingManager bindingManager, PetManagerList petManagerList, PetManagerBindingOptions options) + { + var petManager = new PetManagerBinding(petManagerList); + var hookResult = bindingManager.CreateHookFromPattern + ( + "PetManagerBinding.PetWalk", + petManager.PetWalkDetour, + options.PetWalkPattern, + hook: options.HookPetWalk + ); + + if (!hookResult.IsSuccess) + { + return Result.FromError(hookResult); + } + + petManager._petWalkHook = hookResult.Entity; + return petManager; + } + + [Function + ( + new[] { FunctionAttribute.Register.eax, FunctionAttribute.Register.edx, FunctionAttribute.Register.ecx }, + FunctionAttribute.Register.eax, + FunctionAttribute.StackCleanup.Callee + )] + private delegate bool PetWalkDelegate + ( + IntPtr petManagerPtr, + uint position, + short unknown0 = 0, + int unknown1 = 1, + int unknown2 = 1 + ); + + private IHook _petWalkHook = null!; + + private PetManagerBinding(PetManagerList petManagerList) + { + PetManagerList = petManagerList; + } + + /// + /// Gets the hook of the pet walk function. + /// + public IHook PetWalkHook => _petWalkHook; + + /// + /// Gets pet manager list. + /// + public PetManagerList PetManagerList { get; } + + /// + /// Walk the given pet to the given location. + /// + /// Index of the pet to walk. -1 for every pet currently available. + /// The x coordinate. + /// The y coordinate. + /// A result returned from NosTale or an error. + public Result PetWalk(int selector, ushort x, ushort y) + { + uint position = ((uint)y << 16) | x; + if (PetManagerList.Length < selector + 1) + { + return new NotFoundError("Could not find the pet using the given selector."); + } + + if (selector == -1) + { + bool lastResult = true; + for (int i = 0; i < PetManagerList.Length; i++) + { + lastResult = _petWalkHook.OriginalFunction(PetManagerList[i].Address, position); + } + + return lastResult; + } + else + { + return _petWalkHook.OriginalFunction(PetManagerList[selector].Address, position); + } + } + + private bool PetWalkDetour + ( + IntPtr petManagerPtr, + uint position, + short unknown0 = 0, + int unknown1 = 1, + int unknown2 = 1 + ) + { + return _petWalkHook.OriginalFunction + ( + petManagerPtr, + position, + unknown0, + unknown1, + unknown2 + ); + } +} \ No newline at end of file diff --git a/Local/NosSmooth.LocalBinding/Options/PetManagerBindingOptions.cs b/Local/NosSmooth.LocalBinding/Options/PetManagerBindingOptions.cs new file mode 100644 index 0000000..e63ed5c --- /dev/null +++ b/Local/NosSmooth.LocalBinding/Options/PetManagerBindingOptions.cs @@ -0,0 +1,26 @@ +// +// PetManagerBindingOptions.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 PetManagerBindingOptions +{ + /// + /// Gets or sets the pattern of a pet walk function. + /// + public string PetWalkPattern { get; set; } + = "55 8b ec 83 c4 e4 53 56 57 8b f9 89 55 fc 8b d8 c6 45 fb 00"; + + /// + /// Gets or sets whether to hook the pet walk function. + /// + public bool HookPetWalk { get; set; } = true; +} \ No newline at end of file -- 2.48.1