// // PetWalkHook.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.EventArgs; using NosSmooth.LocalBinding.Structs; using Reloaded.Memory.Sources; using Remora.Results; namespace NosSmooth.LocalBinding.Hooks.Implementations; /// /// A hook of NetworkManager.PetWalk. /// internal class PetWalkHook : CancelableNostaleHook, IPetWalkHook { /// /// Create the packet send hook. /// /// The binding manager. /// The options for the binding. /// A packet send hook or an error. public static Result Create (NosBindingManager bindingManager, HookOptions options) { var hook = CreateHook ( bindingManager, () => new PetWalkHook(bindingManager.Memory), hook => hook.Detour, options ); return hook; } private IMemory _memory; private PetWalkHook(IMemory memory) { _memory = memory; } /// public override string Name => IHookManager.PetWalkName; /// public override IPetWalkHook.PetWalkWrapperDelegate WrapperFunction => (p, x, y) => OriginalFunction(p.Address, (y << 16) | x) == 1; /// protected override IPetWalkHook.PetWalkDelegate WrapWithCalling(IPetWalkHook.PetWalkDelegate function) => ( petManagerPtr, position, un0, un1 ) => { CallingFromNosSmooth = true; var res = function(petManagerPtr, position, un0, un1); CallingFromNosSmooth = false; return res; }; private nuint Detour ( nuint petManagerPtr, int position, short un0, int un1 ) { var petManager = new PetManager(_memory, petManagerPtr); var walkArgs = new PetWalkEventArgs (petManager, (ushort)(position & 0xFFFF), (ushort)((position >> 16) & 0xFFFF)); return HandleCall(walkArgs); } }