Decompiled source of Cheats v1.0.0

BepInEx/plugins/DiceModders.Cheats.dll

Decompiled 9 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using DiceKingdoms.Game;
using DiceModders.ModCore;
using DiceModders.ModCore.Tools;
using DiceModders.UiKit;
using DiceModders.UiKit.Native;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppSystem.Collections;
using Il2CppSystem.Collections.Generic;
using Microsoft.CodeAnalysis;
using Unity.Mathematics;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("DiceModders")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Cheats for Dice Kingdoms: dev console, unlock all buildings, build anywhere, meteor castle protection lobby rules, force lobby ready.")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("DiceModders.Cheats")]
[assembly: AssemblyTitle("DiceModders.Cheats")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
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;
		}
	}
	[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 DiceModders.Cheats
{
	internal static class CheatSettings
	{
		private static UiScope _ui;

		private static SettingsBlockHandle _cheats;

		internal static void Install(UiScope ui)
		{
			_ui = ui;
			if (_ui == null)
			{
				return;
			}
			Tx.Block(_ui, delegate
			{
				//IL_0000: Unknown result type (might be due to invalid IL or missing references)
				//IL_0005: 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_0017: Unknown result type (might be due to invalid IL or missing references)
				//IL_0027: Unknown result type (might be due to invalid IL or missing references)
				//IL_002f: Unknown result type (might be due to invalid IL or missing references)
				//IL_0055: Expected O, but got Unknown
				SettingsBlockSpec val = new SettingsBlockSpec();
				val.set_Id("org.dicemodders.dicekingdoms.cheats.lobby");
				val.set_Page((SettingsPage)4);
				val.set_Title(Tx.T("block.mods"));
				val.set_Order(20);
				val.set_Build((Action<SettingsBlockHandle>)delegate(SettingsBlockHandle block)
				{
					Toggle(block, Tx.T("forceReady"), Options.ForceLobbyReady);
				});
				return val;
			});
			Options.Unlocked.SettingChanged += delegate
			{
				Gate();
			};
			Texts.LanguageChanged += delegate
			{
				if (_cheats != null)
				{
					((NativeHandle)_cheats).Dispose();
					_cheats = null;
					Gate();
				}
			};
			Gate();
		}

		private static void Gate()
		{
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: 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_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Expected O, but got Unknown
			if (Options.Unlocked.Value && _cheats == null)
			{
				SettingsApi settings = _ui.Native.Settings;
				SettingsBlockSpec val = new SettingsBlockSpec();
				val.set_Id("org.dicemodders.dicekingdoms.cheats.cheats");
				val.set_Page((SettingsPage)3);
				val.set_Title(Tx.T("block.cheats"));
				val.set_Order(900);
				val.set_Build((Action<SettingsBlockHandle>)delegate(SettingsBlockHandle block)
				{
					Toggle(block, Tx.T("devConsole"), Options.DevConsole);
					Toggle(block, Tx.T("unlockAll"), Options.UnlockAllBuildings);
					Toggle(block, Tx.T("buildAnywhere"), Options.BuildAnywhere);
					block.AddInfo(Tx.T("meteorInfo"));
					block.AddButton(Tx.T("hideCheats"), Tx.T("hide"), (Action)delegate
					{
						Options.Unlocked.Value = false;
					});
				});
				_cheats = settings.AddBlock(val);
			}
			else if (!Options.Unlocked.Value && _cheats != null)
			{
				((NativeHandle)_cheats).Dispose();
				_cheats = null;
			}
		}

		private static void Toggle(SettingsBlockHandle block, string label, ConfigEntry<bool> entry)
		{
			SettingsRowHandle row = block.AddToggle(label, entry.Value, (Action<bool>)delegate(bool v)
			{
				entry.Value = v;
			});
			if (row != null)
			{
				entry.SettingChanged += delegate
				{
					row.SetValue(entry.Value);
				};
			}
		}
	}
	internal static class Meteor
	{
		private const string Member = "meteoriteTilesToFullyAllowHitCastle";

		private const int Offset = 56;

		private const int GameRule = 0;

		private const int AnyMeteor = 1;

		private const int Never = 2;

		private static LobbyRule<int> _castle;

		private static LobbyRule<int> _limit;

		private static int? _gameValue;

		internal static bool CastleCanBeHit
		{
			get
			{
				if (Plugin.On)
				{
					return _castle.Value == 1;
				}
				return false;
			}
		}

		internal static void Install()
		{
			LobbyGroup val = Groups.Get("cheats", "Cheats", 500);
			_castle = Rules.Choice("cheats.meteor.castle", "Castle hits", 0, (IReadOnlyList<string>)new string[3] { "Game rule", "Any meteor", "Never" }, (string)null, (Func<bool>)Visible, true).InGroup(val);
			_limit = Rules.Number("cheats.meteor.limitT", "Meteor limit T", 0, 0, 4096, 16, "0 = game (128). Higher = later hits.", (Func<bool>)(() => Visible() && _castle.Live == 0), true).InGroup(val);
			static bool Visible()
			{
				return Options.Unlocked.Value;
			}
		}

		internal static void BeforeExecute(object meteorite)
		{
			object obj = default(object);
			int num = ((Reflect.TryGetMember(meteorite, "meteoriteTilesToFullyAllowHitCastle", ref obj) && obj is int num2) ? num2 : Mem.ReadInt(Mem.PtrOf(meteorite), 56));
			int valueOrDefault = _gameValue.GetValueOrDefault();
			if (!_gameValue.HasValue)
			{
				valueOrDefault = num;
				_gameValue = valueOrDefault;
			}
			int num3 = ((!Plugin.On) ? _gameValue.Value : ((_castle.Value == 2) ? int.MaxValue : ((_castle.Value == 1) ? _gameValue.Value : ((_limit.Value > 0) ? _limit.Value : _gameValue.Value))));
			if (num != num3 && !Reflect.TrySetMember(meteorite, "meteoriteTilesToFullyAllowHitCastle", (object)num3))
			{
				Mem.WriteInt(Mem.PtrOf(meteorite), 56, num3);
			}
		}
	}
	internal static class Options
	{
		internal static ConfigEntry<bool> Unlocked;

		internal static ConfigEntry<bool> DevConsole;

		internal static ConfigEntry<bool> UnlockAllBuildings;

		internal static ConfigEntry<bool> BuildAnywhere;

		internal static ConfigEntry<bool> ForceLobbyReady;

		internal static bool CheatsActive
		{
			get
			{
				if (Plugin.On)
				{
					return Unlocked.Value;
				}
				return false;
			}
		}

		internal static void Bind(ConfigFile config)
		{
			Unlocked = config.Bind<bool>("Cheats", "Unlocked", false, "While false every cheat is off and hidden. Cheats can change the simulation: use them only with everybody's consent.");
			DevConsole = config.Bind<bool>("Cheats", "DevConsole", true, "Let the game's own developer console work (saveisland, loadisland ...).");
			UnlockAllBuildings = config.Bind<bool>("Cheats", "UnlockAllBuildings", false, "Every building can be built without its technology.");
			BuildAnywhere = config.Bind<bool>("Cheats", "BuildAnywhere", false, "Island placement checks are skipped. RISKY: may write outside the island grid and, because placement is networked, crash everyone in the lobby.");
			ForceLobbyReady = config.Bind<bool>("Lobby", "ForceReady", false, "Host: every player counts as ready, so Start unlocks.");
		}
	}
	internal static class Patches
	{
		private static PatchSwitch _unlocked;

		private static PatchSwitch _buildAnywhere;

		internal static void Install(Patcher patcher)
		{
			patcher.Patch("Cheats", "get_Available", typeof(Patches), (string)null, "DevConsole");
			patcher.Patch("Cheats", "CheckAvailable", typeof(Patches), (string)null, "DevConsole");
			_unlocked = patcher.Switch("BuildData", "Unlocked", typeof(Patches), "Unlocked", (string)null);
			_buildAnywhere = patcher.Switch(typeof(IslandGrid), "CanPlaceBuilding", typeof(Patches), (string)null, "CanPlaceBuilding");
			patcher.Patch("Player", "get_StartGameReady", typeof(Patches), "StartGameReady", (string)null);
			patcher.Patch("Meteorite", "Execute", typeof(Patches), "MeteorExecute", (string)null);
			patcher.Patch("IslandGrid", "MeteoriteNumberCastleTiles", typeof(Patches), (string)null, "CastleTiles");
			Options.Unlocked.SettingChanged += delegate
			{
				Refresh();
			};
			Options.UnlockAllBuildings.SettingChanged += delegate
			{
				Refresh();
			};
			Options.BuildAnywhere.SettingChanged += delegate
			{
				Refresh();
			};
		}

		internal static void Refresh()
		{
			if (_unlocked != null)
			{
				_unlocked.On = Options.CheatsActive && Options.UnlockAllBuildings.Value;
			}
			if (_buildAnywhere != null)
			{
				_buildAnywhere.On = Options.CheatsActive && Options.BuildAnywhere.Value;
			}
		}

		internal static void DevConsole(ref bool __result)
		{
			if (Options.CheatsActive && Options.DevConsole.Value)
			{
				__result = true;
			}
		}

		internal static bool Unlocked(ref bool __result)
		{
			__result = true;
			return false;
		}

		internal static void CanPlaceBuilding(IslandGrid __instance, Building __0, ref bool __result)
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			if (__result || (Object)(object)__instance == (Object)null || __0 == (Building)null)
			{
				return;
			}
			try
			{
				int2 size = __instance.size;
				IEnumerator<int2> enumerator = __0.Positions().GetEnumerator();
				IEnumerator val = ((Il2CppObjectBase)enumerator).Cast<IEnumerator>();
				while (val.MoveNext())
				{
					int2 current = enumerator.Current;
					if (current.x < 0 || current.y < 0 || current.x >= size.x || current.y >= size.y || __instance.BuildingAt(current).HasValue || __instance.RemainsAt(current).HasValue)
					{
						return;
					}
				}
				__result = true;
			}
			catch (Exception ex)
			{
				Plugin.L.LogWarning((object)("build anywhere: " + ex.Message));
			}
		}

		internal static bool StartGameReady(ref bool __result)
		{
			if (!Plugin.On || !Options.ForceLobbyReady.Value || !Lobby.IsHost)
			{
				return true;
			}
			__result = true;
			return false;
		}

		internal static void MeteorExecute(object __instance)
		{
			try
			{
				Meteor.BeforeExecute(__instance);
			}
			catch (Exception ex)
			{
				Plugin.L.LogWarning((object)("meteor: " + ex.Message));
			}
		}

		internal static void CastleTiles(ref int __result)
		{
			if (Meteor.CastleCanBeHit)
			{
				__result = 0;
			}
		}
	}
	[BepInPlugin("org.dicemodders.dicekingdoms.cheats", "DiceKingdoms Cheats", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInProcess("Dice-Kingdoms.exe")]
	public sealed class Plugin : BasePlugin
	{
		public const string Guid = "org.dicemodders.dicekingdoms.cheats";

		public const string Name = "DiceKingdoms Cheats";

		public const string Version = "1.0.0";

		internal static ManualLogSource L;

		internal static bool On;

		public override void Load()
		{
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Expected O, but got Unknown
			Tx.Load();
			L = ((BasePlugin)this).Log;
			Options.Bind(((BasePlugin)this).Config);
			Meteor.Install();
			Patcher val = new Patcher("org.dicemodders.dicekingdoms.cheats", ((BasePlugin)this).Log);
			Patches.Install(val);
			val.Summary();
			CheatSettings.Install(UiKit.CreateScope("org.dicemodders.dicekingdoms.cheats"));
			Mods.Switch(((BasePlugin)this).Config, true, false, "Turns every cheat of this mod on or off.").WhenOn((Action)delegate
			{
				On = true;
				Patches.Refresh();
			}, (Action)delegate
			{
				On = false;
				Patches.Refresh();
			});
		}
	}
	internal static class Tx
	{
		internal static TextSet Set;

		internal static void Load()
		{
			Set = Texts.Load("texts.json", (string)null);
		}

		internal static string T(string key)
		{
			TextSet set = Set;
			return ((set != null) ? set.Get(key) : null) ?? key;
		}

		internal static string T(string key, params object[] values)
		{
			TextSet set = Set;
			return ((set != null) ? set.Get(key, values) : null) ?? key;
		}

		internal static SettingsBlockHandle Block(UiScope ui, Func<SettingsBlockSpec> spec)
		{
			if (ui == null)
			{
				return null;
			}
			SettingsBlockHandle handle = ui.Native.Settings.AddBlock(spec());
			Texts.LanguageChanged += delegate
			{
				SettingsBlockHandle obj = handle;
				if (obj != null)
				{
					((NativeHandle)obj).Dispose();
				}
				handle = ui.Native.Settings.AddBlock(spec());
			};
			return handle;
		}
	}
}