using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using Permadeath.Patches;
using Unity.Netcode;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("Permadeath")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Permadeath")]
[assembly: AssemblyCopyright("Copyright © 2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("7457d4cf-66db-4992-a8a2-53cdb87b0fe7")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Permadeath
{
[BepInPlugin("Sproutian.Permadeath", "Permadeath", "1.0.0.0")]
public class PermadeathMod : BaseUnityPlugin
{
private const string modGUID = "Sproutian.Permadeath";
private const string modName = "Permadeath";
private const string modVersion = "1.0.0.0";
private readonly Harmony harmony = new Harmony("Sproutian.Permadeath");
private static PermadeathMod Instance;
internal ManualLogSource mls;
private void Awake()
{
if ((Object)(object)Instance == (Object)null)
{
Instance = this;
}
mls = Logger.CreateLogSource("Sproutian.Permadeath");
mls.LogInfo((object)"Sproutian's Permadeath loaded");
harmony.PatchAll(typeof(PermadeathMod));
harmony.PatchAll(typeof(StartOfRoundPatch));
harmony.PatchAll(typeof(HUDManagerPatch));
}
}
}
namespace Permadeath.Patches
{
[HarmonyPatch(typeof(HUDManager))]
internal class HUDManagerPatch
{
[HarmonyPatch("HideHUD")]
[HarmonyPrefix]
private static bool HideHUDPatch(bool hide)
{
PlayerControllerB localPlayerController = GameNetworkManager.Instance.localPlayerController;
if (localPlayerController.isPlayerDead && !hide)
{
return false;
}
return true;
}
}
[HarmonyPatch(typeof(StartOfRound))]
internal class StartOfRoundPatch
{
[HarmonyPatch("ReviveDeadPlayers")]
[HarmonyPrefix]
private static bool revivePlayersPatch(ref bool ___shipIsLeaving, ref PlayerControllerB[] ___allPlayerScripts, ref StartOfRound __instance, out PlayerControllerB[] __state)
{
//IL_0222: Unknown result type (might be due to invalid IL or missing references)
__state = ___allPlayerScripts;
TimeOfDay val = Object.FindObjectOfType<TimeOfDay>();
int daysUntilDeadline = val.daysUntilDeadline;
if (daysUntilDeadline == 0 || (daysUntilDeadline == 1 && ___shipIsLeaving) || (daysUntilDeadline == 3 && !___shipIsLeaving))
{
return true;
}
List<PlayerControllerB> list = new List<PlayerControllerB>();
DeadBodyInfo[] array = Object.FindObjectsOfType<DeadBodyInfo>();
for (int i = 0; i < array.Length; i++)
{
if (!array[i].isInShip)
{
continue;
}
for (int j = 0; j < ___allPlayerScripts.Length; j++)
{
if (___allPlayerScripts[j].isPlayerDead && (Object)(object)___allPlayerScripts[j].deadBody == (Object)(object)array[i])
{
list.Add(___allPlayerScripts[j]);
j = list.Count;
}
}
}
PlayerControllerB[] array2 = (PlayerControllerB[])(object)new PlayerControllerB[list.Count];
for (int k = 0; k < list.Count; k++)
{
array2[k] = list[k];
}
__instance.allPlayersDead = false;
for (int l = 0; l < array2.Length; l++)
{
Debug.Log((object)"Reviving players A");
array2[l].ResetPlayerBloodObjects(array2[l].isPlayerDead);
if (!array2[l].isPlayerDead && !array2[l].isPlayerControlled)
{
continue;
}
array2[l].isClimbingLadder = false;
array2[l].ResetZAndXRotation();
((Collider)array2[l].thisController).enabled = true;
array2[l].health = 100;
array2[l].disableLookInput = false;
Debug.Log((object)"Reviving players B");
if (array2[l].isPlayerDead)
{
array2[l].isPlayerDead = false;
array2[l].isPlayerControlled = true;
array2[l].isInElevator = true;
array2[l].isInHangarShipRoom = true;
array2[l].isInsideFactory = false;
array2[l].wasInElevatorLastFrame = false;
__instance.SetPlayerObjectExtrapolate(false);
array2[l].TeleportPlayer(((Component)array2[l].deadBody).transform.position, false, 0f, false, true);
array2[l].setPositionOfDeadPlayer = false;
array2[l].DisablePlayerModel(((Component)array2[l]).gameObject, true, true);
((Behaviour)array2[l].helmetLight).enabled = false;
Debug.Log((object)"Reviving players C");
array2[l].Crouch(false);
array2[l].criticallyInjured = false;
if ((Object)(object)array2[l].playerBodyAnimator != (Object)null)
{
array2[l].playerBodyAnimator.SetBool("Limp", false);
}
array2[l].bleedingHeavily = false;
array2[l].activatingItem = false;
array2[l].twoHanded = false;
array2[l].inSpecialInteractAnimation = false;
array2[l].disableSyncInAnimation = false;
array2[l].inAnimationWithEnemy = null;
array2[l].holdingWalkieTalkie = false;
array2[l].speakingToWalkieTalkie = false;
Debug.Log((object)"Reviving players D");
array2[l].isSinking = false;
array2[l].isUnderwater = false;
array2[l].sinkingValue = 0f;
array2[l].statusEffectAudio.Stop();
array2[l].DisableJetpackControlsLocally();
array2[l].health = 100;
Debug.Log((object)"Reviving players E");
array2[l].mapRadarDotAnimator.SetBool("dead", false);
if (((NetworkBehaviour)array2[l]).IsOwner)
{
HUDManager.Instance.gasHelmetAnimator.SetBool("gasEmitting", false);
array2[l].hasBegunSpectating = false;
HUDManager.Instance.RemoveSpectateUI();
HUDManager.Instance.gameOverAnimator.SetTrigger("revive");
array2[l].hinderedMultiplier = 1f;
array2[l].isMovementHindered = 0;
array2[l].sourcesCausingSinking = 0;
Debug.Log((object)"Reviving players E2");
array2[l].reverbPreset = __instance.shipReverb;
}
}
Debug.Log((object)"Reviving players F");
SoundManager.Instance.earsRingingTimer = 0f;
array2[l].voiceMuffledByEnemy = false;
SoundManager.Instance.playerVoicePitchTargets[l] = 1f;
SoundManager.Instance.SetPlayerPitch(1f, l);
if ((Object)(object)array2[l].currentVoiceChatIngameSettings == (Object)null)
{
__instance.RefreshPlayerVoicePlaybackObjects();
}
if ((Object)(object)array2[l].currentVoiceChatIngameSettings != (Object)null)
{
if ((Object)(object)array2[l].currentVoiceChatIngameSettings.voiceAudio == (Object)null)
{
array2[l].currentVoiceChatIngameSettings.InitializeComponents();
}
if ((Object)(object)array2[l].currentVoiceChatIngameSettings.voiceAudio == (Object)null)
{
return false;
}
((Component)array2[l].currentVoiceChatIngameSettings.voiceAudio).GetComponent<OccludeAudio>().overridingLowPass = false;
}
Debug.Log((object)"Reviving players G");
}
PlayerControllerB localPlayerController = GameNetworkManager.Instance.localPlayerController;
if (!localPlayerController.isPlayerDead)
{
localPlayerController.bleedingHeavily = false;
localPlayerController.criticallyInjured = false;
localPlayerController.playerBodyAnimator.SetBool("Limp", false);
localPlayerController.health = 100;
HUDManager.Instance.UpdateHealthUI(100, false);
localPlayerController.spectatedPlayerScript = null;
((Behaviour)HUDManager.Instance.audioListenerLowPass).enabled = false;
__instance.SetSpectateCameraToGameOverMode(false, localPlayerController);
}
Debug.Log((object)"Reviving players H");
RagdollGrabbableObject[] array3 = Object.FindObjectsOfType<RagdollGrabbableObject>();
for (int m = 0; m < array3.Length; m++)
{
if (!((GrabbableObject)array3[m]).isHeld)
{
if (((NetworkBehaviour)__instance).IsServer)
{
if (((NetworkBehaviour)array3[m]).NetworkObject.IsSpawned)
{
((NetworkBehaviour)array3[m]).NetworkObject.Despawn(true);
}
else
{
Object.Destroy((Object)(object)((Component)array3[m]).gameObject);
}
}
}
else if (((GrabbableObject)array3[m]).isHeld && (Object)(object)((GrabbableObject)array3[m]).playerHeldBy != (Object)null)
{
((GrabbableObject)array3[m]).playerHeldBy.DropAllHeldItems(true, false);
}
}
DeadBodyInfo[] array4 = Object.FindObjectsOfType<DeadBodyInfo>();
for (int n = 0; n < array4.Length; n++)
{
Object.Destroy((Object)(object)((Component)array4[n]).gameObject);
}
__instance.livingPlayers = __instance.connectedPlayersAmount + 1;
__instance.allPlayersDead = false;
__instance.UpdatePlayerVoiceEffects();
__instance.shipAnimator.ResetTrigger("ShipLeave");
for (int num = 0; num < ___allPlayerScripts.Length; num++)
{
if (___allPlayerScripts[num].isPlayerDead)
{
StartOfRound obj = __instance;
obj.livingPlayers--;
}
}
return false;
}
[HarmonyPatch("ReviveDeadPlayers")]
[HarmonyPostfix]
private static void revivePlayersPostfix(ref PlayerControllerB[] ___allPlayerScripts, PlayerControllerB[] __state)
{
___allPlayerScripts = __state;
}
[HarmonyPatch("EndOfGame")]
[HarmonyPostfix]
private static void endofgamePostfix(ref StartOfRound __instance, ref PlayerControllerB[] ___allPlayerScripts)
{
if (!((NetworkBehaviour)__instance).IsOwner)
{
return;
}
bool flag = true;
for (int i = 0; i < ___allPlayerScripts.Length; i++)
{
if ((___allPlayerScripts[i].isPlayerDead || ___allPlayerScripts[i].isPlayerControlled) && !___allPlayerScripts[i].isPlayerDead)
{
flag = false;
}
}
if (flag)
{
__instance.FirePlayersAfterDeadlineClientRpc(new int[4]
{
__instance.gameStats.daysSpent,
__instance.gameStats.scrapValueCollected,
__instance.gameStats.deaths,
__instance.gameStats.allStepsTaken
}, false);
}
}
}
}