Skip to content
Merged
45 changes: 20 additions & 25 deletions BepInExResoniteShim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,29 @@ public override void Load()
Log.LogError($"Failed to register generic type converters (Last attempted = {lastAttempted?.ToString() ?? "NULL"}): " + e);
}

RunPatches();
RunPatches(HarmonyInstance);
}

void RunPatches()
internal static bool AnyPatchFailed { get; private set; }

/// <summary>
/// Apply all patches, incompatible patches are skipped gracefully.
/// </summary>
static void RunPatches(Harmony harmony)
{
HarmonyInstance.PatchAllUncategorized();//core patches. if these fail everything fails.
HarmonyInstance.SafePatchCategory(nameof(GraphicalClientPatch));
HarmonyInstance.SafePatchCategory(nameof(WindowTitlePatcher));
HarmonyInstance.SafePatchCategory(nameof(LogAlerter));
HarmonyInstance.SafePatchCategory(nameof(RelativePathFixer));
var assembly = Assembly.GetExecutingAssembly();
foreach (var type in AccessTools.GetTypesFromAssembly(assembly))
{
try
{
harmony.CreateClassProcessor(type).Patch();
}
catch (Exception e)
{
Log.LogDebug($"Skipped patching {type.Name}: {e.Message}");
AnyPatchFailed = true;
}
}
}

[HarmonyPatch]
Expand Down Expand Up @@ -182,7 +195,6 @@ public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructio
}
}

[HarmonyPatchCategory(nameof(WindowTitlePatcher))]
[HarmonyPatch(typeof(RendererInitData), "Pack")]
class WindowTitlePatcher
{
Expand All @@ -203,20 +215,3 @@ public static void Prefix(RendererInitData __instance)
}
}
}

static class HarmonyExtensions
{
public static bool AnyPatchFailed { get; private set; }
public static void SafePatchCategory(this Harmony instance, string categoryName)
{
try
{
instance.PatchCategory(categoryName);
}
catch (Exception e)
{
BepInExResoniteShim.Log.LogError($"Failed to patch {categoryName}: {e}");
AnyPatchFailed = true;
}
}
}
1 change: 0 additions & 1 deletion GraphicalClientPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace BepInExResoniteShim;

[HarmonyPatchCategory(nameof(GraphicalClientPatch))]
[HarmonyPatch(typeof(GraphicalClientRunner), MethodType.StaticConstructor)]
class GraphicalClientPatch
{
Expand Down
23 changes: 17 additions & 6 deletions LogAlerter.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
using Elements.Core;
using System.Reflection;
using Elements.Core;
using HarmonyLib;

namespace BepInExResoniteShim;
using static HarmonyExtensions;


[HarmonyPatchCategory(nameof(LogAlerter))]
[HarmonyPatch(typeof(UniLog), "add_OnLog")]
class LogAlerter
{
static readonly FieldInfo? _logStreamField = AccessTools.Field(
Type.GetType("FrooxEngine.Headless.Program, Resonite"), "logStream");

static void Postfix(Action<string> value)
{
if(AnyPatchFailed) value($"[BepisLoader] BepInExResoniteShim partially loaded.");
else value($"[BepisLoader] BepInExResoniteShim loaded successfully.");
Task.Run(async () =>
{
if (_logStreamField != null)
{
var timeout = DateTime.UtcNow.AddSeconds(10);
while (_logStreamField.GetValue(null) is null && DateTime.UtcNow < timeout)
await Task.Delay(1);
}

if (BepInExResoniteShim.AnyPatchFailed) value($"[BepisLoader] BepInExResoniteShim partially loaded.");
else value($"[BepisLoader] BepInExResoniteShim loaded successfully.");
});
}
}
2 changes: 0 additions & 2 deletions RelativePathFixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace BepInExResoniteShim;

class RelativePathFixer
{
[HarmonyPatchCategory(nameof(RelativePathFixer))]
[HarmonyPatch(typeof(Program), "<Main>$", MethodType.Async)]
class RenderiteHostPathFixes
{
Expand Down Expand Up @@ -45,7 +44,6 @@ public static void FileWriteInjected(string path, string? contents)
}
}

[HarmonyPatchCategory(nameof(RelativePathFixer))]
[HarmonyPatch(typeof(RenderSystem), "StartRenderer", MethodType.Async)]
public class RenderiteWorkingDirectoryFix
{
Expand Down
Loading