Decompiled source of SoloMode v1.0.0

LethalSoloMode.dll

Decompiled 27 minutes ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyVersion("0.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace LethalSoloMode
{
	[BepInPlugin("SoulMods.SoloMode", "Solo Mode", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "SoulMods.SoloMode";

		public const string Name = "Solo Mode";

		public const string Version = "1.0.0";

		public static ManualLogSource Log;

		public static ConfigEntry<bool> QuotaEnabled;

		public static ConfigEntry<float> QuotaMultiplier;

		public static ConfigEntry<bool> SecondChanceEnabled;

		public static ConfigEntry<int> ReviveHealth;

		public static ConfigEntry<bool> ShowHeart;

		public static ConfigEntry<float> HeartX;

		public static ConfigEntry<float> HeartY;

		public static ConfigEntry<float> HeartSize;

		private void Awake()
		{
			//IL_012e: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			((Object)((Component)this).gameObject).hideFlags = (HideFlags)61;
			QuotaEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("Quota", "Enabled", true, "Scale the profit quota. Only the host's setting matters (the host's game sets the quota).");
			QuotaMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("Quota", "Multiplier", 0.67f, "Starting quota and how fast it grows, compared with normal. 0.67 = about 2/3. An existing save keeps its current quota; the change applies from the next quota on.");
			SecondChanceEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("SecondChance", "Enabled", true, "Once per quota, a death sends you back to the ship instead - but only while it's on: type /lifeon in the ship terminal (/lifeoff saves it for later). Works on your own game even if others don't have the mod.");
			ReviveHealth = ((BaseUnityPlugin)this).Config.Bind<int>("SecondChance", "ReviveHealth", 20, "Health you wake up with in the ship.");
			ShowHeart = ((BaseUnityPlugin)this).Config.Bind<bool>("HUD", "ShowHeart", true, "Show a heart icon while your second chance is on and not used yet.");
			HeartX = ((BaseUnityPlugin)this).Config.Bind<float>("HUD", "HeartX", 36f, "Heart position from the left edge (pixels at 1080p).");
			HeartY = ((BaseUnityPlugin)this).Config.Bind<float>("HUD", "HeartY", 250f, "Heart position from the top edge (pixels at 1080p).");
			HeartSize = ((BaseUnityPlugin)this).Config.Bind<float>("HUD", "HeartSize", 34f, "Heart size (pixels at 1080p).");
			new Harmony("SoulMods.SoloMode").PatchAll();
			((Component)this).gameObject.AddComponent<HeartHud>();
			Log.LogInfo((object)"Solo Mode 1.0.0 loaded");
		}
	}
	[HarmonyPatch(typeof(TimeOfDay), "Awake")]
	internal static class QuotaPatch
	{
		private static void Prefix(TimeOfDay __instance)
		{
			SecondChance.ResetForNewSession();
			if (Plugin.QuotaEnabled.Value && __instance.quotaVariables != null)
			{
				float num = Mathf.Clamp(Plugin.QuotaMultiplier.Value, 0.05f, 5f);
				QuotaSettings quotaVariables = __instance.quotaVariables;
				int startingQuota = quotaVariables.startingQuota;
				float baseIncrease = quotaVariables.baseIncrease;
				quotaVariables.startingQuota = Mathf.Max(1, Mathf.RoundToInt((float)quotaVariables.startingQuota * num));
				quotaVariables.baseIncrease *= num;
				Plugin.Log.LogInfo((object)$"Quota scaled x{num}: starting {startingQuota} -> {quotaVariables.startingQuota}, base increase {baseIncrease} -> {quotaVariables.baseIncrease}");
			}
		}
	}
	[HarmonyPatch(typeof(PlayerControllerB), "KillPlayer")]
	internal static class SecondChance
	{
		private static int _usedForQuota = -1;

		private static bool _reviving;

		public static bool Armed { get; private set; }

		public static bool Ready
		{
			get
			{
				if (!Plugin.SecondChanceEnabled.Value || (Object)(object)TimeOfDay.Instance == (Object)null)
				{
					return false;
				}
				return _usedForQuota != TimeOfDay.Instance.timesFulfilledQuota;
			}
		}

		public static void ResetForNewSession()
		{
			_usedForQuota = -1;
			Armed = false;
		}

		public static bool TryArm()
		{
			if (!Ready)
			{
				return false;
			}
			Armed = true;
			return true;
		}

		public static void Disarm()
		{
			Armed = false;
		}

		private static bool Prefix(PlayerControllerB __instance, CauseOfDeath causeOfDeath)
		{
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			if (_reviving || !Ready || !Armed)
			{
				return true;
			}
			if (!((NetworkBehaviour)__instance).IsOwner || __instance.isPlayerDead || !__instance.AllowPlayerDeath())
			{
				return true;
			}
			if ((Object)(object)__instance != (Object)(object)GameNetworkManager.Instance.localPlayerController)
			{
				return true;
			}
			StartOfRound instance = StartOfRound.Instance;
			if ((Object)(object)instance == (Object)null || instance.inShipPhase || instance.shipIsLeaving)
			{
				return true;
			}
			try
			{
				_reviving = true;
				Revive(__instance, instance);
				_usedForQuota = TimeOfDay.Instance.timesFulfilledQuota;
				Armed = false;
				Plugin.Log.LogInfo((object)$"Second chance used (would have died: {causeOfDeath})");
				return false;
			}
			catch (Exception ex)
			{
				Plugin.Log.LogError((object)("Second chance failed, letting the death happen: " + ex));
				return true;
			}
			finally
			{
				_reviving = false;
			}
		}

		private static void Revive(PlayerControllerB p, StartOfRound round)
		{
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_0129: Unknown result type (might be due to invalid IL or missing references)
			//IL_012e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0156: 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_015d: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)p.inAnimationWithEnemy != (Object)null)
			{
				try
				{
					p.inAnimationWithEnemy.CancelSpecialAnimationWithPlayer();
				}
				catch
				{
				}
				p.inAnimationWithEnemy = null;
			}
			p.inSpecialInteractAnimation = false;
			p.DropAllHeldItemsAndSync(((Component)p).transform.position, p.localItemHolder.position, p.localItemHolder.eulerAngles, ((Component)p.playerEye).transform.position, ((Component)p.playerEye).transform.eulerAngles);
			p.ResetFallGravity();
			p.fallValue = 0f;
			p.fallValueUncapped = 0f;
			p.takingFallDamage = false;
			p.isSinking = false;
			p.sinkingValue = 0f;
			p.sourcesCausingSinking = 0;
			p.isUnderwater = false;
			p.isMovementHindered = 0;
			p.hinderedMultiplier = 1f;
			round.drowningTimer = 1f;
			if ((Object)(object)HUDManager.Instance != (Object)null)
			{
				HUDManager.Instance.setUnderwaterFilter = false;
			}
			AudioReverbPresets val = Object.FindObjectOfType<AudioReverbPresets>();
			if ((Object)(object)val != (Object)null)
			{
				val.audioPresets[3].ChangeAudioReverbForPlayer(p);
			}
			p.isInElevator = true;
			p.isInHangarShipRoom = true;
			p.isInsideFactory = false;
			p.averageVelocity = 0f;
			p.velocityLastFrame = Vector3.zero;
			TimeOfDay.Instance.SetInsideLightingDimness(false, true);
			Vector3 position = round.playerSpawnPositions[(int)p.playerClientId % round.playerSpawnPositions.Length].position;
			p.TeleportPlayer(position, false, 0f, false, true);
			p.health = Mathf.Clamp(Plugin.ReviveHealth.Value, 1, 100);
			if (p.criticallyInjured && p.health >= 10)
			{
				p.MakeCriticallyInjured(false);
			}
			if ((Object)(object)HUDManager.Instance != (Object)null)
			{
				HUDManager.Instance.UpdateHealthUI(p.health, false);
				HUDManager.Instance.DisplayTip("Second chance", "You were pulled back to the ship. Your items are where you fell. Recharges next quota (type /lifeon in the terminal).", true, false, "LC_Tip1");
			}
		}
	}
	[HarmonyPatch]
	internal static class LifeCommand
	{
		private static TerminalNode _onNode;

		private static TerminalNode _offNode;

		[HarmonyPatch(typeof(Terminal), "Start")]
		[HarmonyPostfix]
		private static void AddCommands(Terminal __instance)
		{
			try
			{
				_onNode = AddCommand(__instance, "lifeon", _onNode);
				_offNode = AddCommand(__instance, "lifeoff", _offNode);
				Plugin.Log.LogInfo((object)"Terminal commands /lifeon and /lifeoff added");
			}
			catch (Exception ex)
			{
				Plugin.Log.LogError((object)("Adding the life commands failed: " + ex));
			}
		}

		private static TerminalNode AddCommand(Terminal t, string word, TerminalNode existing)
		{
			TerminalKeyword val = ((IEnumerable<TerminalKeyword>)t.terminalNodes.allKeywords).FirstOrDefault((Func<TerminalKeyword, bool>)((TerminalKeyword k) => (Object)(object)k != (Object)null && k.word == word));
			if ((Object)(object)val != (Object)null)
			{
				return val.specialKeywordResult ?? existing;
			}
			TerminalNode val2 = ScriptableObject.CreateInstance<TerminalNode>();
			((Object)val2).name = "SoloMode_" + word;
			val2.displayText = "";
			val2.clearPreviousText = true;
			val2.maxCharactersToType = 25;
			TerminalKeyword val3 = ScriptableObject.CreateInstance<TerminalKeyword>();
			((Object)val3).name = "SoloMode_" + word + "_Keyword";
			val3.word = word;
			val3.isVerb = false;
			val3.specialKeywordResult = val2;
			t.terminalNodes.allKeywords = t.terminalNodes.allKeywords.Concat((IEnumerable<TerminalKeyword>)(object)new TerminalKeyword[1] { val3 }).ToArray();
			return val2;
		}

		[HarmonyPatch(typeof(Terminal), "LoadNewNode")]
		[HarmonyPrefix]
		private static void OnLoad(TerminalNode node)
		{
			if (!((Object)(object)node == (Object)null))
			{
				if ((Object)(object)node == (Object)(object)_onNode)
				{
					node.displayText = OnText() + "\n\n";
				}
				else if ((Object)(object)node == (Object)(object)_offNode)
				{
					node.displayText = OffText() + "\n\n";
				}
			}
		}

		private static string OnText()
		{
			if (!Plugin.SecondChanceEnabled.Value)
			{
				return "SECOND CHANCE\n\nThe second chance is turned off in the Solo Mode config.";
			}
			if (SecondChance.Armed)
			{
				return "SECOND CHANCE: ON\n\nAlready on. If you die, you will be pulled back to the ship.";
			}
			if (SecondChance.TryArm())
			{
				return "SECOND CHANCE: ON\n\nIf you die before this quota ends, you will be pulled back to the ship with low health. Your items will stay where you fell.\n\nOne use per quota. Type /lifeoff to save it for later.";
			}
			return UsedText();
		}

		private static string OffText()
		{
			if (!Plugin.SecondChanceEnabled.Value)
			{
				return "SECOND CHANCE\n\nThe second chance is turned off in the Solo Mode config.";
			}
			if (!SecondChance.Ready)
			{
				return UsedText();
			}
			SecondChance.Disarm();
			return "SECOND CHANCE: OFF\n\nSaved for later. A death now is a real death.\n\nType /lifeon to turn it back on.";
		}

		private static string UsedText()
		{
			int num = (((Object)(object)TimeOfDay.Instance != (Object)null) ? TimeOfDay.Instance.daysUntilDeadline : 0);
			return "SECOND CHANCE: USED\n\nYou already used it this quota. It recharges when the next quota starts" + ((num > 0) ? string.Format(" ({0} day{1} left).", num, (num == 1) ? "" : "s") : ".");
		}
	}
	public class HeartHud : MonoBehaviour
	{
		private Canvas _canvas;

		private RectTransform _rect;

		private RawImage _img;

		private void LateUpdate()
		{
			//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_013e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0145: Unknown result type (might be due to invalid IL or missing references)
			PlayerControllerB val = (((Object)(object)GameNetworkManager.Instance != (Object)null) ? GameNetworkManager.Instance.localPlayerController : null);
			StartOfRound instance = StartOfRound.Instance;
			bool flag = (Object)(object)val != (Object)null && (Object)(object)val.quickMenuManager != (Object)null && val.quickMenuManager.isMenuOpen;
			bool flag2 = Plugin.ShowHeart.Value && SecondChance.Ready && SecondChance.Armed && (Object)(object)val != (Object)null && !val.isPlayerDead && (Object)(object)instance != (Object)null && !flag && !val.inTerminalMenu;
			if (flag2 && (Object)(object)_canvas == (Object)null)
			{
				Build();
			}
			if (!((Object)(object)_canvas == (Object)null))
			{
				((Behaviour)_canvas).enabled = flag2;
				if (flag2)
				{
					float value = Plugin.HeartSize.Value;
					_rect.sizeDelta = new Vector2(value, value);
					_rect.anchoredPosition = new Vector2(Plugin.HeartX.Value, 0f - Plugin.HeartY.Value);
					float num = 1f + 0.06f * Mathf.Pow(Mathf.Max(0f, Mathf.Sin(Time.time * 2.4f)), 8f);
					((Transform)_rect).localScale = Vector3.one * num;
				}
			}
		}

		private void Build()
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Expected O, but got Unknown
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: Unknown result type (might be due to invalid IL or missing references)
			//IL_008c: Expected O, but got Unknown
			//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00db: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = new GameObject("SoloModeHUD");
			Object.DontDestroyOnLoad((Object)(object)val);
			_canvas = val.AddComponent<Canvas>();
			_canvas.renderMode = (RenderMode)0;
			_canvas.sortingOrder = 4;
			CanvasScaler obj = val.AddComponent<CanvasScaler>();
			obj.uiScaleMode = (ScaleMode)1;
			obj.referenceResolution = new Vector2(1920f, 1080f);
			obj.matchWidthOrHeight = 1f;
			GameObject val2 = new GameObject("Heart", new Type[2]
			{
				typeof(RectTransform),
				typeof(RawImage)
			});
			val2.transform.SetParent(val.transform, false);
			_rect = val2.GetComponent<RectTransform>();
			RectTransform rect = _rect;
			RectTransform rect2 = _rect;
			RectTransform rect3 = _rect;
			Vector2 val3 = default(Vector2);
			((Vector2)(ref val3))..ctor(0f, 1f);
			rect3.pivot = val3;
			Vector2 anchorMin = (rect2.anchorMax = val3);
			rect.anchorMin = anchorMin;
			_img = val2.GetComponent<RawImage>();
			_img.texture = (Texture)(object)MakeHeart(64);
		}

		private static Texture2D MakeHeart(int n)
		{
			//IL_0004: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Expected O, but got Unknown
			//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00df: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
			Texture2D val = new Texture2D(n, n, (TextureFormat)4, false)
			{
				filterMode = (FilterMode)0,
				wrapMode = (TextureWrapMode)1
			};
			Color val2 = default(Color);
			((Color)(ref val2))..ctor(1f, 0.42f, 0.08f, 0.95f);
			Color val3 = default(Color);
			((Color)(ref val3))..ctor(0.45f, 0.12f, 0.02f, 0.95f);
			for (int i = 0; i < n; i++)
			{
				for (int j = 0; j < n; j++)
				{
					float num = ((float)j + 0.5f) / (float)n * 2.6f - 1.3f;
					float num2 = ((float)i + 0.5f) / (float)n * 2.6f - 1.4f;
					float num3 = Mathf.Pow(num * num + num2 * num2 - 1f, 3f) - num * num * num2 * num2 * num2;
					Color val4 = Color.clear;
					if (num3 <= 0f)
					{
						val4 = ((num3 > -0.12f) ? val3 : val2);
					}
					val.SetPixel(j, i, val4);
				}
			}
			val.Apply();
			return val;
		}
	}
}