Skip to content

Commit

Permalink
cleanup and support icc mods
Browse files Browse the repository at this point in the history
  • Loading branch information
xKiraiChan committed Dec 3, 2021
1 parent 6530cd4 commit ba214a5
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 89 deletions.
103 changes: 14 additions & 89 deletions AstralBypass.cs
Original file line number Diff line number Diff line change
@@ -1,104 +1,29 @@
using MelonLoader;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

[assembly: MelonInfo(typeof(Astrum.AstralBypass), nameof(Astrum.AstralBypass), "0.1.0", downloadLink: "github.com/Astrum-Project/" + nameof(Astrum.AstralBypass))]
[assembly: MelonInfo(typeof(Astrum.AstralBypass), nameof(Astrum.AstralBypass), "0.1.1", downloadLink: "github.com/Astrum-Project/" + nameof(Astrum.AstralBypass))]
[assembly: MelonColor(ConsoleColor.DarkMagenta)]

namespace Astrum
{
public class AstralBypass : MelonPlugin
public partial class AstralBypass : MelonPlugin
{
private const string pluginsPath = "Plugins/AstralBypass.dll";
private const string modsPath = "Mods/AstralBypass.dll";
private const int renameExitStatus = 69_8153697; // random number, but the 69 should indicate that it's human written;
private const BindingFlags PrivateStatic = BindingFlags.NonPublic | BindingFlags.Static;

private static IntPtr pIC = IntPtr.Zero;
static AstralBypass() => PEBKACHelper.CheckForUserError();

static AstralBypass()
{
CheckLocation();

FindIC();
}

private static void CheckLocation()
{
if (File.Exists(pluginsPath)) return;

if (File.Exists(modsPath)) MoveToPlugins(modsPath);

// since people love renaming their dlls for some reason
string plugin = Directory.EnumerateFiles("Plugins")
.Where(x => x.ToLower().EndsWith(".dll"))
.Where(x => new FileInfo(x).Length < 100_000) // 100kb
.FirstOrDefault(x => File.ReadAllText(x).Contains("AstralBypass.pdb"));

if (plugin != null) MoveToPlugins(plugin);

string mod = Directory.EnumerateFiles("Mods")
.Where(x => x.ToLower().EndsWith(".dll"))
.Where(x => new FileInfo(x).Length < 100_000)
.FirstOrDefault(x => File.ReadAllText(x).Contains("AstralBypass.pdb"));

if (mod != null) MoveToPlugins(mod);

MessageBox.Show("Failed to locate AstralBypass.\nPlease place \"AstralBypass.dll\" into your plugins folder.\nThe game has been halted for you to do this.", "Problems loading AstralBypass");
Environment.Exit(renameExitStatus);
}

private static void MoveToPlugins(string orig)
{
Console.Beep();
Console.Beep();
Console.Beep();
MessageBox.Show("AstralBypass was in the wrong folder or incorrectly named.\nIt will now be relocated to your plugins folder", "Restarting game...");

if (File.Exists(pluginsPath))
File.Delete(pluginsPath);
File.Move(orig, pluginsPath);

Environment.Exit(renameExitStatus);
}

private static void FindIC()
{
// TODO: add other ML versions to this
pIC = AstralCore.Utils.PatternScanner.Scan(
"bootstrap.dll",
"66 0F 2F C7" + // comisd xmm0, xmm7
"77 1E" + // ja bootstrap.7FFF9D5688D6
"66 0F 2F ?? ?? ?? ?? ??" + // comisd xmm7,qword ptr ds:[7FFF9D583EC8]
"77 14" + // ja bootstrap.7FFF9D5688D6
"48 8D 4D B0" + // lea rcx,qword ptr ss:[rbp-50]
"E8 ?? ?? ?? ??" + // call bootstrap.7FFF9D567AF0
"49 8B CF" + // mov rcx,r15
"FF 15 ?? ?? ?? ??" + // call qword ptr ds:[< &mono_image_close >]
"EB 2E" + // jmp bootstrap.7FFF9D568904
"48 8D 4D B0" + // lea rcx,qword ptr ss:[rbp-50]
"E8 ?? ?? ?? ??" + // call bootstrap.7FFF9D567AF0
"49 8B CF" + // mov rcx,r15
"FF 15 ?? ?? ?? ??" + // call qword ptr ds:[< &mono_image_close >]
"48 8B ?? ?? ?? ?? ??" + // mov rbx, qword ptr ds:[< &mono_raise_exception >]
"48 8B ?? ?? ?? ?? ??" + // mov rax, qword ptr ds:[< &mono_get_exception_bad_image_format >]
"48 8D ?? ?? ?? ?? ??" + // lea rcx, qword ptr ds:[7FFF9D5820B8]
"FF D0" + // call rax
"48 8B C8" + // mov rcx, rax
"FF D3", // call rbx ; <== PATCHING THIS
80
public override void OnApplicationEarlyStart() =>
HarmonyInstance.Patch(
typeof(Assembly).GetMethod(nameof(Assembly.Load), new Type[2] { typeof(byte[]), typeof(byte[]) }),
typeof(AstralBypass).GetMethod(nameof(AssemblyLoadPre), PrivateStatic).ToNewHarmonyMethod(),
typeof(AstralBypass).GetMethod(nameof(AssemblyLoadPost), PrivateStatic).ToNewHarmonyMethod()
);

if (pIC == IntPtr.Zero) AstralCore.Logger.Error("Failed to find Integrity Check");
else BypassIC();
}

public static void BypassIC() => AstralCore.Utils.MemoryUtils.WriteBytes(pIC, new byte[2] { 0x90, 0x90 });
public static void RepairIC() => AstralCore.Utils.MemoryUtils.WriteBytes(pIC, new byte[2] { 0xFF, 0xD3 });
private static void AssemblyLoadPre() => IntegrityChecks.Bypass();
private static void AssemblyLoadPost() => IntegrityChecks.Repair();

// if this bypass breaks, open an issue on the github
// i have a more aggressive fix than this non-intrusive one
}
}
2 changes: 2 additions & 0 deletions AstralBypass.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
</ItemGroup>
<ItemGroup>
<Compile Include="AstralBypass.cs" />
<Compile Include="IntegrityChecks.cs" />
<Compile Include="PEBKACHelper.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
Expand Down
37 changes: 37 additions & 0 deletions IntegrityChecks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;

namespace Astrum
{
partial class AstralBypass
{
public static class IntegrityChecks
{
private static IntPtr pIC = AstralCore.Utils.PatternScanner.Scan(
"bootstrap.dll",
"66 0F 2F C7" + // comisd xmm0, xmm7
"77 1E" + // ja bootstrap.7FFF9D5688D6
"66 0F 2F ?? ?? ?? ?? ??" + // comisd xmm7,qword ptr ds:[7FFF9D583EC8]
"77 14" + // ja bootstrap.7FFF9D5688D6
"48 8D 4D B0" + // lea rcx,qword ptr ss:[rbp-50]
"E8 ?? ?? ?? ??" + // call bootstrap.7FFF9D567AF0
"49 8B CF" + // mov rcx,r15
"FF 15 ?? ?? ?? ??" + // call qword ptr ds:[< &mono_image_close >]
"EB 2E" + // jmp bootstrap.7FFF9D568904
"48 8D 4D B0" + // lea rcx,qword ptr ss:[rbp-50]
"E8 ?? ?? ?? ??" + // call bootstrap.7FFF9D567AF0
"49 8B CF" + // mov rcx,r15
"FF 15 ?? ?? ?? ??" + // call qword ptr ds:[< &mono_image_close >]
"48 8B ?? ?? ?? ?? ??" + // mov rbx, qword ptr ds:[< &mono_raise_exception >]
"48 8B ?? ?? ?? ?? ??" + // mov rax, qword ptr ds:[< &mono_get_exception_bad_image_format >]
"48 8D ?? ?? ?? ?? ??" + // lea rcx, qword ptr ds:[7FFF9D5820B8]
"FF D0" + // call rax
"48 8B C8" + // mov rcx, rax
"FF D3", // call rbx ; <== PATCHING THIS
80
);

public static void Bypass() => AstralCore.Utils.MemoryUtils.WriteBytes(pIC, new byte[2] { 0x90, 0x90 });
public static void Repair() => AstralCore.Utils.MemoryUtils.WriteBytes(pIC, new byte[2] { 0xFF, 0xD3 });
}
}
}
56 changes: 56 additions & 0 deletions PEBKACHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace Astrum
{
partial class AstralBypass
{
public static class PEBKACHelper
{
private const string pluginsPath = "Plugins/AstralBypass.dll";
private const string modsPath = "Mods/AstralBypass.dll";
private const int renameExitStatus = 69_8153697; // random number, but the 69 should indicate that it's human written;

public static void CheckForUserError()
{
if (File.Exists(pluginsPath)) return;

if (File.Exists(modsPath)) MoveToPlugins(modsPath);

// since people love renaming their dlls for some reason
string plugin = Directory.EnumerateFiles("Plugins")
.Where(x => x.ToLower().EndsWith(".dll"))
.Where(x => new FileInfo(x).Length < 100_000) // 100kb
.FirstOrDefault(x => File.ReadAllText(x).Contains("AstralBypass.pdb"));

if (plugin != null) MoveToPlugins(plugin);

string mod = Directory.EnumerateFiles("Mods")
.Where(x => x.ToLower().EndsWith(".dll"))
.Where(x => new FileInfo(x).Length < 100_000)
.FirstOrDefault(x => File.ReadAllText(x).Contains("AstralBypass.pdb"));

if (mod != null) MoveToPlugins(mod);

MessageBox.Show("Failed to locate AstralBypass.\nPlease place \"AstralBypass.dll\" into your plugins folder.\nThe game has been halted for you to do this.", "Problems loading AstralBypass");
Environment.Exit(renameExitStatus);
}

private static void MoveToPlugins(string orig)
{
Console.Beep();
Console.Beep();
Console.Beep();
MessageBox.Show("AstralBypass was in the wrong folder or incorrectly named.\nIt will now be relocated to your plugins folder", "Restarting game...");

if (File.Exists(pluginsPath))
File.Delete(pluginsPath);
File.Move(orig, pluginsPath);

Environment.Exit(renameExitStatus);
}
}
}
}

0 comments on commit ba214a5

Please sign in to comment.