try-finally multiple UniqueEvents.ToList() to avoid issues(Destination array was not long enough. Check the destination index, length, and the array's lower bounds)

This commit is contained in:
NotAlwaysTrue
2025-12-22 22:43:02 +08:00
parent b891ec7793
commit dadd6c598f
2 changed files with 29 additions and 16 deletions
@@ -1218,18 +1218,25 @@ namespace Barotrauma.Networking
errorLines.Add(""); errorLines.Add("");
errorLines.Add("EntitySpawner events:"); errorLines.Add("EntitySpawner events:");
foreach (var entityEvent in entityEventManager.UniqueEvents.ToList()) try
{ {
if (entityEvent.Entity is EntitySpawner) foreach (var entityEvent in entityEventManager.UniqueEvents.ToList())
{ {
var spawnData = entityEvent.Data as EntitySpawner.SpawnOrRemove; if (entityEvent.Entity is EntitySpawner)
errorLines.Add( {
entityEvent.ID + ": " + var spawnData = entityEvent.Data as EntitySpawner.SpawnOrRemove;
(spawnData is EntitySpawner.RemoveEntity ? "Remove " : "Create ") + errorLines.Add(
spawnData.Entity.ToString() + entityEvent.ID + ": " +
" (" + spawnData.ID + ", " + spawnData.Entity.ID + ")"); (spawnData is EntitySpawner.RemoveEntity ? "Remove " : "Create ") +
spawnData.Entity.ToString() +
" (" + spawnData.ID + ", " + spawnData.Entity.ID + ")");
}
} }
} }
finally
{
errorLines.Add("Catch event snapshot failed.");
}
errorLines.Add(""); errorLines.Add("");
errorLines.Add("Last debug messages:"); errorLines.Add("Last debug messages:");
@@ -170,16 +170,22 @@ namespace Barotrauma
#if SERVER #if SERVER
if (GameMain.Server != null && Entity.Spawner != null) if (GameMain.Server != null && Entity.Spawner != null)
{ {
if (GameMain.Server.EntityEventManager.UniqueEvents.ToList().Any(ev => ev.Entity == item)) try
{ {
string errorMsg = $"Error while spawning job items. Item {item.Name} created network events before the spawn event had been created."; if (GameMain.Server.EntityEventManager.UniqueEvents.ToList().Any(ev => ev.Entity == item))
DebugConsole.ThrowError(errorMsg); {
GameAnalyticsManager.AddErrorEventOnce("Job.InitializeJobItem:EventsBeforeSpawning", GameAnalyticsManager.ErrorSeverity.Error, errorMsg); string errorMsg = $"Error while spawning job items. Item {item.Name} created network events before the spawn event had been created.";
GameMain.Server.EntityEventManager.UniqueEvents.RemoveAll(ev => ev.Entity == item); DebugConsole.ThrowError(errorMsg);
GameMain.Server.EntityEventManager.Events.RemoveAll(ev => ev.Entity == item); GameAnalyticsManager.AddErrorEventOnce("Job.InitializeJobItem:EventsBeforeSpawning", GameAnalyticsManager.ErrorSeverity.Error, errorMsg);
GameMain.Server.EntityEventManager.UniqueEvents.RemoveAll(ev => ev.Entity == item);
GameMain.Server.EntityEventManager.Events.RemoveAll(ev => ev.Entity == item);
}
} }
finally
Entity.Spawner.CreateNetworkEvent(new EntitySpawner.SpawnEntity(item)); {
Entity.Spawner.CreateNetworkEvent(new EntitySpawner.SpawnEntity(item));
}
} }
#endif #endif