Fix some crashes in mod communication

This commit is contained in:
Brant Martin
2018-12-26 10:52:54 -05:00
parent 66b7adf485
commit f9d75856d1
2 changed files with 16 additions and 16 deletions

View File

@@ -24,18 +24,16 @@ namespace Torch.Mod.Messages
public override void ProcessClient() public override void ProcessClient()
{ {
MyAPIGateway.Parallel.ForEach(EntityId, id => //MyAPIGateway.Parallel.ForEach(EntityId, id =>
{ foreach (var id in EntityId)
IMyEntity e; {
if (!MyAPIGateway.Entities.TryGetEntityById(id, out e)) IMyEntity e;
return; if (!MyAPIGateway.Entities.TryGetEntityById(id, out e))
continue;
var v = e as IMyVoxelBase; var v = e as IMyVoxelBase;
if (v == null) v?.Storage.Reset(MyStorageDataTypeFlags.All);
return; }
v.Storage.Reset(MyStorageDataTypeFlags.All);
});
} }
public override void ProcessServer() public override void ProcessServer()

View File

@@ -18,7 +18,7 @@ namespace Torch.Mod
public static class ModCommunication public static class ModCommunication
{ {
public const ushort NET_ID = 4352; public const ushort NET_ID = 4352;
private static bool _closing; private static bool _closing = false;
private static BlockingCollection<MessageBase> _processing; private static BlockingCollection<MessageBase> _processing;
private static MyConcurrentPool<IncomingMessage> _messagePool; private static MyConcurrentPool<IncomingMessage> _messagePool;
private static List<IMyPlayer> _playerCache; private static List<IMyPlayer> _playerCache;
@@ -32,6 +32,7 @@ namespace Torch.Mod
MyAPIGateway.Multiplayer.RegisterMessageHandler(NET_ID, MessageHandler); MyAPIGateway.Multiplayer.RegisterMessageHandler(NET_ID, MessageHandler);
//background thread to handle de/compression and processing //background thread to handle de/compression and processing
_closing = false;
MyAPIGateway.Parallel.StartBackground(DoProcessing); MyAPIGateway.Parallel.StartBackground(DoProcessing);
MyLog.Default.WriteLineAndConsole("TORCH MOD: Mod communication registered successfully."); MyLog.Default.WriteLineAndConsole("TORCH MOD: Mod communication registered successfully.");
} }
@@ -40,7 +41,7 @@ namespace Torch.Mod
{ {
MyLog.Default.WriteLineAndConsole("TORCH MOD: Unregistering mod communication."); MyLog.Default.WriteLineAndConsole("TORCH MOD: Unregistering mod communication.");
MyAPIGateway.Multiplayer?.UnregisterMessageHandler(NET_ID, MessageHandler); MyAPIGateway.Multiplayer?.UnregisterMessageHandler(NET_ID, MessageHandler);
_processing.CompleteAdding(); _processing?.CompleteAdding();
_closing = true; _closing = true;
//_task.Wait(); //_task.Wait();
} }
@@ -59,6 +60,8 @@ namespace Torch.Mod
try try
{ {
var m = _processing.Take(); var m = _processing.Take();
MyLog.Default.WriteLineAndConsole($"Processing message: {m.GetType().Name}");
if (m is IncomingMessage) if (m is IncomingMessage)
{ {
MessageBase i; MessageBase i;
@@ -129,9 +132,9 @@ namespace Torch.Mod
MyLog.Default.WriteLineAndConsole("TORCH MOD: COMMUNICATION THREAD: EXIT SIGNAL RECEIVED!"); MyLog.Default.WriteLineAndConsole("TORCH MOD: COMMUNICATION THREAD: EXIT SIGNAL RECEIVED!");
//exit signal received. Clean everything and GTFO //exit signal received. Clean everything and GTFO
_processing.Dispose(); _processing?.Dispose();
_processing = null; _processing = null;
_messagePool.Clean(); _messagePool?.Clean();
_messagePool = null; _messagePool = null;
_playerCache = null; _playerCache = null;
} }
@@ -146,7 +149,6 @@ namespace Torch.Mod
message.Target = target; message.Target = target;
message.TargetType = MessageTarget.Single; message.TargetType = MessageTarget.Single;
MyLog.Default.WriteLineAndConsole($"Sending message of type {message.GetType().FullName}");
_processing.Add(message); _processing.Add(message);
} }