From 511f98ec184f066fb44f8053665221e8d6c3bccd Mon Sep 17 00:00:00 2001 From: MapleWheels Date: Mon, 9 Feb 2026 15:52:37 -0500 Subject: [PATCH] - Added pre-touch to the ContentPath.FullPath to make them thread-safe. --- .../ContentManagement/ContentPath.cs | 7 +++-- .../_Services/PackageManagementService.cs | 29 +++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/Barotrauma/BarotraumaShared/SharedSource/ContentManagement/ContentPath.cs b/Barotrauma/BarotraumaShared/SharedSource/ContentManagement/ContentPath.cs index 0d9a4a112..2661aef1d 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/ContentManagement/ContentPath.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/ContentManagement/ContentPath.cs @@ -99,12 +99,13 @@ namespace Barotrauma public static ContentPath FromRaw(ContentPackage? contentPackage, string? rawValue) { var newRaw = new ContentPath(contentPackage, rawValue); - if (prevCreatedRaw is not null && prevCreatedRaw.ContentPackage == contentPackage && + // Removed as this almost never happens but makes the constructor not thread-safe. + /*if (prevCreatedRaw is not null && prevCreatedRaw.ContentPackage == contentPackage && prevCreatedRaw.RawValue == rawValue) { newRaw.cachedValue = prevCreatedRaw.Value; } - prevCreatedRaw = newRaw; + prevCreatedRaw = newRaw;*/ return newRaw; } @@ -158,4 +159,4 @@ namespace Barotrauma public override string? ToString() => Value; } -} \ No newline at end of file +} diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/PackageManagementService.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/PackageManagementService.cs index a51ee79b6..5483fab56 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/PackageManagementService.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/PackageManagementService.cs @@ -3,6 +3,7 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; +using System.Runtime.CompilerServices; using System.Threading.Tasks; using Barotrauma.Extensions; using Barotrauma.LuaCs.Data; @@ -138,7 +139,9 @@ public sealed class PackageManagementService : IPackageManagementService { result.WithReasons(pkgConfig.Config.Reasons); if (pkgConfig.Config.IsSuccess) + { result.WithReasons(UnsafeAddPackageInternal(pkgConfig.Source, pkgConfig.Config.Value).Reasons); + } } return result; @@ -152,6 +155,32 @@ public sealed class PackageManagementService : IPackageManagementService return FluentResults.Result.Ok(); } + // We need to touch ContentPath.Fullpath once in a single-threaded context to make it thread-safe. + foreach (var info in config.Assemblies) + { + TouchMeFullPaths(info); + } + + foreach (var info in config.Configs) + { + TouchMeFullPaths(info); + } + + foreach (var info in config.LuaScripts) + { + TouchMeFullPaths(info); + } + + // We need to touch ContentPath.Fullpath once in a single-threaded context to make it thread-safe. + [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.PreserveSig)] + void TouchMeFullPaths(IBaseResourceInfo info) + { + foreach (var contentPath in info.FilePaths) + { + var s = contentPath.FullPath; + } + } + _loadedPackages[package] = config; try {