Decompiled source of CaveCrawlersNightmare v1.0.0

plugins/CaveCrawlersNightmare/CaveCrawlersNightmare.dll

Decompiled 2 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using HarmonyLib;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Il2CppSystem.Collections.Generic;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("CaveCrawlersNightmare")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("CaveCrawlersNightmare")]
[assembly: AssemblyTitle("CaveCrawlersNightmare")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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]
	[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;
		}
	}
}
namespace CaveCrawlersNightmare
{
	internal static class NightmareArtwork
	{
		private static readonly byte[] PngSignature = new byte[8] { 137, 80, 78, 71, 13, 10, 26, 10 };

		private static bool attempted;

		private static bool warned;

		private static Texture2D? cachedTexture;

		private static Sprite? cachedSprite;

		internal static void Assign(DiffSelectionInfo? info)
		{
			//IL_0004: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Invalid comparison between Unknown and I4
			try
			{
				if (info != null && (int)info.Difficulty == 3)
				{
					Sprite orLoad = GetOrLoad();
					if ((Object)(object)orLoad != (Object)null)
					{
						info.Background = orLoad;
					}
				}
			}
			catch (Exception ex)
			{
				WarnOnce("Não foi possível atribuir a imagem: " + ex.Message);
			}
		}

		private static Sprite? GetOrLoad()
		{
			//IL_0086: Unknown result type (might be due to invalid IL or missing references)
			//IL_008c: Expected O, but got Unknown
			//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
			if (attempted)
			{
				if (!((Object)(object)cachedTexture != (Object)null))
				{
					return null;
				}
				return cachedSprite;
			}
			attempted = true;
			string text = ResolveImagePath(Paths.PluginPath, typeof(Plugin).Assembly.Location);
			Texture2D val = null;
			Sprite val2 = null;
			try
			{
				byte[] array = File.ReadAllBytes(text);
				if (array.Length < PngSignature.Length || !array.AsSpan(0, PngSignature.Length).SequenceEqual(PngSignature))
				{
					throw new InvalidDataException("O arquivo não contém uma assinatura PNG válida.");
				}
				val = new Texture2D(2, 2, (TextureFormat)4, false);
				if (!ImageConversion.LoadImage(val, new Il2CppStructArray<byte>(array), false))
				{
					throw new InvalidDataException("ImageConversion.LoadImage não conseguiu decodificar o PNG.");
				}
				((Object)val).name = "Nightmare External Texture";
				((Texture)val).filterMode = (FilterMode)1;
				((Texture)val).wrapMode = (TextureWrapMode)1;
				val2 = Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), new Vector2(0.5f, 0.5f), 100f, 0u, (SpriteMeshType)0);
				if ((Object)(object)val2 == (Object)null)
				{
					throw new InvalidOperationException("Sprite.Create não retornou um Sprite.");
				}
				((Object)val2).name = "Nightmare External Sprite";
				((Object)val).hideFlags = (HideFlags)32;
				((Object)val2).hideFlags = (HideFlags)32;
				cachedTexture = val;
				cachedSprite = val2;
				val = null;
				val2 = null;
				return cachedSprite;
			}
			catch (Exception ex)
			{
				WarnOnce($"Não foi possível carregar '{text}': {ex.Message} A imagem herdada de Hard será mantida.");
				return null;
			}
			finally
			{
				DestroyPending((Object?)(object)val2);
				DestroyPending((Object?)(object)val);
			}
		}

		internal static string ResolveImagePath(string pluginRoot, string assemblyPath)
		{
			string text = Path.Combine(pluginRoot, "CaveCrawlersNightmare", "Nightmare.png");
			if (File.Exists(text))
			{
				return text;
			}
			string directoryName = Path.GetDirectoryName(assemblyPath);
			if (!string.IsNullOrEmpty(directoryName))
			{
				return Path.Combine(directoryName, "Nightmare.png");
			}
			return text;
		}

		internal static void PreserveAspect(DiffSelectionItem? item)
		{
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Invalid comparison between Unknown and I4
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if (!((Object)(object)cachedTexture == (Object)null) && !((Object)(object)cachedSprite == (Object)null) && !((Object)(object)item == (Object)null) && item.Info != null && (int)item.Info.Difficulty == 3 && !((Object)(object)item.Info.Background != (Object)(object)cachedSprite))
				{
					Image background = item.Background;
					if ((Object)(object)background != (Object)null && (Object)(object)background.sprite == (Object)(object)cachedSprite && (int)background.type == 0)
					{
						background.preserveAspect = true;
					}
				}
			}
			catch (Exception ex)
			{
				WarnOnce("Não foi possível preservar a proporção da imagem: " + ex.Message);
			}
		}

		private static void DestroyPending(Object? resource)
		{
			try
			{
				if (resource != (Object)null)
				{
					Object.Destroy(resource);
				}
			}
			catch (Exception ex)
			{
				WarnOnce("Não foi possível liberar uma imagem incompleta: " + ex.Message);
			}
		}

		private static void WarnOnce(string reason)
		{
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Expected O, but got Unknown
			if (warned)
			{
				return;
			}
			warned = true;
			try
			{
				ManualLogSource logSource = Plugin.LogSource;
				if (logSource != null)
				{
					bool flag = default(bool);
					BepInExWarningLogInterpolatedStringHandler val = new BepInExWarningLogInterpolatedStringHandler(100, 1, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral("[Nightmare Artwork] ");
						((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(reason);
						((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" O menu mantém seu fluxo original; o carregamento não é repetido nesta execução.");
					}
					logSource.LogWarning(val);
				}
			}
			catch
			{
			}
		}
	}
	[HarmonyPatch(typeof(DiffSelectionItem), "SetItem", new Type[]
	{
		typeof(DiffSelectionInfo),
		typeof(DiffSelection)
	})]
	internal static class SetNightmareArtwork
	{
		private static void Prefix(DiffSelectionInfo __0)
		{
			NightmareArtwork.Assign(__0);
		}

		private static void Postfix(DiffSelectionItem __instance)
		{
			NightmareArtwork.PreserveAspect(__instance);
		}
	}
	internal static class NightmareRegistration
	{
		internal const Difficulty Nightmare = (Difficulty)3;

		private static readonly HashSet<string> ReportedProblems = new HashSet<string>();

		internal static bool EnsureSettings(GameSettings? settings)
		{
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Invalid comparison between Unknown and I4
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Invalid comparison between Unknown and I4
			//IL_013d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0142: Unknown result type (might be due to invalid IL or missing references)
			//IL_0149: Unknown result type (might be due to invalid IL or missing references)
			//IL_014b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0155: Unknown result type (might be due to invalid IL or missing references)
			//IL_015c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0163: Unknown result type (might be due to invalid IL or missing references)
			//IL_016e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0179: Unknown result type (might be due to invalid IL or missing references)
			//IL_0184: Unknown result type (might be due to invalid IL or missing references)
			//IL_018b: Unknown result type (might be due to invalid IL or missing references)
			//IL_018c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0191: Unknown result type (might be due to invalid IL or missing references)
			//IL_0198: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01af: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01db: Unknown result type (might be due to invalid IL or missing references)
			//IL_01eb: Expected O, but got Unknown
			//IL_01ed: Expected O, but got Unknown
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ab: Expected O, but got Unknown
			//IL_00af: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dd: Expected I4, but got Unknown
			if ((Object)(object)settings == (Object)null)
			{
				return false;
			}
			List<DifficultySettings> allDifficultySettings = settings.AllDifficultySettings;
			if (allDifficultySettings == null)
			{
				return false;
			}
			DifficultySettings val = null;
			for (int i = 0; i < allDifficultySettings.Count; i++)
			{
				DifficultySettings val2 = allDifficultySettings[i];
				if (val2 != null)
				{
					if ((int)val2.Difficulty == 3)
					{
						return true;
					}
					if ((int)val2.Difficulty == 2)
					{
						val = val2;
					}
				}
			}
			if (val == null || val.MobSetting == null || val.Rarities == null)
			{
				ReportOnce("Hard incompleto; registro de Nightmare adiado.");
				return false;
			}
			List<RaritySettings> val3 = new List<RaritySettings>();
			for (int j = 0; j < val.Rarities.Count; j++)
			{
				RaritySettings val4 = val.Rarities[j];
				if (val4 == null)
				{
					ReportOnce("Raridade nula em Hard; registro de Nightmare adiado.");
					return false;
				}
				List<RaritySettings> val5 = val3;
				RaritySettings val6 = new RaritySettings();
				val6.Rarity = val4.Rarity;
				RaritySettings val7 = val6;
				Rarity rarity = val4.Rarity;
				val7.RarityMultiplier = (int)rarity switch
				{
					0 => 0.6f, 
					1 => 1f, 
					2 => 1.5f, 
					3 => 1.5f, 
					_ => val4.RarityMultiplier, 
				};
				val5.Add(val6);
			}
			MobDifficultySetting mobSetting = val.MobSetting;
			DifficultySettings val8 = new DifficultySettings
			{
				Difficulty = (Difficulty)3,
				DiffColor = val.DiffColor,
				RequiredLevel = 0,
				RequiredPrestige = 0,
				RequiredKilledMobIds = new List<int>(),
				XPMultiplier = 3f,
				TorchDurationLossMultiplier = 1.6f,
				Rarities = val3,
				MobSetting = new MobDifficultySetting
				{
					Difficulty = (Difficulty)3,
					MobHealthMultiplier = 1.7f,
					MobAttackRangeMultiplier = mobSetting.MobAttackRangeMultiplier,
					ThrustDistanceMultiplier = 1.2f,
					ThrustSpeedMultiplier = 1.2f,
					MobAttackCooldownMultiplier = 0.7f,
					MobTurnSpeedMultiplier = 1.5f,
					BossHealthMultiplier = 1.7f
				}
			};
			allDifficultySettings.Add(val8);
			ManualLogSource? logSource = Plugin.LogSource;
			if (logSource != null)
			{
				logSource.LogInfo((object)"Nightmare registrado com os multiplicadores da versão 1.0.0.");
			}
			return true;
		}

		internal static void EnsureMenu(DiffSelection? selection)
		{
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Invalid comparison between Unknown and I4
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0087: Unknown result type (might be due to invalid IL or missing references)
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Expected O, but got Unknown
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Invalid comparison between Unknown and I4
			if ((Object)(object)selection == (Object)null || !EnsureSettings(GameSettings.Instance))
			{
				return;
			}
			List<DiffSelectionInfo> infos = selection.Infos;
			if (infos == null)
			{
				return;
			}
			DiffSelectionInfo val = null;
			for (int i = 0; i < infos.Count; i++)
			{
				DiffSelectionInfo val2 = infos[i];
				if (val2 != null)
				{
					if ((int)val2.Difficulty == 3)
					{
						return;
					}
					if ((int)val2.Difficulty == 2)
					{
						val = val2;
					}
				}
			}
			if (val == null)
			{
				ReportOnce("Cartão de Hard ausente; entrada de Nightmare adiada.");
				return;
			}
			infos.Add(new DiffSelectionInfo
			{
				Difficulty = (Difficulty)3,
				Background = val.Background,
				BackgroundOffset = val.BackgroundOffset,
				Description = "Nightmare: stronger enemies, faster attacks, shorter torch life, more XP and more epic and legendary loot.",
				Requirements = string.Empty
			});
			ManualLogSource? logSource = Plugin.LogSource;
			if (logSource != null)
			{
				logSource.LogInfo((object)"Nightmare adicionado ao menu original.");
			}
		}

		internal static void Safely(Action action)
		{
			try
			{
				action();
			}
			catch (Exception value)
			{
				ReportOnce($"Falha ao preparar Nightmare: {value}");
			}
		}

		private static void ReportOnce(string message)
		{
			if (ReportedProblems.Add(message))
			{
				ManualLogSource? logSource = Plugin.LogSource;
				if (logSource != null)
				{
					logSource.LogWarning((object)message);
				}
			}
		}
	}
	[HarmonyPatch(typeof(GameSettings), "Awake")]
	internal static class RegisterNightmareOnAwake
	{
		private static void Prefix(GameSettings __instance)
		{
			NightmareRegistration.Safely(delegate
			{
				NightmareRegistration.EnsureSettings(__instance);
			});
		}
	}
	[HarmonyPatch(typeof(GameSettings), "OnMainMenuAwake")]
	internal static class RegisterNightmareOnMainMenu
	{
		private static void Prefix(GameSettings __instance)
		{
			NightmareRegistration.Safely(delegate
			{
				NightmareRegistration.EnsureSettings(__instance);
			});
		}
	}
	[HarmonyPatch(typeof(DiffSelection), "Populate")]
	internal static class RegisterNightmareBeforePopulate
	{
		private static void Prefix(DiffSelection __instance)
		{
			NightmareRegistration.Safely(delegate
			{
				NightmareRegistration.EnsureMenu(__instance);
			});
		}
	}
	[HarmonyPatch(typeof(DiffSelectionItem), "SetItem")]
	internal static class SetNightmareTitle
	{
		private static void Postfix(DiffSelectionItem __instance)
		{
			NightmareRegistration.Safely(delegate
			{
				//IL_0026: Unknown result type (might be due to invalid IL or missing references)
				//IL_002c: Invalid comparison between Unknown and I4
				if (!((Object)(object)__instance == (Object)null) && __instance.Info != null && (int)__instance.Info.Difficulty == 3)
				{
					TextMeshProUGUI diffText = __instance.DiffText;
					if ((Object)(object)diffText != (Object)null)
					{
						((TMP_Text)diffText).text = "Nightmare";
					}
				}
			});
		}
	}
	[HarmonyPatch(typeof(DiffSelectionItem), "SetItem", new Type[]
	{
		typeof(DiffSelectionInfo),
		typeof(DiffSelection)
	})]
	internal static class SetNightmareTitleColor
	{
		private static void Postfix(DiffSelectionItem __instance)
		{
			NightmareRegistration.Safely(delegate
			{
				//IL_0026: Unknown result type (might be due to invalid IL or missing references)
				//IL_002c: Invalid comparison between Unknown and I4
				//IL_0046: Unknown result type (might be due to invalid IL or missing references)
				//IL_004b: Unknown result type (might be due to invalid IL or missing references)
				//IL_0071: Unknown result type (might be due to invalid IL or missing references)
				if (!((Object)(object)__instance == (Object)null) && __instance.Info != null && (int)__instance.Info.Difficulty == 3)
				{
					TextMeshProUGUI diffText = __instance.DiffText;
					if (!((Object)(object)diffText == (Object)null))
					{
						Color color = ((Graphic)diffText).color;
						color.r = 49f / 85f;
						color.g = 0.2f;
						color.b = 78f / 85f;
						((Graphic)diffText).color = color;
					}
				}
			});
		}
	}
	[BepInPlugin("com.pedro.cavecrawlers.nightmare", "Cave Crawlers Nightmare", "1.0.0")]
	public class Plugin : BasePlugin
	{
		internal static ManualLogSource? LogSource { get; private set; }

		public override void Load()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			LogSource = ((BasePlugin)this).Log;
			new Harmony("com.pedro.cavecrawlers.nightmare").PatchAll(typeof(Plugin).Assembly);
			((BasePlugin)this).Log.LogInfo((object)"Nightmare (Difficulty 3): patches de registro e menu carregados.");
		}
	}
}