Decompiled source of HarpoonAnimFix v1.1.1

plugins/HarpoonAnimFix.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using EntityStates.Engi.EngiMissilePainter;
using IL.EntityStates.Engi.EngiMissilePainter;
using Microsoft.CodeAnalysis;
using Mono.Cecil.Cil;
using Mono.Collections.Generic;
using MonoMod.Cil;
using On.RoR2;
using R2API;
using R2API.Models;
using R2API.Utils;
using RoR2;
using RoR2.ContentManagement;
using RoR2BepInExPack.GameAssetPathsBetter;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.ResourceManagement.ResourceLocations;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
[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;
		}
	}
}
namespace HarpoonAnimFix
{
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
	[BepInPlugin("FluxSquido.HarpoonAnimFix", "HarpoonAnimFix", "1.1.1")]
	public class HarpoonAnimFixPlugin : BaseUnityPlugin
	{
		[CompilerGenerated]
		private static class <>O
		{
			public static Manipulator <0>__UseRestoredCrossfades;

			public static hook_SpawnEffect_EffectIndex_EffectData_bool <1>__SpawnEffect;
		}

		public const string PluginGUID = "FluxSquido.HarpoonAnimFix";

		public const string PluginAuthor = "FluxSquido";

		public const string PluginName = "HarpoonAnimFix";

		public const string PluginVersion = "1.1.1";

		private const string GestureLayer = "Gesture, Additive";

		private const string LeftCannonLayer = "Gesture Left Cannon, Additive";

		private const string RightCannonLayer = "Gesture Right Cannon, Additive";

		private static readonly Dictionary<string, string> restoredCrossfades = new Dictionary<string, string>
		{
			["PrepHarpoons"] = "PrepHarpoonsRestored",
			["ExitHarpoons"] = "ExitHarpoonsRestored"
		};

		private static readonly int fireHarpoon = Animator.StringToHash("FireHarpoonRestored");

		private static EffectIndex harpoonMuzzleflash = (EffectIndex)(-1);

		private void Awake()
		{
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_0096: Expected O, but got Unknown
			//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b6: Expected O, but got Unknown
			//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d6: Expected O, but got Unknown
			//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f6: Expected O, but got Unknown
			AssetBundle clips = AssetBundle.LoadFromFile(Path.Combine(Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location), "harpoonanimfix"));
			string key = RoR2_Base_Engi.animEngi_controller;
			AsyncOperationHandle<RuntimeAnimatorController> val = AssetAsyncReferenceManager<RuntimeAnimatorController>.LoadAsset(new AssetReferenceT<RuntimeAnimatorController>(key), (AsyncReferenceHandleUnloadType)2);
			val.Completed += delegate(AsyncOperationHandle<RuntimeAnimatorController> controller)
			{
				//IL_000e: Unknown result type (might be due to invalid IL or missing references)
				//IL_000f: Unknown result type (might be due to invalid IL or missing references)
				//IL_001b: Unknown result type (might be due to invalid IL or missing references)
				//IL_0020: Unknown result type (might be due to invalid IL or missing references)
				AsyncOperationHandle<IList<IResourceLocation>> val6 = Addressables.LoadResourceLocationsAsync((object)key, (Type)null);
				val6.Completed += delegate(AsyncOperationHandle<IList<IResourceLocation>> locations)
				{
					//IL_000e: Unknown result type (might be due to invalid IL or missing references)
					//IL_000f: Unknown result type (might be due to invalid IL or missing references)
					//IL_001f: Unknown result type (might be due to invalid IL or missing references)
					//IL_0024: Unknown result type (might be due to invalid IL or missing references)
					AsyncOperationHandle<GameObject> val7 = AssetAsyncReferenceManager<GameObject>.LoadAsset(new AssetReferenceT<GameObject>(RoR2_Base_Engi.EngiBody_prefab), (AsyncReferenceHandleUnloadType)2);
					val7.Completed += delegate(AsyncOperationHandle<GameObject> body)
					{
						RestoreHarpoonStates(clips, controller.Result, locations.Result[0].Dependencies[0].InternalId, body.Result);
					};
				};
			};
			Fire.IdleHarpoonsStateHash = Animator.StringToHash("IdleHarpoonsRestored");
			Fire.FireHarpoonStateHash = fireHarpoon;
			object obj = <>O.<0>__UseRestoredCrossfades;
			if (obj == null)
			{
				Manipulator val2 = UseRestoredCrossfades;
				<>O.<0>__UseRestoredCrossfades = val2;
				obj = (object)val2;
			}
			Paint.OnEnter += (Manipulator)obj;
			object obj2 = <>O.<0>__UseRestoredCrossfades;
			if (obj2 == null)
			{
				Manipulator val3 = UseRestoredCrossfades;
				<>O.<0>__UseRestoredCrossfades = val3;
				obj2 = (object)val3;
			}
			Paint.OnExit += (Manipulator)obj2;
			object obj3 = <>O.<0>__UseRestoredCrossfades;
			if (obj3 == null)
			{
				Manipulator val4 = UseRestoredCrossfades;
				<>O.<0>__UseRestoredCrossfades = val4;
				obj3 = (object)val4;
			}
			Fire.OnExit += (Manipulator)obj3;
			object obj4 = <>O.<1>__SpawnEffect;
			if (obj4 == null)
			{
				hook_SpawnEffect_EffectIndex_EffectData_bool val5 = SpawnEffect;
				<>O.<1>__SpawnEffect = val5;
				obj4 = (object)val5;
			}
			EffectManager.SpawnEffect_EffectIndex_EffectData_bool += (hook_SpawnEffect_EffectIndex_EffectData_bool)obj4;
			RoR2Application.onLoad = (Action)Delegate.Combine(RoR2Application.onLoad, (Action)delegate
			{
				//IL_0005: Unknown result type (might be due to invalid IL or missing references)
				//IL_000a: Unknown result type (might be due to invalid IL or missing references)
				harpoonMuzzleflash = EffectCatalog.FindEffectIndexFromPrefab(Fire.muzzleflashEffectPrefab);
			});
		}

		private void RestoreHarpoonStates(AssetBundle clips, RuntimeAnimatorController controller, string controllerBundle, GameObject body)
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Expected O, but got Unknown
			AnimatorModifications val = new AnimatorModifications(((BaseUnityPlugin)this).Info.Metadata);
			val.NewStates.Add("Gesture, Additive", new List<State>(3)
			{
				NewState("PrepHarpoonsRestored", clips.LoadAsset<AnimationClip>("PrepHarpoons"), 1f),
				NewState("IdleHarpoonsRestored", clips.LoadAsset<AnimationClip>("IdleHarpoons"), 1f),
				NewState("ExitHarpoonsRestored", clips.LoadAsset<AnimationClip>("ExitHarpoons"), 0.6f)
			});
			val.NewStates.Add("Gesture Left Cannon, Additive", new List<State>(1) { NewShot(clips.LoadAsset<AnimationClip>("FireHarpoonLeft"), 1f, 0.1f) });
			val.NewStates.Add("Gesture Right Cannon, Additive", new List<State>(1) { NewShot(clips.LoadAsset<AnimationClip>("FireHarpoonRight"), 17f / 32f, 0.25f) });
			AnimationsAPI.AddModifications(controllerBundle, controller, val);
			AnimationsAPI.AddAnimatorController(((Component)body.GetComponent<ModelLocator>().modelTransform).GetComponent<Animator>(), controller);
		}

		private static State NewState(string name, AnimationClip clip, float speed)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Expected O, but got Unknown
			return new State
			{
				Name = name,
				Clip = clip,
				Speed = speed,
				WriteDefaultValues = true
			};
		}

		private static State NewShot(AnimationClip clip, float exitTime, float exitDuration)
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Expected O, but got Unknown
			State obj = NewState("FireHarpoonRestored", clip, 1f);
			obj.Transitions.Add(new Transition
			{
				IsExit = true,
				HasExitTime = true,
				ExitTime = exitTime,
				HasFixedDuration = true,
				TransitionDuration = exitDuration,
				OrderedInterruption = true,
				CanTransitionToSelf = true
			});
			return obj;
		}

		private static void UseRestoredCrossfades(ILContext il)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			Enumerator<Instruction> enumerator = il.Instrs.GetEnumerator();
			try
			{
				string key = default(string);
				while (enumerator.MoveNext())
				{
					Instruction current = enumerator.Current;
					if (ILPatternMatchingExt.MatchLdstr(current, ref key) && restoredCrossfades.TryGetValue(key, out var value))
					{
						current.Operand = value;
					}
				}
			}
			finally
			{
				((IDisposable)enumerator/*cast due to .constrained prefix*/).Dispose();
			}
		}

		private static void SpawnEffect(orig_SpawnEffect_EffectIndex_EffectData_bool orig, EffectIndex effectIndex, EffectData effectData, bool transmit)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Invalid comparison between Unknown and I4
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			orig.Invoke(effectIndex, effectData, transmit);
			if ((int)effectIndex == -1 || effectIndex != harpoonMuzzleflash || !Object.op_Implicit((Object)(object)effectData.rootObject) || Util.HasEffectiveAuthority(effectData.rootObject))
			{
				return;
			}
			ModelLocator component = effectData.rootObject.GetComponent<ModelLocator>();
			Animator val = (Object.op_Implicit((Object)(object)component) ? component.modelAnimator : null);
			if (Object.op_Implicit((Object)(object)val) && Object.op_Implicit((Object)(object)component.modelChildLocator))
			{
				string text = component.modelChildLocator.FindChildName((int)effectData.modelChildIndex);
				int layerIndex = val.GetLayerIndex((text == "MuzzleLeft") ? "Gesture Left Cannon, Additive" : "Gesture Right Cannon, Additive");
				if (layerIndex >= 0 && val.HasState(layerIndex, fireHarpoon))
				{
					val.PlayInFixedTime(fireHarpoon, layerIndex, 0f);
				}
			}
		}
	}
}