From 961c6cd9bcdf4a8e7d04a996daccd3f65fea6ff9 Mon Sep 17 00:00:00 2001
From: Chris <2648373+QuiCM@users.noreply.github.com>
Date: Tue, 2 Jun 2020 14:18:22 +0930
Subject: [PATCH] Clean up NetModulePacketHandler a little
---
.../NetModules/CreativeUnlocksHandler.cs | 12 +++
.../NetModules/NetModulePacketHandler.cs | 89 +++++++------------
TShockAPI/TShockAPI.csproj | 1 +
3 files changed, 47 insertions(+), 55 deletions(-)
diff --git a/TShockAPI/Handlers/NetModules/CreativeUnlocksHandler.cs b/TShockAPI/Handlers/NetModules/CreativeUnlocksHandler.cs
index 97030945..77e9d6d2 100644
--- a/TShockAPI/Handlers/NetModules/CreativeUnlocksHandler.cs
+++ b/TShockAPI/Handlers/NetModules/CreativeUnlocksHandler.cs
@@ -1,5 +1,6 @@
using System.IO;
using System.IO.Streams;
+using Terraria;
using Terraria.GameContent.NetModules;
using Terraria.Net;
@@ -45,6 +46,17 @@ namespace TShockAPI.Handlers.NetModules
///
public void HandlePacket(TSPlayer player, out bool rejectPacket)
{
+ if (!Main.GameModeInfo.IsJourneyMode)
+ {
+ TShock.Log.ConsoleDebug(
+ "NetModuleHandler received attempt to unlock sacrifice while not in journey mode from",
+ player.Name
+ );
+
+ rejectPacket = true;
+ return;
+ }
+
if (UnknownField != 0)
{
TShock.Log.ConsoleDebug(
diff --git a/TShockAPI/Handlers/NetModules/NetModulePacketHandler.cs b/TShockAPI/Handlers/NetModules/NetModulePacketHandler.cs
index 007c505b..8c1b2c8e 100644
--- a/TShockAPI/Handlers/NetModules/NetModulePacketHandler.cs
+++ b/TShockAPI/Handlers/NetModules/NetModulePacketHandler.cs
@@ -1,4 +1,6 @@
-using Terraria;
+using System;
+using System.Collections.Generic;
+using Terraria;
using static TShockAPI.GetDataHandlers;
namespace TShockAPI.Handlers.NetModules
@@ -8,6 +10,19 @@ namespace TShockAPI.Handlers.NetModules
///
public class NetModulePacketHandler : IPacketHandler
{
+ ///
+ /// Maps net module types to handlers for the net module type. Add to or edit this dictionary to customise handling
+ ///
+ public static Dictionary NetModulesToHandlersMap = new Dictionary
+ {
+ { NetModulesTypes.CreativePowers, typeof(CreativePowerHandler) },
+ { NetModulesTypes.CreativeUnlocksPlayerReport, typeof(CreativeUnlocksHandler) },
+ { NetModulesTypes.TeleportPylon, typeof(PylonHandler) },
+ { NetModulesTypes.Liquid, typeof(LiquidHandler) },
+ { NetModulesTypes.Bestiary, typeof(BestiaryHandler) },
+ { NetModulesTypes.Ambience, typeof(AmbienceHandler) }
+ };
+
///
/// Invoked when a load net module packet is received. This method picks a based on the
/// net module type being loaded, then forwards the data to the chosen handler to process
@@ -18,61 +33,25 @@ namespace TShockAPI.Handlers.NetModules
{
INetModuleHandler handler;
- switch (args.ModuleType)
+ if (NetModulesToHandlersMap.ContainsKey(args.ModuleType))
{
- case NetModulesTypes.CreativePowers:
- {
- handler = new CreativePowerHandler();
- break;
- }
-
- case NetModulesTypes.CreativeUnlocksPlayerReport:
- {
- if (!Main.GameModeInfo.IsJourneyMode)
- {
- TShock.Log.ConsoleDebug(
- "NetModuleHandler received attempt to unlock sacrifice while not in journey mode from",
- args.Player.Name
- );
-
- args.Handled = true;
- return;
- }
-
- handler = new CreativeUnlocksHandler();
- break;
- }
- case NetModulesTypes.TeleportPylon:
- {
- handler = new PylonHandler();
- break;
- }
- case NetModulesTypes.Liquid:
- {
- handler = new LiquidHandler();
- break;
- }
- case NetModulesTypes.Bestiary:
- {
- handler = new BestiaryHandler();
- break;
- }
- default:
- {
- // As of 1.4.x.x, this is now used for more things:
- // NetCreativePowersModule
- // NetCreativePowerPermissionsModule
- // NetLiquidModule
- // NetParticlesModule
- // NetPingModule
- // NetTeleportPylonModule
- // NetTextModule
- // I (particles) have disabled the original return here, which means that we need to
- // handle this more. In the interm, this unbreaks parts of vanilla. Originally
- // we just blocked this because it was a liquid exploit.
- args.Handled = false;
- return;
- }
+ handler = (INetModuleHandler)Activator.CreateInstance(NetModulesToHandlersMap[args.ModuleType]);
+ }
+ else
+ {
+ // As of 1.4.x.x, this is now used for more things:
+ // NetCreativePowersModule
+ // NetCreativePowerPermissionsModule
+ // NetLiquidModule
+ // NetParticlesModule
+ // NetPingModule
+ // NetTeleportPylonModule
+ // NetTextModule
+ // I (particles) have disabled the original return here, which means that we need to
+ // handle this more. In the interm, this unbreaks parts of vanilla. Originally
+ // we just blocked this because it was a liquid exploit.
+ args.Handled = false;
+ return;
}
handler.Deserialize(args.Data);
diff --git a/TShockAPI/TShockAPI.csproj b/TShockAPI/TShockAPI.csproj
index 83c31374..bc26b96d 100644
--- a/TShockAPI/TShockAPI.csproj
+++ b/TShockAPI/TShockAPI.csproj
@@ -89,6 +89,7 @@
+