//
// EntityFocusHook.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.EntityFocus.
///
internal class EntityFocusHook : CancelableNostaleHook, IEntityFocusHook
{
///
/// Create the packet send hook.
///
/// The binding manager.
/// The browser manager.
/// The options for the binding.
/// A packet send hook or an error.
public static Result Create
(NosBindingManager bindingManager, NosBrowserManager browserManager, HookOptions options)
{
var hook = CreateHook
(
bindingManager,
() => new EntityFocusHook(browserManager.UnitManager),
hook => hook.Detour,
options
);
return hook;
}
private readonly UnitManager _unitManager;
private EntityFocusHook(UnitManager unitManager)
{
_unitManager = unitManager;
}
///
public override string Name => IHookManager.EntityFocusName;
///
public override IEntityFocusHook.EntityFocusWrapperDelegate WrapperFunction => (entity) => OriginalFunction
(_unitManager.Address, entity?.Address ?? 0);
///
protected override IEntityFocusHook.EntityFocusDelegate WrapWithCalling(IEntityFocusHook.EntityFocusDelegate function)
=> (unitManagerPtr, entityPtr) =>
{
CallingFromNosSmooth = true;
var res = function(unitManagerPtr, entityPtr);
CallingFromNosSmooth = false;
return res;
};
private nuint Detour
(
nuint unitManagerPtr,
nuint entityPtr
)
{
var entity = new MapBaseObj(_unitManager.Memory, entityPtr);
var entityArgs = new EntityEventArgs(entity);
return HandleCall(entityArgs);
}
}