-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- To request a reference assembly to be generated add an empty txt file under `References` with the name of the dll you want to target, then run MultiplayerCompat without Multiplayer. - Generated assemblies are referenced in the Source_Referenced project, compat classes in this project are loaded dynamically while avoiding TypeLoadException issues. - It's discouraged to abuse this feature as it's expensive to maintain, consider submitting code with proper Reflection first.
- Loading branch information
Showing
7 changed files
with
168 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -330,3 +330,4 @@ ASALocalRun/ | |
.mfractor/ | ||
|
||
Assemblies/ | ||
Referenced/ |
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
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,82 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
using Mono.Cecil; | ||
using Verse; | ||
|
||
namespace Multiplayer.Compat | ||
{ | ||
static class ReferenceBuilder | ||
{ | ||
public static void Restore(string refsFolder) | ||
{ | ||
var requestedFiles = Directory.CreateDirectory(refsFolder).GetFiles("*.txt"); | ||
|
||
foreach(var request in requestedFiles) | ||
{ | ||
BuildReference(refsFolder, request); | ||
} | ||
} | ||
|
||
static void BuildReference(string refsFolder, FileInfo request) | ||
{ | ||
var asmId = Path.GetFileNameWithoutExtension(request.Name); | ||
|
||
var assembly = LoadedModManager.RunningModsListForReading | ||
.SelectMany(m => ModContentPack.GetAllFilesForModPreserveOrder(m, "Assemblies/", f => f.ToLower() == ".dll")) | ||
.FirstOrDefault(f => f.Item2.Name == asmId + ".dll")?.Item2; | ||
|
||
var hash = ComputeHash(assembly.FullName); | ||
var hashFile = Path.Combine(refsFolder, asmId + ".txt"); | ||
|
||
if (File.Exists(hashFile) && File.ReadAllText(hashFile) == hash) | ||
return; | ||
|
||
Console.WriteLine($"MpCompat References: Writing {asmId}.dll"); | ||
|
||
var outFile = Path.Combine(refsFolder, asmId + ".dll"); | ||
var asmDef = AssemblyDefinition.ReadAssembly(assembly.FullName); | ||
|
||
foreach (var t in asmDef.MainModule.GetTypes()) | ||
{ | ||
if (t.IsNested) | ||
t.IsNestedPublic = true; | ||
else | ||
t.IsPublic = true; | ||
|
||
foreach (var m in t.Methods) | ||
{ | ||
m.IsPublic = true; | ||
m.Body = new Mono.Cecil.Cil.MethodBody(m); | ||
} | ||
|
||
foreach (var f in t.Fields) | ||
{ | ||
f.IsInitOnly = false; | ||
f.IsPublic = true; | ||
} | ||
} | ||
|
||
asmDef.Write(outFile); | ||
File.WriteAllText(hashFile, hash); | ||
} | ||
|
||
static string ComputeHash(string assemblyPath) | ||
{ | ||
var res = new StringBuilder(); | ||
|
||
using var hash = SHA1.Create(); | ||
using FileStream file = File.Open(assemblyPath, FileMode.Open, FileAccess.Read); | ||
|
||
hash.ComputeHash(file); | ||
|
||
foreach (byte b in hash.Hash) | ||
res.Append(b.ToString("X2")); | ||
|
||
return res.ToString(); | ||
} | ||
} | ||
} |
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,33 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>net472</TargetFramework> | ||
<LangVersion>9.0</LangVersion> | ||
<GenerateAssemblyInfo>false</GenerateAssemblyInfo> | ||
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute> | ||
<OutputPath>..\Referenced\</OutputPath> | ||
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath> | ||
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath> | ||
<DebugType>None</DebugType> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="../Source/Multiplayer_Compat.csproj"> | ||
<Private>false</Private> | ||
</ProjectReference> | ||
<PackageReference Include="Lib.Harmony"> | ||
<Version>2.1.*</Version> | ||
<ExcludeAssets>runtime</ExcludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="RimWorld.MultiplayerAPI"> | ||
<Version>0.3.0</Version> | ||
<ExcludeAssets>runtime</ExcludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Krafs.Rimworld.Ref"> | ||
<Version>1.3.*</Version> | ||
<ExcludeAssets>runtime</ExcludeAssets> | ||
</PackageReference> | ||
<Reference Include="..\References\*.dll"> | ||
<Private>false</Private> | ||
</Reference> | ||
</ItemGroup> | ||
</Project> |