Decompiled source of BuildingLimits v1.0.1

BepInEx/plugins/DiceModders.BuildingLimits.dll

Decompiled 4 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 System.Text;
using System.Text.Json;
using BepInEx;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using DiceKingdoms.Game;
using DiceModders.ModCore;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using Microsoft.CodeAnalysis;
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("Building limits for Dice Kingdoms: the host sets how many buildings of each kind one island may have. A lobby rule.")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1")]
[assembly: AssemblyProduct("DiceModders.BuildingLimits")]
[assembly: AssemblyTitle("DiceModders.BuildingLimits")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.1.0")]
[module: UnverifiableCode]
[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 DiceModders.BuildingLimits
{
	internal static class Limits
	{
		private sealed class Kind
		{
			public string Key;

			public LobbyRule<int> Rule;
		}

		private static LobbyGroup _heading;

		private static BuildingTypes _types;

		private static readonly List<Kind> Kinds = new List<Kind>();

		private static readonly Dictionary<int, Kind> ByType = new Dictionary<int, Kind>();

		internal static void Install()
		{
			_heading = Groups.Get("limits", "Building limits", 500);
			Buildings.SetLimits("Building limits", (Func<BuildingTypeName, int?>)LimitOf);
			Civilizations.GameDataLoaded += Scan;
			Lobby.Changed += Scan;
			Texts.LanguageChanged += GameNames;
			Scan();
		}

		private static int? LimitOf(BuildingTypeName type)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Expected I4, but got Unknown
			if (!Plugin.On || !ByType.TryGetValue((int)type, out var value))
			{
				return null;
			}
			return Math.Max(0, value.Rule.Value);
		}

		private static void Scan()
		{
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_0101: Expected O, but got Unknown
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c1: Expected I4, but got Unknown
			try
			{
				if (Rules.Fixed)
				{
					return;
				}
				if ((Object)(object)_types == (Object)null)
				{
					using IEnumerator<BuildingTypes> enumerator = Resources.FindObjectsOfTypeAll<BuildingTypes>().GetEnumerator();
					if (enumerator.MoveNext())
					{
						_types = enumerator.Current;
					}
				}
				Il2CppReferenceArray<BuildingType> val = (((Object)(object)_types != (Object)null) ? _types.Types : null);
				if (val == null || ((Il2CppArrayBase<BuildingType>)(object)val).Length == 0)
				{
					return;
				}
				int count = Kinds.Count;
				foreach (BuildingType item in (Il2CppArrayBase<BuildingType>)(object)val)
				{
					if (item != null && item.IsBuildable && Buildings.BaseOf(item.name) == item.name && !ByType.ContainsKey((int)item.name))
					{
						Add(item);
					}
				}
				if (Kinds.Count != count)
				{
					ManualLogSource l = Plugin.L;
					bool flag = default(bool);
					BepInExInfoLogInterpolatedStringHandler val2 = new BepInExInfoLogInterpolatedStringHandler(42, 2, ref flag);
					if (flag)
					{
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("building limits: ");
						((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<int>(Kinds.Count);
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" kinds of building (");
						((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<int>(Kinds.Count - count);
						((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" new)");
					}
					l.LogInfo(val2);
					GameNames();
				}
			}
			catch (Exception ex)
			{
				Plugin.Once("scan", "building limits (building list): " + ex);
			}
		}

		private static void Add(BuildingType type)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b3: Expected I4, but got Unknown
			string text = ((object)type.name/*cast due to .constrained prefix*/).ToString();
			bool flag = type.bulkSize > 1;
			int num = ((!flag) ? 1 : type.bulkSize);
			int num2 = 50 * num;
			int num3 = Math.Clamp(Buildings.SuggestedLimit(type.name), 0, num2);
			Kind kind = new Kind
			{
				Key = text
			};
			kind.Rule = Rules.Number("limits." + text, Pretty(text), num3, 0, num2, num, flag ? "Counted in pieces." : null, (Func<bool>)null, true);
			kind.Rule.InGroup(_heading);
			Kinds.Add(kind);
			ByType[(int)type.name] = kind;
		}

		private static string Pretty(string name)
		{
			if (name.Length > 0 && char.IsDigit(name[0]))
			{
				return "Building " + name;
			}
			StringBuilder stringBuilder = new StringBuilder();
			string[] array = name.Split('_', StringSplitOptions.RemoveEmptyEntries);
			foreach (string text in array)
			{
				if (stringBuilder.Length > 0)
				{
					stringBuilder.Append(' ');
				}
				stringBuilder.Append(char.ToUpperInvariant(text[0])).Append(text.Substring(1).ToLowerInvariant());
			}
			return stringBuilder.ToString();
		}

		private static void GameNames()
		{
			try
			{
				string language = Texts.Language;
				Dictionary<string, Dictionary<string, string>> dictionary = new Dictionary<string, Dictionary<string, string>>();
				foreach (Kind kind in Kinds)
				{
					string value = Texts.Game("building." + kind.Key + ".name", "");
					if (!string.IsNullOrWhiteSpace(value))
					{
						dictionary["rule.limits." + kind.Key + ".title"] = new Dictionary<string, string> { [language] = value };
					}
				}
				if (dictionary.Count > 0)
				{
					TextSet set = Tx.Set;
					if (set != null)
					{
						set.Merge(TextSet.Parse(JsonSerializer.Serialize(dictionary), (string)null, ""));
					}
				}
			}
			catch (Exception ex)
			{
				Plugin.Once("names", "building limits (building names): " + ex.Message);
			}
		}
	}
	[BepInPlugin("org.dicemodders.dicekingdoms.buildinglimits", "DiceKingdoms Building Limits", "1.0.1")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInProcess("Dice-Kingdoms.exe")]
	public sealed class Plugin : BasePlugin
	{
		public const string Guid = "org.dicemodders.dicekingdoms.buildinglimits";

		public const string Name = "DiceKingdoms Building Limits";

		public const string Version = "1.0.1";

		internal static ManualLogSource L;

		internal static bool On;

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

		public override void Load()
		{
			Tx.Load();
			L = ((BasePlugin)this).Log;
			Limits.Install();
			Mods.Switch(((BasePlugin)this).Config, true, false, "Turns the Building limits rules of this mod on or off.").WhenOn((Action)delegate
			{
				On = true;
			}, (Action)delegate
			{
				On = false;
			});
		}

		internal static void Once(string key, string message)
		{
			if (Logged.Add(key))
			{
				ManualLogSource l = L;
				if (l != null)
				{
					l.LogError((object)message);
				}
			}
		}
	}
	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;
		}
	}
}