-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from BUTR/dev
v1.4.11
- Loading branch information
Showing
9 changed files
with
114 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/Bannerlord.BLSE.Loaders.LauncherEx/ModuleInitializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System; | ||
using System.IO; | ||
using System.IO.Compression; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Threading; | ||
|
||
internal static class ModuleInitializer | ||
{ | ||
private static int _isAttached; | ||
|
||
[ModuleInitializer] | ||
internal static void Action() | ||
{ | ||
if (Interlocked.Exchange(ref _isAttached, 1) == 1) | ||
return; | ||
AppDomain.CurrentDomain.AssemblyResolve += ResolveAssembly; | ||
} | ||
|
||
private static Assembly? ResolveAssembly(object? sender, ResolveEventArgs e) | ||
{ | ||
if (e.Name is null) | ||
return null; | ||
|
||
var assemblyName = new AssemblyName(e.Name); | ||
if (assemblyName.Name is "System.Drawing.Common") | ||
return ResolveLauncherExAssemblies(assemblyName); | ||
if (assemblyName.Name is "System.Memory") | ||
return ResolveLauncherExAssemblies(assemblyName); | ||
if (assemblyName.Name is "System.Buffers") | ||
return ResolveLauncherExAssemblies(assemblyName); | ||
if (assemblyName.Name is "System.Runtime.CompilerServices.Unsafe") | ||
return ResolveLauncherExAssemblies(assemblyName); | ||
|
||
return null; | ||
} | ||
|
||
private static Assembly? ResolveLauncherExAssemblies(AssemblyName assemblyName) | ||
{ | ||
var name = assemblyName.Name; | ||
|
||
var @namespace = "Bannerlord.BLSE.Loaders.LauncherEx."; | ||
var resources = typeof(ModuleInitializer).Assembly.GetManifestResourceNames().Select(x => x.Remove(0, @namespace.Length)); | ||
var toLoad = resources.FirstOrDefault(x => x.StartsWith(name)); | ||
if (toLoad is not null) | ||
{ | ||
using var resourceStream = typeof(ModuleInitializer).Assembly.GetManifestResourceStream($"{@namespace}{toLoad}")!; | ||
using var ms = new MemoryStream(); | ||
resourceStream.CopyTo(ms); | ||
return Assembly.Load(ms.ToArray()); | ||
} | ||
|
||
return null; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/Bannerlord.BLSE.Loaders.LauncherEx/ModuleInitializerAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// ReSharper disable once CheckNamespace | ||
namespace System.Runtime.CompilerServices; | ||
|
||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] | ||
public sealed class ModuleInitializerAttribute : Attribute { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters