Skip to content

Commit

Permalink
cgp unload checks
Browse files Browse the repository at this point in the history
  • Loading branch information
InvalidArgument3 committed Oct 27, 2024
1 parent d1fdc12 commit 5d173d6
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,26 @@ public class HeartNetwork
public ushort NetworkId { get; private set; }
public int TotalNetworkLoad { get; private set; }

public void LoadData(ushort networkId)
{
I = this;
public void LoadData(ushort networkId) {
if (I != null)
return;

I = this;
NetworkId = networkId;
MyAPIGateway.Multiplayer.RegisterSecureMessageHandler(NetworkId, ReceivedPacket);

foreach (var type in PacketBase.Types) TypeNetworkLoad.Add(type, 0);
TypeNetworkLoad.Clear();
foreach (var type in PacketBase.Types)
TypeNetworkLoad.Add(type, 0);

MyAPIGateway.Multiplayer.RegisterSecureMessageHandler(NetworkId, ReceivedPacket);
}

public void UnloadData()
{
MyAPIGateway.Multiplayer.UnregisterSecureMessageHandler(NetworkId, ReceivedPacket);
public void UnloadData() {
if (NetworkId > 0)
MyAPIGateway.Multiplayer.UnregisterSecureMessageHandler(NetworkId, ReceivedPacket);

TypeNetworkLoad?.Clear();
NetworkId = 0;
I = null;
}

Expand All @@ -54,29 +61,37 @@ public void Update()
}
}

private void ReceivedPacket(ushort channelId, byte[] serialized, ulong senderSteamId, bool isSenderServer)
{
try
{
private void ReceivedPacket(ushort channelId, byte[] serialized, ulong senderSteamId, bool isSenderServer) {
// Add check if mod is unloaded
if (MasterSession.I == null || Log.IsUnloaded)
return;

try {
var packet = MyAPIGateway.Utilities.SerializeFromBinary<PacketBase>(serialized);
TypeNetworkLoad[packet.GetType()] += serialized.Length;
if (packet == null)
return;

if (TypeNetworkLoad.ContainsKey(packet.GetType()))
TypeNetworkLoad[packet.GetType()] += serialized.Length;

HandlePacket(packet, senderSteamId);
}
catch (Exception ex)
{
Log.Error(ex);
catch (Exception ex) {
if (!Log.IsUnloaded)
Log.Error(ex);
}
}

private void HandlePacket(PacketBase packet, ulong senderSteamId)
{
try
{
private void HandlePacket(PacketBase packet, ulong senderSteamId) {
if (packet == null || MasterSession.I == null)
return;

try {
packet.Received(senderSteamId);
}
catch (Exception ex)
{
Log.Error(ex);
catch (Exception ex) {
if (!Log.IsUnloaded)
Log.Error(ex);
}
}

Expand Down
Loading

0 comments on commit 5d173d6

Please sign in to comment.