-
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.
- Loading branch information
0 parents
commit 699d9ea
Showing
6 changed files
with
167 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
bin/ | ||
obj/ | ||
/packages/ | ||
riderModule.iml | ||
/_ReSharper.Caches/ | ||
.idea/ | ||
*DotSettings.user |
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,108 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.IO; | ||
using System.Reflection; | ||
using System.Reflection.Emit; | ||
using System.Runtime.Loader; | ||
using Barotrauma; | ||
using HarmonyLib; | ||
|
||
|
||
[SuppressMessage("ReSharper", "UnusedType.Global")] | ||
[SuppressMessage("ReSharper", "CheckNamespace")] | ||
[SuppressMessage("ReSharper", "UnusedMember.Global")] | ||
internal class StartupHook | ||
{ | ||
public static void Initialize() | ||
{ | ||
if (Assembly.GetEntryAssembly()?.GetName().Name != "DedicatedServer") return; | ||
|
||
AssemblyLoadContext.Default.Resolving += SharedHostPolicy.SharedAssemblyResolver.LoadAssemblyFromSharedLocation; | ||
TraumaWeaver.Utils.AssemblyLoadPathHelper("0Harmony.dll"); | ||
TraumaWeaver.Utils.AssemblyLoadPathHelper("Mono.Cecil.dll"); | ||
TraumaWeaver.Utils.AssemblyLoadPathHelper("MonoMod.RuntimeDetour.dll"); | ||
TraumaWeaver.Utils.AssemblyLoadPathHelper("MonoMod.Utils.dll"); | ||
TraumaWeaver.MainPatcher.doHarmony(); | ||
} | ||
} | ||
|
||
namespace TraumaWeaver | ||
{ | ||
|
||
public static class MainPatcher | ||
{ | ||
public static void doHarmony() | ||
{ | ||
var harmony = new Harmony("TraumaWeaverLoader"); | ||
|
||
|
||
harmony.Patch(AccessTools.Method(typeof(Program), nameof(Program.Main)), | ||
transpiler: new HarmonyMethod(typeof(Patches), nameof(Patches.MainTranspiler))); | ||
} | ||
} | ||
public static class Utils | ||
{ | ||
public static Assembly AssemblyLoadPathHelper(string assemblyName) | ||
{ | ||
return AssemblyLoadContext.Default.LoadFromAssemblyPath(Directory.GetCurrentDirectory() + "/" + assemblyName); | ||
} | ||
} | ||
|
||
public class Hooks | ||
{ | ||
public static void onMain() | ||
{ | ||
Console.WriteLine("TraumaWeaver " + Assembly.GetExecutingAssembly().GetName().Version + " Loaded"); | ||
Console.WriteLine("Loading mods..."); | ||
DirectoryInfo serverDllMods = Directory.CreateDirectory("ServerDllMods"); | ||
var mods = serverDllMods.EnumerateFiles(); | ||
foreach (FileInfo mod in mods) | ||
{ | ||
Console.WriteLine($"Loading {mod.Name}"); | ||
try | ||
{ | ||
Assembly modAssembly = Assembly.LoadFrom(mod.FullName); | ||
modAssembly.GetTypes().Find(e => e.Name == "Mod").GetMethod("ModMain")?.Invoke(null, null); | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.WriteLine($"Could not load {mod.FullName}\nException:\n{e}"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public class Patches | ||
{ | ||
public static IEnumerable<CodeInstruction> MainTranspiler(IEnumerable<CodeInstruction> instructions) | ||
{ | ||
var found = false; | ||
foreach (CodeInstruction instruction in instructions) | ||
{ | ||
if (instruction.Calls(AccessTools.Method(typeof(AppDomain), "get_CurrentDomain")) && !found) | ||
{ | ||
yield return new CodeInstruction(OpCodes.Call, typeof(Hooks).GetMethod(nameof(Hooks.onMain))); | ||
found = true; | ||
} | ||
yield return instruction; | ||
} | ||
if (!found) | ||
{ | ||
Console.WriteLine("Cannot find Program.Main"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
namespace SharedHostPolicy | ||
{ | ||
internal static class SharedAssemblyResolver | ||
{ | ||
public static Assembly LoadAssemblyFromSharedLocation(AssemblyLoadContext context, AssemblyName assemblyName) | ||
{ | ||
string sharedAssemblyPath = Directory.GetCurrentDirectory(); // find assemblyName in shared location... | ||
return sharedAssemblyPath != null ? AssemblyLoadContext.Default.LoadFromAssemblyPath(sharedAssemblyPath) : null; | ||
} | ||
} | ||
} |
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,8 @@ | ||
using System; | ||
namespace TraumaWeaver | ||
{ | ||
public interface IMod | ||
{ | ||
static string ModMain() => throw new NotImplementedException(); | ||
} | ||
} |
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,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Library</OutputType> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
<EnableDynamicLoading>true</EnableDynamicLoading> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="HarmonyX" Version="2.8.0" /> | ||
<PackageReference Include="Mono.Cecil" Version="0.11.4" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Reference Include="DedicatedServer, Version=0.15.23.0, Culture=neutral, PublicKeyToken=null"> | ||
<HintPath>..\HolyTraumaGrail\bin\Debug\netcoreapp3.1\DedicatedServer-nstrip.dll</HintPath> | ||
<Private>False</Private> | ||
</Reference> | ||
</ItemGroup> | ||
|
||
</Project> |
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,16 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TraumaWeaver", "TraumaWeaver.csproj", "{9531E183-CFA3-46DF-99A6-6D8311417709}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{9531E183-CFA3-46DF-99A6-6D8311417709}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{9531E183-CFA3-46DF-99A6-6D8311417709}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{9531E183-CFA3-46DF-99A6-6D8311417709}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{9531E183-CFA3-46DF-99A6-6D8311417709}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
EndGlobal |
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,7 @@ | ||
{ | ||
"sdk": { | ||
"version": "3.1.0", | ||
"rollForward": "latestMinor", | ||
"allowPrerelease": false | ||
} | ||
} |