Skip to content

Commit

Permalink
Handle packets earlier
Browse files Browse the repository at this point in the history
  • Loading branch information
sgkoishi committed Apr 4, 2024
1 parent c9e0f4c commit 53ac4fe
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
13 changes: 9 additions & 4 deletions Core/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public class Config
/// </summary>
public Optional<string> DateTimeFormat = Optional.Default("yyyy-MM-dd HH:mm:ss.fff");

/// <summary>
/// Let Chireiden.TShock.Omni to handle packets earlier.
/// </summary>
public Optional<bool> PrioritizedPacketHandle = Optional.Default(true);

/// <summary>
/// The wildcard of matching all players. Directly using "*" itself is not
/// suggested as some commands might have special meaning for it.
Expand Down Expand Up @@ -673,7 +678,7 @@ public record class MitigationSettings

/// <summary>
/// <para>
/// The `WorldGen.countTiles` is recursive and might cause stack overflow.
/// The `WorldGen.countTiles` is recursive and might cause stack overflow.
/// Reported by a Mac (rosetta) user @ Discord during spider cave gen every time.
/// </para>
/// <para>
Expand All @@ -687,7 +692,7 @@ public record class MitigationSettings
/// Allow journey and non-journey players to join the server.
/// </para>
/// <para>
/// There is nothing wrong with this option. It is inside the Mitigations because it
/// There is nothing wrong with this option. It is inside the Mitigations because it
/// requires GetData detour like many other mitigations and I put them together.
/// </para>
/// </summary>
Expand All @@ -710,15 +715,15 @@ public record class MitigationSettings
/// <para>
/// Sample:
/// {
///
///
/// }
/// </para>
/// </summary>
public Optional<Dictionary<PacketFilter, LimiterConfig>?> PacketSpamLimit = Optional.Default<Dictionary<PacketFilter, LimiterConfig>?>(null);

/// <summary>
/// <para>
/// Restrict all socket send operations to have exactly one message/packet per
/// Restrict all socket send operations to have exactly one message/packet per
/// Send call. Requires AnotherAsyncSocket.
/// </para>
/// <para>
Expand Down
15 changes: 12 additions & 3 deletions Core/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ public Plugin(Main game) : base(game)
.GetMethod(nameof(TShockAPI.Utils.GetRealIP), _bfany),
this.Detour_RealIP_IPv6Support
);

if (this.config.PrioritizedPacketHandle)
{
OTAPI.Hooks.MessageBuffer.GetData += this.OTHook_Modded_GetData;
OTAPI.Hooks.MessageBuffer.GetData += this.OTHook_Mitigation_GetData;
}
}

private Assembly? AssemblyResolveHandler(object? sender, ResolveEventArgs args)
Expand Down Expand Up @@ -286,8 +292,13 @@ public override void Initialize()
On.Terraria.Initializers.ChatInitializer.Load += this.MMHook_Mitigation_I18nCommand;
On.Terraria.WorldGen.nextCount += this.MMHook_Mitigation_WorldGenNextCount;
OTAPI.Hooks.NetMessage.SendBytes += this.OTHook_Ghost_SendBytes;
OTAPI.Hooks.NetMessage.SendBytes += this.OTHook_DebugPacket_SendBytes;
OTAPI.Hooks.MessageBuffer.GetData += this.OTHook_Ping_GetData;
OTAPI.Hooks.MessageBuffer.GetData += this.OTHook_Modded_GetData;
if (!this.config.PrioritizedPacketHandle)
{
OTAPI.Hooks.MessageBuffer.GetData += this.OTHook_Modded_GetData;
OTAPI.Hooks.MessageBuffer.GetData += this.OTHook_Mitigation_GetData;
}
TerrariaApi.Server.ServerApi.Hooks.NetNameCollision.Register(this, this.TAHook_NameCollision);
TerrariaApi.Server.ServerApi.Hooks.GamePostInitialize.Register(this, this.OnGamePostInitialize);
TerrariaApi.Server.ServerApi.Hooks.GameUpdate.Register(this, this.TAHook_Update);
Expand Down Expand Up @@ -360,8 +371,6 @@ protected override void Dispose(bool disposing)

private void OnGamePostInitialize(EventArgs args)
{
OTAPI.Hooks.MessageBuffer.GetData += this.OTHook_Mitigation_GetData;
OTAPI.Hooks.NetMessage.SendBytes += this.OTHook_DebugPacket_SendBytes;
On.Terraria.NetMessage.SendData += this.MMHook_DebugPacket_SendData;
On.Terraria.MessageBuffer.GetData += this.MMHook_DebugPacket_GetData;
On.Terraria.NetMessage.SendData += this.MMHook_DebugPacket_CatchSend;
Expand Down

0 comments on commit 53ac4fe

Please sign in to comment.