Skip to content

Commit

Permalink
Implemented Cpp2IL's StrippedCodeRegSupport plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
HerpDerpinstine committed Jul 19, 2024
1 parent 14ae678 commit 020896b
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
13. Fixed an issue with MonoMod DetourContext Disposal not working properly
14. Fixed an issue with Debugger Launch Option causing crashes
15. Fixed an issue with Console not having the Game Name and Version in the title
16. Fixed an issue with Sharing Violation during Log Initialization
17. Implemented Cpp2IL's StrippedCodeRegSupport plugin

---

Expand Down
1 change: 1 addition & 0 deletions Dependencies/Il2CppAssemblyGenerator/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class AssemblyGeneratorConfiguration
public string DeobfuscationRegex = null;
public string UnityVersion = "0.0.0.0";
public string DumperVersion = "0.0.0.0";
public string DumperSCRSVersion = "0.0.0.0";
public bool UseInterop = true;
public List<string> OldFiles = new List<string>();
}
Expand Down
5 changes: 5 additions & 0 deletions Dependencies/Il2CppAssemblyGenerator/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ internal class Core : MelonModule
internal static HttpClient webClient = null;

internal static Cpp2IL cpp2il = null;
internal static Cpp2IL_StrippedCodeRegSupport cpp2il_scrs = null;

internal static Packages.Il2CppInterop il2cppinterop = null;
internal static UnityDependencies unitydependencies = null;
internal static DeobfuscationMap deobfuscationMap = null;
Expand Down Expand Up @@ -58,6 +60,8 @@ private static int Run()
RemoteAPI.Contact();

cpp2il = new Cpp2IL();
cpp2il_scrs = new Cpp2IL_StrippedCodeRegSupport(cpp2il);

il2cppinterop = new Packages.Il2CppInterop();
unitydependencies = new UnityDependencies();
deobfuscationMap = new DeobfuscationMap();
Expand All @@ -69,6 +73,7 @@ private static int Run()
Logger.Msg($"Using Deobfuscation Regex = {(string.IsNullOrEmpty(deobfuscationRegex.Regex) ? "null" : deobfuscationRegex.Regex)}");

if (!cpp2il.Setup()
|| !cpp2il_scrs.Setup()
|| !il2cppinterop.Setup()
|| !unitydependencies.Setup()
|| !deobfuscationMap.Setup())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
<PackageReference Include="Il2CppInterop.Runtime" Version="1.4.5" ExcludeAssets="Runtime" />
<PackageReference Include="Mono.Cecil" Version="0.11.5" ExcludeAssets="Runtime" />
<PackageReference Include="AssetRipper.VersionUtilities" Version="1.5.0" ExcludeAssets="Runtime" />
<PackageReference Include="MonoMod" Version="22.7.31.1" ExcludeAssets="Runtime" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
using MelonLoader.Il2CppAssemblyGenerator.Packages.Models;
using MelonLoader.Utils;
using Mono.Cecil;
using MonoMod;
using Semver;
using System;
using System.IO;


namespace MelonLoader.Il2CppAssemblyGenerator.Packages
{
internal class Cpp2IL_StrippedCodeRegSupport : PackageBase
{
private static SemVersion _minVer = SemVersion.Parse("2022.1.0-pre-release.13");
private string _pluginsFolder;

internal class CustomMonoModder : MonoModder
{
public override void Log(string text)
{
//if (!MelonDebug.IsEnabled())
// return;
//Core.Logger.Msg(text);
}
public override void LogVerbose(string text)
{
//if (!MelonDebug.IsEnabled())
// return;
//Core.Logger.Msg(text);
}
}

internal Cpp2IL_StrippedCodeRegSupport(Cpp2IL cpp2IL)
{
Name = $"{cpp2IL.Name}.Plugin.StrippedCodeRegSupport";
Version = cpp2IL.Version;

string fileName = $"{Name}.dll";
_pluginsFolder = Path.Combine(cpp2IL.Destination, "Plugins");

FilePath =
Destination =
Path.Combine(_pluginsFolder, $"{fileName}.tmp");

URL = $"https://github.com/SamboyCoding/{cpp2IL.Name}/releases/download/{cpp2IL.Version}/{fileName}";
}

internal override bool ShouldSetup()
{
if (SemVersion.Parse(Version) < _minVer)
return false;

return !Directory.Exists(_pluginsFolder)
|| !File.Exists(FilePath)
|| string.IsNullOrEmpty(Config.Values.DumperSCRSVersion)
|| !Config.Values.DumperSCRSVersion.Equals(Version);
}

internal override bool Setup()
{
if (SemVersion.Parse(Version) < _minVer)
return true;

if (!Directory.Exists(_pluginsFolder))
Directory.CreateDirectory(_pluginsFolder);

return base.Setup();
}

internal override bool OnProcess()
{
bool wasSuccess = Generate(FilePath,
Destination.Replace(".tmp", ""),
removePatchReferences: true,
upgradeMSCORLIB: true);

if (File.Exists(FilePath))
File.Delete(FilePath);

return wasSuccess;
}

internal override void Save()
=> Save(ref Config.Values.DumperSCRSVersion);

private static bool Generate(
string pathIn,
string pathOut,
ReadingMode readingMode = ReadingMode.Deferred,
bool missingDependencyThrow = false,
bool logVerboseEnabled = false,
bool cleanupEnabled = false,
bool publicEverything = false,
bool preventInline = false,
bool strict = false,
bool removePatchReferences = false,
bool upgradeMSCORLIB = false,
bool gacEnabled = false)
{
bool success = false;
CustomMonoModder mm = null;

try
{
mm = new CustomMonoModder();
mm.InputPath = pathIn;
mm.OutputPath = pathOut;
mm.ReadingMode = readingMode;
mm.MissingDependencyThrow = missingDependencyThrow;
mm.LogVerboseEnabled = logVerboseEnabled;
mm.CleanupEnabled = cleanupEnabled;
mm.PreventInline = preventInline;
mm.Strict = strict;
mm.PublicEverything = publicEverything;
mm.RemovePatchReferences = removePatchReferences;
mm.UpgradeMSCORLIB = upgradeMSCORLIB;
mm.GACEnabled = gacEnabled;

mm.DependencyDirs.Add(MelonEnvironment.MelonManagedDirectory);
mm.DependencyDirs.Add(MelonEnvironment.UnityGameManagedDirectory);
mm.DependencyDirs.Add(MelonEnvironment.OurRuntimeDirectory);

mm.Read();

foreach (var foundRef in mm.Module.AssemblyReferences)
{
if (foundRef.Name == "System.Runtime")
foundRef.Name = "mscorlib";
foundRef.Version = new Version(0, 0, 0, 0);
}

mm.MapDependencies();
mm.Write(null, pathOut);

success = true;
}
catch (Exception ex)
{
Core.Logger.Error(ex);
success = false;
}

mm?.Dispose();
return success;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ internal class PackageBase
internal string Version;

internal virtual bool ShouldSetup() => true;
internal bool Setup()

internal virtual bool OnProcess()
=> FileHandler.Process(FilePath, Destination, MelonUtils.IsWindows ? null : Name);

internal virtual bool Setup()
{
if (string.IsNullOrEmpty(Version) || string.IsNullOrEmpty(URL))
return true;
Expand All @@ -36,7 +40,7 @@ internal bool Setup()
}

Core.Logger.Msg($"Processing {Name}...");
if (!FileHandler.Process(FilePath, Destination, MelonUtils.IsWindows ? null : Name))
if (!OnProcess())
{
ThrowInternalFailure($"Failed to Process {Name}!");
return false;
Expand Down
23 changes: 11 additions & 12 deletions MelonLoader/InternalUtils/MonoModHookGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,16 +210,7 @@ private static bool Generate(
// Remove Existing File
DeleteFile(pathOut, true);

// Create CustomMonoModder Instance with Settings and HookGen Environment Options
if (!string.IsNullOrEmpty(namespace_on))
Environment.SetEnvironmentVariable("MONOMOD_HOOKGEN_NAMESPACE", namespace_on);
if (!string.IsNullOrEmpty(namespace_il))
Environment.SetEnvironmentVariable("MONOMOD_HOOKGEN_NAMESPACE_IL", namespace_il);
if (orig)
Environment.SetEnvironmentVariable("MONOMOD_HOOKGEN_ORIG", "1");
if (privat)
Environment.SetEnvironmentVariable("MONOMOD_HOOKGEN_PRIVATE", "1");

// Create CustomMonoModder
mm = new CustomMonoModder();
mm.InputPath = pathIn;
mm.OutputPath = pathOut;
Expand All @@ -242,11 +233,19 @@ private static bool Generate(
mm.Read();
mm.MapDependencies();

// Create Hook Generator
// Create HookGenerator
string hookFileName = Path.GetFileName(pathOut);
logger.Msg($"Generating Assembly: {hookFileName}");

HookGenerator hookGen = new HookGenerator(mm, hookFileName);

if (!string.IsNullOrEmpty(namespace_on))
hookGen.Namespace = namespace_on;
if (!string.IsNullOrEmpty(namespace_il))
hookGen.NamespaceIL = namespace_il;

hookGen.HookOrig = orig;
hookGen.HookPrivate = privat;

mOut = hookGen.OutputModule;

// Assembly Caching
Expand Down

0 comments on commit 020896b

Please sign in to comment.