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 Peak;
using Photon.Pun;
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("OvrSlepr_PeakZombieModDagger")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("OvrSlepr_PeakZombieModDagger")]
[assembly: AssemblyTitle("OvrSlepr_PeakZombieModDagger")]
[assembly: AssemblyVersion("1.0.0.0")]
[BepInPlugin("com.ovrslepr.peak.zombiedagger", "PEAK Zombie Dagger Mod", "1.1.4")]
public class ZombieDaggerPlugin : BaseUnityPlugin
{
private void Awake()
{
//IL_0006: Unknown result type (might be due to invalid IL or missing references)
new Harmony("com.ovrslepr.peak.zombiedagger").PatchAll();
((BaseUnityPlugin)this).Logger.LogInfo((object)"[ZombieDaggerMod] Version 1.1.4 loaded.");
}
}
[HarmonyPatch(typeof(MushroomZombie), "Awake")]
public static class ZombieNetworkSetupPatch
{
[HarmonyPostfix]
public static void Postfix(MushroomZombie __instance)
{
if ((Object)(object)__instance != (Object)null)
{
ZombieSacrificeNetwork.EnsureInstalled(__instance);
}
}
}
[HarmonyPatch(typeof(CharacterInteractible), "IsInteractible")]
public static class MushroomZombieInteractionPatch
{
[HarmonyPostfix]
public static void Postfix(CharacterInteractible __instance, Character interactor, ref bool __result)
{
if ((Object)(object)__instance == (Object)null || (Object)(object)interactor == (Object)null)
{
return;
}
Character character = __instance.character;
if (!((Object)(object)character == (Object)null) && !character.data.dead)
{
MushroomZombie component = ((Component)character).GetComponent<MushroomZombie>();
if (!((Object)(object)component == (Object)null))
{
__result = true;
ZombieSacrificeNetwork.EnsureInstalled(component);
}
}
}
}
[HarmonyPatch(typeof(RitualDaggerFeedBehavior), "FeederAction")]
public static class RitualDaggerZombieFeedPatch
{
[HarmonyPostfix]
public static void Postfix(Character feeder, Character fedCharacter)
{
if ((Object)(object)fedCharacter == (Object)null)
{
return;
}
MushroomZombie component = ((Component)fedCharacter).GetComponent<MushroomZombie>();
if ((Object)(object)component == (Object)null || (Object)(object)((MonoBehaviourPun)component).photonView == (Object)null)
{
return;
}
ZombieSacrificeNetwork.EnsureInstalled(component);
ZombieSacrificeNetwork component2 = ((Component)component).GetComponent<ZombieSacrificeNetwork>();
if (!((Object)(object)component2 == (Object)null))
{
if (PhotonNetwork.IsMasterClient)
{
ZombieSacrificeNetwork.ProcessSacrifice(component);
}
else
{
component2.RequestSacrifice();
}
}
}
}
public class ZombieSacrificeNetwork : MonoBehaviour
{
private MushroomZombie zombie;
private bool requestRunning;
private const int MaxRequestAttempts = 5;
private const float RetryDelaySeconds = 0.75f;
private static readonly HashSet<int> Processing = new HashSet<int>();
private static readonly HashSet<int> DaggerSacrificeIds = new HashSet<int>();
private void Awake()
{
zombie = ((Component)this).GetComponent<MushroomZombie>();
}
public static void EnsureInstalled(MushroomZombie target)
{
if (!((Object)(object)target == (Object)null) && (Object)(object)((Component)target).GetComponent<ZombieSacrificeNetwork>() == (Object)null)
{
((Component)target).gameObject.AddComponent<ZombieSacrificeNetwork>();
}
}
public void RequestSacrifice()
{
if (!requestRunning)
{
if ((Object)(object)zombie == (Object)null)
{
zombie = ((Component)this).GetComponent<MushroomZombie>();
}
if ((Object)(object)zombie == (Object)null || (Object)(object)((MonoBehaviourPun)zombie).photonView == (Object)null)
{
Debug.LogWarning((object)"[ZombieDaggerMod] Cannot request sacrifice: no PhotonView.");
}
else if (!PhotonNetwork.InRoom)
{
Debug.LogWarning((object)"[ZombieDaggerMod] Cannot request sacrifice outside a room.");
}
else
{
((MonoBehaviour)this).StartCoroutine(RetrySacrificeRequest());
}
}
}
private IEnumerator RetrySacrificeRequest()
{
requestRunning = true;
try
{
for (int attempt = 1; attempt <= 5; attempt++)
{
if ((Object)(object)zombie == (Object)null)
{
break;
}
if ((Object)(object)((MonoBehaviourPun)zombie).photonView == (Object)null)
{
break;
}
if (!PhotonNetwork.InRoom)
{
break;
}
if (PhotonNetwork.IsMasterClient)
{
ProcessSacrifice(zombie);
break;
}
int viewId = ((MonoBehaviourPun)zombie).photonView.ViewID;
Debug.Log((object)("[ZombieDaggerMod] Sacrifice request attempt " + attempt + "/" + 5 + ", ViewID=" + viewId));
((MonoBehaviourPun)zombie).photonView.RPC("RPC_RequestZombieSacrifice", (RpcTarget)2, Array.Empty<object>());
if (attempt < 5)
{
yield return (object)new WaitForSecondsRealtime(0.75f);
}
}
}
finally
{
requestRunning = false;
}
}
[PunRPC]
public void RPC_RequestZombieSacrifice()
{
if (PhotonNetwork.IsMasterClient)
{
if ((Object)(object)zombie == (Object)null)
{
zombie = ((Component)this).GetComponent<MushroomZombie>();
}
ProcessSacrifice(zombie);
}
}
public static bool WasKilledByDagger(int viewId)
{
return DaggerSacrificeIds.Contains(viewId);
}
public static void ProcessSacrifice(MushroomZombie target)
{
//IL_0089: Unknown result type (might be due to invalid IL or missing references)
//IL_008f: Invalid comparison between Unknown and I4
if (!PhotonNetwork.IsMasterClient || (Object)(object)target == (Object)null || (Object)(object)((MonoBehaviourPun)target).photonView == (Object)null)
{
return;
}
int viewID = ((MonoBehaviourPun)target).photonView.ViewID;
if (Processing.Contains(viewID) || DaggerSacrificeIds.Contains(viewID))
{
return;
}
if (!((MonoBehaviourPun)target).photonView.IsMine)
{
Debug.LogWarning((object)("[ZombieDaggerMod] Host does not own zombie. ID=" + viewID));
}
else
{
if ((int)target._currentState == 6)
{
return;
}
Processing.Add(viewID);
try
{
MethodInfo methodInfo = AccessTools.Method(typeof(MushroomZombie), "Die", (Type[])null, (Type[])null);
if (methodInfo == null)
{
Debug.LogError((object)"[ZombieDaggerMod] Die method not found.");
return;
}
DaggerSacrificeIds.Add(viewID);
Debug.Log((object)("[ZombieDaggerMod] Processing dagger sacrifice. ID=" + viewID));
methodInfo.Invoke(target, null);
target.DestroyZombie();
}
catch (Exception ex)
{
DaggerSacrificeIds.Remove(viewID);
Debug.LogError((object)("[ZombieDaggerMod] Sacrifice failed. ID=" + viewID + ", Error=" + ex));
}
finally
{
Processing.Remove(viewID);
}
}
}
}
[HarmonyPatch(typeof(MushroomZombie), "DestroyZombie")]
public static class ZombieDestroyPatch
{
private static readonly HashSet<int> StatueCreated = new HashSet<int>();
[HarmonyPrefix]
public static bool Prefix(MushroomZombie __instance)
{
//IL_0059: Unknown result type (might be due to invalid IL or missing references)
//IL_005f: Invalid comparison between Unknown and I4
if ((Object)(object)__instance == (Object)null || (Object)(object)((MonoBehaviourPun)__instance).photonView == (Object)null)
{
return true;
}
int viewID = ((MonoBehaviourPun)__instance).photonView.ViewID;
if (!ZombieSacrificeNetwork.WasKilledByDagger(viewID))
{
return true;
}
if (!PhotonNetwork.IsMasterClient || !((MonoBehaviourPun)__instance).photonView.IsMine || (int)__instance._currentState != 6)
{
return true;
}
if (!StatueCreated.Add(viewID))
{
return true;
}
try
{
Character component = ((Component)__instance).GetComponent<Character>();
if ((Object)(object)component == (Object)null)
{
StatueCreated.Remove(viewID);
return true;
}
component.SpawnStatue(true);
Debug.Log((object)("[ZombieDaggerMod] Dagger statue spawned. ID=" + viewID));
}
catch (Exception ex)
{
StatueCreated.Remove(viewID);
Debug.LogError((object)("[ZombieDaggerMod] Statue creation failed: " + ex));
}
return true;
}
}
[HarmonyPatch(typeof(MushroomZombie), "RPC_SpawnSkelly")]
public static class PreventZombieSkeletonPatch
{
[HarmonyPrefix]
public static bool Prefix(MushroomZombie __instance)
{
if ((Object)(object)__instance == (Object)null || (Object)(object)((MonoBehaviourPun)__instance).photonView == (Object)null)
{
return true;
}
int viewID = ((MonoBehaviourPun)__instance).photonView.ViewID;
return !ZombieSacrificeNetwork.WasKilledByDagger(viewID);
}
}
[HarmonyPatch(typeof(PetrifiedScout), "RPC_SpawnPetrifiedScout")]
public static class PetrifiedScoutZombiePosePatch
{
[HarmonyPrefix]
public static bool Prefix(PetrifiedScout __instance, int characterID, bool killedByDagger)
{
//IL_014d: Unknown result type (might be due to invalid IL or missing references)
//IL_0182: Unknown result type (might be due to invalid IL or missing references)
//IL_018e: Unknown result type (might be due to invalid IL or missing references)
if ((Object)(object)__instance == (Object)null)
{
return true;
}
Character val = null;
if (!Character.GetCharacterWithPhotonID(characterID, ref val))
{
foreach (Character allBotCharacter in Character.AllBotCharacters)
{
if ((Object)(object)allBotCharacter == (Object)null || (Object)(object)((MonoBehaviourPun)allBotCharacter).photonView == (Object)null || ((MonoBehaviourPun)allBotCharacter).photonView.ViewID != characterID)
{
continue;
}
val = allBotCharacter;
break;
}
}
if ((Object)(object)val == (Object)null || (Object)(object)((Component)val).GetComponent<MushroomZombie>() == (Object)null)
{
return true;
}
try
{
FieldInfo fieldInfo = AccessTools.Field(typeof(PetrifiedScout), "spawnedFromCharacter");
if (fieldInfo != null)
{
fieldInfo.SetValue(__instance, val);
}
MethodInfo methodInfo = AccessTools.Method(typeof(Character), "GetBodypart", (Type[])null, (Type[])null);
if (methodInfo == null)
{
return false;
}
Bodypart[] componentsInChildren = ((Component)__instance).GetComponentsInChildren<Bodypart>(true);
foreach (Bodypart val2 in componentsInChildren)
{
if (!((Object)(object)val2 == (Object)null))
{
object? obj = methodInfo.Invoke(val, new object[1] { val2.partType });
Bodypart val3 = (Bodypart)((obj is Bodypart) ? obj : null);
if (!((Object)(object)val3 == (Object)null))
{
val2.transform.SetPositionAndRotation(val3.transform.position, val3.transform.rotation);
}
}
}
if ((Object)(object)__instance.daggerTransform != (Object)null)
{
((Component)__instance.daggerTransform).gameObject.SetActive(killedByDagger);
}
return false;
}
catch (Exception ex)
{
Debug.LogError((object)("[ZombieDaggerMod] NPC statue setup failed: " + ex));
return false;
}
}
}