Skip to content

Commit

Permalink
- Added option to RunConfig.cs for mods to compile with non-publicize…
Browse files Browse the repository at this point in the history
…d assemblies.

- Added default values attrib to RunConfig.cs
- Added thread sleeping to CsPackageManager.Dispose() to give time before generating a strong-ref to check acl unload status.
  • Loading branch information
TBN-MapleWheels committed Oct 24, 2023
1 parent 91e2ab8 commit bc1e591
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -203,10 +204,19 @@ public bool TryGetLoadedPluginsForPackage(ContentPackage package, out IEnumerabl
/// </summary>
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)
{
Expand Down Expand Up @@ -240,15 +250,18 @@ 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;
}
}

_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;
Expand All @@ -268,7 +281,6 @@ public void Dispose()
_currentPackagesByLoadOrder.Clear();

AssembliesLoaded = false;
GC.SuppressFinalize(this);
}

/// <summary>
Expand Down Expand Up @@ -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<ContentPackage> badPackages = new();
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -1077,7 +1090,7 @@ private enum PackageProcRet : byte
BadPackage
}

private record LoadableData(ImmutableList<string> AssembliesFilePaths, ImmutableList<string> ScriptsFilePaths);
private record LoadableData(ImmutableList<string> AssembliesFilePaths, ImmutableList<string> ScriptsFilePaths, RunConfig config);

#endregion
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.ComponentModel;
using System.Xml.Serialization;

namespace Barotrauma;
Expand All @@ -9,12 +10,16 @@ public sealed class RunConfig
/// <summary>
/// How should scripts be run on the server.
/// </summary>
[XmlElement(ElementName = "Server")] public string Server;
[XmlElement(ElementName = "Server")]
[DefaultValue("Standard")]
public string Server;

/// <summary>
/// How should scripts be run on the client.
/// </summary>
[XmlElement(ElementName = "Client")] public string Client;
[XmlElement(ElementName = "Client")]
[DefaultValue("Standard")]
public string Client;

/// <summary>
/// List of dependencies by either Steam Workshop ID or by Partial Inclusive Name (ie. "ModDep" will match a mod named "A ModDependency").
Expand All @@ -23,7 +28,14 @@ public sealed class RunConfig
[XmlArrayItem(ElementName = "Dependency", IsNullable = true, Type = typeof(Dependency))]
[XmlArray]
public Dependency[] Dependencies { get; set; }


/// <summary>
/// Compiles the mod using non-publicized assemblies.
/// </summary>
[XmlElement(ElementName = "UseNonPubAssemblies")]
[DefaultValue(false)]
public bool UseNonPublicizedAssemblies { get; set; }

[XmlElement(ElementName = "AutoGenerated")]
public bool AutoGenerated { get; set; }

Expand All @@ -33,6 +45,7 @@ public RunConfig(bool autoGenerated)
if (autoGenerated)
{
(Client, Server) = ("Standard", "Standard");
UseNonPublicizedAssemblies = false;
}
}

Expand Down

0 comments on commit bc1e591

Please sign in to comment.