Decompiled source of RtDSpawnerReferenceFix v1.0.1

plugins/RtDSpawnerReferenceFix.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8.1", FrameworkDisplayName = ".NET Framework 4.8.1")]
[assembly: AssemblyCompany("RtDSpawnerReferenceFix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("RtDSpawnerReferenceFix")]
[assembly: AssemblyTitle("RtDSpawnerReferenceFix")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
[BepInPlugin("local.rtd.spawnerreferencefix", "RtD Spawner Reference Fix", "1.0.0")]
[BepInDependency("soloredis.rtdlegends", "1.3.58")]
public sealed class Plugin : BaseUnityPlugin
{
	public const string Id = "local.rtd.spawnerreferencefix";

	private static ManualLogSource log;

	private static readonly HashSet<string> ReportedFailures = new HashSet<string>();

	private Harmony harmony;

	internal static readonly Dictionary<string, string> Targets = new Dictionary<string, string>(StringComparer.Ordinal)
	{
		["DG_Spawner_PlainsHorror01_RtD"] = "PlainsDragonide_RtD",
		["Spawner_PlainsDragonide_RtD"] = "PlainsDragonide_RtD",
		["DG_Spawner_MountainUndead_RtD"] = "MountainUndead_RtD",
		["DG_Spawner_MountainHorror_RtD"] = "MountainUndead_RtD",
		["Spawner_MountainUndead_RtD"] = "MountainUndead_RtD"
	};

	private void Awake()
	{
		//IL_0064: Unknown result type (might be due to invalid IL or missing references)
		//IL_006e: Expected O, but got Unknown
		//IL_009a: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a8: Expected O, but got Unknown
		log = ((BaseUnityPlugin)this).Logger;
		Version version = Chainloader.PluginInfos["soloredis.rtdlegends"].Metadata.Version;
		if (version != new Version(1, 3, 58) && version != new Version(1, 3, 63))
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)$"RtDLegends {version} has not been inspected. Reference repair is inactive.");
			return;
		}
		harmony = new Harmony("local.rtd.spawnerreferencefix");
		harmony.Patch((MethodBase)AccessTools.Method(typeof(CreatureSpawner), "Spawn", (Type[])null, (Type[])null), new HarmonyMethod(typeof(Plugin), "RepairBeforeSpawn", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		((BaseUnityPlugin)this).Logger.LogInfo((object)$"Ready for RtDLegends {version}; only five known spawner names with missing creature references are repaired.");
	}

	private static void RepairBeforeSpawn(CreatureSpawner __instance)
	{
		//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)__instance == (Object)null || (Object)(object)__instance.m_creaturePrefab != (Object)null)
		{
			return;
		}
		string text = ((Object)((Component)__instance).gameObject).name;
		if (text.EndsWith("(Clone)", StringComparison.Ordinal))
		{
			text = text.Substring(0, text.Length - "(Clone)".Length);
		}
		if (!Targets.TryGetValue(text, out var value))
		{
			return;
		}
		ZNetScene instance = ZNetScene.instance;
		GameObject val = (((Object)(object)instance == (Object)null) ? null : instance.GetPrefab(value));
		if ((Object)(object)val == (Object)null || (Object)(object)val.GetComponent<Character>() == (Object)null || (Object)(object)val.GetComponent<ZNetView>() == (Object)null)
		{
			if (ReportedFailures.Add(text))
			{
				log.LogError((object)("Cannot repair " + text + ": registered creature " + value + " is missing or invalid. Original spawn behaviour retained."));
			}
		}
		else
		{
			__instance.m_creaturePrefab = val;
			log.LogInfo((object)$"Restored {text} -> {value} at {((Component)__instance).transform.position}. Native spawn, ownership, respawn and level logic retained.");
		}
	}

	private void OnDestroy()
	{
		Harmony obj = harmony;
		if (obj != null)
		{
			obj.UnpatchSelf();
		}
	}
}