Decompiled source of SurvivorToCash v1.0.0

SurvivorToCash.dll

Decompiled 8 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: IgnoresAccessChecksTo("")]
[assembly: AssemblyCompany("REPOJP")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("zabuMod")]
[assembly: AssemblyTitle("zabuMod")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 SurvivorToCashMod
{
	[BepInPlugin("REPOJP.SurvivorToCash", "SurvivorToCash", "1.0.0")]
	public class SurvivorToCashPlugin : BaseUnityPlugin
	{
		internal enum CurrencyApplyMode
		{
			Add,
			Set
		}

		public const string PluginGuid = "REPOJP.SurvivorToCash";

		public const string PluginName = "SurvivorToCash";

		public const string PluginVersion = "1.0.0";

		internal static SurvivorToCashPlugin Instance = null;

		internal static ManualLogSource Log = null;

		internal static Harmony HarmonyInstance = null;

		internal static ConfigEntry<int> BonusPerAlivePlayer = null;

		internal static ConfigEntry<CurrencyApplyMode> ApplyMode = null;

		internal static ConfigEntry<bool> ShowBigMessage = null;

		internal static ConfigEntry<bool> WriteDebugLog = null;

		internal static string LastAppliedKey = string.Empty;

		private void Awake()
		{
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Expected O, but got Unknown
			//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b7: Expected O, but got Unknown
			Instance = this;
			Log = ((BaseUnityPlugin)this).Logger;
			BonusPerAlivePlayer = ((BaseUnityPlugin)this).Config.Bind<int>("General", "BonusPerAlivePlayer", 10, new ConfigDescription("Money value per surviving player at the very end of a successful level. / クリア成功レベルの最後の最後で生存していたプレイヤー1人あたりの金額", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 100000), Array.Empty<object>()));
			ApplyMode = ((BaseUnityPlugin)this).Config.Bind<CurrencyApplyMode>("General", "ApplyMode", CurrencyApplyMode.Add, "How to apply the calculated survivor cash to current run currency. Add = add to current money, Set = overwrite current money. / 計算した生存人数分の金額を現在のラン通貨へどう反映するか Add=現在額へ加算 Set=現在額を上書き");
			ShowBigMessage = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ShowBigMessage", true, "Show a big on-screen message when SurvivorToCash is applied. / SurvivorToCash適用時に画面中央へ大きな通知を表示");
			WriteDebugLog = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "WriteDebugLog", true, "Write SurvivorToCash calculation details to the BepInEx log. / SurvivorToCashの計算詳細をBepInExログへ出力");
			HarmonyInstance = new Harmony("REPOJP.SurvivorToCash");
			HarmonyInstance.PatchAll();
			Log.LogInfo((object)"SurvivorToCash 1.0.0 loaded");
		}
	}
	[HarmonyPatch(typeof(RunManager))]
	internal static class SurvivorToCashRunManagerPatch
	{
		[HarmonyPatch("ChangeLevel")]
		[HarmonyPrefix]
		private static void ChangeLevelPrefix(RunManager __instance, bool _completedLevel, bool _levelFailed, ChangeLevelType _changeLevelType)
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if (!ShouldApply(__instance, _completedLevel, _levelFailed, _changeLevelType))
				{
					return;
				}
				string text = BuildApplyKey(__instance);
				if (string.Equals(SurvivorToCashPlugin.LastAppliedKey, text, StringComparison.Ordinal))
				{
					if (SurvivorToCashPlugin.WriteDebugLog != null && SurvivorToCashPlugin.WriteDebugLog.Value)
					{
						SurvivorToCashPlugin.Log.LogInfo((object)("Skip duplicate apply: " + text));
					}
					return;
				}
				int num = CountAlivePlayers();
				int value = SurvivorToCashPlugin.BonusPerAlivePlayer.Value;
				int num2 = num * value;
				int num3 = SemiFunc.StatGetRunCurrency();
				int num4 = ((SurvivorToCashPlugin.ApplyMode.Value != SurvivorToCashPlugin.CurrencyApplyMode.Set) ? (num3 + num2) : num2);
				SemiFunc.StatSetRunCurrency(num4);
				SurvivorToCashPlugin.LastAppliedKey = text;
				if (SurvivorToCashPlugin.WriteDebugLog != null && SurvivorToCashPlugin.WriteDebugLog.Value)
				{
					SurvivorToCashPlugin.Log.LogInfo((object)("Applied SurvivorToCash | Level=" + GetLevelNameSafe(__instance) + " | Alive=" + num + " | Unit=" + value + " | Calculated=" + num2 + " | Mode=" + SurvivorToCashPlugin.ApplyMode.Value.ToString() + " | Before=" + num3 + " | After=" + num4));
				}
				ShowApplyMessage(num, num2);
			}
			catch (Exception ex)
			{
				if (SurvivorToCashPlugin.Log != null)
				{
					SurvivorToCashPlugin.Log.LogError((object)("Failure: ChangeLevelPrefix\n" + ex));
				}
			}
		}

		[HarmonyPatch("ResetProgress")]
		[HarmonyPostfix]
		private static void ResetProgressPostfix()
		{
			try
			{
				SurvivorToCashPlugin.LastAppliedKey = string.Empty;
			}
			catch (Exception ex)
			{
				if (SurvivorToCashPlugin.Log != null)
				{
					SurvivorToCashPlugin.Log.LogError((object)("Failure: ResetProgressPostfix\n" + ex));
				}
			}
		}

		private static bool ShouldApply(RunManager runManager, bool completedLevel, bool levelFailed, ChangeLevelType changeLevelType)
		{
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Invalid comparison between Unknown and I4
			if ((Object)(object)runManager == (Object)null)
			{
				return false;
			}
			if (SurvivorToCashPlugin.BonusPerAlivePlayer == null || SurvivorToCashPlugin.ApplyMode == null)
			{
				return false;
			}
			if (!completedLevel)
			{
				return false;
			}
			if (levelFailed)
			{
				return false;
			}
			if ((int)changeLevelType > 0)
			{
				return false;
			}
			if ((Object)(object)runManager.levelCurrent == (Object)null)
			{
				return false;
			}
			if ((Object)(object)runManager.levelCurrent == (Object)(object)runManager.levelLobby)
			{
				return false;
			}
			if ((Object)(object)runManager.levelCurrent == (Object)(object)runManager.levelShop)
			{
				return false;
			}
			if ((Object)(object)runManager.levelCurrent == (Object)(object)runManager.levelArena)
			{
				return false;
			}
			if ((Object)(object)runManager.levelCurrent == (Object)(object)runManager.levelMainMenu)
			{
				return false;
			}
			if ((Object)(object)runManager.levelCurrent == (Object)(object)runManager.levelLobbyMenu)
			{
				return false;
			}
			if ((Object)(object)runManager.levelCurrent == (Object)(object)runManager.levelTutorial)
			{
				return false;
			}
			if ((Object)(object)runManager.levelCurrent == (Object)(object)runManager.levelRecording)
			{
				return false;
			}
			if (runManager.restarting)
			{
				return false;
			}
			return true;
		}

		private static string BuildApplyKey(RunManager runManager)
		{
			string levelNameSafe = GetLevelNameSafe(runManager);
			int levelsCompleted = runManager.levelsCompleted;
			return levelNameSafe + "|" + levelsCompleted;
		}

		private static string GetLevelNameSafe(RunManager runManager)
		{
			if ((Object)(object)runManager == (Object)null)
			{
				return "UnknownLevel";
			}
			if ((Object)(object)runManager.levelCurrent == (Object)null)
			{
				return "UnknownLevel";
			}
			if (string.IsNullOrEmpty(((Object)runManager.levelCurrent).name))
			{
				return "UnknownLevel";
			}
			return ((Object)runManager.levelCurrent).name;
		}

		private static int CountAlivePlayers()
		{
			try
			{
				if ((Object)(object)GameDirector.instance == (Object)null)
				{
					return 0;
				}
				List<PlayerAvatar> playerList = GameDirector.instance.PlayerList;
				if (playerList == null)
				{
					return 0;
				}
				int num = 0;
				foreach (PlayerAvatar item in playerList)
				{
					if (!((Object)(object)item == (Object)null) && !item.isDisabled)
					{
						num++;
					}
				}
				return num;
			}
			catch (Exception ex)
			{
				if (SurvivorToCashPlugin.Log != null)
				{
					SurvivorToCashPlugin.Log.LogError((object)("Failure: CountAlivePlayers\n" + ex));
				}
				return 0;
			}
		}

		private static void ShowApplyMessage(int aliveCount, int calculatedValue)
		{
			//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if (SurvivorToCashPlugin.ShowBigMessage != null && SurvivorToCashPlugin.ShowBigMessage.Value && !((Object)(object)BigMessageUI.instance == (Object)null))
				{
					string text;
					string text2;
					if (SurvivorToCashPlugin.ApplyMode.Value == SurvivorToCashPlugin.CurrencyApplyMode.Set)
					{
						text = SemiFunc.DollarGetString(calculatedValue);
						text2 = "SET";
					}
					else
					{
						text = "+" + SemiFunc.DollarGetString(calculatedValue);
						text2 = "ADD";
					}
					string text3 = "SURVIVOR TO CASH\n" + aliveCount + " SURVIVORS  " + text2 + " " + text;
					BigMessageUI.instance.BigMessage(text3, "moneybag", 32f, Color.green, Color.yellow);
				}
			}
			catch (Exception ex)
			{
				if (SurvivorToCashPlugin.Log != null)
				{
					SurvivorToCashPlugin.Log.LogError((object)("Failure: ShowApplyMessage\n" + ex));
				}
			}
		}
	}
}