Migration Step 1
This commit is contained in:
1 parent
541ec4c556
commit
b59688ff50
16 files changed
+502
-161
No files matched your search
+38
-154
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using CalamityMod.Events;
|
||||
using CalamityMod.Items;
|
||||
@@ -28,39 +29,50 @@ namespace CalamityMod
|
||||
|
||||
public override void OnModLoad()
|
||||
{
|
||||
_PacketRegistry = new CalamityPacket[byte.MaxValue];
|
||||
_PacketRegistry = new CalamityPacket[256]; // This should allow to use 0-255 range (full byte range)
|
||||
|
||||
foreach (var mod in ModLoader.Mods)
|
||||
var types = ModLoader.Mods.SelectMany(mod => AssemblyManager.GetLoadableTypes(mod.Code));
|
||||
foreach (var type in types)
|
||||
{
|
||||
foreach (var type in AssemblyManager.GetLoadableTypes(mod.Code))
|
||||
if (type.IsAbstract || !type.IsSubclassOf(typeof(CalamityPacket)))
|
||||
continue;
|
||||
|
||||
if (Activator.CreateInstance(type) is not CalamityPacket packetHandler)
|
||||
continue;
|
||||
|
||||
var msgType = packetHandler.MessageType;
|
||||
var existingHandler = _PacketRegistry[msgType];
|
||||
if (existingHandler != null)
|
||||
{
|
||||
if (type.IsAbstract || !type.IsSubclassOf(typeof(CalamityPacket)))
|
||||
continue;
|
||||
|
||||
if (Activator.CreateInstance(type) is not CalamityPacket packetHandler)
|
||||
continue;
|
||||
|
||||
var msgType = packetHandler.MessageType;
|
||||
var existingHandler = _PacketRegistry[msgType];
|
||||
if (existingHandler != null)
|
||||
{
|
||||
CalamityMod.Instance.Logger.Error($"Packet instance has already registered by other type!" +
|
||||
$" [Failed: '{type.FullName}'" +
|
||||
$" Current Owner: '{existingHandler.GetType().FullName}'," +
|
||||
$" msgTypeToRegister: '{msgType}']");
|
||||
continue;
|
||||
}
|
||||
|
||||
_PacketRegistry[packetHandler.MessageType] = packetHandler;
|
||||
|
||||
var instanceProperty = type.GetProperty("Instance", BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic);
|
||||
instanceProperty?.SetValue(null, packetHandler);
|
||||
CalamityMod.Instance.Logger.Error($"Packet instance has already registered by other type!" +
|
||||
$" [Failed: '{type.FullName}'" +
|
||||
$" Current Owner: '{existingHandler.GetType().FullName}'," +
|
||||
$" msgTypeToRegister: '{msgType}']");
|
||||
continue;
|
||||
}
|
||||
|
||||
_PacketRegistry[packetHandler.MessageType] = packetHandler;
|
||||
|
||||
var instanceProperty = type.GetProperty("Instance", BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic);
|
||||
if (instanceProperty?.PropertyType.IsAssignableFrom(type) ?? false)
|
||||
{
|
||||
instanceProperty.SetValue(null, packetHandler);
|
||||
packetHandler._Prop_Static_Instance = instanceProperty;
|
||||
}
|
||||
|
||||
packetHandler.OnLoaded();
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnModUnload()
|
||||
{
|
||||
foreach (var packetHandler in _PacketRegistry ?? Enumerable.Empty<CalamityPacket>())
|
||||
{
|
||||
packetHandler.OnUnloaded();
|
||||
packetHandler._Prop_Static_Instance?.SetValue(null, null);
|
||||
packetHandler._Prop_Static_Instance = null;
|
||||
}
|
||||
|
||||
_PacketRegistry = null;
|
||||
}
|
||||
|
||||
@@ -76,7 +88,8 @@ namespace CalamityMod
|
||||
}
|
||||
else
|
||||
{
|
||||
// Error Message will be goes here!
|
||||
CalamityMod.Instance.Logger.Error($"Failed to parse Calamity packet: No Calamity packet exists with ID {msgType}.");
|
||||
throw new Exception("Failed to parse Calamity packet: Invalid Calamity packet ID.");
|
||||
}
|
||||
|
||||
switch (msgType)
|
||||
@@ -84,16 +97,6 @@ namespace CalamityMod
|
||||
//
|
||||
// Player mechanic syncs
|
||||
//
|
||||
|
||||
case CalamityModMessageType.DefenseDamageSync:
|
||||
Main.player[reader.ReadInt32()].Calamity().HandleDefenseDamage(reader);
|
||||
break;
|
||||
case CalamityModMessageType.RageSync:
|
||||
Main.player[reader.ReadInt32()].Calamity().HandleRage(reader);
|
||||
break;
|
||||
case CalamityModMessageType.AdrenalineSync:
|
||||
Main.player[reader.ReadInt32()].Calamity().HandleAdrenaline(reader);
|
||||
break;
|
||||
case CalamityModMessageType.CooldownAddition:
|
||||
Main.player[reader.ReadInt32()].Calamity().HandleCooldownAddition(reader);
|
||||
break;
|
||||
@@ -107,73 +110,6 @@ namespace CalamityMod
|
||||
//
|
||||
// Syncs for specific bosses or entities
|
||||
//
|
||||
|
||||
case CalamityModMessageType.SyncDestroyerLaserColor:
|
||||
byte npcIdx3 = reader.ReadByte();
|
||||
int laserColor = reader.ReadInt32();
|
||||
|
||||
// If the NPC in question isn't valid, don't do anything.
|
||||
NPC npc3 = Main.npc[npcIdx3];
|
||||
if (!npc3.active)
|
||||
break;
|
||||
|
||||
CalamityGlobalNPC cgn3 = npc3.Calamity();
|
||||
cgn3.destroyerLaserColor = laserColor;
|
||||
break;
|
||||
|
||||
// This code has been edited to fail gracefully when trying to provide data for an invalid NPC.
|
||||
case CalamityModMessageType.SyncCalamityNPCAIArray:
|
||||
// Read the entire packet regardless of anything
|
||||
byte npcIdx = reader.ReadByte();
|
||||
float ai0 = reader.ReadSingle();
|
||||
float ai1 = reader.ReadSingle();
|
||||
float ai2 = reader.ReadSingle();
|
||||
float ai3 = reader.ReadSingle();
|
||||
|
||||
// If the NPC in question isn't valid, don't do anything.
|
||||
NPC npc = Main.npc[npcIdx];
|
||||
if (!npc.active)
|
||||
break;
|
||||
|
||||
CalamityGlobalNPC cgn = npc.Calamity();
|
||||
cgn.newAI[0] = ai0;
|
||||
cgn.newAI[1] = ai1;
|
||||
cgn.newAI[2] = ai2;
|
||||
cgn.newAI[3] = ai3;
|
||||
break;
|
||||
|
||||
case CalamityModMessageType.SyncVanillaNPCLocalAIArray:
|
||||
// Read the entire packet regardless of anything
|
||||
byte npcIdx2 = reader.ReadByte();
|
||||
float localAI0 = reader.ReadSingle();
|
||||
float localAI1 = reader.ReadSingle();
|
||||
float localAI2 = reader.ReadSingle();
|
||||
float localAI3 = reader.ReadSingle();
|
||||
|
||||
// If the NPC in question isn't valid, don't do anything.
|
||||
NPC npc2 = Main.npc[npcIdx2];
|
||||
if (!npc2.active)
|
||||
break;
|
||||
|
||||
npc2.localAI[0] = localAI0;
|
||||
npc2.localAI[1] = localAI1;
|
||||
npc2.localAI[2] = localAI2;
|
||||
npc2.localAI[3] = localAI3;
|
||||
break;
|
||||
|
||||
case CalamityModMessageType.SpawnSuperDummy:
|
||||
int x = reader.ReadInt32();
|
||||
int y = reader.ReadInt32();
|
||||
// Not strictly necessary, but helps prevent unnecessary packetstorm in MP
|
||||
if (Main.netMode != NetmodeID.MultiplayerClient)
|
||||
NPC.NewNPC(new EntitySource_WorldEvent(), x, y, ModContent.NPCType<SuperDummyNPC>());
|
||||
break;
|
||||
|
||||
case CalamityModMessageType.DeleteAllSuperDummies:
|
||||
if (Main.netMode != NetmodeID.MultiplayerClient)
|
||||
SuperDummy.DeleteDummies();
|
||||
break;
|
||||
|
||||
case CalamityModMessageType.SyncAndroombaSolution:
|
||||
int index = reader.ReadInt32();
|
||||
int solType = reader.ReadInt32();
|
||||
@@ -231,58 +167,6 @@ namespace CalamityMod
|
||||
(Main.npc[npcIndex4].ModNPC as Providence).challenge = reader.ReadBoolean();
|
||||
break;
|
||||
|
||||
//
|
||||
// General syncs for entities
|
||||
//
|
||||
|
||||
case CalamityModMessageType.SpawnNPCOnPlayer:
|
||||
x = reader.ReadInt32();
|
||||
y = reader.ReadInt32();
|
||||
int npcType = reader.ReadInt32();
|
||||
int player = reader.ReadInt32();
|
||||
Vector2 spawnPosition = reader.ReadVector2();
|
||||
if (Main.netMode != NetmodeID.MultiplayerClient)
|
||||
{
|
||||
int spawnedNPC = NPC.NewNPC(new EntitySource_WorldEvent(), x, y, npcType, Target: player);
|
||||
NetMessage.SendData(MessageID.SyncNPC, -1, player, null, spawnedNPC);
|
||||
}
|
||||
break;
|
||||
|
||||
case CalamityModMessageType.SyncNPCMotionDataToServer:
|
||||
int npcIndex = reader.ReadInt32();
|
||||
Vector2 center = reader.ReadVector2();
|
||||
Vector2 velocity = reader.ReadVector2();
|
||||
if (Main.netMode != NetmodeID.MultiplayerClient)
|
||||
{
|
||||
Main.npc[npcIndex].Center = center;
|
||||
Main.npc[npcIndex].velocity = velocity;
|
||||
NetMessage.SendData(MessageID.SyncNPC, -1, -1, null, npcIndex);
|
||||
}
|
||||
break;
|
||||
|
||||
case CalamityModMessageType.SyncNPCPosAndRotOnly:
|
||||
npcIndex = reader.ReadByte();
|
||||
Vector2 position = reader.ReadVector2();
|
||||
float rotation = (float)reader.ReadHalf(); //rotation unit is radian (-π/2 ≤ rotation ≤ π/2) so Half precision should works
|
||||
|
||||
if (npcIndex >= Main.maxNPCs)
|
||||
break;
|
||||
|
||||
npc = Main.npc[npcIndex];
|
||||
npc.position = position;
|
||||
npc.rotation = rotation;
|
||||
|
||||
if (Main.dedServ)
|
||||
{
|
||||
ModPacket packet = CalamityMod.Instance.GetPacket();
|
||||
packet.Write((byte)CalamityModMessageType.SyncNPCPosAndRotOnly);
|
||||
packet.Write((byte)npcIndex);
|
||||
packet.WriteVector2(position);
|
||||
packet.Write((Half)rotation);
|
||||
packet.Send(ignoreClient: whoAmI);
|
||||
}
|
||||
break;
|
||||
|
||||
//
|
||||
// Tile Entities
|
||||
//
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Terraria;
|
||||
@@ -15,6 +16,11 @@ namespace CalamityMod.Packets
|
||||
public abstract byte MessageType { get; }
|
||||
public abstract void HandlePacket(in BinaryReader packet, int sender);
|
||||
|
||||
public virtual void OnLoaded() { }
|
||||
public virtual void OnUnloaded() { }
|
||||
|
||||
internal PropertyInfo _Prop_Static_Instance;
|
||||
|
||||
public void CloneAndBroadcast(in BinaryReader packet, long startIndex, int length, int ignoreClient = -1)
|
||||
{
|
||||
if (!Main.dedServ)
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Terraria.DataStructures;
|
||||
using Terraria.ID;
|
||||
using Terraria;
|
||||
|
||||
namespace CalamityMod.Packets
|
||||
{
|
||||
public sealed class SpawnNPCOnPlayerPacket : CalamityPacket
|
||||
{
|
||||
public static SpawnNPCOnPlayerPacket Instance { get; private set; }
|
||||
|
||||
public override byte MessageType => (byte)CalamityModMessageType.SpawnNPCOnPlayer;
|
||||
|
||||
public static void Send(Player player, int x, int y, int npcType, int toClient = -1, int ignoreClient = -1)
|
||||
{
|
||||
var packet = Instance.CreateBasePacket();
|
||||
packet.WriteWhoAmI(player);
|
||||
packet.Write(x);
|
||||
packet.Write(y);
|
||||
packet.Write(npcType);
|
||||
packet.Send(toClient, ignoreClient);
|
||||
}
|
||||
|
||||
public override void HandlePacket(in BinaryReader packet, int sender)
|
||||
{
|
||||
var player = packet.ReadPlayer();
|
||||
var x = packet.ReadInt32();
|
||||
var y = packet.ReadInt32();
|
||||
var npcType = packet.ReadInt32();
|
||||
|
||||
if (player is null)
|
||||
return;
|
||||
|
||||
if (Main.dedServ)
|
||||
{
|
||||
int spawnedNPC = NPC.NewNPC(new EntitySource_WorldEvent(), x, y, npcType, Target: player.whoAmI);
|
||||
NetMessage.SendData(MessageID.SyncNPC, -1, player.whoAmI, null, spawnedNPC);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Terraria;
|
||||
using Terraria.ID;
|
||||
|
||||
namespace CalamityMod.Packets
|
||||
{
|
||||
public sealed class SyncNPCMotionDataToServerPacket : CalamityPacket
|
||||
{
|
||||
public static SyncNPCMotionDataToServerPacket Instance { get; private set; }
|
||||
|
||||
public override byte MessageType => (byte)CalamityModMessageType.SyncNPCMotionDataToServer;
|
||||
|
||||
public static void Send(NPC npc, int toClient = -1, int ignoreClient = -1)
|
||||
{
|
||||
var packet = Instance.CreateBasePacket();
|
||||
packet.WriteWhoAmI(npc);
|
||||
packet.WriteVector2(npc.Center);
|
||||
packet.WriteVector2(npc.velocity);
|
||||
packet.Send(toClient, ignoreClient);
|
||||
}
|
||||
|
||||
public override void HandlePacket(in BinaryReader packet, int sender)
|
||||
{
|
||||
var npc = packet.ReadNPC();
|
||||
var center = packet.ReadVector2();
|
||||
var velocity = packet.ReadVector2();
|
||||
if (Main.dedServ && npc is not null)
|
||||
{
|
||||
npc.Center = center;
|
||||
npc.velocity = velocity;
|
||||
NetMessage.SendData(MessageID.SyncNPC, -1, -1, null, npc.whoAmI);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Terraria;
|
||||
using Terraria.ModLoader;
|
||||
|
||||
namespace CalamityMod.Packets
|
||||
{
|
||||
public sealed class SyncNPCPosAndRotOnlyPacket : CalamityPacket
|
||||
{
|
||||
public static SyncNPCPosAndRotOnlyPacket Instance { get; private set; }
|
||||
|
||||
public override byte MessageType => (byte)CalamityModMessageType.SyncNPCPosAndRotOnly;
|
||||
|
||||
public static void Send(NPC npc, int toClient = -1, int ignoreClient = -1)
|
||||
{
|
||||
var packet = Instance.CreateBasePacket();
|
||||
packet.WriteWhoAmI(npc);
|
||||
packet.WriteVector2(npc.position);
|
||||
packet.Write((Half)npc.rotation); //rotation unit is radian (-π/2 ≤ rotation ≤ π/2) so Half precision should works
|
||||
packet.Send(toClient, ignoreClient);
|
||||
}
|
||||
|
||||
public override void HandlePacket(in BinaryReader packet, int sender)
|
||||
{
|
||||
var npc = packet.ReadNPC();
|
||||
var position = packet.ReadVector2();
|
||||
var rotation = (float)packet.ReadHalf();
|
||||
|
||||
if (npc is null)
|
||||
return;
|
||||
|
||||
npc.position = position;
|
||||
npc.rotation = rotation;
|
||||
|
||||
if (Main.dedServ)
|
||||
{
|
||||
Send(npc, ignoreClient: sender);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using CalamityMod.CalPlayer;
|
||||
using Terraria;
|
||||
|
||||
namespace CalamityMod.Packets
|
||||
{
|
||||
public sealed class AdrenalineSyncPacket : CalamityPacket
|
||||
{
|
||||
public static AdrenalineSyncPacket Instance { get; private set; }
|
||||
|
||||
public override byte MessageType => (byte)CalamityModMessageType.AdrenalineSync;
|
||||
|
||||
public static void Send(CalamityPlayer playerToSync, int toClient = -1, int ignoreClient = -1)
|
||||
{
|
||||
var packet = Instance.CreateBasePacket();
|
||||
packet.WriteWhoAmI(playerToSync.Player);
|
||||
packet.Write(playerToSync.adrenaline);
|
||||
packet.Send(toClient, ignoreClient);
|
||||
}
|
||||
|
||||
public override void HandlePacket(in BinaryReader packet, int sender)
|
||||
{
|
||||
var player = packet.ReadCalamityPlayer();
|
||||
var adrenaline = packet.ReadSingle();
|
||||
|
||||
if (player is null)
|
||||
return;
|
||||
|
||||
player.adrenaline = adrenaline;
|
||||
|
||||
if (Main.dedServ)
|
||||
Send(player, ignoreClient: sender);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CalamityMod.Packets
|
||||
{
|
||||
public sealed class CooldownAdditionPacket : CalamityPacket
|
||||
{
|
||||
}
|
||||
}
|
||||
File renamed without changes.
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using CalamityMod.CalPlayer;
|
||||
using Terraria;
|
||||
|
||||
namespace CalamityMod.Packets
|
||||
{
|
||||
public sealed class RageSyncPacket : CalamityPacket
|
||||
{
|
||||
public static RageSyncPacket Instance { get; private set; }
|
||||
|
||||
public override byte MessageType => (byte)CalamityModMessageType.RageSync;
|
||||
|
||||
public static void Send(CalamityPlayer playerToSync, int toClient = -1, int ignoreClient = -1)
|
||||
{
|
||||
var packet = Instance.CreateBasePacket();
|
||||
packet.WriteWhoAmI(playerToSync.Player);
|
||||
packet.Write(playerToSync.rage);
|
||||
packet.Write(playerToSync.rageCombatFrames);
|
||||
packet.Send(toClient, ignoreClient);
|
||||
}
|
||||
|
||||
public override void HandlePacket(in BinaryReader packet, int sender)
|
||||
{
|
||||
var player = packet.ReadCalamityPlayer();
|
||||
var rage = packet.ReadSingle();
|
||||
var rageCombatFrames = packet.ReadInt32();
|
||||
|
||||
if (player is null)
|
||||
return;
|
||||
|
||||
player.rage = rage;
|
||||
player.rageCombatFrames = rageCombatFrames;
|
||||
|
||||
if (Main.dedServ)
|
||||
Send(player, ignoreClient: sender);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using CalamityMod.Items;
|
||||
using Terraria;
|
||||
|
||||
namespace CalamityMod.Packets
|
||||
{
|
||||
public sealed class DeleteAllSuperDummiesPacket : CalamityPacket
|
||||
{
|
||||
public static DeleteAllSuperDummiesPacket Instance { get; private set; }
|
||||
|
||||
public override byte MessageType => (byte)CalamityModMessageType.DeleteAllSuperDummies;
|
||||
|
||||
public static void Send(int toClient = -1, int ignoreClient = -1)
|
||||
{
|
||||
var packet = Instance.CreateBasePacket();
|
||||
packet.Send(toClient, ignoreClient);
|
||||
}
|
||||
|
||||
public override void HandlePacket(in BinaryReader packet, int sender)
|
||||
{
|
||||
// There is nothing in the packet! We only need to call delete method on Server
|
||||
if (Main.dedServ)
|
||||
SuperDummy.DeleteDummies();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using CalamityMod.NPCs.NormalNPCs;
|
||||
using Terraria;
|
||||
using Terraria.DataStructures;
|
||||
using Terraria.ModLoader;
|
||||
|
||||
namespace CalamityMod.Packets
|
||||
{
|
||||
public sealed class SpawnSuperDummyPacket : CalamityPacket
|
||||
{
|
||||
public static SpawnSuperDummyPacket Instance { get; private set; }
|
||||
|
||||
public override byte MessageType => (byte)CalamityModMessageType.SpawnSuperDummy;
|
||||
|
||||
public static void Send(int x, int y, int toClient = -1, int ignoreClient = -1)
|
||||
{
|
||||
var packet = Instance.CreateBasePacket();
|
||||
packet.Write(x);
|
||||
packet.Write(y);
|
||||
packet.Send(toClient, ignoreClient);
|
||||
}
|
||||
|
||||
public override void HandlePacket(in BinaryReader packet, int sender)
|
||||
{
|
||||
var x = packet.ReadInt32();
|
||||
var y = packet.ReadInt32();
|
||||
|
||||
// Not strictly necessary, but helps prevent unnecessary packetstorm in MP
|
||||
if (Main.dedServ)
|
||||
NPC.NewNPC(new EntitySource_WorldEvent(), x, y, ModContent.NPCType<SuperDummyNPC>());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Terraria;
|
||||
|
||||
namespace CalamityMod.Packets
|
||||
{
|
||||
public sealed class SyncCalamityNPCAIArrayPacket : CalamityPacket
|
||||
{
|
||||
// MIGRATED COMMENTS FROM: 'CalamityNetcode.cs'
|
||||
// - This code has been edited to fail gracefully when trying to provide data for an invalid NPC.
|
||||
|
||||
public static SyncCalamityNPCAIArrayPacket Instance { get; private set; }
|
||||
|
||||
public override byte MessageType => (byte)CalamityModMessageType.SyncCalamityNPCAIArray;
|
||||
|
||||
public static void Send(NPC npc, int toClient = -1, int ignoreClient = -1)
|
||||
{
|
||||
var packet = Instance.CreateBasePacket();
|
||||
packet.WriteWhoAmI(npc);
|
||||
|
||||
var calNPC = npc.Calamity();
|
||||
packet.Write(calNPC.newAI[0]);
|
||||
packet.Write(calNPC.newAI[1]);
|
||||
packet.Write(calNPC.newAI[2]);
|
||||
packet.Write(calNPC.newAI[3]);
|
||||
packet.Send(toClient, ignoreClient);
|
||||
}
|
||||
|
||||
public override void HandlePacket(in BinaryReader packet, int sender)
|
||||
{
|
||||
var npc = packet.ReadNPC();
|
||||
var ai0 = packet.ReadSingle();
|
||||
var ai1 = packet.ReadSingle();
|
||||
var ai2 = packet.ReadSingle();
|
||||
var ai3 = packet.ReadSingle();
|
||||
|
||||
if (npc is null)
|
||||
return;
|
||||
|
||||
var calNPC = npc.Calamity();
|
||||
calNPC.newAI[0] = ai0;
|
||||
calNPC.newAI[1] = ai1;
|
||||
calNPC.newAI[2] = ai2;
|
||||
calNPC.newAI[3] = ai3;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using CalamityMod.NPCs;
|
||||
using Terraria;
|
||||
|
||||
namespace CalamityMod.Packets
|
||||
{
|
||||
public sealed class SyncDestroyerLaserColorPacket : CalamityPacket
|
||||
{
|
||||
public static SyncDestroyerLaserColorPacket Instance { get; private set; }
|
||||
|
||||
public override byte MessageType => (byte)CalamityModMessageType.SyncDestroyerLaserColor;
|
||||
|
||||
public static void Send(NPC npc, int toClient = -1, int ignoreClient = -1)
|
||||
{
|
||||
var packet = Instance.CreateBasePacket();
|
||||
packet.WriteWhoAmI(npc);
|
||||
packet.Write(npc.Calamity().destroyerLaserColor);
|
||||
packet.Send(toClient, ignoreClient);
|
||||
}
|
||||
|
||||
public override void HandlePacket(in BinaryReader packet, int sender)
|
||||
{
|
||||
var npc = packet.ReadNPC();
|
||||
var laserColor = packet.ReadInt32();
|
||||
|
||||
if (npc is null)
|
||||
return;
|
||||
|
||||
npc.Calamity().destroyerLaserColor = laserColor;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Terraria;
|
||||
|
||||
namespace CalamityMod.Packets
|
||||
{
|
||||
public sealed class SyncVanillaNPCLocalAIArrayPacket : CalamityPacket
|
||||
{
|
||||
public static SyncVanillaNPCLocalAIArrayPacket Instance { get; private set; }
|
||||
|
||||
public override byte MessageType => (byte)CalamityModMessageType.SyncVanillaNPCLocalAIArray;
|
||||
|
||||
public static void Send(NPC npc, int toClient = -1, int ignoreClient = -1)
|
||||
{
|
||||
var packet = Instance.CreateBasePacket();
|
||||
packet.WriteWhoAmI(npc);
|
||||
packet.Write(npc.localAI[0]);
|
||||
packet.Write(npc.localAI[1]);
|
||||
packet.Write(npc.localAI[2]);
|
||||
packet.Write(npc.localAI[3]);
|
||||
packet.Send(toClient, ignoreClient);
|
||||
}
|
||||
|
||||
public override void HandlePacket(in BinaryReader packet, int sender)
|
||||
{
|
||||
var npc = packet.ReadNPC();
|
||||
var ai0 = packet.ReadSingle();
|
||||
var ai1 = packet.ReadSingle();
|
||||
var ai2 = packet.ReadSingle();
|
||||
var ai3 = packet.ReadSingle();
|
||||
|
||||
if (npc is null)
|
||||
return;
|
||||
|
||||
npc.localAI[0] = ai0;
|
||||
npc.localAI[1] = ai1;
|
||||
npc.localAI[2] = ai2;
|
||||
npc.localAI[3] = ai3;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -39,6 +39,19 @@ namespace CalamityMod
|
||||
Main.LocalPlayer.GetModPlayer<CalamityPlayer>().GeneralScreenShakePower += (intensity * dist);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if Entity is null or Inactive (!active)
|
||||
/// </summary>
|
||||
/// <param name="entity">Entity to check</param>
|
||||
/// <returns>true if entity is null or inactive, otherwise false</returns>
|
||||
public static bool IsNullOrInactive(this Entity entity)
|
||||
{
|
||||
if (entity is null) return true;
|
||||
if (!entity.active) return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
#region Fallback Method for IndexInRange
|
||||
/// <summary>
|
||||
/// Fallback method for Main.npc.IndexInRange which provide accurate range check (0 <= index < Main.maxNPCs)
|
||||
|
||||
@@ -30,27 +30,37 @@ namespace CalamityMod
|
||||
}
|
||||
}
|
||||
|
||||
public static CalamityPlayer ReadCalamityPlayer(this BinaryReader reader) => ReadPlayer(reader)?.Calamity() ?? null;
|
||||
public static Player ReadPlayer(this BinaryReader reader)
|
||||
public static CalamityPlayer ReadCalamityPlayer(this BinaryReader reader, bool nullOnInactive = true) => ReadPlayer(reader, nullOnInactive)?.Calamity() ?? null;
|
||||
public static Player ReadPlayer(this BinaryReader reader, bool nullOnInactive = true)
|
||||
{
|
||||
int index = reader.ReadByte();
|
||||
|
||||
if (index >= Main.maxPlayers)
|
||||
return null;
|
||||
|
||||
return Main.player[index];
|
||||
var player = Main.player[index];
|
||||
|
||||
if (nullOnInactive && player.IsNullOrInactive())
|
||||
return null;
|
||||
|
||||
return player;
|
||||
}
|
||||
|
||||
public static NPCType ReadModNPC<NPCType>(this BinaryReader reader) where NPCType : ModNPC => ReadNPC(reader)?.ModNPC as NPCType;
|
||||
public static ModNPC ReadModNPC(this BinaryReader reader) => ReadNPC(reader)?.ModNPC ?? null;
|
||||
public static NPC ReadNPC(this BinaryReader reader)
|
||||
public static NPCType ReadModNPC<NPCType>(this BinaryReader reader, bool nullOnInactive = true) where NPCType : ModNPC => ReadNPC(reader, nullOnInactive)?.ModNPC as NPCType;
|
||||
public static ModNPC ReadModNPC(this BinaryReader reader, bool nullOnInactive = true) => ReadNPC(reader, nullOnInactive)?.ModNPC ?? null;
|
||||
public static NPC ReadNPC(this BinaryReader reader, bool nullOnInactive = true)
|
||||
{
|
||||
int index = reader.ReadByte();
|
||||
|
||||
if (index >= Main.maxNPCs)
|
||||
return null;
|
||||
|
||||
return Main.npc[index];
|
||||
var npc = Main.npc[index];
|
||||
|
||||
if (nullOnInactive && npc.IsNullOrInactive())
|
||||
return null;
|
||||
|
||||
return npc;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user