Decompiled source of StaminaSystem v1.0.1

plugins/StaminaSystem.dll

Decompiled a month ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using HarmonyLib;
using Il2CppSystem.Collections.Generic;
using SOD.Common;
using StaminaSystem;
using UnityEngine;
using UnityEngine.UI;
using UnityStandardAssets.Characters.FirstPerson;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("StaminaSystem")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+4ee4c8bf1b327ab921b4c3875354113647261849")]
[assembly: AssemblyProduct("StaminaSystem")]
[assembly: AssemblyTitle("StaminaSystem")]
[assembly: AssemblyVersion("1.0.0.0")]
public static class CustomColors
{
	public static Color Transparent => new Color(1f, 1f, 1f, 0f);

	public static Color Red => new Color(1f, 0f, 0f, 1f);

	public static Color Green => new Color(0f, 1f, 0f, 1f);
}
public class NewGameHandler
{
	public static ManualLogSource Logger;

	public NewGameHandler()
	{
		Lib.SaveGame.OnAfterLoad += HandleGameLoaded;
		Lib.SaveGame.OnAfterNewGame += HandleNewGameStarted;
	}

	private void HandleNewGameStarted(object sender, EventArgs e)
	{
		StaminaBar.isGameLoaded = true;
	}

	private void HandleGameLoaded(object sender, EventArgs e)
	{
		StaminaBar.isGameLoaded = true;
	}
}
public class PlayerInfoProvider
{
	public Vector3 GetPlayerLocation()
	{
		//IL_0008: Unknown result type (might be due to invalid IL or missing references)
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		//IL_0015: Unknown result type (might be due to invalid IL or missing references)
		Player instance = Player.Instance;
		return Vector3Int.op_Implicit(((Actor)instance).currentNodeCoord);
	}

	public NewNode PlayerNode()
	{
		Player instance = Player.Instance;
		return ((Actor)instance).currentNode;
	}

	public bool GetIsRunning()
	{
		Player instance = Player.Instance;
		return ((Actor)instance).isRunning;
	}

	public void SetIsRunning(bool isRunning)
	{
		Player instance = Player.Instance;
		((Actor)instance).isRunning = isRunning;
	}

	public bool GetHasJumped()
	{
		FirstPersonController val = Object.FindObjectOfType<FirstPersonController>();
		return val.m_Jump;
	}

	public bool GetIsJumping()
	{
		FirstPersonController val = Object.FindObjectOfType<FirstPersonController>();
		return val.m_Jumping;
	}

	public bool GetIsGrounded()
	{
		Player instance = Player.Instance;
		return instance.isGrounded;
	}

	public float GetMovementRunSpeed()
	{
		GameplayControls instance = GameplayControls.Instance;
		return instance.playerRunSpeed;
	}

	public void SetMovementRunSpeed(float setMovementRunSpeed)
	{
		GameplayControls instance = GameplayControls.Instance;
		instance.playerRunSpeed = setMovementRunSpeed;
	}

	public float GetMovementWalkSpeed()
	{
		GameplayControls instance = GameplayControls.Instance;
		return instance.playerWalkSpeed;
	}

	public void SetMovementWalkSpeed(float setMovementWalkSpeed)
	{
		GameplayControls instance = GameplayControls.Instance;
		instance.playerWalkSpeed = setMovementWalkSpeed;
	}

	public float GetCurrentHealth()
	{
		Player instance = Player.Instance;
		return ((Actor)instance).currentHealth;
	}

	public void SetCurrentHealth(float health)
	{
		Player instance = Player.Instance;
		((Actor)instance).currentHealth = health;
	}

	public bool IsOnDuty()
	{
		Player instance = Player.Instance;
		return ((Actor)instance).isOnDuty;
	}

	public bool IsCurrentlyInAutoTravel()
	{
		Player instance = Player.Instance;
		return instance.autoTravelActive;
	}

	public float GetBlackEye()
	{
		Player instance = Player.Instance;
		return ((Human)instance).blackEye;
	}

	public void SetBlackEye(float amount)
	{
		Player instance = Player.Instance;
		((Human)instance).blackEye = amount;
	}

	public float GetBlackedOut()
	{
		Player instance = Player.Instance;
		return ((Human)instance).blackedOut;
	}

	public void SetBlackedOut(float amount)
	{
		Player instance = Player.Instance;
		((Human)instance).blackedOut = amount;
	}

	public Telephone GetAnsweringPhone()
	{
		Player instance = Player.Instance;
		return instance.answeringPhone;
	}

	public List<NewAddress> ApartmentsOwned()
	{
		Player instance = Player.Instance;
		return instance.apartmentsOwned;
	}
}
namespace StaminaSystem;

public class CreateStaminaBar
{
	public static void CreateBar()
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Expected O, but got Unknown
		//IL_0075: Unknown result type (might be due to invalid IL or missing references)
		//IL_008b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
		//IL_00da: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e1: Expected O, but got Unknown
		//IL_010f: Unknown result type (might be due to invalid IL or missing references)
		//IL_011f: Unknown result type (might be due to invalid IL or missing references)
		//IL_012f: Unknown result type (might be due to invalid IL or missing references)
		//IL_013f: Unknown result type (might be due to invalid IL or missing references)
		//IL_015b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0185: Unknown result type (might be due to invalid IL or missing references)
		//IL_019c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0025: Unknown result type (might be due to invalid IL or missing references)
		GameObject val = new GameObject("StaminaBarBackground");
		Canvas val2 = Object.FindObjectOfType<Canvas>();
		if ((Object)(object)val2 == (Object)null)
		{
			val2 = new GameObject("Canvas").AddComponent<Canvas>();
			val2.renderMode = (RenderMode)0;
			((Component)val2).gameObject.AddComponent<CanvasScaler>();
			((Component)val2).gameObject.AddComponent<GraphicRaycaster>();
		}
		val.transform.SetParent(((Component)val2).transform);
		RectTransform val3 = val.AddComponent<RectTransform>();
		val3.sizeDelta = new Vector2(0f, 0f);
		val3.anchorMin = new Vector2(0.5f, 0f);
		val3.anchorMax = new Vector2(0.5f, 0f);
		val3.anchoredPosition = new Vector2(0f, 20f);
		Image val4 = val.AddComponent<Image>();
		((Graphic)val4).color = Color.black;
		GameObject val5 = new GameObject("StaminaFill");
		val5.transform.SetParent(val.transform);
		StaminaBar.staminaFillRect = val5.AddComponent<RectTransform>();
		StaminaBar.staminaFillRect.sizeDelta = new Vector2(500f, 10f);
		StaminaBar.staminaFillRect.anchorMin = Vector2.zero;
		StaminaBar.staminaFillRect.anchorMax = Vector2.one;
		StaminaBar.staminaFillRect.anchoredPosition = Vector2.zero;
		StaminaBar.staminaFill = val5.AddComponent<Image>();
		((Graphic)StaminaBar.staminaFill).color = StaminaBar.currentColor;
		Outline val6 = val5.AddComponent<Outline>();
		((Shadow)val6).effectColor = new Color(0f, 0f, 0f, 1f);
		((Shadow)val6).effectDistance = new Vector2(4f, 4f);
	}

	public static void UpdateStamina(float amount)
	{
		//IL_0068: Unknown result type (might be due to invalid IL or missing references)
		StaminaBar.currentStamina += amount;
		StaminaBar.currentStamina = Mathf.Clamp(StaminaBar.currentStamina, 0f, StaminaBar.maxStamina);
		if (StaminaBar.staminaBarCreated)
		{
			StaminaBar.staminaFill.fillAmount = StaminaBar.currentStamina / StaminaBar.maxStamina;
			float num = StaminaBar.currentStamina / StaminaBar.maxStamina * 500f;
			((Graphic)StaminaBar.staminaFill).rectTransform.sizeDelta = new Vector2(num, 10f);
		}
	}

	public static void ResetStamina()
	{
		//IL_001a: Unknown result type (might be due to invalid IL or missing references)
		StaminaBar.currentStamina = StaminaBar.maxStamina;
		StaminaBar.staminaFillRect.sizeDelta = new Vector2(500f, 10f);
	}
}
[BepInPlugin("StaminaSystem", "StaminaSystem", "1.0.1")]
public class StaminaSystem : BasePlugin
{
	public static ManualLogSource Logger;

	private Harmony harmony;

	public static ConfigEntry<float> staminaDrain;

	public static ConfigEntry<float> staminaRegain;

	public static ConfigEntry<float> staminaJumpDrain;

	public static ConfigEntry<float> minStamToRunJump;

	public static ConfigEntry<bool> staminaBar;

	public override void Load()
	{
		//IL_010f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0115: Expected O, but got Unknown
		//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e3: Expected O, but got Unknown
		Logger = ((BasePlugin)this).Log;
		NewGameHandler newGameHandler = new NewGameHandler();
		Logger.LogInfo((object)"Loading Stamina System...");
		staminaDrain = ((BasePlugin)this).Config.Bind<float>("Stamina Drain", "Drain Speed", -20f, "How fast stamina drains.");
		staminaRegain = ((BasePlugin)this).Config.Bind<float>("Stamina Regain", "Regain Speed", 20f, "How fast stamina regains.");
		staminaJumpDrain = ((BasePlugin)this).Config.Bind<float>("Stamina Jump Drain", "Jump Drain", -15f, "How much stamina drained by jumping.");
		minStamToRunJump = ((BasePlugin)this).Config.Bind<float>("Allowed Run/Jump At", "Minimum", 35f, "Minimum amount of stamina before allowing player to run/jump again.");
		staminaBar = ((BasePlugin)this).Config.Bind<bool>("Stamina Bar", "Enabled", true, "Show's a visible stamina bar at the bottom of the screen.");
		try
		{
			harmony = new Harmony("LifeIsHard");
			harmony.PatchAll();
			Logger.LogInfo((object)"All patches applied.");
		}
		catch (Exception ex)
		{
			ManualLogSource logger = Logger;
			bool flag = default(bool);
			BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(19, 1, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Error during Load: ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<Exception>(ex);
			}
			logger.LogError(val);
		}
	}
}
[HarmonyPatch(typeof(SessionData))]
[HarmonyPatch("PauseGame")]
public class PauseManager
{
	public static void Prefix(ref bool showPauseText, ref bool delayOverride, ref bool openDesktopMode)
	{
		StaminaBar.isGamePaused = true;
	}
}
[HarmonyPatch(typeof(SessionData))]
[HarmonyPatch("ResumeGame")]
public class ResumeGameManager
{
	public static void Prefix()
	{
		StaminaBar.isGamePaused = false;
	}
}
[HarmonyPatch(typeof(Player), "Update")]
public class StaminaBar
{
	public static bool staminaBarCreated;

	private static bool startedRunningOverStam;

	public static bool isGamePaused = false;

	public static bool isGameLoaded = false;

	public static bool isPlayerJumping;

	public static Image staminaFill;

	public static Color currentColor;

	public static float maxStamina = 100f;

	public static float currentStamina;

	public static RectTransform staminaFillRect;

	private static PlayerInfoProvider playerInfo;

	private static FirstPersonController control;

	[HarmonyPrefix]
	public static void Prefix(Player __instance)
	{
		//IL_005a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0151: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_025b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0215: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ae: Unknown result type (might be due to invalid IL or missing references)
		if (!staminaBarCreated)
		{
			playerInfo = new PlayerInfoProvider();
			control = Object.FindObjectOfType<FirstPersonController>();
			currentStamina = maxStamina;
			CreateStaminaBar.CreateBar();
			staminaBarCreated = true;
		}
		if (!StaminaSystem.staminaBar.Value && staminaBarCreated)
		{
			((Graphic)staminaFill).color = CustomColors.Transparent;
		}
		if (playerInfo.GetIsRunning() && !isGamePaused)
		{
			if (currentStamina > StaminaSystem.minStamToRunJump.Value)
			{
				startedRunningOverStam = true;
			}
			CreateStaminaBar.UpdateStamina(StaminaSystem.staminaDrain.Value * Time.deltaTime);
		}
		else if (!playerInfo.GetIsRunning() && !isGamePaused && isGameLoaded)
		{
			CreateStaminaBar.UpdateStamina(StaminaSystem.staminaRegain.Value * Time.deltaTime);
			startedRunningOverStam = false;
		}
		if (currentStamina <= 0f && !isGamePaused && isGameLoaded)
		{
			StatusController.Instance.disabledSprint = true;
			StatusController.Instance.disabledJump = true;
			control.m_Jump = false;
			if (staminaBarCreated)
			{
				((Graphic)staminaFill).color = CustomColors.Red;
			}
		}
		else if (currentStamina < StaminaSystem.minStamToRunJump.Value && !startedRunningOverStam && !isGamePaused && isGameLoaded)
		{
			StatusController.Instance.disabledSprint = true;
			StatusController.Instance.disabledJump = true;
			control.m_Jump = false;
			if (staminaBarCreated)
			{
				((Graphic)staminaFill).color = CustomColors.Red;
			}
		}
		else if (!isGamePaused && isGameLoaded)
		{
			StatusController.Instance.disabledSprint = false;
			StatusController.Instance.disabledJump = false;
			if (staminaBarCreated)
			{
				((Graphic)staminaFill).color = CustomColors.Green;
			}
		}
		if (isGamePaused && isGameLoaded && currentStamina != maxStamina && staminaBarCreated)
		{
			((Graphic)staminaFill).color = CustomColors.Transparent;
		}
		if (playerInfo.GetIsJumping())
		{
			CreateStaminaBar.UpdateStamina(StaminaSystem.staminaJumpDrain.Value);
			control.m_Jumping = false;
		}
		if (__instance.playerKOInProgress)
		{
			CreateStaminaBar.ResetStamina();
			((Graphic)staminaFill).color = CustomColors.Transparent;
		}
		if (currentStamina == maxStamina)
		{
			((Graphic)staminaFill).CrossFadeAlpha(0f, 0.2f, true);
		}
		else if (currentStamina != maxStamina)
		{
			((Graphic)staminaFill).CrossFadeAlpha(1f, 1f, true);
		}
	}
}