using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.Audio;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("WhiteKnuckleEatAnything")]
[assembly: AssemblyConfiguration("release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("WhiteKnuckleEatAnything")]
[assembly: AssemblyTitle("WhiteKnuckleEatAnything")]
[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;
}
}
}
namespace WhiteKnuckleEatAnything
{
[BepInPlugin("whiteknuckle.eatanything", "White Knuckle Eat Anything", "1.2.0")]
public sealed class Plugin : BaseUnityPlugin
{
private Harmony harmony;
private static Plugin instance;
private static RuntimeAnimatorController eatingController;
private static AudioClip eatingClip;
private static bool searchedAssets;
private readonly HashSet<int> eatingItems = new HashSet<int>();
private void Awake()
{
//IL_000c: Unknown result type (might be due to invalid IL or missing references)
//IL_0016: Expected O, but got Unknown
//IL_0042: Unknown result type (might be due to invalid IL or missing references)
//IL_0050: Expected O, but got Unknown
instance = this;
harmony = new Harmony("whiteknuckle.eatanything");
harmony.Patch((MethodBase)AccessTools.Method(typeof(HandItem), "TryUse", (Type[])null, (Type[])null), new HarmonyMethod(typeof(Plugin), "TryUsePrefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
((BaseUnityPlugin)this).Logger.LogInfo((object)"Eat Anything 1.2.0 loaded.");
}
private void OnDestroy()
{
if (harmony != null)
{
harmony.UnpatchSelf();
}
if ((Object)(object)instance == (Object)(object)this)
{
instance = null;
}
}
private static bool TryUsePrefix(HandItem __instance)
{
if ((Object)(object)__instance == (Object)null || __instance.item == null || (Object)(object)instance == (Object)null)
{
return true;
}
Item item = __instance.item;
if (IsExcluded(__instance, item) || item.HasTag("unpurchased"))
{
return true;
}
int hashCode = ((object)item).GetHashCode();
if (!instance.eatingItems.Add(hashCode))
{
return false;
}
((MonoBehaviour)instance).StartCoroutine(instance.EatRoutine(__instance, item, hashCode));
return false;
}
private IEnumerator EatRoutine(HandItem handItem, Item item, int id)
{
Transform itemTransform = (((Object)(object)handItem != (Object)null) ? ((Component)handItem).transform : null);
Vector3 originalPosition = (((Object)(object)itemTransform != (Object)null) ? itemTransform.localPosition : Vector3.zero);
Quaternion originalRotation = (((Object)(object)itemTransform != (Object)null) ? itemTransform.localRotation : Quaternion.identity);
Vector3 originalScale = (((Object)(object)itemTransform != (Object)null) ? itemTransform.localScale : Vector3.one);
SpriteRenderer[] sprites = (SpriteRenderer[])(((Object)(object)handItem != (Object)null) ? ((Array)((Component)handItem).GetComponentsInChildren<SpriteRenderer>(true)) : ((Array)new SpriteRenderer[0]));
Color[] originalColors = (Color[])(object)new Color[sprites.Length];
for (int i = 0; i < sprites.Length; i++)
{
originalColors[i] = (((Object)(object)sprites[i] != (Object)null) ? sprites[i].color : Color.white);
}
try
{
ResolveEatingAssets();
if ((Object)(object)eatingClip != (Object)null && (Object)(object)handItem != (Object)null)
{
AudioManager.PlaySound(eatingClip, ((Component)handItem).transform, 1f, 1f, 0f, false, 1f, (AudioMixerGroup)null, "");
}
}
catch (Exception ex)
{
((BaseUnityPlugin)this).Logger.LogError((object)("Could not start eating sound: " + ex));
}
if ((Object)(object)itemTransform != (Object)null)
{
float elapsed = 0f;
while (elapsed < 0.22f && (Object)(object)itemTransform != (Object)null)
{
elapsed += Time.deltaTime;
float num = Mathf.Clamp01(elapsed / 0.22f);
num = num * num * (3f - 2f * num);
itemTransform.localPosition = Vector3.Lerp(originalPosition, originalPosition + new Vector3(0f, 0.28f, -0.08f), num);
itemTransform.localRotation = Quaternion.Slerp(originalRotation, originalRotation * Quaternion.Euler(0f, 0f, -18f), num);
yield return null;
}
float biteElapsed = 0f;
while (biteElapsed < 0.48f && (Object)(object)itemTransform != (Object)null)
{
biteElapsed += Time.deltaTime;
float num2 = Mathf.Abs(Mathf.Sin(Mathf.Clamp01(biteElapsed / 0.48f) * MathF.PI * 5f));
itemTransform.localPosition = originalPosition + new Vector3(0f, 0.28f + num2 * 0.035f, -0.08f);
itemTransform.localRotation = originalRotation * Quaternion.Euler(0f, 0f, -18f + num2 * 7f);
yield return null;
}
float vanishElapsed = 0f;
while (vanishElapsed < 0.2f && (Object)(object)itemTransform != (Object)null)
{
vanishElapsed += Time.deltaTime;
float num3 = Mathf.Clamp01(vanishElapsed / 0.2f);
itemTransform.localScale = Vector3.Lerp(originalScale, Vector3.zero, num3);
for (int j = 0; j < sprites.Length; j++)
{
if (!((Object)(object)sprites[j] == (Object)null))
{
Color color = originalColors[j];
color.a = originalColors[j].a * (1f - num3);
sprites[j].color = color;
}
}
yield return null;
}
}
else
{
yield return (object)new WaitForSeconds(0.9f);
}
try
{
if (!((Object)(object)handItem != (Object)null) || item == null || handItem.item != item)
{
yield break;
}
try
{
Type type = AccessTools.TypeByName("ENT_Player");
MethodInfo methodInfo = ((type != null) ? AccessTools.Method(type, "GetPlayer", (Type[])null, (Type[])null) : null);
object obj = ((methodInfo != null) ? methodInfo.Invoke(null, null) : null);
MethodInfo methodInfo2 = ((type != null) ? AccessTools.Method(type, "OnEat", new Type[1] { typeof(string) }, (Type[])null) : null);
if (obj != null && methodInfo2 != null)
{
methodInfo2.Invoke(obj, new object[1] { "food" });
}
else
{
((BaseUnityPlugin)this).Logger.LogWarning((object)"Could not find ENT_Player.GetPlayer/OnEat; hunger bar may not recover.");
}
}
catch (Exception ex2)
{
((BaseUnityPlugin)this).Logger.LogWarning((object)("Could not notify hunger perk: " + ex2.Message));
}
try
{
Type type2 = AccessTools.TypeByName("ENT_Player");
MethodInfo methodInfo3 = ((type2 != null) ? AccessTools.Method(type2, "GetPlayer", (Type[])null, (Type[])null) : null);
object obj2 = ((methodInfo3 != null) ? methodInfo3.Invoke(null, null) : null);
if (obj2 != null)
{
MethodInfo methodInfo4 = AccessTools.Method(type2, "GetCurrentGripStrengthTimer", new Type[1] { typeof(int) }, (Type[])null);
float num4 = 10f;
if (methodInfo4 != null)
{
object obj3 = methodInfo4.Invoke(obj2, new object[1] { -1 });
if (obj3 is float)
{
num4 = (float)obj3;
}
}
MethodInfo methodInfo5 = AccessTools.Method(type2, "AddGripStrength", new Type[3]
{
typeof(float),
typeof(bool),
typeof(bool)
}, (Type[])null);
if (methodInfo5 != null)
{
methodInfo5.Invoke(obj2, new object[3]
{
num4 * 0.5f,
false,
false
});
}
else
{
((BaseUnityPlugin)this).Logger.LogWarning((object)"Could not find ENT_Player.AddGripStrength; stamina was not restored.");
}
}
}
catch (Exception ex3)
{
((BaseUnityPlugin)this).Logger.LogWarning((object)("Could not restore stamina: " + ex3.Message));
}
handItem.RemoveItem();
}
catch (Exception ex4)
{
((BaseUnityPlugin)this).Logger.LogError((object)("Could not remove eaten item: " + ex4));
}
finally
{
eatingItems.Remove(id);
}
}
private static bool HasTrigger(Animator animator, string name)
{
//IL_001e: Unknown result type (might be due to invalid IL or missing references)
//IL_0025: Invalid comparison between Unknown and I4
AnimatorControllerParameter[] parameters = animator.parameters;
foreach (AnimatorControllerParameter val in parameters)
{
if (val.name == name && (int)val.type == 9)
{
return true;
}
}
return false;
}
private static void ResolveEatingAssets()
{
if (searchedAssets)
{
return;
}
searchedAssets = true;
try
{
HandItem_Buff[] array = Resources.FindObjectsOfTypeAll<HandItem_Buff>();
foreach (HandItem_Buff val in array)
{
if ((Object)(object)val == (Object)null)
{
continue;
}
string text = (((Object)val).name + " " + (((Object)(object)((Component)val).gameObject != (Object)null) ? ((Object)((Component)val).gameObject).name : "")).ToLowerInvariant();
if (text.Contains("lemon") && (text.Contains("roach") || text.Contains("cockroach")))
{
eatingClip = val.audioClip;
if ((Object)(object)eatingClip != (Object)null)
{
Debug.Log((object)("[Eat Anything] Found Lemon Cockroach sound: " + text));
return;
}
}
}
Debug.LogWarning((object)"[Eat Anything] Could not locate Lemon Cockroach AudioClip by asset name. Sound may not play; inspect game asset names/logs.");
}
catch (Exception ex)
{
Debug.LogWarning((object)("[Eat Anything] Asset lookup failed: " + ex.Message));
}
}
private static bool IsExcluded(HandItem handItem, Item item)
{
if (handItem is HandItem_Flashlight)
{
return true;
}
if (!ExcludedName(item.itemName) && !ExcludedName(item.prefabName) && !ExcludedName(item.itemTag))
{
if (item.itemTags != null)
{
return item.itemTags.Exists(ExcludedName);
}
return false;
}
return true;
}
private static bool ExcludedName(string value)
{
if (string.IsNullOrEmpty(value))
{
return false;
}
string text = value.ToLowerInvariant().Replace("_", "").Replace("-", "")
.Replace(" ", "");
if (!text.Contains("flashlight") && !text.Contains("flaregun") && !text.Contains("pistol") && !text.Contains("hammer") && !text.Contains("mallet") && !text.Contains("scanner") && !text.Contains("scan") && !text.Contains("cryopushka") && !text.Contains("cryocannon") && !text.Contains("cryogun") && !text.Contains("cryo") && !text.Contains("amalgamator") && !text.Contains("amalgam") && !text.Contains("bandage"))
{
return text.Contains("bandages");
}
return true;
}
}
}