Move OnGetSection to Bouncer

Bonus: Introduces a new GetDataHandledEventArgs for use in events that
have players, data, and need to be handled.

I was originally going to modify GetDataHandlerArgs, but that comes from
the EventArgs class of args that isn't handled, and changing that to
extend HandledEventArgs would imply we care or check to see if those are
handled and we don't so we're stuck with this implementation for a
teenie tiny bit.
This commit is contained in:
Lucas Nicodemus 2017-12-21 18:41:34 -07:00
parent f9a1819e26
commit ef1486b78c
3 changed files with 74 additions and 12 deletions

View file

@ -60,6 +60,7 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin
* Added `TSPlayer.CheckIgnores()` and removed `TShock.CheckIgnores(TSPlayer)`. (@hakusaro)
* Hooks inside TShock can now be registered with their `Register` method and can be prioritized according to the TShock HandlerList system. (@hakusaro)
* Fix message requiring login not using the command specifier set in the config file. (@hakusaro)
* Fix stack hack detection being inconsistent between two different check points. Moved `TShock.HackedInventory` to `TSPlayer.HasHackedItemStacks`. Added `GetDataHandlers.GetDataHandledEventArgs` which is where most hooks will inherit from in the future. (@hakusaro)
## TShock 4.3.25
* Fixed a critical exploit in the Terraria protocol that could cause massive unpreventable world corruption as well as a number of other problems. Thanks to @bartico6 for reporting. Fixed by the efforts of @QuiCM, @hakusaro, and tips in the right directioon from @bartico6.

View file

@ -42,6 +42,7 @@ namespace TShockAPI
{
// Setup hooks
GetDataHandlers.GetSection += OnGetSection;
GetDataHandlers.PlaceItemFrame += OnPlaceItemFrame;
GetDataHandlers.GemLockToggle += OnGemLockToggle;
GetDataHandlers.PlaceTileEntity += OnPlaceTileEntity;
@ -64,6 +65,28 @@ namespace TShockAPI
GetDataHandlers.TileEdit += OnTileEdit;
}
internal void OnGetSection(object sender, GetDataHandlers.GetSectionEventArgs args)
{
if (args.Player.RequestedSection)
{
args.Handled = true;
return;
}
args.Player.RequestedSection = true;
if (String.IsNullOrEmpty(args.Player.Name))
{
TShock.Utils.ForceKick(args.Player, "Blank name.", true);
args.Handled = true;
return;
}
if (!args.Player.HasPermission(Permissions.ignorestackhackdetection))
{
args.Player.IsDisabledForStackDetection = args.Player.HasHackedItemStacks(true);
}
}
/// <summary>Fired when an item frame is placed for anti-cheat detection.</summary>
/// <param name="sender">The object that triggered the event.</param>
/// <param name="args">The packet arguments that the event has.</param>

View file

@ -58,6 +58,19 @@ namespace TShockAPI
}
}
/// <summary>
/// A custom HandledEventArgs that contains TShock's TSPlayer for the triggering uesr and the Terraria MP data stream.
/// Differentiated by GetDataHandlerArgs because it can be handled and responds to being handled.
/// </summary>
public class GetDataHandledEventArgs : HandledEventArgs
{
/// <summary>The TSPlayer that triggered the event.</summary>
public TSPlayer Player { get; set; }
/// <summary>The raw MP packet data associated with the event.</summary>
public MemoryStream Data { get; set; }
}
public static class GetDataHandlers
{
private static Dictionary<PacketTypes, GetDataHandlerDelegate> GetDataHandlerDelegates;
@ -1903,21 +1916,46 @@ namespace TShockAPI
return true;
}
/// <summary>The arguments to a GetSection packet.</summary>
public class GetSectionEventArgs : GetDataHandledEventArgs
{
/// <summary>The X position requested. Or -1 for spawn.</summary>
public int X { get; set; }
/// <summary>The Y position requested. Or -1 for spawn.</summary>
public int Y { get; set; }
}
/// <summary>The hook for a GetSection event.</summary>
public static HandlerList<GetSectionEventArgs> GetSection = new HandlerList<GetSectionEventArgs>();
/// <summary>Fires a GetSection event.</summary>
/// <param name="player">The TSPlayer that caused the GetSection.</param>
/// <param name="data">The raw MP protocol data.</param>
/// <param name="x">The x coordinate requested or -1 for spawn.</param>
/// <param name="y">The y coordinate requested or -1 for spawn.</param>
/// <returns>bool</returns>
private static bool OnGetSection(TSPlayer player, MemoryStream data, int x, int y)
{
if (GetSection == null)
return false;
var args = new GetSectionEventArgs
{
Player = player,
Data = data,
X = x,
Y = y,
};
GetSection.Invoke(null, args);
return args.Handled;
}
private static bool HandleGetSection(GetDataHandlerArgs args)
{
if (args.Player.RequestedSection)
if (OnGetSection(args.Player, args.Data, args.Data.ReadInt32(), args.Data.ReadInt32()))
return true;
args.Player.RequestedSection = true;
if (String.IsNullOrEmpty(args.Player.Name))
{
TShock.Utils.ForceKick(args.Player, "Blank name.", true);
return true;
}
if (!args.Player.HasPermission(Permissions.ignorestackhackdetection))
{
args.Player.IsDisabledForStackDetection = args.Player.HasHackedItemStacks(true);
}
if (TShock.Utils.ActivePlayers() + 1 > TShock.Config.MaxSlots &&
!args.Player.HasPermission(Permissions.reservedslot))