Skip to content

Commit

Permalink
Fix for new game patch (#275)
Browse files Browse the repository at this point in the history
* bump gamebuild

* Fix unreliable LoadRoR2ContentEarly method retrieval
  • Loading branch information
xiaoxiao921 authored Apr 20, 2021
1 parent 020aead commit f9830b3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
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

0 comments on commit f9830b3

Please sign in to comment.