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

Commit

Permalink
Drist.
Browse files Browse the repository at this point in the history
  • Loading branch information
noverd committed Mar 11, 2024
1 parent ad09ab5 commit 105c418
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 116 deletions.
22 changes: 14 additions & 8 deletions Marsey/Config/MarseyConf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,38 @@ public static class MarseyConf
/// <see cref="Utility.SetupFlags"/>
/// </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;

/// <see cref="Marsey.Game.Resources.Dumper.Dumper"/>
public static bool Dumper;

/// <see cref="Jammer"/>
public static bool JamDials;

/// <see cref="Blackhole"/>
public static bool DisableREC;

/// <summary>
/// Enables backports and fixes for the game
/// </summary>
/// <see cref="Marsey.Game.Patches.Marseyports.MarseyPortMan"/>
public static bool Backports;

/// <summary>
/// Enables backports and fixes for the game
/// </summary>
public static bool SubvertPreload;

public static readonly Dictionary<string, Action<bool>> EnvVarMap = new Dictionary<string, Action<bool>>
{
{ "MARSEY_LOGGING", value => Logging = value },
Expand All @@ -75,6 +80,7 @@ public static class MarseyConf
{ "MARSEY_DUMP_ASSEMBLIES", value => Dumper = value },
{ "MARSEY_JAMMER", value => JamDials = value },
{ "MARSEY_DISABLE_REC", value => DisableREC = value },
{ "MARSEY_BACKPORTS", value => Backports = value}
{ "MARSEY_BACKPORTS", value => Backports = value},
{ "MARSEY_SUBVERTER_PRELOAD", value => SubvertPreload = value}
};
}
}
47 changes: 30 additions & 17 deletions Marsey/Subversion/Subverse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,69 +23,82 @@ namespace Marsey.Subversion;
public static class Subverse
{
private static List<string>? _subverters = null;

/// <summary>
/// Check if we have any subversions enabled
/// </summary>
public static bool CheckSubversions()
{
_subverters = Marserializer.Deserialize(new string[]{MarseyVars.MarseyPatchFolder}, Subverter.MarserializerFile);
_subverters =
Marserializer.Deserialize(new string[] { MarseyVars.MarseyPatchFolder }, Subverter.MarserializerFile);

return _subverters != null && _subverters.Count != 0;
}

/// <summary>
/// Patches subverter ahead of everything else
/// This is done as we attach to the assembly loading function
/// </summary>
public static void PatchSubverter()
{

MethodInfo? Target = Helpers.GetMethod("Robust.Shared.ContentPack.ModLoader", "TryLoadModules");
MethodInfo? Postfix = Helpers.GetMethod(typeof(Subverse), "Postfix");
MethodInfo? PatchMethod;
if (!MarseyConf.SubvertPreload)
{
MarseyLogger.Log(MarseyLogger.LogType.DEBG, "Subversion", "Start postfix patch of TryLoadModules");
PatchMethod = Helpers.GetMethod(typeof(Subverse), "Postfix");
}
else
{
MarseyLogger.Log(MarseyLogger.LogType.DEBG, "Subversion", "Start prefix patch of TryLoadModules");
PatchMethod = Helpers.GetMethod(typeof(Subverse), "Prefix");
}

if (Target != null && Postfix != null)
if (Target != null && PatchMethod != null)
{
MarseyLogger.Log(MarseyLogger.LogType.DEBG, "Subversion", $"Hooking {Target.Name} with {Postfix.Name}");
Manual.Patch(Target, Postfix, HarmonyPatchType.Postfix);
MarseyLogger.Log(MarseyLogger.LogType.DEBG, "Subversion", $"Hooking {Target.Name} with {PatchMethod.Name}");
Manual.Patch(Target, PatchMethod,
!MarseyConf.SubvertPreload ? HarmonyPatchType.Postfix : HarmonyPatchType.Prefix);

return;
}

MarseyLogger.Log(MarseyLogger.LogType.ERRO, "Subverter failed load!");
}

private static void Postfix(object __instance)
{
MarseyLogger.Log(MarseyLogger.LogType.DEBG, "Subversion", "Detour");
MethodInfo? loadGameAssemblyMethod = AccessTools.Method(AccessTools.TypeByName("Robust.Shared.ContentPack.BaseModLoader"), "InitMod");

MethodInfo? loadGameAssemblyMethod =
AccessTools.Method(AccessTools.TypeByName("Robust.Shared.ContentPack.BaseModLoader"), "InitMod");

if (loadGameAssemblyMethod == null)
{
MarseyLogger.Log(MarseyLogger.LogType.FATL, "Subversion", "Failed to find InitMod method.");
return;
}

foreach (string path in _subverters!)
{
Assembly subverterAssembly = Assembly.LoadFrom(path);
MarseyLogger.Log(MarseyLogger.LogType.DEBG, "Subversion", $"Sideloading {path}");
AssemblyFieldHandler.InitLogger(subverterAssembly, subverterAssembly.FullName);

loadGameAssemblyMethod.Invoke(__instance, new object[] { subverterAssembly });

MethodInfo? entryMethod = CheckEntry(subverterAssembly);
if (entryMethod != null)
{
Doorbreak.Enter(entryMethod);
}

Subverter.Hide(subverterAssembly);
}
}

private static MethodInfo? CheckEntry(Assembly assembly)
{
MethodInfo? entryMethod = AssemblyFieldHandler.GetEntry(assembly);
return entryMethod;
}
}
}
48 changes: 25 additions & 23 deletions SS14.Launcher/Models/Connector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -433,13 +433,13 @@ private async Task<ContentLaunchInfo> InstallContentBundleAsync(
}

Marsify(startInfo);

ConfigureEnvironmentVariables(startInfo, launchInfo, engineVersion);
ConfigureLogging(startInfo);
SetDynamicPgo(startInfo);
UnfuckGlibcLinux(startInfo);
ConfigureMultiWindow(launchInfo, startInfo);


startInfo.UseShellExecute = false;
startInfo.ArgumentList.AddRange(extraArgs);
Expand All @@ -452,48 +452,48 @@ private async Task<ContentLaunchInfo> InstallContentBundleAsync(

return process;
}

private void ConfigureEnvironmentVariables(ProcessStartInfo startInfo, ContentLaunchInfo launchInfo, string engineVersion)
{
// Set environment variables for engine modules.
foreach (var (moduleName, moduleVersion) in launchInfo.ModuleInfo)
{
if (moduleName == "Robust")
continue;

var modulePath = _engineManager.GetEngineModule(moduleName, moduleVersion);
var envVar = $"ROBUST_MODULE_{moduleName.ToUpperInvariant().Replace('.', '_')}";
startInfo.EnvironmentVariables[envVar] = modulePath;
}

// Set other necessary environment variables.
startInfo.EnvironmentVariables["SS14_DISABLE_SIGNING"] = _cfg.GetCVar(CVars.DisableSigning) ? "true" : null;
startInfo.EnvironmentVariables["DOTNET_MULTILEVEL_LOOKUP"] = "0";
}

private void ConfigureLogging(ProcessStartInfo startInfo)
{
if (!_cfg.GetCVar(CVars.LogClient)) return;

startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;

if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
startInfo.EnvironmentVariables["SS14_LOG_CLIENT"] = LauncherPaths.PathClientMacLog;
}
}

private void SetDynamicPgo(ProcessStartInfo startInfo)
{
if (!_cfg.GetCVar(CVars.DynamicPgo)) return;

Log.Debug("Dynamic PGO is enabled.");
startInfo.EnvironmentVariables["DOTNET_TieredPGO"] = "1";
startInfo.EnvironmentVariables["DOTNET_TC_QuickJitForLoops"] = "1";
startInfo.EnvironmentVariables["DOTNET_ReadyToRun"] = "0";
}

private void UnfuckGlibcLinux(ProcessStartInfo startInfo)
{
if (OperatingSystem.IsLinux())
Expand All @@ -502,20 +502,20 @@ private void UnfuckGlibcLinux(ProcessStartInfo startInfo)
startInfo.EnvironmentVariables["GLIBC_TUNABLES"] = "glibc.rtld.dynamic_sort=1";
}
}

private void SetupManualPipeLogging(Process process)
{
// Set up manual-pipe logging for new client with PID.
Log.Debug("Setting up manual-pipe logging for new client with PID {pid}.", process.Id);

var fileStdout = new FileStream(
LauncherPaths.PathClientStdoutLog,
FileMode.Create,
FileAccess.Write,
FileShare.Delete | FileShare.ReadWrite,
4096,
FileOptions.Asynchronous);

var fileStderr = new FileStream(
LauncherPaths.PathClientStderrLog,
FileMode.Create,
Expand All @@ -526,9 +526,9 @@ private void SetupManualPipeLogging(Process process)

File.Delete(LauncherPaths.PathClientStdmarseyLog);
FileStream? fileStdmarsey = null;

MarseyConf.SeparateLogger = _cfg.GetCVar(CVars.SeparateLogging);

if (MarseyConf.MarseyHide < HideLevel.Explicit)
{
fileStdmarsey = new FileStream(
Expand Down Expand Up @@ -566,12 +566,12 @@ private async Task<ProcessStartInfo> GetLoaderStartInfo(string engineVersion, lo

return startInfo;
}

private void Marsify(ProcessStartInfo startInfo)
{
Log.Debug("Preparing patch assemblies.");
FileHandler.PrepareMods();

ConfigureMarsey(startInfo);
MarseyCleanup();
}
Expand All @@ -585,7 +585,7 @@ private void ConfigureMarsey(ProcessStartInfo startInfo)
startInfo.EnvironmentVariables["MARSEY_LOADER_DEBUG"] = _cfg.GetCVar(CVars.LogLoaderDebug) ? "true" : null;
startInfo.EnvironmentVariables["MARSEY_LOGGING"] = _cfg.GetCVar(CVars.LogPatcher) ? "true" : null;
startInfo.EnvironmentVariables["MARSEY_SEPARATE_LOGGER"] = MarseyConf.SeparateLogger ? "true" : null;

// Safety
startInfo.EnvironmentVariables["MARSEY_THROW_FAIL"] = _cfg.GetCVar(CVars.ThrowPatchFail) ? "true" : null;
startInfo.EnvironmentVariables["MARSEY_HIDE_LEVEL"] = $"{_cfg.GetCVar(CVars.MarseyHide)}";
Expand All @@ -599,24 +599,26 @@ private void ConfigureMarsey(ProcessStartInfo startInfo)
startInfo.EnvironmentVariables["MARSEY_FORCINGHWID"] = "true";
startInfo.EnvironmentVariables["MARSEY_FORCEDHWID"] = MarseyGetHWID();
}

// Data
startInfo.EnvironmentVariables["MARSEY_FORKID"] = _forkid;
startInfo.EnvironmentVariables["MARSEY_ENGINE"] = _engine;

// Backporter
startInfo.EnvironmentVariables["MARSEY_BACKPORTS"] = _cfg.GetCVar(CVars.Backports) ? "true" : null;

// ResPacks
startInfo.EnvironmentVariables["MARSEY_DISABLE_STRICT"] = _cfg.GetCVar(CVars.DisableStrict) ? "true" : null;
if (MarseyConf.Dumper)
startInfo.EnvironmentVariables["MARSEY_DUMP_ASSEMBLIES"] = "true";
// Subverter Preload
startInfo.EnvironmentVariables["MARSEY_SUBVERTER_PRELOAD"] = _cfg.GetCVar(CVars.SubverterPreload) ? "true" : null;
}

private string MarseyGetHWID()
{
string forcedHWID = _cfg.GetCVar(CVars.ForcedHWId);
if (_cfg.GetCVar(CVars.RandHWID))
if (_cfg.GetCVar(CVars.RandHWID))
{
forcedHWID = HWID.GenerateRandom(32);
}
Expand Down
Loading

0 comments on commit 105c418

Please sign in to comment.