Decompiled source of LateRepo Fix v1.0.0

LateRepoFix.dll

Decompiled 4 hours ago
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Photon.Pun;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace LateRepoFix;

[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInPlugin("laterepo.fix.runtime", "LateRepo Fix", "1.0.0")]
public sealed class Plugin : BaseUnityPlugin
{
	internal static ManualLogSource Log;

	internal static ConfigEntry<bool> DebugLogging;

	private void Awake()
	{
		//IL_0030: Unknown result type (might be due to invalid IL or missing references)
		Log = ((BaseUnityPlugin)this).Logger;
		DebugLogging = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "DebugLogging", false, "Enable extra logs for LateRepo Fix.");
		new Harmony("laterepo.fix.runtime").PatchAll();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Patched LateRepo LevelGenerator hook guard.");
	}
}
internal static class ModLog
{
	private static bool loggedSuppressedException;

	public static void Info(string message)
	{
		if (Plugin.Log != null)
		{
			Plugin.Log.LogInfo((object)message);
		}
	}

	public static void Debug(string message)
	{
		if (Plugin.DebugLogging != null && Plugin.DebugLogging.Value && Plugin.Log != null)
		{
			Plugin.Log.LogInfo((object)("[Debug] " + message));
		}
	}

	public static void SuppressedSingleplayerPool()
	{
		if (loggedSuppressedException)
		{
			Debug("Suppressed another outdated RunManager.singleplayerPool access.");
			return;
		}
		Info("Suppressed outdated RunManager.singleplayerPool access during LevelGenerator.Start.");
		loggedSuppressedException = true;
	}
}
internal static class ExceptionFilter
{
	public static bool IsSingleplayerPoolMissing(Exception ex)
	{
		while (ex != null)
		{
			if (ex is MissingFieldException && ex.ToString().IndexOf("RunManager.singleplayerPool", StringComparison.OrdinalIgnoreCase) >= 0)
			{
				return true;
			}
			ex = ex.InnerException;
		}
		return false;
	}
}
[HarmonyPatch(typeof(LevelGenerator), "Start")]
internal static class LevelGeneratorStartPatch
{
	private static Exception Finalizer(Exception __exception)
	{
		if (__exception != null && ExceptionFilter.IsSingleplayerPoolMissing(__exception))
		{
			ModLog.SuppressedSingleplayerPool();
			return null;
		}
		return __exception;
	}
}
[HarmonyPatch]
internal static class LateRepoLevelGeneratorHookPatch
{
	private static MethodBase TargetMethod()
	{
		Type type = AccessTools.TypeByName("LateRepo.Patches.LateJoinPatch");
		if (type == null)
		{
			ModLog.Info("LateRepo hook type was not found.");
			return null;
		}
		return AccessTools.Method(type, "LevelGeneratorHook", (Type[])null, (Type[])null);
	}

	private static bool Prefix(Action<LevelGenerator> __0, LevelGenerator __1)
	{
		try
		{
			ClearLobbyBufferedRpcs(__1);
			ModLog.Debug("Calling original LevelGenerator.Start through LateRepo hook.");
			__0(__1);
		}
		catch (Exception ex)
		{
			if (ExceptionFilter.IsSingleplayerPoolMissing(ex))
			{
				ModLog.SuppressedSingleplayerPool();
				return false;
			}
			throw;
		}
		return false;
	}

	private static void ClearLobbyBufferedRpcs(LevelGenerator levelGenerator)
	{
		if (PhotonNetwork.IsMasterClient && SemiFunc.RunIsLobby() && !((Object)(object)levelGenerator == (Object)null) && !((Object)(object)levelGenerator.PhotonView == (Object)null))
		{
			PhotonNetwork.RemoveBufferedRPCs(levelGenerator.PhotonView.ViewID, (string)null, (int[])null);
		}
	}
}