diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Plugins/CsPackageManager.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Plugins/CsPackageManager.cs index f041279e48..f25b28ea95 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Plugins/CsPackageManager.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Plugins/CsPackageManager.cs @@ -5,6 +5,7 @@ using System.IO; using System.Linq; using System.Reflection; +using System.Runtime.CompilerServices; using System.Text; using System.Threading; using Barotrauma.Steam; @@ -203,10 +204,19 @@ public bool TryGetLoadedPluginsForPackage(ContentPackage package, out IEnumerabl /// public event Action OnDispose; + [MethodImpl(MethodImplOptions.Synchronized)] public void Dispose() { // send events for cleanup - OnDispose?.Invoke(); + try + { + OnDispose?.Invoke(); + } + catch (Exception e) + { + ModUtils.Logging.PrintError($"Error while executing Dispose event: {e.Message}"); + } + // cleanup events if (OnDispose is not null) { @@ -240,6 +250,7 @@ public void Dispose() // we can't wait forever or app dies but we can try to be graceful while (!_assemblyManager.TryBeginDispose()) { + Thread.Sleep(20); // give the assembly context unloader time to run (async) if (_assemblyUnloadStartTime.AddSeconds(_assemblyUnloadTimeoutSeconds) > DateTime.Now) { break; @@ -247,8 +258,10 @@ public void Dispose() } _assemblyUnloadStartTime = DateTime.Now; + Thread.Sleep(100); // give the garbage collector time to finalize the disposed assemblies. while (!_assemblyManager.FinalizeDispose()) { + Thread.Sleep(100); // give the garbage collector time to finalize the disposed assemblies. if (_assemblyUnloadStartTime.AddSeconds(_assemblyUnloadTimeoutSeconds) > DateTime.Now) { break; @@ -268,7 +281,6 @@ public void Dispose() _currentPackagesByLoadOrder.Clear(); AssembliesLoaded = false; - GC.SuppressFinalize(this); } /// @@ -480,7 +492,8 @@ public AssemblyLoadingSuccessState LoadAssemblyPackages() cp, new LoadableData( TryScanPackagesForAssemblies(cp, out var list1) ? list1 : null, - TryScanPackageForScripts(cp, out var list2) ? list2 : null))) + TryScanPackageForScripts(cp, out var list2) ? list2 : null, + GetRunConfigForPackage(cp)))) .ToImmutableDictionary(); HashSet badPackages = new(); @@ -568,7 +581,7 @@ public AssemblyLoadingSuccessState LoadAssemblyPackages() syntaxTrees, null, CompilationOptions, - pair.Key.Name, ref id, publicizedAssemblies); + pair.Key.Name, ref id, pair.Value.config.UseNonPublicizedAssemblies ? null : publicizedAssemblies); if (successState is not AssemblyLoadingSuccessState.Success) { @@ -1077,7 +1090,7 @@ private enum PackageProcRet : byte BadPackage } - private record LoadableData(ImmutableList AssembliesFilePaths, ImmutableList ScriptsFilePaths); + private record LoadableData(ImmutableList AssembliesFilePaths, ImmutableList ScriptsFilePaths, RunConfig config); #endregion } diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Plugins/RunConfig.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Plugins/RunConfig.cs index 7ca448edd8..ab4239e03e 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Plugins/RunConfig.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Plugins/RunConfig.cs @@ -1,4 +1,5 @@ using System; +using System.ComponentModel; using System.Xml.Serialization; namespace Barotrauma; @@ -9,12 +10,16 @@ public sealed class RunConfig /// /// How should scripts be run on the server. /// - [XmlElement(ElementName = "Server")] public string Server; + [XmlElement(ElementName = "Server")] + [DefaultValue("Standard")] + public string Server; /// /// How should scripts be run on the client. /// - [XmlElement(ElementName = "Client")] public string Client; + [XmlElement(ElementName = "Client")] + [DefaultValue("Standard")] + public string Client; /// /// List of dependencies by either Steam Workshop ID or by Partial Inclusive Name (ie. "ModDep" will match a mod named "A ModDependency"). @@ -23,7 +28,14 @@ public sealed class RunConfig [XmlArrayItem(ElementName = "Dependency", IsNullable = true, Type = typeof(Dependency))] [XmlArray] public Dependency[] Dependencies { get; set; } - + + /// + /// Compiles the mod using non-publicized assemblies. + /// + [XmlElement(ElementName = "UseNonPubAssemblies")] + [DefaultValue(false)] + public bool UseNonPublicizedAssemblies { get; set; } + [XmlElement(ElementName = "AutoGenerated")] public bool AutoGenerated { get; set; } @@ -33,6 +45,7 @@ public RunConfig(bool autoGenerated) if (autoGenerated) { (Client, Server) = ("Standard", "Standard"); + UseNonPublicizedAssemblies = false; } }