using Harmony; using QSB.Events; using QSB.Patches; using System.Collections.Generic; using System.Linq; using System.Reflection.Emit; namespace QSB.DeathSync.Patches { public class DeathPatches : QSBPatch { public override QSBPatchTypes Type => QSBPatchTypes.OnModStart; public override void DoPatches() { QSBCore.Helper.HarmonyHelper.AddPrefix("KillPlayer", typeof(DeathPatches), nameof(PreFinishDeathSequence)); QSBCore.Helper.HarmonyHelper.AddPostfix("KillPlayer", typeof(DeathPatches), nameof(BroadcastDeath)); QSBCore.Helper.HarmonyHelper.Transpile("Detach", typeof(DeathPatches), nameof(ReturnNull)); QSBCore.Helper.HarmonyHelper.Transpile("Detach", typeof(DeathPatches), nameof(ReturnNull)); QSBCore.Helper.HarmonyHelper.EmptyMethod("OnPressInteract"); QSBCore.Helper.HarmonyHelper.AddPostfix("Awake", typeof(DeathPatches), nameof(DamageController_Exploded)); QSBCore.Helper.HarmonyHelper.AddPrefix("VanishShip", typeof(DeathPatches), nameof(DestructionVolume_VanishShip)); } public static bool PreFinishDeathSequence(DeathType deathType) { if (RespawnOnDeath.Instance == null) { return true; } if (RespawnOnDeath.Instance.AllowedDeathTypes.Contains(deathType)) { return true; } RespawnOnDeath.Instance.ResetShip(); RespawnOnDeath.Instance.ResetPlayer(); return false; } public static void BroadcastDeath(DeathType deathType) => GlobalMessenger.FireEvent(EventNames.QSBPlayerDeath, deathType); public static void DamageController_Exploded(ref bool ____exploded) => ____exploded = true; public static IEnumerable ReturnNull(IEnumerable instructions) { return new List { new CodeInstruction(OpCodes.Ldnull), new CodeInstruction(OpCodes.Ret) }; } public static bool DestructionVolume_VanishShip(DeathType ____deathType) { if (RespawnOnDeath.Instance == null) { return true; } if (PlayerState.IsInsideShip() || PlayerState.UsingShipComputer() || PlayerState.AtFlightConsole()) { Locator.GetDeathManager().KillPlayer(____deathType); } // Ship is being destroyed, but player isn't in it. RespawnOnDeath.Instance.ResetShip(); return false; } } }