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

Commit

Permalink
Merge pull request #10 from ValidHunters/ResSwap
Browse files Browse the repository at this point in the history
Prep ResPacks
  • Loading branch information
misandrie authored Jan 27, 2024
2 parents e4f07c4 + 8fedd52 commit 2b66740
Show file tree
Hide file tree
Showing 26 changed files with 426 additions and 124 deletions.
9 changes: 7 additions & 2 deletions Marsey/Config/MarseyConf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,14 @@ public static class MarseyConf
/// </summary>
public static bool ThrowOnFail;

/// <summary>
/// Disable strict fork checks when applying resource packs.
/// </summary>
public static bool DisableResPackStrict;

/// <see cref="HWID"/>
public static bool ForceHWID;



/// <see cref="DiscordRPC"/>
public static bool KillRPC;

Expand All @@ -59,6 +63,7 @@ public static class MarseyConf
{ "MARSEY_LOADER_DEBUG", value => DebugAllowed = value },
{ "MARSEY_THROW_FAIL", value => ThrowOnFail = value },
{ "MARSEY_SEPARATE_LOGGER", value => SeparateLogger = value },
{ "MARSEY_DISABLE_STRICT", value => DisableResPackStrict = value},
{ "MARSEY_FORCINGHWID", value => ForceHWID = value },
{ "MARSEY_DISABLE_PRESENCE", value => KillRPC = value },
{ "MARSEY_DUMP_ASSEMBLIES", value => Dumper = value },
Expand Down
18 changes: 15 additions & 3 deletions Marsey/Config/MarseyVars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,29 @@ public static class MarseyVars
public static readonly int LoopCooldown = 200;

/// <summary>
/// Marseypatches are saved to this one
/// Name of folder containing files used by Marsey
/// </summary>
public static readonly string MarseyPatchFolder = "Marsey";
public static readonly string MarseyFolder = "Marsey";

/// <summary>
/// Folder containing mods
/// </summary>
public static readonly string MarseyPatchFolder = Path.Combine(MarseyFolder, "Mods");

/// <summary>
/// Folder containing Resource Packs
/// </summary>
public static readonly string MarseyResourceFolder = Path.Combine(MarseyFolder, "ResourcePacks");

/// <summary>
/// Log identified for marseyloader
/// </summary>
public static readonly string MarseyLoggerPrefix = "MARSEY";

public static readonly string MarseyLoggerFileName = "client.marsey.log";

/// <summary>
/// Refuse to play on servers over this engine version if hidesey is disabled
/// Refuse to play on servers over or equal to this engine version if hidesey is disabled
/// <seealso cref="Abjure"/>
/// </summary>
public static readonly Version Detection = new Version("183.0.0");
Expand Down
9 changes: 3 additions & 6 deletions Marsey/Game/Patches/Dumper/Dumper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Reflection;
using Marsey.Stealthsey;
using Marsey.Game.ResourcePack;

namespace Marsey.Game.Patches;

Expand All @@ -8,27 +8,24 @@ namespace Marsey.Game.Patches;
/// </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 fork = ResMan.GetForkID() ?? "marsey";

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

private static void Patch()
{
_res.Patch();
ResourceDumper.Patch();
}
}
9 changes: 4 additions & 5 deletions Marsey/Game/Patches/Dumper/Resource/ResDumpPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ private static void PostfixCFF(ref object __instance, ref dynamic __result)
{
if (ResourceDumper.CFRMi == null) return; // If FileReader handle is not available, return

FileHandler.CheckRenameDirectory(Dumper.path);

foreach (dynamic file in __result)
foreach (dynamic file in __result)
{
string canonPath = Dumper.path.StartsWith($"/") ? Dumper.path[1..] : Dumper.path;
string fullpath = Path.Combine(Dumper.path, canonPath);
string canonPath = file.CanonPath;
string fixedCanonPath = canonPath.StartsWith($"/") ? canonPath[1..] : canonPath;
string fullpath = Path.Combine(Dumper.path, fixedCanonPath);

FileHandler.CreateDir(fullpath);

Expand Down
23 changes: 12 additions & 11 deletions Marsey/Game/Patches/Dumper/Resource/ResourceDumper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Reflection;
using HarmonyLib;
using Marsey.Game.ResourcePack;
using Marsey.Game.ResourcePack.Reflection;
using Marsey.Handbreak;
using Marsey.Misc;

Expand All @@ -8,35 +10,34 @@ namespace Marsey.Game.Patches;
/// <summary>
/// By complete accident this dumps everything
/// </summary>
public class ResourceDumper
public static class ResourceDumper
{
public static MethodInfo? CFRMi;
public void Patch()
public static 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)
FileHandler.CheckRenameDirectory(Dumper.path);

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

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

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

Helpers.PatchMethod(
targetType: ProtoMan,
targetType: ResourceTypes.ProtoMan,
targetMethodName: "ContentFindFiles",
patchType: typeof(ResDumpPatches),
patchMethodName: "PostfixCFF",
patchingType: HarmonyPatchType.Postfix,
new Type[]{ ResPath }
new Type[]{ ResourceTypes.ResPath }
);
}
}
9 changes: 9 additions & 0 deletions Marsey/Game/ResourcePack/Reflection/Patches.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Marsey.Game.ResourcePack.Reflection;

public static class Patches
{
private static bool PrefixCFF(ref object __instance, ref dynamic path)
{
return true;
}
}
18 changes: 18 additions & 0 deletions Marsey/Game/ResourcePack/Reflection/ResourceTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Marsey.Handbreak;

namespace Marsey.Game.ResourcePack.Reflection;

/// <summary>
/// Holds reflection data related to Resources
/// </summary>
public static class ResourceTypes
{
public static Type? ProtoMan { get; private set; }
public static Type? ResPath { get; private set; }

public static void Initialize()
{
ProtoMan = Helpers.TypeFromQualifiedName("Robust.Shared.ContentPack.ResourceManager");
ResPath = Helpers.TypeFromQualifiedName("Robust.Shared.Utility.ResPath");
}
}
78 changes: 78 additions & 0 deletions Marsey/Game/ResourcePack/ResMan.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using Marsey.Config;
using Marsey.Game.ResourcePack.Reflection;
using Marsey.Misc;
using Marsey.Serializer;
using Marsey.Stealthsey;

namespace Marsey.Game.ResourcePack;

public static class ResMan
{
public static readonly string MarserializerFile = "rpacks.marsey";
private static readonly List<ResourcePack> _resourcePacks = [];
private static string? _fork;
private static readonly string _forkEnvVar = "MARSEY_FORKID";

/// <summary>
/// Executed by the loader
/// </summary>
public static void Initialize()
{
ResourceTypes.Initialize();

_fork = Environment.GetEnvironmentVariable(_forkEnvVar) ?? "marsey";
Envsey.CleanFlag(_forkEnvVar);

List<string> enabledPacks = Marserializer.Deserialize([MarseyVars.MarseyResourceFolder], MarserializerFile) ?? [];
foreach (string dir in enabledPacks)
{
InitializeRPack(dir, !MarseyConf.DisableResPackStrict);
}
}

/// <summary>
/// Executed by the launcher
/// </summary>
public static void LoadDir()
{
_resourcePacks.Clear();
string[] subDirs = Directory.GetDirectories(MarseyVars.MarseyResourceFolder);
foreach (string subdir in subDirs)
{
InitializeRPack(subdir);
}
}

/// <summary>
/// Creates a ResourcePack object from a given path to a directory
/// </summary>
/// <param name="path">resource pack directory</param>
/// <param name="strict">match fork id</param>
private static void InitializeRPack(string path, bool strict = false)
{
ResourcePack rpack = new ResourcePack(path);

try
{
rpack.ParseMeta();
}
catch (RPackException e)
{
MarseyLogger.Log(MarseyLogger.LogType.FATL, e.ToString());
return;
}

AddRPack(rpack, strict);
}

private static void AddRPack(ResourcePack rpack, bool strict)
{
if (_resourcePacks.Any(rp => rp.Dir == rpack.Dir)) return;
if (strict && rpack.Target != _fork && rpack.Target != "") return;

_resourcePacks.Add(rpack);
}

public static List<ResourcePack> GetRPacks() => _resourcePacks;
public static string? GetForkID() => _fork;
}
33 changes: 33 additions & 0 deletions Marsey/Game/ResourcePack/ResourcePack.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Marsey.Misc;
using Newtonsoft.Json;

namespace Marsey.Game.ResourcePack;

/// <summary>
/// Metadata class for Resource Packs
/// </summary>
public class ResourcePack(string dir)
{
public string Dir { get; } = dir;
public string? Name { get; private set; }
public string? Desc { get; private set; }
public string? Target { get; private set; } // Specify fork for which this is used
public bool Enabled { get; set; }

public void ParseMeta()
{
string metaPath = Path.Combine(Dir, "meta.json");
if (!File.Exists(metaPath))
throw new RPackException($"Found folder {Dir}, but it didn't have a meta.json");

string jsonData = File.ReadAllText(metaPath);
dynamic? meta = JsonConvert.DeserializeObject(jsonData);

if (meta == null || meta?.Name == null || meta?.Description == null || meta?.Target == null)
throw new RPackException("Meta.json is incorrectly formatted.");

Name = meta?.Name ?? string.Empty;
Desc = meta?.Description ?? string.Empty;
Target = meta?.Target ?? string.Empty;
}
}
30 changes: 30 additions & 0 deletions Marsey/Game/ResourcePack/RessourceSwapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Reflection;
using HarmonyLib;
using Marsey.Game.ResourcePack.Reflection;

namespace Marsey.Game.ResourcePack;

public static class ResourceSwapper
{
private static List<string> filepaths = [];

public static void Start()
{
List<ResourcePack> RPacks = ResMan.GetRPacks();
foreach (ResourcePack rpack in RPacks)
PopulateFiles(rpack.Dir);
}

private static void PopulateFiles(string directory)
{
string[] files = Directory.GetFiles(directory, "*", SearchOption.AllDirectories);
foreach (string file in files)
{
if (file.EndsWith("meta.json")) continue;
string relativePath = file.Replace(directory + Path.DirectorySeparatorChar, "");
filepaths.Add(relativePath);
}
}

public static List<string> ResourceFiles() => filepaths;
}
8 changes: 0 additions & 8 deletions Marsey/Handbreak/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,4 @@ private static void LogPatchSuccess(HarmonyPatchType patchingType, string target
{
MarseyLogger.Log(MarseyLogger.LogType.DEBG, "Handbreak", $"{patchingType}: Patched {targetMethodName} with {patchMethodName}.");
}

public static void HandleFail(string methodName, MethodInfo? method)
{
string message = $"Failed to patch {methodName} using {method?.Name}";
if (MarseyConf.ThrowOnFail)
throw new HideseyException(message);
MarseyLogger.Log(MarseyLogger.LogType.FATL, message);
}
}
3 changes: 1 addition & 2 deletions Marsey/Handbreak/Manual.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@ public static bool Patch(MethodInfo? method, MethodInfo? patch, HarmonyPatchType
catch (Exception e)
{
string message = $"Encountered an issue with patching {method?.Name} against {patch?.Name}!\n{e}";
MarseyLogger.Log(MarseyLogger.LogType.ERRO, "HandBreak", message);

if (MarseyConf.ThrowOnFail)
throw new HandBreakException(message);

MarseyLogger.Log(MarseyLogger.LogType.ERRO, "HandBreak", message);

return false;
}

Expand Down
6 changes: 5 additions & 1 deletion Marsey/MarseyPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Marsey.Game.Managers;
using Marsey.Game.Misc;
using Marsey.Game.Patches;
using Marsey.Game.ResourcePack;
using Marsey.PatchAssembly;
using Marsey.Patches;
using Marsey.Stealthsey;
Expand Down Expand Up @@ -79,7 +80,10 @@ public void Preload()
// If set - Disable redialing and remote command execution
Jammer.Patch();
Blackhole.Patch();


// Prep Resource Manager
ResMan.Initialize();

// Dump the game, if enabled
if (MarseyConf.Dumper)
Dumper.Start();
Expand Down
Loading

0 comments on commit 2b66740

Please sign in to comment.