Skip to content

Commit

Permalink
Fixed Load Order
Browse files Browse the repository at this point in the history
  • Loading branch information
HerpDerpinstine committed Feb 5, 2025
1 parent 1ab17d7 commit d1c65fb
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 76 deletions.
19 changes: 12 additions & 7 deletions Dependencies/Modules/Engines/Unity/Engine.Unity/UnityLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ public override bool Validate()
return false;
}

public override void Stage2()
public override void Stage3(string supportModulePath)
{
if (!IsIl2Cpp)
{
// Run Stage3
base.Stage3(supportModulePath);
return;
}

string genBasePath = Path.Combine(LoaderPath, "Il2CppAssemblyGenerator");
if (!Directory.Exists(genBasePath))
Expand All @@ -50,9 +54,6 @@ public override void Stage2()
Il2CppInteropFixes.Install(genOutputPath);
Il2CppICallInjector.Install();

// Run Stage2
base.Stage2();

// Generate Il2Cpp Wrapper Assemblies
try
{
Expand All @@ -77,8 +78,8 @@ public override void Stage2()
return;
}

// Il2Cpp Games just run Stage3 after Assembly Generation
Stage3(SupportModulePath);
// Run Stage3 after Assembly Generation
base.Stage3(supportModulePath);
}

public override void Initialize()
Expand All @@ -104,11 +105,15 @@ public override void Initialize()

SetEngineInfo("Unity", UnityInformationHandler.EngineVersion.ToStringWithoutType(), indentifier);
SetApplicationInfo(UnityInformationHandler.GameDeveloper, UnityInformationHandler.GameName, UnityInformationHandler.GameVersion);
PrintAppInfo();

if (IsIl2Cpp)
{
// Run Stage2
Stage2();

// Initialize Il2Cpp Loader
Il2CppLoader.Initialize(this, new(GameAssemblyPath,
Il2CppLoader.Initialize(this, new(GameAssemblyPath, SupportModulePath,
[
"Internal_ActiveSceneChanged",
"UnityEngine.ISerializationCallbackReceiver.OnAfterSerialize"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ private static IntPtr h_il2cpp_runtime_invoke(IntPtr method, IntPtr obj, void**
// Detach il2cpp_runtime_invoke Detour
il2cpp_runtime_invoke_detour.Detach();

// Initiate Stage2
EngineModule.Stage2();
// Initiate Stage3
EngineModule.Stage3(RuntimeInfo.SupportModulePath);

// Return original Invoke without Trampoline
return _lib.il2cpp_runtime_invoke(method, obj, param, ref exc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ public class Il2CppRuntimeInfo : MelonRuntimeInfo

public Il2CppRuntimeInfo(
string libPath,
string supportModulePath,
string[] triggerMethods)
{
LibPath = libPath;
SupportModulePath = supportModulePath;
TriggerMethods = triggerMethods;
}

Expand Down
42 changes: 23 additions & 19 deletions MelonLoader/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal static class Core
internal static HarmonyLib.Harmony HarmonyInstance;

// Runtime Initialization
internal static void Stage1()
internal static void Stage1(bool isNativeHost)
{
ServicePointManager.DefaultConnectionLimit = int.MaxValue;

Expand All @@ -34,25 +34,24 @@ internal static void Stage1()

MelonLaunchOptions.Load();

#if NET35

// Disabled for now because of issues
//Net20Compatibility.TryInstall();

#elif NET6_0_OR_GREATER

if (LoaderConfig.Current.Loader.LaunchDebugger && MelonEnvironment.IsDotnetRuntime)
#if NET6_0_OR_GREATER
if (isNativeHost)
{
MelonLogger.Msg("[Init] User requested debugger, attempting to launch now...");
Debugger.Launch();
MelonEnvironment.PrintBuild();

if (LoaderConfig.Current.Loader.LaunchDebugger && MelonEnvironment.IsDotnetRuntime)
{
MelonLogger.Msg("[Init] User requested debugger, attempting to launch now...");
Debugger.Launch();
}
}

// if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
// NativeStackWalk.LogNativeStackTrace();

#endif

if (OsUtils.IsWineOrProton())
if (isNativeHost && OsUtils.IsWineOrProton())
Pastel.ConsoleExtensions.Disable();

Fixes.UnhandledException.Install(AppDomain.CurrentDomain);
Expand All @@ -63,26 +62,31 @@ internal static void Stage1()

HarmonyInstance = new HarmonyLib.Harmony(BuildInfo.Name);

Fixes.DetourContextDisposeFix.Install();
Fixes.ForcedCultureInfo.Install();
Fixes.InstancePatchFix.Install();
Fixes.ProcessFix.Install();
#if NET35

// Disabled for now because of issues
Fixes.Net20Compatibility.TryInstall();

#elif NET6_0_OR_GREATER

#if NET6_0_OR_GREATER
Fixes.AsmResolverFix.Install();
Fixes.DotnetAssemblyLoadContextFix.Install();
Fixes.DotnetModHandlerRedirectionFix.Install();

#endif

Fixes.DetourContextDisposeFix.Install();
Fixes.ForcedCultureInfo.Install();
Fixes.InstancePatchFix.Install();
Fixes.ProcessFix.Install();

PatchShield.Install();
MelonPreferences.Load();
}

// After Engine Module Initializes
internal static void Stage2()
{
MelonEnvironment.WelcomeMessage();

ModuleFolderHandler.ScanForFolders();
ModuleFolderHandler.LoadMelons(ModuleFolderHandler.eScanType.UserLibs);
ModuleFolderHandler.LoadMelons(ModuleFolderHandler.eScanType.Plugins);
Expand Down
2 changes: 1 addition & 1 deletion MelonLoader/InternalUtils/BootstrapInterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ internal static void Stage1(nint bootstrapHandle)

try
{
Core.Stage1();
Core.Stage1(true);
}
catch (Exception ex)
{
Expand Down
31 changes: 4 additions & 27 deletions MelonLoader/Modules/MelonEngineModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,13 @@ public abstract class MelonEngineModule : MelonModule

public void SetEngineInfo(string name, string version, string variant = null)
=> MelonEnvironment.SetEngineInfo(name, version, variant);

public void SetApplicationInfo(string developer, string name, string version)
=> MelonEnvironment.SetApplicationInfo(developer, name, version);

public void PrintAppInfo()
=> MelonEnvironment.PrintAppInfo();
public virtual void Stage2()
{
try
{
Core.Stage2();
}
catch (Exception ex)
{
MelonLogger.Error("Failed to run Stage2 of MelonLoader");
MelonLogger.Error(ex);
throw new("Error at Stage2");
}
}

=> ModuleInterop.Stage2();
public virtual void Stage3(string supportModulePath)
{
try
{
Core.Stage3(supportModulePath);
}
catch (Exception ex)
{
MelonLogger.Error("Failed to run Stage3 of MelonLoader");
MelonLogger.Error(ex);
throw new("Error at Stage3");
}
}
=> ModuleInterop.Stage3(supportModulePath);
}
}
38 changes: 35 additions & 3 deletions MelonLoader/Modules/ModuleInterop.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace MelonLoader.Modules
using MelonLoader.Utils;
using System;

namespace MelonLoader.Modules
{
internal static class ModuleInterop
{
Expand All @@ -11,8 +14,9 @@ internal static void StartEngine()
if (Engine == null)
{
MelonLogger.Warning("No Engine Module Found! Using Fallback Environment...");
Core.Stage2();
Core.Stage3(null);
MelonEnvironment.PrintAppInfo();
Stage2();
Stage3(null);
return;
}

Expand All @@ -29,5 +33,33 @@ internal static void StartSupport(string path)
MelonLogger.Msg($"Support Module Found: {path}");
Support.Initialize();
}

public static void Stage2()
{
try
{
Core.Stage2();
}
catch (Exception ex)
{
MelonLogger.Error("Failed to run Stage2 of MelonLoader");
MelonLogger.Error(ex);
throw new("Error at Stage2");
}
}

public static void Stage3(string supportModulePath)
{
try
{
Core.Stage3(supportModulePath);
}
catch (Exception ex)
{
MelonLogger.Error("Failed to run Stage3 of MelonLoader");
MelonLogger.Error(ex);
throw new("Error at Stage3");
}
}
}
}
42 changes: 25 additions & 17 deletions MelonLoader/Utils/MelonEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Diagnostics;
using System;
using MelonLoader.Properties;
using System.Drawing;

namespace MelonLoader.Utils
{
Expand Down Expand Up @@ -133,35 +134,42 @@ internal static void Initialize(AppDomain domain)
SetApplicationInfo(ApplicationExecutableName, ApplicationExecutableName, "0.0.0");
}

internal static void WelcomeMessage()
internal static void PrintBuild()
{
MelonUtils.SetConsoleTitle($"{GetVersionString()} - {CurrentApplicationInfo.Name} {CurrentApplicationInfo.Version ?? ""}");

MelonLogger.WriteSpacer();
MelonLogger.MsgDirect("------------------------------");
MelonLogger.MsgDirect(Color.Pink, "------------------------------");
MelonLogger.MsgDirect(GetVersionString());
MelonLogger.MsgDirect("------------------------------");
MelonLogger.MsgDirect(Color.Pink, "------------------------------");
MelonLogger.MsgDirect($"OS: {OsUtils.GetOSVersion()}");
MelonLogger.MsgDirect($"Arch: {(OsUtils.Is32Bit ? "x86" : "x64")}");
MelonLogger.MsgDirect($"Runtime: {OurRuntimeName}");
MelonLogger.MsgDirect($"Hash Code: {HashCode}");
MelonLogger.MsgDirect("------------------------------");
MelonLogger.MsgDirect($"Engine: {CurrentEngineInfo.Name} {CurrentEngineInfo.Version}");
MelonLogger.MsgDirect($"Engine Version: {CurrentEngineInfo.Version}");
if (!string.IsNullOrEmpty(CurrentEngineInfo.Variant))
MelonLogger.MsgDirect($"Engine Variant: {CurrentEngineInfo.Variant}");
MelonLogger.MsgDirect("------------------------------");
MelonLogger.MsgDirect($"Application Name: {CurrentApplicationInfo.Name}");
MelonLogger.MsgDirect($"Application Developer: {CurrentApplicationInfo.Name}");
MelonLogger.MsgDirect($"Application Version: {CurrentApplicationInfo.Version}");
MelonLogger.MsgDirect("------------------------------");
MelonLogger.MsgDirect(Color.Pink, "------------------------------");
MelonLogger.MsgDirect("Command-Line: ");
foreach (var pair in MelonLaunchOptions.InternalArguments)
if (string.IsNullOrEmpty(pair.Value))
MelonLogger.MsgDirect($" {pair.Key}");
else
MelonLogger.MsgDirect($" {pair.Key} = {pair.Value}");
MelonLogger.MsgDirect("------------------------------");
MelonLogger.MsgDirect(Color.Pink, "------------------------------");
MelonLogger.WriteSpacer();
}

internal static void PrintAppInfo()
{
MelonUtils.SetConsoleTitle($"{GetVersionString()} - {CurrentApplicationInfo.Name} {CurrentApplicationInfo.Version ?? ""}");
MelonLogger.WriteSpacer();
MelonLogger.MsgDirect(Color.Pink, "------------------------------");
MelonLogger.MsgDirect($"Runtime: {OurRuntimeName}");
MelonLogger.MsgDirect(Color.Pink, "------------------------------");
MelonLogger.MsgDirect($"Engine: {CurrentEngineInfo.Name}");
MelonLogger.MsgDirect($"Engine Version: {CurrentEngineInfo.Version}");
if (!string.IsNullOrEmpty(CurrentEngineInfo.Variant))
MelonLogger.MsgDirect($"Engine Variant: {CurrentEngineInfo.Variant}");
MelonLogger.MsgDirect(Color.Pink, "------------------------------");
MelonLogger.MsgDirect($"Application Name: {CurrentApplicationInfo.Name}");
MelonLogger.MsgDirect($"Application Developer: {CurrentApplicationInfo.Name}");
MelonLogger.MsgDirect($"Application Version: {CurrentApplicationInfo.Version}");
MelonLogger.MsgDirect(Color.Pink, "------------------------------");
}
}
}

0 comments on commit d1c65fb

Please sign in to comment.