Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for new game patch #275

Merged
merged 2 commits into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion R2API/LoadRoR2ContentEarly.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using EntityStates;
using Mono.Cecil;
using MonoMod.Cil;
using MonoMod.RuntimeDetour;
using R2API.Utils;
Expand All @@ -8,6 +9,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;

namespace R2API {
Expand All @@ -16,13 +18,48 @@ internal static class LoadRoR2ContentEarly {
private static RoR2Content RoR2Content;

internal static void Init() {
_ = new ILHook(typeof(RoR2Application).GetNestedTypeCached("<>c").GetMethodCached("<LoadGameContent>b__57_1"), new ILContext.Manipulator(TakeOurInstanceInstead));

var methodBase = RetrieveMethodThatInstantiateRoR2Content();

if (methodBase == null) {
R2API.Logger.LogError("LoadRoR2ContentEarly failed. Stuff will not work properly");
return;
}

_ = new ILHook(RetrieveMethodThatInstantiateRoR2Content(), new ILContext.Manipulator(TakeOurInstanceInstead));

On.RoR2.RoR2Content.LoadStaticContentAsync += LoadOnlyOnce;

EarlyLoad();
}

private static MethodBase RetrieveMethodThatInstantiateRoR2Content() {
var assemblyDefinition = AssemblyDefinition.ReadAssembly(System.IO.Path.Combine(BepInEx.Paths.ManagedPath, "Assembly-CSharp.dll"));
var type = assemblyDefinition.MainModule.Types.First(t => t.Name == nameof(RoR2Application));
var nestedType = type.NestedTypes.First(t => t.Name == "<>c");
foreach (var method in nestedType.Methods) {
var ilContext = new ILContext(method);

foreach (var instruction in method.Body.Instructions) {
if (instruction.OpCode == Mono.Cecil.Cil.OpCodes.Newobj) {
var methodDefinition = instruction.Operand as MethodDefinition;
if (methodDefinition != null && methodDefinition.FullName.Contains(nameof(RoR2Content))) {

var methodName = method.FullName;
int start = methodName.IndexOf("::") + "::".Length;
int end = methodName.IndexOf("(");

var subString = methodName.Substring(start, end - start);

return typeof(RoR2Application).GetNestedTypeCached("<>c").GetMethods((BindingFlags)(-1)).First(m => m.Name.Contains(subString));
}
}
}
}

return null;
}

internal static void EarlyLoad() {
RoR2Content = new RoR2Content();

Expand Down
2 changes: 1 addition & 1 deletion R2API/R2API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class R2API : BaseUnityPlugin {
public const string PluginName = "R2API";
public const string PluginVersion = "0.0.1";

private const int GameBuild = 6499925;
private const int GameBuild = 6537444;

internal new static ManualLogSource Logger { get; set; }
public static bool DebugMode { get; private set; } = false;
Expand Down