//
// HooksConfigBuilder.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 Microsoft.Extensions.DependencyInjection;
using NosSmooth.LocalBinding.Options;
namespace NosSmooth.LocalBinding.Hooks;
///
/// Provides user-friendly builder for configuring
/// the hooks.
///
///
/// Use this by invoking .ConfigureHooks on IServiceCollection.
///
/// May change hook's pattern or offset as well as enable or disable
/// hooking altogether.
///
/// By default, networking (packet send, packet receive) hooking is enabled,
/// everything else is disabled.
///
/// The methods may be chained, you may call HookAll() and then start disabling
/// the ones you don't need.
///
public class HooksConfigBuilder
{
private readonly HookOptionsBuilder _packetSendHook;
private readonly HookOptionsBuilder _packetReceiveHook;
private readonly HookOptionsBuilder _playerWalkHook;
private readonly HookOptionsBuilder _petWalkHook;
private readonly HookOptionsBuilder _entityFocusHook;
private readonly HookOptionsBuilder _entityFollowHook;
private readonly HookOptionsBuilder _entityUnfollowHook;
private readonly HookOptionsBuilder _periodicHook;
///
/// Initializes a new instance of the class.
///
/// The default options to build from.
internal HooksConfigBuilder(HookManagerOptions options)
{
_packetSendHook = new HookOptionsBuilder(options.PacketSendHook);
_packetReceiveHook = new HookOptionsBuilder(options.PacketReceiveHook);
_playerWalkHook = new HookOptionsBuilder(options.PlayerWalkHook);
_petWalkHook = new HookOptionsBuilder(options.PetWalkHook);
_entityFocusHook = new HookOptionsBuilder(options.EntityFocusHook);
_entityFollowHook = new HookOptionsBuilder(options.EntityFollowHook);
_entityUnfollowHook = new HookOptionsBuilder(options.EntityUnfollowHook);
_periodicHook = new HookOptionsBuilder(options.PeriodicHook);
}
///
/// Disable all hooks.
///
/// This builder.
public HooksConfigBuilder HookNone()
{
_packetSendHook.Hook(false);
_packetReceiveHook.Hook(false);
_playerWalkHook.Hook(false);
_petWalkHook.Hook(false);
_entityFocusHook.Hook(false);
_entityFollowHook.Hook(false);
_entityUnfollowHook.Hook(false);
_periodicHook.Hook(false);
return this;
}
///
/// Enable all hooks.
///
/// This builder.
public HooksConfigBuilder HookAll()
{
_packetSendHook.Hook(true);
_packetReceiveHook.Hook(true);
_playerWalkHook.Hook(true);
_petWalkHook.Hook(true);
_entityFocusHook.Hook(true);
_entityFollowHook.Hook(true);
_entityUnfollowHook.Hook(true);
_periodicHook.Hook(true);
return this;
}
///
/// Enable networking (packet send, packet receive) hooks.
///
/// This builder.
public HooksConfigBuilder HookNetworking()
{
_packetSendHook.Hook(true);
_packetReceiveHook.Hook(true);
return this;
}
///
/// Configure periodic hook. Can be any periodic function in NosTale called on every frame.
/// Enables the hook. In case you just want to change the pattern or offset
/// and do not want to enable the hook, call .Hook(false) in the builder.
///
/// The configuring action.
/// This builder.
public HooksConfigBuilder HookPeriodic(Action>? configure = default)
{
if (configure is not null)
{
_periodicHook.Hook();
configure(_periodicHook);
}
else
{
_periodicHook.Hook(true);
}
return this;
}
///
/// Configure packet send hooks.
/// Enables the hook. In case you just want to change the pattern or offset
/// and do not want to enable the hook, call .Hook(false) in the builder.
///
/// The configuring action.
/// This builder.
public HooksConfigBuilder HookPacketSend(Action>? configure = default)
{
if (configure is not null)
{
_packetSendHook.Hook();
configure(_packetSendHook);
}
else
{
_packetSendHook.Hook(true);
}
return this;
}
///
/// Configure packet receive hooks.
/// Enables the hook. In case you just want to change the pattern or offset
/// and do not want to enable the hook, call .Hook(false) in the builder.
///
/// The configuring action.
/// This builder.
public HooksConfigBuilder HookPacketReceive(Action>? configure = default)
{
if (configure is not null)
{
_packetReceiveHook.Hook();
configure(_packetReceiveHook);
}
else
{
_packetReceiveHook.Hook(true);
}
return this;
}
///
/// Configure player walk hooks.
/// Enables the hook. In case you just want to change the pattern or offset
/// and do not want to enable the hook, call .Hook(false) in the builder.
///
/// The configuring action.
/// This builder.
public HooksConfigBuilder HookPlayerWalk(Action>? configure = default)
{
if (configure is not null)
{
_playerWalkHook.Hook();
configure(_playerWalkHook);
}
else
{
_playerWalkHook.Hook(true);
}
return this;
}
///
/// Configure pet walk hooks.
/// Enables the hook. In case you just want to change the pattern or offset
/// and do not want to enable the hook, call .Hook(false) in the builder.
///
/// The configuring action.
/// This builder.
public HooksConfigBuilder HookPetWalk(Action>? configure = default)
{
if (configure is not null)
{
_petWalkHook.Hook();
configure(_petWalkHook);
}
else
{
_petWalkHook.Hook(true);
}
return this;
}
///
/// Configure entity focus hooks.
/// Enables the hook. In case you just want to change the pattern or offset
/// and do not want to enable the hook, call .Hook(false) in the builder.
///
/// The configuring action.
/// This builder.
public HooksConfigBuilder HookEntityFocus(Action>? configure = default)
{
if (configure is not null)
{
_entityFocusHook.Hook();
configure(_entityFocusHook);
}
else
{
_entityFocusHook.Hook(true);
}
return this;
}
///
/// Configure entity follow hooks.
/// Enables the hook. In case you just want to change the pattern or offset
/// and do not want to enable the hook, call .Hook(false) in the builder.
///
/// The configuring action.
/// This builder.
public HooksConfigBuilder HookEntityFollow(Action>? configure = default)
{
if (configure is not null)
{
_entityFollowHook.Hook();
configure(_entityFollowHook);
}
else
{
_entityFollowHook.Hook(true);
}
return this;
}
///
/// Configure entity unfollow hooks.
/// Enables the hook. In case you just want to change the pattern or offset
/// and do not want to enable the hook, call .Hook(false) in the builder.
///
/// The configuring action.
/// This builder.
public HooksConfigBuilder HookEntityUnfollow(Action>? configure = default)
{
if (configure is not null)
{
_entityUnfollowHook.Hook();
configure(_entityUnfollowHook);
}
else
{
_entityUnfollowHook.Hook(true);
}
return this;
}
///
/// Applies configurations to the given collection.
///
/// The service collection.
internal void Apply(IServiceCollection serviceCollection)
{
serviceCollection.Configure
(
o =>
{
o.PeriodicHook = _periodicHook.Build();
o.EntityFocusHook = _entityFocusHook.Build();
o.EntityFollowHook = _entityFollowHook.Build();
o.EntityUnfollowHook = _entityUnfollowHook.Build();
o.PacketSendHook = _packetSendHook.Build();
o.PacketReceiveHook = _packetReceiveHook.Build();
o.PetWalkHook = _petWalkHook.Build();
o.PlayerWalkHook = _playerWalkHook.Build();
}
);
}
}