Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Commit

Permalink
gg sloth
Browse files Browse the repository at this point in the history
  • Loading branch information
misandrie committed Nov 29, 2023
1 parent 55a1001 commit 1b820a1
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 26 deletions.
40 changes: 25 additions & 15 deletions Marsey/GameAssemblyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static void Init(Harmony harmony)
Harmony.DEBUG = true;
}

public static Harmony? GetHarmony => _harmony;
public static Harmony? GetHarmony() => _harmony;
}

public static class GamePatcher
Expand All @@ -35,7 +35,7 @@ public static class GamePatcher
/// <param name="patchlist">A list of patches</param>
public static void Patch<T>(List<T> patchlist) where T : IPatch
{
Harmony? harmony = HarmonyManager.GetHarmony;
Harmony? harmony = HarmonyManager.GetHarmony();
if (harmony == null) return;

foreach (T patch in patchlist)
Expand Down Expand Up @@ -83,20 +83,30 @@ public static void GetGameAssemblies(out Assembly? clientAss, out Assembly? robu
int loops;
for (loops = 0; loops < MarseyVars.MaxLoops; loops++)
{
foreach (var asm in AppDomain.CurrentDomain.GetAssemblies())
Assembly[] asmlist = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly asm in asmlist)
{
string? fullName = asm.FullName;
if (fullName == null) continue;

if (robustSharedAss == null && fullName.Contains("Robust.Shared,"))
robustSharedAss = asm;
else if (clientAss == null && fullName.Contains("Content.Client,"))
clientAss = asm;
else if (clientSharedAss == null && fullName.Contains("Content.Shared,"))
clientSharedAss = asm;

if (robustSharedAss != null && clientAss != null && clientSharedAss != null)
break;
try
{
Console.WriteLine("Trying " + asm.FullName);
string? fullName = asm.FullName;
if (fullName == null) continue;

if (robustSharedAss == null && fullName.Contains("Robust.Shared,"))
robustSharedAss = asm;
else if (clientAss == null && fullName.Contains("Content.Client,"))
clientAss = asm;
else if (clientSharedAss == null && fullName.Contains("Content.Shared,"))
clientSharedAss = asm;

if (robustSharedAss != null && clientAss != null && clientSharedAss != null)
break;
}
catch (AccessViolationException e)

Check warning on line 105 in Marsey/GameAssemblyManager.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'e' is declared but never used

Check warning on line 105 in Marsey/GameAssemblyManager.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'e' is declared but never used

Check warning on line 105 in Marsey/GameAssemblyManager.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'e' is declared but never used

Check warning on line 105 in Marsey/GameAssemblyManager.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'e' is declared but never used

Check warning on line 105 in Marsey/GameAssemblyManager.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'e' is declared but never used

Check warning on line 105 in Marsey/GameAssemblyManager.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'e' is declared but never used
{
Console.WriteLine($"{asm.FullName} told you to fuck off");
throw;
}
}

if (robustSharedAss != null && clientAss != null && clientSharedAss != null)
Expand Down
28 changes: 28 additions & 0 deletions Marsey/Handbreak/Manual.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Reflection;
using HarmonyLib;

namespace Marsey.Handbreak;

/// <summary>
/// In some cases we are required to patch functions publicly
/// </summary>
public static class Manual
{
public static void ManualPrefix(MethodInfo method, MethodInfo prefix)
{
Harmony? harm = HarmonyManager.GetHarmony();
harm?.Patch(method, prefix: prefix);
}

public static void ManualPostfix(MethodInfo method, MethodInfo postfix)
{
Harmony? harm = HarmonyManager.GetHarmony();
harm?.Patch(method, postfix: postfix);
}

public static void ManualTranspiler(MethodInfo method, MethodInfo transpiler)
{
Harmony? harm = HarmonyManager.GetHarmony();
harm?.Patch(method, transpiler: transpiler);
}
}
25 changes: 16 additions & 9 deletions Marsey/MarseyPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading;
using HarmonyLib;
using Marsey.Preloader;
using Marsey.Stealthsey;
using Marsey.Subversion;

namespace Marsey;
Expand All @@ -15,14 +16,7 @@ namespace Marsey;
/// </summary>
public class MarseyPatcher
{
/// <summary>
/// Boots up the patcher
///
/// Executed by the loader.
/// </summary>
/// <param name="robClientAssembly">Robust.Client assembly as *loaded* by the *loader*</param>
/// <exception cref="Exception">Excepts if Robust.Client assembly is null</exception>
public static void Boot(Assembly? robClientAssembly)
public MarseyPatcher(Assembly? robClientAssembly)
{
if (robClientAssembly == null) throw new Exception("Robust.Client was null.");

Expand All @@ -33,16 +27,29 @@ public static void Boot(Assembly? robClientAssembly)
Utility.SetupFlags();
HarmonyManager.Init(new Harmony(MarseyVars.Identifier));

// Hide the loader
Hidesey.Initialize();
}

/// <summary>
/// Boots up the patcher
///
/// Executed by the loader.
/// </summary>
/// <exception cref="Exception">Excepts if Robust.Client assembly is null</exception>
public void Boot()
{
// Preload marseypatches, if available
PreloadManager.Preload();
MarseyLogger.Log(MarseyLogger.LogType.DEBG, "Passed preload");

// Initialize subverter if enabled and present
if (MarseyVars.Subverter && Subverse.InitSubverter())
{
// Side-load custom code
Subverse.PatchSubverter();
}

MarseyLogger.Log(MarseyLogger.LogType.DEBG, "Passed subverter");
// Manage game assemblies
GameAssemblyManager.GetGameAssemblies(out Assembly? clientAss, out Assembly? robustSharedAss, out Assembly? clientSharedAss);
AssemblyFieldHandler.SetAssemblies(clientAss, robustSharedAss, clientSharedAss);
Expand Down
6 changes: 5 additions & 1 deletion Marsey/PatchAssemblyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using System.Reflection;
using Marsey.Stealthsey;
using Marsey.Subversion;
namespace Marsey;

Expand Down Expand Up @@ -70,6 +71,9 @@ public static void Initialize(Assembly assembly)


AssemblyFieldHandler.GetFields(DataType, out string name, out string description);

// Hide the assembly
Hidesey.Hide(assembly);

if (SubverterType == null)
{
Expand Down Expand Up @@ -175,7 +179,7 @@ public static class AssemblyFieldHandler
/// <summary>
/// Sets Robust.Client assembly in class
/// </summary>
public static void Init(Assembly RobustClient)
public static void Init(Assembly? RobustClient)
{
_robustAss = RobustClient;
}
Expand Down
70 changes: 70 additions & 0 deletions Marsey/Stealthsey/Hidesey.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Linq;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Loader;
using Marsey.Handbreak;
using Marsey.Subversion;

namespace Marsey.Stealthsey;

/// <summary>
/// Hides marseys from the game
/// </summary>
public static class Hidesey
{
private static List<Assembly> _hideseys = new List<Assembly>();

/// <summary>
/// Hides 0Harmony from assembly list
/// Finally, a patch loader that loads with a patch
/// </summary>
public static void Initialize()
{
Hide("0Harmony");
AppDomain.CurrentDomain.GetAssemblies();
// Is it really insane to patch system functions?
MethodInfo? target = typeof(AppDomain)
.GetMethod("GetAssemblies", BindingFlags.Public | BindingFlags.Instance);
MethodInfo? postfix =
typeof(HideseyPatches)
.GetMethod("LieLoader", BindingFlags.Public | BindingFlags.Static);

if (target == null || postfix == null) return;

Manual.ManualPostfix(target, postfix);

MarseyLogger.Log(MarseyLogger.LogType.DEBG, "Hidesey started.");
}

/// <summary>
/// Add assembly to _hideseys list
/// </summary>
/// <param name="marsey">string of assembly name</param>
public static void Hide(string marsey)
{
Assembly[] asms = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly asm in asms)
{
if (asm.FullName != null && asm.FullName.Contains(marsey))
{
Hide(asm);
return;
}
}
}

/// <summary>
/// If we have the assembly object
/// </summary>
/// <param name="marsey">marsey assembly</param>
public static void Hide(Assembly marsey)
{
_hideseys.Add(marsey);
}

public static Assembly[] LyingDomain(Assembly[] original)
{
return original.Where(assembly => !_hideseys.Contains(assembly)).ToArray();
}
}
15 changes: 15 additions & 0 deletions Marsey/Stealthsey/HideseyPatches.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Reflection;

namespace Marsey.Stealthsey;

public static class HideseyPatches
{
/// <summary>
/// This is a postfix patch which swaps an assembly list with a less honest one
/// </summary>
/// <returns></returns>
public static void LieLoader(ref Assembly[] __result)
{
__result = Hidesey.LyingDomain(__result);
}
}
3 changes: 2 additions & 1 deletion SS14.Loader/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ private bool Run()
SQLitePCL.Batteries_V2.Init();

// Start the MarseyPatcher
new Thread(() => MarseyPatcher.Boot(clientAssembly)).Start();
MarseyPatcher marsey = new MarseyPatcher(clientAssembly);
new Thread(() => marsey.Boot()).Start();

var launcher = Environment.GetEnvironmentVariable("SS14_LAUNCHER_PATH");
var redialApi = launcher != null ? new RedialApi(launcher) : null;
Expand Down

0 comments on commit 1b820a1

Please sign in to comment.