using System;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("thejacksmoo_AleAndTale_TPV")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+77eb94344b9a2c801168578850b384536e844e89")]
[assembly: AssemblyProduct("thejacksmoo_AleAndTale_TPV")]
[assembly: AssemblyTitle("thejacksmoo_AleAndTale_TPV")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace thejacksmoo_AleAndTale_TPV;
[BepInPlugin("com.thejacksmoo.aleandtale.tpv", "TheJacksMoo Ale & Tale TPV", "0.1.6")]
public class Plugin : BaseUnityPlugin
{
private bool thirdPersonEnabled = false;
private bool savedOriginalPositions = false;
private Vector3 originalFPViewPosition;
private Vector3 originalCameraPosition;
private FieldInfo interactiveHitDistField;
private MethodInfo onSelectedHandItemDataIdMethod;
private FieldInfo playerAnimTPAnimatorField;
private static readonly int axeChopAnimHash = Animator.StringToHash("Axe Chop");
private FieldInfo invNumsGoField;
private static readonly int dualWeaponsHitAnimHash = Animator.StringToHash("Dual Weapons Hit");
private bool attackRecoverySkipped = false;
private Vector3 temporaryFPViewPosition;
private Vector3 temporaryCameraPosition;
private float originalInteractiveHitDist;
private Harmony harmony;
private static Plugin Instance;
private void Awake()
{
//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
//IL_00ba: Expected O, but got Unknown
//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
//IL_0106: Expected O, but got Unknown
//IL_0146: Unknown result type (might be due to invalid IL or missing references)
//IL_0153: Expected O, but got Unknown
//IL_0180: Unknown result type (might be due to invalid IL or missing references)
//IL_0195: Unknown result type (might be due to invalid IL or missing references)
//IL_01a2: Expected O, but got Unknown
//IL_01a2: Expected O, but got Unknown
//IL_01cf: Unknown result type (might be due to invalid IL or missing references)
//IL_01e4: Unknown result type (might be due to invalid IL or missing references)
//IL_01f1: Expected O, but got Unknown
//IL_01f1: Expected O, but got Unknown
//IL_021e: Unknown result type (might be due to invalid IL or missing references)
//IL_0233: Unknown result type (might be due to invalid IL or missing references)
//IL_0240: Expected O, but got Unknown
//IL_0240: Expected O, but got Unknown
//IL_026e: Unknown result type (might be due to invalid IL or missing references)
//IL_027b: Expected O, but got Unknown
Instance = this;
((BaseUnityPlugin)this).Logger.LogInfo((object)"Ale & Tale Third Person mod loaded!");
interactiveHitDistField = typeof(PlayerInventory).GetField("interactiveHitDist", BindingFlags.Instance | BindingFlags.NonPublic);
playerAnimTPAnimatorField = typeof(PlayerAnimTP).GetField("animator", BindingFlags.Instance | BindingFlags.NonPublic);
invNumsGoField = typeof(PlayerInventory).GetField("_invNumsGo", BindingFlags.Instance | BindingFlags.NonPublic);
onSelectedHandItemDataIdMethod = typeof(PlayerAnimTP).GetMethod("OnSelectedHandItemDataId", BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[2]
{
typeof(uint),
typeof(uint)
}, null);
harmony = new Harmony("com.thejacksmoo.aleandtale.tpv");
harmony.Patch((MethodBase)AccessTools.Method(typeof(PlayerInventory), "OnInventoryAdd", new Type[1] { typeof(ushort) }, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(Plugin), "OnInventoryAddPostfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
harmony.Patch((MethodBase)AccessTools.Method(typeof(PlayerInventory), "OnInventoryChange", new Type[1] { typeof(ushort) }, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(Plugin), "OnInventoryAddPostfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
harmony.Patch((MethodBase)AccessTools.Method(typeof(SurfaceSnapTool), "FixedUpdate", (Type[])null, (Type[])null), new HarmonyMethod(typeof(Plugin), "SurfaceSnapToolPrefix", (Type[])null), new HarmonyMethod(typeof(Plugin), "SurfaceSnapToolPostfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
harmony.Patch((MethodBase)AccessTools.Method(typeof(SurfaceSnapFurnitureTool), "FixedUpdate", (Type[])null, (Type[])null), new HarmonyMethod(typeof(Plugin), "SurfaceSnapToolPrefix", (Type[])null), new HarmonyMethod(typeof(Plugin), "SurfaceSnapToolPostfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
harmony.Patch((MethodBase)AccessTools.Method(typeof(PlaceSnapFurnitureTool), "FixedUpdate", (Type[])null, (Type[])null), new HarmonyMethod(typeof(Plugin), "SurfaceSnapToolPrefix", (Type[])null), new HarmonyMethod(typeof(Plugin), "SurfaceSnapToolPostfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
harmony.Patch((MethodBase)AccessTools.Method(typeof(Dishwasher), "MGCancel", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(Plugin), "DishwasherMGCancelPostfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
}
private void Update()
{
//IL_0138: Unknown result type (might be due to invalid IL or missing references)
//IL_013f: Expected O, but got Unknown
//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
//IL_00ce: Expected O, but got Unknown
//IL_0154: Unknown result type (might be due to invalid IL or missing references)
//IL_0159: Unknown result type (might be due to invalid IL or missing references)
//IL_02f2: Unknown result type (might be due to invalid IL or missing references)
//IL_02f7: Unknown result type (might be due to invalid IL or missing references)
//IL_02fe: Unknown result type (might be due to invalid IL or missing references)
//IL_0303: Unknown result type (might be due to invalid IL or missing references)
//IL_047e: Unknown result type (might be due to invalid IL or missing references)
//IL_048b: Unknown result type (might be due to invalid IL or missing references)
//IL_03df: Unknown result type (might be due to invalid IL or missing references)
//IL_03e4: Unknown result type (might be due to invalid IL or missing references)
//IL_03f3: Unknown result type (might be due to invalid IL or missing references)
//IL_03fc: Unknown result type (might be due to invalid IL or missing references)
//IL_0401: Unknown result type (might be due to invalid IL or missing references)
//IL_0410: Unknown result type (might be due to invalid IL or missing references)
if (thirdPersonEnabled && (Object)(object)PlayerInput.Instance != (Object)null && (Object)(object)PlayerInventory.Instance != (Object)null && (Object)(object)PlayerNet.Instance != (Object)null && (Object)(object)PlayerNet.Instance.playerAnimTP != (Object)null)
{
GameObject[] array = (GameObject[])invNumsGoField.GetValue(PlayerInventory.Instance);
byte selectedSlot = PlayerInventory.Instance.selectedSlot;
WeaponTool val = default(WeaponTool);
if (array != null && selectedSlot < array.Length && (Object)(object)array[selectedSlot] != (Object)null && array[selectedSlot].TryGetComponent<WeaponTool>(ref val) && val.canBlock && PlayerInput.Instance.GetAimInputHeld())
{
Animator val2 = (Animator)playerAnimTPAnimatorField.GetValue(PlayerNet.Instance.playerAnimTP);
val2.Play(axeChopAnimHash, 1, 0.65f);
}
}
if (thirdPersonEnabled && (Object)(object)PlayerNet.Instance != (Object)null && (Object)(object)PlayerNet.Instance.playerAnimTP != (Object)null && playerAnimTPAnimatorField != null)
{
Animator val3 = (Animator)playerAnimTPAnimatorField.GetValue(PlayerNet.Instance.playerAnimTP);
if ((Object)(object)val3 != (Object)null)
{
AnimatorStateInfo currentAnimatorStateInfo = val3.GetCurrentAnimatorStateInfo(1);
bool flag = ((AnimatorStateInfo)(ref currentAnimatorStateInfo)).shortNameHash == axeChopAnimHash || ((AnimatorStateInfo)(ref currentAnimatorStateInfo)).shortNameHash == dualWeaponsHitAnimHash;
bool flag2 = (Object)(object)PlayerInput.Instance != (Object)null && PlayerInput.Instance.GetAimInputHeld();
if (flag && !flag2)
{
if (((AnimatorStateInfo)(ref currentAnimatorStateInfo)).normalizedTime <= 0.33f)
{
val3.speed = 1.5f;
}
else if (((AnimatorStateInfo)(ref currentAnimatorStateInfo)).normalizedTime >= 0.66f)
{
val3.speed = 6f;
}
else
{
val3.speed = 3f;
}
}
else
{
val3.speed = 1f;
attackRecoverySkipped = false;
}
}
}
if (!Input.GetKeyDown((KeyCode)286))
{
return;
}
if ((Object)(object)PlayerMovement.Instance == (Object)null || (Object)(object)PlayerMovement.Instance.mainCamera == (Object)null || (Object)(object)PlayerNet.Instance == (Object)null || (Object)(object)PlayerNet.Instance.playerAnimTP == (Object)null || (Object)(object)PlayerNet.Instance.playerAnimTP.playerAvatar == (Object)null)
{
((BaseUnityPlugin)this).Logger.LogWarning((object)"Player objects not found yet.");
return;
}
Transform transform = ((Component)PlayerMovement.Instance.mainCamera).transform;
Transform parent = transform.parent;
Transform transform2 = ((Component)PlayerNet.Instance.playerAnimTP.playerAvatar).transform;
if (!savedOriginalPositions)
{
originalFPViewPosition = parent.localPosition;
originalCameraPosition = transform.localPosition;
originalInteractiveHitDist = (float)interactiveHitDistField.GetValue(PlayerInventory.Instance);
savedOriginalPositions = true;
}
thirdPersonEnabled = !thirdPersonEnabled;
if (thirdPersonEnabled)
{
PlayerNet.Instance.playerAnimTP.playerAvatar.SetVisible(true, true);
uint value = PlayerNet.Instance.selectedHandItemDataId.Value;
onSelectedHandItemDataIdMethod?.Invoke(PlayerNet.Instance.playerAnimTP, new object[2] { 1u, value });
interactiveHitDistField.SetValue(PlayerInventory.Instance, 5f);
SetHeldItemRenderers(parent, "_fp(Clone)", visible: false);
SetHeldItemRenderers(transform2, "_tp(Clone)", visible: true);
Vector3 localPosition = parent.localPosition;
localPosition.y = 1.8f;
parent.localPosition = localPosition;
Vector3 localPosition2 = transform.localPosition;
localPosition2.z = -2f;
transform.localPosition = localPosition2;
((BaseUnityPlugin)this).Logger.LogInfo((object)"Third Person ON");
}
else
{
PlayerNet.Instance.playerAnimTP.playerAvatar.SetVisible(false, true);
interactiveHitDistField.SetValue(PlayerInventory.Instance, originalInteractiveHitDist);
SetHeldItemRenderers(parent, "_fp(Clone)", visible: true);
SetHeldItemRenderers(transform2, "_tp(Clone)", visible: false);
parent.localPosition = originalFPViewPosition;
transform.localPosition = originalCameraPosition;
((BaseUnityPlugin)this).Logger.LogInfo((object)"Third Person OFF");
}
}
private static void SurfaceSnapToolPrefix()
{
//IL_0041: Unknown result type (might be due to invalid IL or missing references)
//IL_0046: Unknown result type (might be due to invalid IL or missing references)
//IL_0051: Unknown result type (might be due to invalid IL or missing references)
//IL_0056: Unknown result type (might be due to invalid IL or missing references)
//IL_0061: Unknown result type (might be due to invalid IL or missing references)
//IL_0072: Unknown result type (might be due to invalid IL or missing references)
if (!((Object)(object)Instance == (Object)null) && Instance.thirdPersonEnabled)
{
Transform transform = ((Component)PlayerMovement.Instance.mainCamera).transform;
Transform parent = transform.parent;
Instance.temporaryFPViewPosition = parent.localPosition;
Instance.temporaryCameraPosition = transform.localPosition;
parent.localPosition = Instance.originalFPViewPosition;
transform.localPosition = Instance.originalCameraPosition;
}
}
private static void SurfaceSnapToolPostfix()
{
//IL_0041: Unknown result type (might be due to invalid IL or missing references)
//IL_0052: Unknown result type (might be due to invalid IL or missing references)
if (!((Object)(object)Instance == (Object)null) && Instance.thirdPersonEnabled)
{
Transform transform = ((Component)PlayerMovement.Instance.mainCamera).transform;
Transform parent = transform.parent;
parent.localPosition = Instance.temporaryFPViewPosition;
transform.localPosition = Instance.temporaryCameraPosition;
}
}
private static void OnInventoryAddPostfix()
{
if ((Object)(object)Instance != (Object)null)
{
((BaseUnityPlugin)Instance).Logger.LogInfo((object)"OnInventoryAddPostfix fired!");
}
if (!((Object)(object)Instance == (Object)null) && Instance.thirdPersonEnabled && !((Object)(object)PlayerMovement.Instance == (Object)null) && !((Object)(object)PlayerMovement.Instance.mainCamera == (Object)null))
{
Transform parent = ((Component)PlayerMovement.Instance.mainCamera).transform.parent;
Instance.SetHeldItemRenderers(parent, "_fp(Clone)", visible: false);
}
}
private void SetHeldItemRenderers(Transform root, string itemSuffix, bool visible)
{
Transform[] componentsInChildren = ((Component)root).GetComponentsInChildren<Transform>(true);
Transform[] array = componentsInChildren;
foreach (Transform val in array)
{
if (((Object)val).name.EndsWith(itemSuffix))
{
Renderer[] componentsInChildren2 = ((Component)val).GetComponentsInChildren<Renderer>(true);
Renderer[] array2 = componentsInChildren2;
foreach (Renderer val2 in array2)
{
val2.enabled = visible;
}
}
}
}
private IEnumerator RestoreThirdPersonAfterCameraReset()
{
yield return (object)new WaitForSeconds(1.1f);
if (thirdPersonEnabled && !((Object)(object)PlayerMovement.Instance == (Object)null) && !((Object)(object)PlayerMovement.Instance.mainCamera == (Object)null) && !((Object)(object)PlayerNet.Instance == (Object)null) && !((Object)(object)PlayerNet.Instance.playerAnimTP == (Object)null) && !((Object)(object)PlayerNet.Instance.playerAnimTP.playerAvatar == (Object)null))
{
Transform mainCamera = ((Component)PlayerMovement.Instance.mainCamera).transform;
Transform fpView = mainCamera.parent;
Transform tpAvatar = ((Component)PlayerNet.Instance.playerAnimTP.playerAvatar).transform;
Vector3 fpPosition = fpView.localPosition;
fpPosition.y = 1.8f;
fpView.localPosition = fpPosition;
Vector3 cameraPosition = mainCamera.localPosition;
cameraPosition.z = -2f;
mainCamera.localPosition = cameraPosition;
PlayerNet.Instance.playerAnimTP.playerAvatar.SetVisible(true, true);
SetHeldItemRenderers(fpView, "_fp(Clone)", visible: false);
SetHeldItemRenderers(tpAvatar, "_tp(Clone)", visible: true);
}
}
private static void DishwasherMGCancelPostfix()
{
if (!((Object)(object)Instance == (Object)null) && Instance.thirdPersonEnabled)
{
((MonoBehaviour)Instance).StartCoroutine(Instance.RestoreThirdPersonAfterCameraReset());
}
}
}