Decompiled source of CustomDifficulty v1.0.0

CustomDifficulty.dll

Decompiled 3 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using CustomDifficulty.Challenges;
using DiskCardGame;
using HarmonyLib;
using InscryptionAPI.Ascension;
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.0", FrameworkDisplayName = ".NET Standard 2.0")]
[assembly: AssemblyCompany("Arina Orlova (StillRinny)")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyCopyright("Arina Orlova (StillRinny)")]
[assembly: AssemblyDescription("A Custom Difficulty Inscryption mod")]
[assembly: AssemblyFileVersion("1.2")]
[assembly: AssemblyInformationalVersion("1.0+63215799dfcd5672cc7110ba52fabf92930dd74d")]
[assembly: AssemblyProduct("Custom Difficulty Inscryption Mod")]
[assembly: AssemblyTitle("CustomDifficulty")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace CustomDifficulty
{
	internal static class Config
	{
		internal enum Difficulty
		{
			Custom,
			Random,
			Easy,
			Normal,
			Hard,
			Annoying
		}

		private static ConfigEntry<bool> _shouldAddChallenges;

		private static ConfigEntry<Difficulty> _currentDifficulty;

		private static Difficulty _currentDifficultyValue;

		private static ConfigEntry<byte> _minDifficulty;

		private static ConfigEntry<byte> _maxDifficulty;

		private static ConfigEntry<sbyte> _difficultyModifier;

		private static ConfigEntry<Vector3> _randomState;

		internal static bool ShouldAddChallenges => _shouldAddChallenges.Value;

		internal static Difficulty CurrentDifficulty
		{
			get
			{
				return _currentDifficultyValue;
			}
			set
			{
				_currentDifficultyValue = value;
				SetupBlueprint();
			}
		}

		internal static int MinDifficulty { get; private set; }

		internal static int MaxDifficulty { get; private set; }

		internal static int DifficultyModifier { get; private set; }

		internal static void Initialize(Plugin plugin)
		{
			//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
			_shouldAddChallenges = ((BaseUnityPlugin)plugin).Config.Bind<bool>("Сhallenges", "Add Challenges", true, "Should add custom KCM challenges?");
			_currentDifficulty = ((BaseUnityPlugin)plugin).Config.Bind<Difficulty>("Base Presets", "Difficulty", Difficulty.Normal, "");
			_minDifficulty = ((BaseUnityPlugin)plugin).Config.Bind<byte>("Manual Setting", "Min difficulty value", (byte)0, "Difficulty will be not less than this value. \nTip: Parameter \"Difficulty\" must be \"Custom\"\nTip: Value greater than 20 will make opponent don't place cards.");
			_maxDifficulty = ((BaseUnityPlugin)plugin).Config.Bind<byte>("Manual Setting", "Max difficulty value", (byte)20, "Difficulty will be not less than this value. \nTip: Parameter \"Difficulty\" must be \"Custom\"!\nTip: Value greater than 20 will make opponent don't place cards.");
			_difficultyModifier = ((BaseUnityPlugin)plugin).Config.Bind<sbyte>("Manual Setting", "Difficulty Modifier", (sbyte)0, "Adder to the current difficulty value. \nTip: Parameter \"Difficulty\" must be \"Custom\"!");
			_randomState = ((BaseUnityPlugin)plugin).Config.Bind<Vector3>("Тechnical", "Random State", new Vector3(0f, 0f, 0f), "Technical Stuff. Do not change!");
			RefreshDifficulty();
			if (CurrentDifficulty != 0)
			{
				SetupBlueprint();
			}
			if (MinDifficulty > MaxDifficulty)
			{
				MaxDifficulty = MinDifficulty;
			}
		}

		internal static void RefreshDifficulty()
		{
			_currentDifficultyValue = _currentDifficulty.Value;
			MinDifficulty = _minDifficulty.Value;
			MaxDifficulty = _maxDifficulty.Value;
			DifficultyModifier = _difficultyModifier.Value;
		}

		private static void SetupBlueprint()
		{
			byte minDifficulty = 0;
			byte maxDifficulty = 20;
			sbyte difficultyModifier = 0;
			switch (CurrentDifficulty)
			{
			case Difficulty.Easy:
				minDifficulty = 0;
				maxDifficulty = 10;
				difficultyModifier = -2;
				break;
			case Difficulty.Hard:
				minDifficulty = 10;
				maxDifficulty = 20;
				difficultyModifier = 1;
				break;
			case Difficulty.Annoying:
				minDifficulty = 20;
				maxDifficulty = 20;
				difficultyModifier = 0;
				break;
			case Difficulty.Random:
				LoadRandomState();
				return;
			}
			MinDifficulty = minDifficulty;
			MaxDifficulty = maxDifficulty;
			DifficultyModifier = difficultyModifier;
		}

		internal static int ChangeDifficulty(int difficulty)
		{
			if (!SaveFile.IsAscension)
			{
				return difficulty;
			}
			if (CurrentDifficulty == Difficulty.Annoying)
			{
				return MaxDifficulty;
			}
			difficulty += DifficultyModifier;
			difficulty = ((difficulty >= 0) ? difficulty : 0);
			difficulty += MinDifficulty;
			return Mathf.Min(difficulty, MaxDifficulty);
		}

		internal static void RandomizeDifficulty()
		{
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			if (SaveFile.IsAscension && CurrentDifficulty == Difficulty.Random)
			{
				int currentRandomSeed = SaveManager.SaveFile.GetCurrentRandomSeed();
				MinDifficulty = SeededRandom.Range(0, 20, currentRandomSeed);
				MaxDifficulty = SeededRandom.Range(MinDifficulty, 20, currentRandomSeed * 10 + 10);
				DifficultyModifier = SeededRandom.Range(-3, 3, currentRandomSeed);
				_randomState.Value = new Vector3((float)MinDifficulty, (float)MaxDifficulty, (float)DifficultyModifier);
			}
		}

		internal static void LoadRandomState()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			Vector3 value = _randomState.Value;
			MinDifficulty = (int)value.x;
			MaxDifficulty = (int)value.y;
			DifficultyModifier = (int)value.z;
		}
	}
	[HarmonyPatch]
	public class Patches
	{
		[HarmonyPatch(typeof(GameFlowManager), "Start")]
		[HarmonyPrefix]
		private static void StartPrefix()
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: 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)
			if (SaveFile.IsAscension)
			{
				if (AscensionSaveData.Data.ChallengeIsActive(Challenge.AnnoyingDifficulty))
				{
					Config.CurrentDifficulty = Config.Difficulty.Annoying;
					Plugin.CurrentChallenge = Challenge.AnnoyingDifficulty;
				}
				else if (AscensionSaveData.Data.ChallengeIsActive(Challenge.RandomDifficulty))
				{
					Config.CurrentDifficulty = Config.Difficulty.Random;
					Config.LoadRandomState();
					Plugin.CurrentChallenge = Challenge.RandomDifficulty;
				}
				else
				{
					Config.RefreshDifficulty();
				}
			}
		}

		[HarmonyPatch(typeof(EncounterBuilder), "BuildOpponentTurnPlan")]
		[HarmonyPrefix]
		public static void BuildOpponentTurnPlanPrefix(ref int difficulty)
		{
			difficulty = Config.ChangeDifficulty(difficulty);
		}

		[HarmonyPatch(typeof(EncounterBuilder), "BuildOpponentTotem")]
		[HarmonyPrefix]
		public static void BuildOpponentTotemPrefix(ref int difficulty)
		{
			difficulty = Config.ChangeDifficulty(difficulty);
		}

		[HarmonyTranspiler]
		[HarmonyPatch(typeof(Opponent), "SpawnOpponent")]
		public static IEnumerable<CodeInstruction> Opponent_SpawnOpponent(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Expected O, but got Unknown
			List<CodeInstruction> list = new List<CodeInstruction>(instructions);
			int i;
			for (i = 166; !(list[i].opcode == OpCodes.Callvirt) || !(list[i + 1].opcode == OpCodes.Callvirt) || !(list[i + 1].operand.ToString() == "Void set_Difficulty(Int32)"); i++)
			{
			}
			MethodInfo methodInfo = AccessTools.Method(typeof(Config), "ChangeDifficulty", new Type[1] { typeof(int) }, (Type[])null);
			list.Insert(i + 1, new CodeInstruction(OpCodes.Call, (object)methodInfo));
			return list;
		}

		[HarmonyTranspiler]
		[HarmonyPatch(/*Could not decode attribute arguments.*/)]
		public static IEnumerable<CodeInstruction> TurnManager_SetupPhase(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_008c: Expected O, but got Unknown
			List<CodeInstruction> list = new List<CodeInstruction>(instructions);
			int i;
			for (i = 102; !(list[i].opcode == OpCodes.Stfld) || !(list[i].operand.ToString() == "System.Int32 <>1__state") || !(list[i + 1].opcode == OpCodes.Ldarg_0); i++)
			{
			}
			MethodInfo methodInfo = AccessTools.Method(typeof(Plugin), "ShowChallengeActivation", (Type[])null, (Type[])null);
			list.Insert(i + 1, new CodeInstruction(OpCodes.Call, (object)methodInfo));
			return list;
		}

		[HarmonyPatch(typeof(TurnManager), "CleanupPhase")]
		[HarmonyPostfix]
		public static void CleanupPhasePostfix()
		{
			Config.RandomizeDifficulty();
		}
	}
	[BepInPlugin("N0.1DEA.CustomDifficulty", "Custom Difficulty", "1.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		private static class PluginInfo
		{
			public const string PLUGIN_GUID = "N0.1DEA.CustomDifficulty";

			public const string PLUGIN_NAME = "Custom Difficulty";

			public const string PLUGIN_VERSION = "1.0";
		}

		private readonly Harmony harmony = new Harmony("Custom Difficulty");

		internal static ManualLogSource Log { get; set; }

		internal static AscensionChallenge CurrentChallenge { get; set; }

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			Log.LogInfo((object)"Coded with <3 | by StillRinny");
			Log.LogInfo((object)"Tip: Config filename is: \"N0.1DEA.CustomDifficulty.cfg\"");
			harmony.PatchAll();
			Config.Initialize(this);
			Log.LogInfo((object)$"Difficulty: \"{Config.CurrentDifficulty}\"; Min: {Config.MinDifficulty}; Max: {Config.MaxDifficulty}; Mod: {Config.DifficultyModifier}");
			if (Config.ShouldAddChallenges)
			{
				AnnoyingDifficultyChallenge.Add();
				RandomDifficultyChallenge.Add();
			}
			Log.LogInfo((object)"Loading finished!");
		}

		internal static void ShowChallengeActivation()
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Invalid comparison between Unknown and I4
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			if ((int)CurrentChallenge > 0)
			{
				Singleton<ChallengeActivationUI>.Instance.ShowActivation(CurrentChallenge);
			}
		}

		internal static Texture2D LoadTex(string path)
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Expected O, but got Unknown
			byte[] array = ExtractEmbeddedResource(path);
			Texture2D val = new Texture2D(2, 2);
			ImageConversion.LoadImage(val, array);
			((Texture)val).filterMode = (FilterMode)0;
			return val;
		}

		private static byte[] ExtractEmbeddedResource(string filePath)
		{
			filePath = filePath.Replace("/", ".");
			filePath = filePath.Replace("\\", ".");
			Assembly callingAssembly = Assembly.GetCallingAssembly();
			using Stream stream = callingAssembly.GetManifestResourceStream(filePath);
			if (stream == null)
			{
				return null;
			}
			byte[] array = new byte[stream.Length];
			stream.Read(array, 0, array.Length);
			return array;
		}
	}
	public abstract class Challenge
	{
		public static readonly AscensionChallenge AnnoyingDifficulty = AnnoyingDifficultyChallenge.ID;

		public static readonly AscensionChallenge RandomDifficulty = RandomDifficultyChallenge.ID;
	}
}
namespace CustomDifficulty.Challenges
{
	public static class AnnoyingDifficultyChallenge
	{
		internal static AscensionChallenge ID;

		internal static void Add()
		{
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			ID = ChallengeManager.Add("N0:1DEA", "Annoying Difficulty", "Sets the maximum in-game difficulty for all Card Battles", 20, Plugin.LoadTex("CustomDifficulty.Resources.AnnoyingChallenge_Icon.png"), Plugin.LoadTex("CustomDifficulty.Resources.AnnoyingChallenge_SelectedIcon.png"), 0, false).Challenge.challengeType;
		}
	}
	public static class RandomDifficultyChallenge
	{
		internal static AscensionChallenge ID;

		internal static void Add()
		{
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			ID = ChallengeManager.Add("N0:1DEA", "Random Difficulty", "Changes the in-game difficulty randomly for every single Card Battles", 10, Plugin.LoadTex("CustomDifficulty.Resources.RandomChallenge_Icon.png"), Plugin.LoadTex("CustomDifficulty.Resources.RandomChallenge_SelectedIcon.png"), 0, false).Challenge.challengeType;
		}
	}
}