-
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.
Restructured everything, added clientside modloading
- Loading branch information
Showing
10 changed files
with
313 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Reflection; | ||
using System.Reflection.Emit; | ||
using System.Runtime.CompilerServices; | ||
using Barotrauma; | ||
using HarmonyLib; | ||
using Microsoft.Xna.Framework; | ||
using Microsoft.Xna.Framework.Graphics; | ||
|
||
[assembly: IgnoresAccessChecksTo("Barotrauma")] | ||
namespace TraumaWeaverClient | ||
{ | ||
|
||
public static class MainPatcher | ||
{ | ||
public static void doHarmony() | ||
{ | ||
AppDomain.CurrentDomain.AssemblyResolve += ResolveHandler; | ||
var harmony = new Harmony("TraumaWeaverClient"); | ||
|
||
|
||
harmony.Patch(AccessTools.Method(typeof(Program), nameof(Program.Main)), | ||
transpiler: new HarmonyMethod(typeof(Patches), nameof(Patches.MainTranspiler))); | ||
harmony.Patch(AccessTools.Method(typeof(MainMenuScreen), nameof(MainMenuScreen.Draw)), | ||
transpiler: new HarmonyMethod(typeof(Patches), nameof(Patches.MainMenuScreenTranspiler))); | ||
} | ||
|
||
private static Assembly ResolveHandler(object o, ResolveEventArgs args) | ||
{ | ||
if (args.Name == "MonoGame.Framework.Linux.NetStandard, Version=3.7.0.0, Culture=neutral, PublicKeyToken=null") | ||
{ | ||
return Array.Find(AppDomain.CurrentDomain.GetAssemblies(), | ||
assembly => assembly.GetName().FullName == "MonoGame.Framework.Windows.NetStandard, Version=3.7.0.0, Culture=neutral, PublicKeyToken=null"); | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
public class Hooks | ||
{ | ||
public static void onMain() | ||
{ | ||
Console.WriteLine("TraumaWeaverClient " + Assembly.GetExecutingAssembly().GetName().Version + " Loaded"); | ||
Console.WriteLine("Loading mods..."); | ||
DirectoryInfo clientDllMods = Directory.CreateDirectory("ClientDllMods"); | ||
var mods = clientDllMods.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 static void onMainMenuScreenDraw(double deltaTime, GraphicsDevice graphics, SpriteBatch spriteBatch) | ||
{ | ||
String versionString = $"TraumaWeaverClient {Assembly.GetExecutingAssembly().GetName().Version} Loaded"; | ||
GUIStyle.SmallFont.DrawString(spriteBatch, versionString, new Vector2(HUDLayoutSettings.Padding, GameMain.GraphicsHeight - GUIStyle.SmallFont.LineHeight * 3f - HUDLayoutSettings.Padding * 0.75f), Color.Teal); | ||
} | ||
} | ||
|
||
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"); | ||
} | ||
} | ||
|
||
public static IEnumerable<CodeInstruction> MainMenuScreenTranspiler(IEnumerable<CodeInstruction> instructions) | ||
{ | ||
var found = false; | ||
foreach (CodeInstruction instruction in instructions) | ||
{ | ||
if (instruction.LoadsField(typeof(GameMain).GetField("Version"))) | ||
{ | ||
yield return new CodeInstruction(OpCodes.Ldarg_1); | ||
yield return new CodeInstruction(OpCodes.Ldarg_2); | ||
yield return new CodeInstruction(OpCodes.Ldarg_3); | ||
yield return new CodeInstruction(OpCodes.Call, typeof(Hooks).GetMethod(nameof(Hooks.onMainMenuScreenDraw))); | ||
found = true; | ||
} | ||
yield return instruction; | ||
} | ||
if (!found) | ||
{ | ||
Console.WriteLine("Cannot find String.Concat"); | ||
} | ||
} | ||
} | ||
} |
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,26 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Reference Include="Barotrauma, Version=0.17.12.0, Culture=neutral, PublicKeyToken=null"> | ||
<HintPath>..\..\..\.steam\steam\steamapps\common\Barotrauma\Barotrauma-nstrip.dll</HintPath> | ||
<Private>False</Private> | ||
</Reference> | ||
<Reference Include="MonoGame.Framework.Linux.NetStandard, Version=3.7.0.0, Culture=neutral, PublicKeyToken=null"> | ||
<HintPath>..\..\..\.steam\steam\steamapps\common\Barotrauma\MonoGame.Framework.Linux.NetStandard.dll</HintPath> | ||
<Private>False</Private> | ||
</Reference> | ||
<Reference Include="XNATypes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"> | ||
<HintPath>..\..\..\.steam\steam\steamapps\common\Barotrauma\XNATypes.dll</HintPath> | ||
<Private>False</Private> | ||
</Reference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="HarmonyX" Version="2.10.0" /> | ||
</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,26 @@ | ||
using System.Diagnostics; | ||
using System.IO; | ||
|
||
namespace TraumaWeaverLauncher | ||
{ | ||
public static class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
ProcessStartInfo pProcess = new ProcessStartInfo | ||
{ | ||
EnvironmentVariables = | ||
{ | ||
["DOTNET_STARTUP_HOOKS"] = $"{Directory.GetCurrentDirectory()}/TraumaWeaverLoader.dll" | ||
}, | ||
UseShellExecute = false, | ||
CreateNoWindow = true, | ||
FileName = "dotnet", | ||
ArgumentList = { "Barotrauma.dll" } | ||
}; | ||
|
||
Process.Start(pProcess); | ||
|
||
} | ||
} | ||
} |
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 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
</Project> |
File renamed without changes.
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,94 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.IO; | ||
using System.Reflection; | ||
using System.Runtime.Loader; | ||
|
||
|
||
[SuppressMessage("ReSharper", "UnusedType.Global")] | ||
[SuppressMessage("ReSharper", "CheckNamespace")] | ||
[SuppressMessage("ReSharper", "UnusedMember.Global")] | ||
internal class StartupHook | ||
{ | ||
enum LaunchType | ||
{ | ||
CLIENT, | ||
SERVER | ||
} | ||
|
||
public static void Initialize() | ||
{ | ||
|
||
LaunchType launch; | ||
|
||
switch (Assembly.GetEntryAssembly()?.GetName().Name) | ||
{ | ||
case "DedicatedServer": | ||
{ | ||
launch = LaunchType.SERVER; | ||
break; | ||
} | ||
case "Barotrauma": | ||
{ | ||
launch = LaunchType.CLIENT; | ||
break; | ||
} | ||
default: | ||
{ | ||
return; | ||
} | ||
} | ||
|
||
AssemblyLoadContext.Default.Resolving += SharedHostPolicy.SharedAssemblyResolver.LoadAssemblyFromSharedLocation; | ||
TraumaWeaverLoader.Utils.AssemblyLoadPathHelper("0Harmony.dll"); | ||
TraumaWeaverLoader.Utils.AssemblyLoadPathHelper("Mono.Cecil.dll"); | ||
TraumaWeaverLoader.Utils.AssemblyLoadPathHelper("MonoMod.RuntimeDetour.dll"); | ||
TraumaWeaverLoader.Utils.AssemblyLoadPathHelper("MonoMod.Utils.dll"); | ||
|
||
Assembly assembly = null; | ||
|
||
switch (launch) | ||
{ | ||
case LaunchType.CLIENT: | ||
{ | ||
assembly = Assembly.LoadFile(Directory.GetCurrentDirectory() + "/TraumaWeaverClient.dll"); | ||
break; | ||
} | ||
case LaunchType.SERVER: | ||
{ | ||
assembly = Assembly.LoadFile(Directory.GetCurrentDirectory() + "/TraumaWeaverServer.dll"); | ||
break; | ||
} | ||
} | ||
|
||
|
||
if (assembly != null) | ||
{ | ||
Array.Find(assembly.GetTypes(), e => e.Name == "MainPatcher")?.GetMethod("doHarmony")?.Invoke(null, null); | ||
} | ||
} | ||
} | ||
|
||
namespace TraumaWeaverLoader | ||
{ | ||
public static class Utils | ||
{ | ||
public static Assembly AssemblyLoadPathHelper(string assemblyName) | ||
{ | ||
return AssemblyLoadContext.Default.LoadFromAssemblyPath(Directory.GetCurrentDirectory() + "/" + assemblyName); | ||
} | ||
} | ||
} | ||
|
||
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
Oops, something went wrong.