Decompiled source of SledgeMajsterLateFix v1.1.0

SledgeMajsterLateFix.dll

Decompiled a week ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using Jotunn.Utils;
using UnityEngine;

[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: CompilationRelaxations(8)]
[assembly: AssemblyVersion("0.0.0.0")]
[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
[BepInDependency(/*Could not decode attribute arguments.*/)]
[BepInPlugin("fix.sledgemajster.lateload", "SledgeMajsterLateFix", "1.1.0")]
public class SledgeMajsterLateFix : BaseUnityPlugin
{
	private void Awake()
	{
		((BaseUnityPlugin)this).Logger.LogInfo((object)"SledgeMajster Late Fix loaded - will apply after world init");
	}

	private void Start()
	{
		((MonoBehaviour)this).StartCoroutine(WaitAndApply());
	}

	private IEnumerator WaitAndApply()
	{
		float elapsed = 0f;
		while ((Object)(object)ZNetScene.instance == (Object)null && elapsed < 120f)
		{
			yield return (object)new WaitForSeconds(1f);
			elapsed += 1f;
		}
		if ((Object)(object)ZNetScene.instance == (Object)null)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)"ZNetScene never initialized, giving up");
			yield break;
		}
		yield return (object)new WaitForSeconds(8f);
		ApplyFix();
	}

	private void ApplyFix()
	{
		ZNetScene instance = ZNetScene.instance;
		GameObject prefab = instance.GetPrefab("SledgeIron");
		if ((Object)(object)prefab == (Object)null)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)"SledgeIron not found - is sledgemajster installed?");
			return;
		}
		ItemDrop component = prefab.GetComponent<ItemDrop>();
		if ((Object)(object)component == (Object)null)
		{
			return;
		}
		Attack attack = component.m_itemData.m_shared.m_attack;
		if (attack == null || string.IsNullOrEmpty(attack.m_attackAnimation))
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)"SledgeIron primary attack has no animation - sledgemajster may not have run");
			return;
		}
		string attackAnimation = attack.m_attackAnimation;
		int num = 0;
		HashSet<string> hashSet = new HashSet<string>();
		if (instance.m_prefabs != null)
		{
			foreach (GameObject prefab2 in instance.m_prefabs)
			{
				if (TryFixSledge(prefab2, attackAnimation, attack))
				{
					hashSet.Add(((Object)prefab2).name);
					num++;
				}
			}
		}
		try
		{
			FieldInfo field = typeof(ZNetScene).GetField("m_namedPrefabs", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (field != null && field.GetValue(instance) is IDictionary dictionary)
			{
				foreach (object value in dictionary.Values)
				{
					GameObject val = (GameObject)((value is GameObject) ? value : null);
					if ((Object)(object)val != (Object)null && !hashSet.Contains(((Object)val).name) && TryFixSledge(val, attackAnimation, attack))
					{
						num++;
					}
				}
			}
		}
		catch (Exception ex)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)("Could not scan m_namedPrefabs: " + ex.Message));
		}
		((BaseUnityPlugin)this).Logger.LogInfo((object)("SledgeMajster Late Fix complete: patched " + num + " additional sledge(s)"));
	}

	private bool TryFixSledge(GameObject go, string battleaxeAnim, Attack refAttack)
	{
		if ((Object)(object)go == (Object)null)
		{
			return false;
		}
		string name = ((Object)go).name;
		if (name.IndexOf("Sledge", StringComparison.Ordinal) < 0)
		{
			return false;
		}
		if (name.IndexOf("Anchor", StringComparison.Ordinal) >= 0)
		{
			return false;
		}
		ItemDrop component = go.GetComponent<ItemDrop>();
		if ((Object)(object)component == (Object)null)
		{
			return false;
		}
		SharedData shared = component.m_itemData.m_shared;
		if (shared == null || shared.m_attack == null)
		{
			return false;
		}
		if (shared.m_attack.m_attackAnimation == battleaxeAnim)
		{
			return false;
		}
		shared.m_secondaryAttack = shared.m_attack;
		shared.m_attack = CloneAttack(refAttack);
		((BaseUnityPlugin)this).Logger.LogInfo((object)("Late-fixed sledge: " + name));
		return true;
	}

	private static Attack CloneAttack(Attack src)
	{
		//IL_0028: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: Expected O, but got Unknown
		//IL_0003: Unknown result type (might be due to invalid IL or missing references)
		//IL_0009: Expected O, but got Unknown
		if (src != null)
		{
			MethodInfo method = typeof(object).GetMethod("MemberwiseClone", BindingFlags.Instance | BindingFlags.NonPublic);
			return (Attack)method.Invoke(src, null);
		}
		return new Attack();
	}
}