Skip to content

Commit

Permalink
Optionally allow invalid data
Browse files Browse the repository at this point in the history
  • Loading branch information
sgkoishi committed Nov 30, 2024
1 parent bbef0f9 commit 791835d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
26 changes: 15 additions & 11 deletions Core/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,17 @@ public record class MitigationSettings
/// </summary>
public Optional<bool> IncrementalChestStack = Optional.Default(false);

/// <summary>
/// Allow non-vanilla name change. Commonly used by cheaters.
/// </summary>
public Optional<bool> AllowNonVanillaNameChange = Optional.Default(false, true);

/// <summary>
/// Allow invalid packets for join states. Used by both cheaters and
/// other valid non-vanilla purposes.
/// </summary>
public Optional<bool> AllowNonVanillaJoinState = Optional.Default(false, true);

public enum DisabledDamageAction
{
AsIs,
Expand Down Expand Up @@ -880,11 +891,11 @@ public static Optional<T> Default<T>(T value, bool hide = false)
}
}

public class Optional<T> : Optional, IEquatable<Optional<T>>
public class Optional<T>(T value, bool hide = false) : Optional, IEquatable<Optional<T>>
{
public bool IsDefault { private set; get; }
public bool HideWhenDefault { private set; get; }
internal T _defaultValue;
public bool IsDefault { private set; get; } = true;
public bool HideWhenDefault { private set; get; } = hide;
internal T _defaultValue = value;
internal T? _value;
public T Value
{
Expand Down Expand Up @@ -941,13 +952,6 @@ public override object? ObjectValue
}
}

public Optional(T value, bool hide = false)
{
this.IsDefault = true;
this._defaultValue = value;
this.HideWhenDefault = hide;
}

public static implicit operator T(Optional<T> self) => self.Value;

public override bool Equals(object? obj)
Expand Down
8 changes: 6 additions & 2 deletions Core/Modded.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@ static bool ModdedFakeName(int whoAmI, Span<byte> data)
return;
}

var mitigation = this.config.Mitigation.Value;

var whoAmI = args.Instance.whoAmI;
if (ModdedEarlyChatSpam(whoAmI, (PacketTypes) args.PacketId, this.AllowedPackets))
if (!mitigation.DisableAllMitigation && mitigation.AllowNonVanillaJoinState &&
ModdedEarlyChatSpam(whoAmI, (PacketTypes) args.PacketId, this.AllowedPackets))
{
this.Statistics.ModdedEarlyChatSpam++;
TShockAPI.TShock.Log.ConsoleInfo($"Unusual packet {args.PacketId} detected at state {Terraria.Netplay.Clients[whoAmI].State} and disconnected. ({Terraria.Netplay.Clients[whoAmI].Socket.GetRemoteAddress()})");
Expand All @@ -86,7 +89,8 @@ static bool ModdedFakeName(int whoAmI, Span<byte> data)
{
// This is actually not working since the client do not sync
// Only sent when related info changed
if (ModdedFakeName(whoAmI, args.Instance.readBuffer.AsSpan(args.ReadOffset + 3, args.Length - 3)))
if (!mitigation.DisableAllMitigation && mitigation.AllowNonVanillaNameChange &&
ModdedFakeName(whoAmI, args.Instance.readBuffer.AsSpan(args.ReadOffset + 3, args.Length - 3)))
{
Terraria.NetMessage.TrySendData((int) PacketTypes.Disconnect, whoAmI, -1, Terraria.Lang.mp[1].ToNetworkText());
this.Statistics.ModdedFakeName++;
Expand Down

0 comments on commit 791835d

Please sign in to comment.