Skip to content

Commit 0afc9bf

Browse files
committed
Added fallback native assembly resolver.
1 parent 699bd57 commit 0afc9bf

3 files changed

Lines changed: 60 additions & 5 deletions

File tree

Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Plugins/IAssemblyLoaderService.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public record LoaderInitData(
4444
/// </summary>
4545
Guid Id { get; }
4646
/// <summary>
47+
/// The owner content package.
48+
/// </summary>
49+
ContentPackage OwnerPackage { get; }
50+
/// <summary>
4751
/// Indicates that the assemblies in this load context are metadata references only and not
4852
/// intended for execution.
4953
/// </summary>

Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/LuaScriptManagementService.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,16 @@ private void RegisterConsoleCommands(IConsoleCommandsService commands)
116116

117117
commands.RegisterCommand("cl_toggleluadebug", "Toggles the MoonSharp Debug Server.", (string[] args) =>
118118
{
119-
int port = 41912;
119+
DebugConsole.Log($"This command is currently not implemented. Please open a github issue if you need this feature.");
120+
/*int port = 41912;
120121
121122
if (args.Length > 0)
122123
{
123124
int.TryParse(args[0], out port);
124125
}
125126
126127
throw new NotImplementedException();
127-
//GameMain.LuaCs.ToggleDebugger(port);
128+
//GameMain.LuaCs.ToggleDebugger(port);*/
128129
});
129130

130131
#elif SERVER

Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/PluginManagementService.cs

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Linq.Expressions;
77
using System.Reflection;
88
using System.Runtime.CompilerServices;
9+
using System.Runtime.InteropServices;
910
using System.Runtime.Loader;
1011
using System.Text;
1112
using System.Threading;
@@ -199,6 +200,7 @@ public FluentResults.Result Reset()
199200
private readonly ConcurrentDictionary<Type, ContentPackage> _pluginPackageLookup = new();
200201
private readonly ConcurrentDictionary<ContentPackage, ImmutableArray<IAssemblyPlugin>> _pluginInstances = new();
201202
private readonly ConditionalWeakTable<IAssemblyLoaderService, ContentPackage> _unloadingAssemblyLoaders = new();
203+
private readonly ConcurrentBag<IntPtr> _loadedNativeLibraries = new();
202204
private readonly AsyncReaderWriterLock _operationsLock = new();
203205
private ServiceContainer _pluginInjectorContainer;
204206

@@ -632,10 +634,39 @@ private string DoSourceCodeTextCompatibilityPass(string sourceCode)
632634
return sourceCode.Replace("GameMain.LuaCs", "LuaCsSetup.Instance");
633635
}
634636

635-
private IntPtr OnAssemblyLoaderResolvingUnmanaged(Assembly arg1, string arg2)
637+
private IntPtr OnAssemblyLoaderResolvingUnmanaged(Assembly callerAssembly, string targetAssemblyName)
636638
{
637-
// TODO: Implement extern assembly lookup for Native/Unmanaged Assemblies.
638-
throw new NotImplementedException();
639+
Guard.IsNull(callerAssembly, nameof(callerAssembly));
640+
Guard.IsNullOrWhiteSpace(targetAssemblyName, nameof(targetAssemblyName));
641+
642+
if (AssemblyLoadContext.GetLoadContext(callerAssembly) is not IAssemblyLoaderService loaderService)
643+
{
644+
return IntPtr.Zero;
645+
}
646+
647+
var targetDirectory = Path.GetFullPath(loaderService.OwnerPackage.Dir);
648+
if (!targetAssemblyName.TrimEnd().EndsWith(".dll"))
649+
{
650+
targetAssemblyName += ".dll";
651+
}
652+
653+
var res = _storageService.FindFilesInPackage(loaderService.OwnerPackage, string.Empty, targetAssemblyName, true);
654+
655+
if (res.IsFailed || !res.Value.Any())
656+
{
657+
return IntPtr.Zero;
658+
}
659+
660+
foreach (var path in res.Value)
661+
{
662+
if (System.Runtime.InteropServices.NativeLibrary.TryLoad(path, out IntPtr asmPtr))
663+
{
664+
_loadedNativeLibraries.Add(asmPtr);
665+
return asmPtr;
666+
}
667+
}
668+
669+
return IntPtr.Zero;
639670
}
640671

641672
private Assembly OnAssemblyLoaderResolvingManaged(IAssemblyLoaderService requestingLoader, AssemblyName searchName)
@@ -708,6 +739,25 @@ public FluentResults.Result UnloadManagedAssemblies()
708739
_assemblyLoaders.Clear();
709740
GC.Collect(GC.MaxGeneration, GCCollectionMode.Aggressive, true);
710741

742+
// clear native libraries
743+
if (_loadedNativeLibraries.Any())
744+
{
745+
foreach (var ptr in _loadedNativeLibraries)
746+
{
747+
try
748+
{
749+
System.Runtime.InteropServices.NativeLibrary.Free(ptr);
750+
}
751+
catch
752+
{
753+
// ignored
754+
continue;
755+
}
756+
}
757+
758+
_loadedNativeLibraries.Clear();
759+
}
760+
711761
return results;
712762
}
713763

0 commit comments

Comments
 (0)