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

Commit

Permalink
Dumper rework
Browse files Browse the repository at this point in the history
  • Loading branch information
misandrie committed Jan 26, 2024
1 parent a27b378 commit 55f0c6d
Show file tree
Hide file tree
Showing 31 changed files with 342 additions and 144 deletions.
2 changes: 1 addition & 1 deletion Marsey.Tests/Hidesey/HideseyTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Reflection;
using HarmonyLib;
using Marsey.GameAssembly;
using Marsey.Game.Managers;
using Marsey.Stealthsey;

namespace Marsey.HideseyTests;
Expand Down
3 changes: 2 additions & 1 deletion Marsey.Tests/Hidesey/RedialTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System.Reflection;
using System.Reflection.Emit;
using HarmonyLib;
using Marsey.GameAssembly;
using Marsey.Game;
using Marsey.Game.Managers;
using Marsey.Stealthsey;

namespace Marsey.HideseyTests;
Expand Down
8 changes: 4 additions & 4 deletions Marsey/Config/MarseyConf.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Marsey.GameAssembly.Patches;
using Marsey.Game.Patches;
using Marsey.Misc;
using Marsey.Stealthsey;

Expand Down Expand Up @@ -44,8 +44,8 @@ public static class MarseyConf
/// <see cref="DiscordRPC"/>
public static bool KillRPC;

/// <see cref="Dumper"/>
public static bool DumpAssemblies;
/// <see cref="Game.Patches.Dumper"/>
public static bool Dumper;

/// <see cref="Jammer"/>
public static bool JamDials;
Expand All @@ -61,7 +61,7 @@ public static class MarseyConf
{ "MARSEY_SEPARATE_LOGGER", value => SeparateLogger = value },
{ "MARSEY_FORCINGHWID", value => ForceHWID = value },
{ "MARSEY_DISABLE_PRESENCE", value => KillRPC = value },
{ "MARSEY_DUMP_ASSEMBLIES", value => DumpAssemblies = value },
{ "MARSEY_DUMP_ASSEMBLIES", value => Dumper = value },
{ "MARSEY_JAMMER", value => JamDials = value },
{ "MARSEY_DISABLE_REC", value => DisableREC = value }
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
using System;
using System.Reflection;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using Marsey.Config;
using Marsey.Game.Misc;
using Marsey.Misc;

namespace Marsey.GameAssembly;
namespace Marsey.Game.Managers;

public static class GameAssemblyManager
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using HarmonyLib;
using Marsey.Config;

namespace Marsey.GameAssembly;
namespace Marsey.Game.Managers;

public static class HarmonyManager
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Reflection;

namespace Marsey.GameAssembly;
namespace Marsey.Game.Misc;

/// <summary>
/// Manages MarseyEntry
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Reflection;
using Marsey.Game.Managers;

namespace Marsey.GameAssembly;
namespace Marsey.Game.Misc;

/// <summary>
/// Bagholds game assemblies
Expand Down
35 changes: 35 additions & 0 deletions Marsey/Game/Misc/Preclusion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Marsey.Misc;

namespace Marsey.Game.Misc;

/// <summary>
/// Closes the game right before content pack is started.
/// EntryPoint of the content pack is assumed to be the start.
/// <seealso cref="Game.Patches.Sentry"/>
/// </summary>
public static class Preclusion
{
private static bool _flag = false;

/// <summary>
/// Signal to marseyloader to close the game before the game starts
/// </summary>
public static void Trigger(string reason)
{
if (_flag)
{
MarseyLogger.Log(MarseyLogger.LogType.INFO, "Preclusion", $"Preclusion was triggered more than once, reason: {reason}");
return;
}

MarseyLogger.Log(MarseyLogger.LogType.INFO, "Preclusion", $"Stopping content boot, reason: {reason}");
_flag = true;
}

public static bool State => _flag;

public static void Fire()
{
Environment.Exit(0);
}
}
8 changes: 5 additions & 3 deletions Marsey/GameAssembly/GamePatcher.cs → Marsey/Game/Patcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
using System.Reflection;
using HarmonyLib;
using Marsey.Config;
using Marsey.Game.Managers;
using Marsey.Game.Misc;
using Marsey.Patches;
using Marsey.Misc;

namespace Marsey.GameAssembly;
namespace Marsey.Game;

public static class GamePatcher
public static class Patcher
{
/// <summary>
/// Patches the game using assemblies in List.
Expand All @@ -24,7 +26,7 @@ public static void Patch<T>(List<T> patchlist) where T : IPatch
}
}

/// <inheritdoc cref="GamePatcher.Patch"/>
/// <inheritdoc cref="Patcher.Patch"/>
private static void PatchAssembly<T>(Harmony harmony, T patch) where T : IPatch
{
AssemblyName assemblyName = patch.Asm.GetName();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System.Reflection;
using HarmonyLib;
using Marsey.Config;
using Marsey.Handbreak;
using Marsey.Misc;

namespace Marsey.GameAssembly.Patches;
namespace Marsey.Game.Patches;

/// <summary>
/// Whitelists command execution that is dependent on server
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using System.Reflection;
using HarmonyLib;
using Marsey.Config;
using Marsey.Handbreak;
using Marsey.Misc;
using Marsey.Stealthsey;

namespace Marsey.GameAssembly.Patches;
namespace Marsey.Game.Patches;

public static class DiscordRPC
{
Expand Down
34 changes: 34 additions & 0 deletions Marsey/Game/Patches/Dumper/Assembly/AsmDumpPatches.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Reflection;
using Marsey.Misc;
using Marsey.Stealthsey;

namespace Marsey.Game.Patches;

static class AsmDumpPatches
{
private static short _counter;

/// <summary>
/// Intercept assemblies, save to disk
/// </summary>
// ReSharper disable once UnusedMember.Local
private static bool LGAPrefix(ref Stream assembly)
{
FileHandler.SaveAssembly(Dumper.path, $"{_counter}.dll", assembly);
_counter++;

return true;
}

/// <summary>
/// Assemblies finished loading, close the game
/// </summary>
// ReSharper disable once UnusedMember.Local
private static void TLMPostfix()
{
MarseyLogger.Log(MarseyLogger.LogType.DEBG, "Dumper", $"Dumps saved to {Dumper.path}");
MarseyLogger.Log(MarseyLogger.LogType.INFO, "Dumper", $"Exited with {_counter} assemblies.");
Environment.Exit(0);
}

}
39 changes: 39 additions & 0 deletions Marsey/Game/Patches/Dumper/Assembly/AssemblyDumper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using HarmonyLib;
using Marsey.Config;
using Marsey.Handbreak;
using Marsey.Misc;

namespace Marsey.Game.Patches;

/// <summary>
/// Deprecated by accident, ResourceDumper does this and better
/// <see cref="ResourceDumper"/>
/// </summary>
public class AssemblyDumper
{
public void Patch()
{
Type? ModLoader = Helpers.TypeFromQualifiedName("Robust.Shared.ContentPack.ModLoader");

if (ModLoader == null)
{
MarseyLogger.Log(MarseyLogger.LogType.ERRO, "ModLoader is null.");
return;
}

MarseyLogger.Log(MarseyLogger.LogType.DEBG, "Dumper", "Patching TLM");
Helpers.PatchMethod(ModLoader,
"TryLoadModules",
typeof(AsmDumpPatches),
"TLMPostfix",
HarmonyPatchType.Postfix);

MarseyLogger.Log(MarseyLogger.LogType.DEBG, "Dumper", "Patching LGA");
Helpers.PatchMethod(ModLoader,
"LoadGameAssembly",
typeof(AsmDumpPatches),
"LGAPrefix",
HarmonyPatchType.Prefix,
new[] { typeof(Stream), typeof(Stream), typeof(bool) });
}
}
34 changes: 34 additions & 0 deletions Marsey/Game/Patches/Dumper/Dumper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Reflection;
using Marsey.Stealthsey;

namespace Marsey.Game.Patches;

/// <summary>
/// Dumps game content
/// </summary>
public static class Dumper
{
private static ResourceDumper _res = new ResourceDumper();
public static string path = "marsey";

public static void Start()
{
//Preclusion.Trigger("Dumper started.");
GetExactPath();
Patch();
}

private static void GetExactPath()
{
string fork = Environment.GetEnvironmentVariable("MARSEY_DUMP_FORKID") ?? "marsey";
Envsey.CleanFlag(fork);

string loc = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!;
path = Path.Combine(loc, "Dumper/", $"{fork}/");
}

private static void Patch()
{
_res.Patch();
}
}
42 changes: 42 additions & 0 deletions Marsey/Game/Patches/Dumper/Resource/ResDumpPatches.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Diagnostics;
using Marsey.Misc;

namespace Marsey.Game.Patches;

static class ResDumpPatches
{
// Okay so this is so schizo I will comment every line from memory
public static void PostfixCFF(ref object __instance, ref dynamic __result)
{
if (ResourceDumper.CFRMi == null) return; // Do we have a handle on FileReader?

foreach (dynamic file in __result) // Since we're patching FindFiles were receiving the full list of resources
{
string canonPath = file.CanonPath; // We get the entire path
if (canonPath.StartsWith("/")) // And remove the first slash else were writing to root and fuck that
{
canonPath = canonPath.Substring(1);
}

string fullpath = Path.Combine(Dumper.path, canonPath);

// Create directory if it doesn't exist
string? directoryName = Path.GetDirectoryName(fullpath);
if (directoryName != null && !Directory.Exists(directoryName))
{
Directory.CreateDirectory(directoryName);
}

// This is where we get the actual file - CFR stands for ContentFileRead
// Since its not a static method we use __instance as the called, which is very convenient, thanks PJB!
object? CFRout = ResourceDumper.CFRMi.Invoke(__instance, new object?[] {file});

if (CFRout is not MemoryStream stream) return; // Cast as MemoryStream (CFROut is MemoryStream as is but whatever

// Save the thing
using FileStream st = new FileStream(fullpath, FileMode.Create, FileAccess.Write);
MarseyLogger.Log(MarseyLogger.LogType.DEBG, $"Saving to {fullpath}");
stream.WriteTo(st);
}
}
}
42 changes: 42 additions & 0 deletions Marsey/Game/Patches/Dumper/Resource/ResourceDumper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Reflection;
using HarmonyLib;
using Marsey.Handbreak;
using Marsey.Misc;

namespace Marsey.Game.Patches;

/// <summary>
/// By complete accident this dumps everything
/// </summary>
public class ResourceDumper
{
public static MethodInfo? CFRMi;
public void Patch()
{
Type? ProtoMan = Helpers.TypeFromQualifiedName("Robust.Shared.ContentPack.ResourceManager");
Type? ResPath = Helpers.TypeFromQualifiedName("Robust.Shared.Utility.ResPath");

CFRMi = AccessTools.Method(ProtoMan, "ContentFileRead", new[] { ResPath });

if (ProtoMan == null)
{
MarseyLogger.Log(MarseyLogger.LogType.ERRO, "PrototypeManager is null.");
return;
}

if (ResPath == null)
{
MarseyLogger.Log(MarseyLogger.LogType.ERRO, "ResPath is null.");
return;
}

Helpers.PatchMethod(
targetType: ProtoMan,
targetMethodName: "ContentFindFiles",
patchType: typeof(ResDumpPatches),
patchMethodName: "PostfixCFF",
patchingType: HarmonyPatchType.Postfix,
new Type[]{ ResPath }
);
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
using System;
using System.Text;
using System.Reflection;
using HarmonyLib;
using Marsey.Config;
using Marsey.Handbreak;
using Marsey.Misc;
using Marsey.PatchAssembly;
using Marsey.Stealthsey;
using Marsey.Stealthsey.Reflection;

namespace Marsey.GameAssembly.Patches;
namespace Marsey.Game.Patches;

/// <summary>
/// Manages HWId variable given to the game.
Expand Down
Loading

0 comments on commit 55f0c6d

Please sign in to comment.