From 190c98d8f2b23f8a7e409268f327a9e05984297e Mon Sep 17 00:00:00 2001 From: NotAlwaysTrue <2136846186@qq.com> Date: Mon, 29 Dec 2025 16:28:34 +0800 Subject: [PATCH] Fixed 2 issues Fixed an issue causing gap.update crashes the game(engine issue) Fixed an potential issue that on MacOS we cannot get core count and cause MaxDegreeOfParallelism will be set to 0. Now if we cant get that number we simply use a fixed 16 instead --- .../SharedSource/Map/MapEntity.cs | 25 ++++++++----------- .../SharedSource/Screens/GameScreen.cs | 2 +- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/Barotrauma/BarotraumaShared/SharedSource/Map/MapEntity.cs b/Barotrauma/BarotraumaShared/SharedSource/Map/MapEntity.cs index 7ef1b529e..6b2a97d06 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Map/MapEntity.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Map/MapEntity.cs @@ -646,13 +646,17 @@ namespace Barotrauma var sw = new System.Diagnostics.Stopwatch(); sw.Start(); #endif - // Buffer lists to avoid repeated allocations var hullList = Hull.HullList.ToList(); var structureList = Structure.WallList.ToList(); var gapList = Gap.GapList.ToList(); var itemList = Item.ItemList.ToList(); + Parallel.ForEach(gapList, parallelOptions, gap => + { + gap.ResetWaterFlowThisFrame(); + }); + // First phase: parallel updates that have no order dependencies Parallel.Invoke(parallelOptions, () => @@ -674,10 +678,13 @@ namespace Barotrauma // Gap reset (must be done before update) () => { - Parallel.ForEach(gapList, parallelOptions, gap => + // Gap update (has order dependencies, keep random order but execute sequentially) + var shuffledGaps = gapList.OrderBy(g => Rand.Int(int.MaxValue)).ToList(); + foreach (var gap in shuffledGaps) { - gap.ResetWaterFlowThisFrame(); - }); + gap.Update(deltaTime, cam); + } + }, // Powered components update () => @@ -689,16 +696,6 @@ namespace Barotrauma #if CLIENT // Hull Cheats need to be executed after Hull update Hull.UpdateCheats(deltaTime, cam); -#endif - - // Gap update (has order dependencies, keep random order but execute sequentially) - var shuffledGaps = gapList.OrderBy(g => Rand.Int(int.MaxValue)).ToList(); - Parallel.ForEach(gapList, parallelOptions, gap => - { - gap.Update(deltaTime, cam); - }); - -#if CLIENT sw.Stop(); GameMain.PerformanceCounter.AddElapsedTicks("Update:MapEntity:Misc", sw.ElapsedTicks); sw.Restart(); diff --git a/Barotrauma/BarotraumaShared/SharedSource/Screens/GameScreen.cs b/Barotrauma/BarotraumaShared/SharedSource/Screens/GameScreen.cs index 7128946ac..c1a07a7d4 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Screens/GameScreen.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Screens/GameScreen.cs @@ -25,7 +25,7 @@ namespace Barotrauma private static readonly ParallelOptions parallelOptions = new ParallelOptions { - MaxDegreeOfParallelism = Environment.ProcessorCount * 2, + MaxDegreeOfParallelism = Environment.ProcessorCount > 0 ? Environment.ProcessorCount * 2 : 16, }; #if CLIENT