using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
[assembly: AssemblyMetadata("Notice", "Copyright (c) casualexpie. All rights reserved. Do NOT decompile, disassemble, or reverse-engineer this assembly. Report bug symptoms on the Nexus Bugs page; do not post decompiled or reconstructed source. Reverse-engineering this DLL is not authorized.")]
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace AlwaysGivenAChance;
[BepInPlugin("alwaysgivenachance", "Always Given a Chance", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
public const string Version = "1.0.0";
internal static ManualLogSource Log;
internal static ConfigEntry<bool> Enabled;
internal static ConfigEntry<int> Chance;
internal static ConfigEntry<bool> EveryTime;
internal static Body Failed;
private void Awake()
{
//IL_0050: Unknown result type (might be due to invalid IL or missing references)
//IL_005a: Expected O, but got Unknown
//IL_0084: Unknown result type (might be due to invalid IL or missing references)
//IL_008a: Expected O, but got Unknown
//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
//IL_0102: Unknown result type (might be due to invalid IL or missing references)
//IL_010f: Expected O, but got Unknown
//IL_010f: Expected O, but got Unknown
Log = ((BaseUnityPlugin)this).Logger;
Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Change the last stand roll.");
Chance = ((BaseUnityPlugin)this).Config.Bind<int>("General", "Chance", 100, new ConfigDescription("Chance in percent that your last stand roll succeeds. 100 = always.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 100), new object[0]));
EveryTime = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "EveryTime", true, "true: you get a last stand roll every time you're dying, not just once per run. false: once per run, like vanilla.");
GameObject val = new GameObject("AlwaysGivenAChanceRunner");
((Object)val).hideFlags = (HideFlags)61;
Object.DontDestroyOnLoad((Object)(object)val);
val.AddComponent<Runner>();
int num = 0;
try
{
MethodInfo methodInfo = AccessTools.Method(typeof(Body), "TryLastStand", (Type[])null, (Type[])null);
if (methodInfo == null)
{
Log.LogWarning((object)"Couldn't find Body.TryLastStand, the mod can't work on this game version.");
}
else
{
new Harmony("alwaysgivenachance").Patch((MethodBase)methodInfo, new HarmonyMethod(typeof(Plugin), "RollPrefix", (Type[])null), new HarmonyMethod(typeof(Plugin), "RollPostfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
num = 1;
}
}
catch (Exception ex)
{
Log.LogWarning((object)("Couldn't patch Body.TryLastStand: " + ex.Message));
}
Log.LogInfo((object)("Always Given a Chance 1.0.0 loaded (" + num + "/1 hooks). Chance " + Chance.Value + "%, " + (EveryTime.Value ? "every time" : "once per run") + ". Game version " + Application.version + "."));
}
private static void RollPrefix(Body __instance, out AnimationCurve __state)
{
__state = null;
if (Enabled.Value && !((Object)(object)PlayerCamera.main == (Object)null) && !((Object)(object)PlayerCamera.main.body != (Object)(object)__instance))
{
int num = Mathf.Clamp(Chance.Value, 0, 100);
__state = __instance.lastLastChanceHappiness;
float num2 = ((num >= 100) ? 2f : ((float)num / 100f));
__instance.lastLastChanceHappiness = AnimationCurve.Constant(-1000f, 1000f, num2);
}
}
private static void RollPostfix(Body __instance, AnimationCurve __state)
{
if (__state == null)
{
return;
}
__instance.lastLastChanceHappiness = __state;
bool flag = __instance.lastStandTime >= 299f;
if (EveryTime.Value)
{
if (flag)
{
__instance.triedRollingLastStand = false;
}
else
{
Failed = __instance;
}
}
Log.LogInfo((object)(flag ? "Last stand roll: you were given a chance." : ("Last stand roll failed (" + Chance.Value + "% chance).")));
}
}
internal class Runner : MonoBehaviour
{
private void Update()
{
Body failed = Plugin.Failed;
if (!((Object)(object)failed == (Object)null))
{
if (!Plugin.Enabled.Value || !Plugin.EveryTime.Value || !failed.alive)
{
Plugin.Failed = null;
}
else if (failed.brainHealth > 15f)
{
failed.triedRollingLastStand = false;
Plugin.Failed = null;
}
}
}
}