Decompiled source of All Civilizations v1.0.1

BepInEx/plugins/AllCivilizations/DiceKingdoms.AllNations.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using DiceKingdoms.Game;
using DiceKingdoms.Interface;
using DiceKingdoms.Meta;
using HarmonyLib;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using MLAPI;
using Microsoft.CodeAnalysis;
using Steamworks;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("DiceKingdoms.AllNations")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("DiceKingdoms.AllNations")]
[assembly: AssemblyTitle("DiceKingdoms.AllNations")]
[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 DiceKingdoms.AllNations
{
	[BepInPlugin("io.github.mikhasan.dicekingdoms.allnations", "All Civilizations", "1.0.0")]
	[BepInProcess("Dice-Kingdoms.exe")]
	public sealed class Plugin : BasePlugin
	{
		public const string Guid = "io.github.mikhasan.dicekingdoms.allnations";

		public const string Version = "1.0.0";

		internal static Plugin Instance;

		internal static ConfigEntry<bool> UnlockAllNations;

		internal static ConfigEntry<bool> DebugLogging;

		private readonly HashSet<string> reported = new HashSet<string>();

		private Harmony? harmony;

		public override void Load()
		{
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Expected O, but got Unknown
			//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_0104: Expected O, but got Unknown
			Instance = this;
			UnlockAllNations = ((BasePlugin)this).Config.Bind<bool>("General", "UnlockAllNations", true, "Enable civilization access and the stock collection checkmarks. Restart after changing.");
			DebugLogging = ((BasePlugin)this).Config.Bind<bool>("General", "DebugLogging", false, "Log session civilization availability.");
			if (!UnlockAllNations.Value)
			{
				((BasePlugin)this).Log.LogInfo((object)"All Civilizations is disabled in configuration.");
				return;
			}
			harmony = new Harmony("io.github.mikhasan.dicekingdoms.allnations");
			Patch(typeof(Player), "NextCivilization", typeof(NextCivilizationPatch), prefix: true);
			Patch(typeof(Player), "NetworkStart", typeof(PlayerStartedPatch), prefix: false);
			Patch(typeof(StoreEntry), "Refresh", typeof(CollectionEntryPatch), prefix: false);
			Patch(typeof(Store), "Refresh", typeof(CollectionDetailsPatch), prefix: false);
			ManualLogSource log = ((BasePlugin)this).Log;
			bool flag = default(bool);
			BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(94, 2, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral("All Civilizations ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>("1.0.0");
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(": ");
				((BepInExLogInterpolatedStringHandler)val).AppendFormatted<int>(NationSelection.Civilizations.Length);
				((BepInExLogInterpolatedStringHandler)val).AppendLiteral(" game-defined civilizations; session-only access; no Modding Kit required.");
			}
			log.LogInfo(val);
			((BasePlugin)this).Log.LogInfo((object)"Multiplayer selection is host-authoritative. A client-only installation cannot unlock a Steam-locked civilization on an unmodified host.");
		}

		private void Patch(Type target, string name, Type patch, bool prefix)
		{
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c5: Expected O, but got Unknown
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Expected O, but got Unknown
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_006a: Expected O, but got Unknown
			bool flag = default(bool);
			try
			{
				MethodInfo methodInfo = AccessTools.Method(target, name, (Type[])null, (Type[])null) ?? throw new MissingMethodException(target.FullName, name);
				HarmonyMethod val = new HarmonyMethod(AccessTools.Method(patch, prefix ? "Prefix" : "Postfix", (Type[])null, (Type[])null));
				harmony.Patch((MethodBase)methodInfo, prefix ? val : null, prefix ? null : val, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
				ManualLogSource log = ((BasePlugin)this).Log;
				BepInExInfoLogInterpolatedStringHandler val2 = new BepInExInfoLogInterpolatedStringHandler(18, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("Installed ");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(target.Name);
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(".");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>(name);
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(" patch.");
				}
				log.LogInfo(val2);
			}
			catch (Exception error)
			{
				Error(error);
				ManualLogSource log2 = ((BasePlugin)this).Log;
				BepInExWarningLogInterpolatedStringHandler val3 = new BepInExWarningLogInterpolatedStringHandler(43, 2, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<string>(target.Name);
					((BepInExLogInterpolatedStringHandler)val3).AppendLiteral(".");
					((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<string>(name);
					((BepInExLogInterpolatedStringHandler)val3).AppendLiteral(" is unavailable; this feature is disabled.");
				}
				log2.LogWarning(val3);
			}
		}

		internal void Error(Exception error)
		{
			lock (reported)
			{
				if (reported.Add(error.GetType().FullName + ":" + error.Message))
				{
					((BasePlugin)this).Log.LogError((object)error);
				}
			}
		}

		internal void Debug(string message)
		{
			if (DebugLogging.Value)
			{
				((BasePlugin)this).Log.LogInfo((object)message);
			}
		}
	}
	internal static class NationSelection
	{
		internal static readonly Civilization[] Civilizations = Enum.GetValues<Civilization>().Distinct().ToArray();

		internal static void Prepare(Player player)
		{
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bf: Expected O, but got Unknown
			try
			{
				if ((Object)(object)player == (Object)null || !Plugin.UnlockAllNations.Value || !((NetworkedBehaviour)player).IsServer)
				{
					return;
				}
				PlayerInventory inventory = player.Inventory;
				Il2CppStructArray<Civilization> val = ((inventory != null) ? inventory.UnlockedCivilizations : null);
				List<Civilization> list = new List<Civilization>();
				if (val != null)
				{
					for (int i = 0; i < ((Il2CppArrayBase<Civilization>)(object)val).Length; i++)
					{
						list.Add(((Il2CppArrayBase<Civilization>)(object)val)[i]);
					}
				}
				Civilization[] civilizations = Civilizations;
				foreach (Civilization item in civilizations)
				{
					if (!list.Contains(item))
					{
						list.Add(item);
					}
				}
				if ((val == null || list.Count != ((Il2CppArrayBase<Civilization>)(object)val).Length) && list.Count != 0)
				{
					player.Inventory = new PlayerInventory(new Il2CppStructArray<Civilization>(list.ToArray()));
					Plugin.Instance.Debug("Session civilization IDs: " + string.Join(",", list.Select((Civilization v) => (int)v)));
				}
			}
			catch (Exception error)
			{
				Plugin.Instance.Error(error);
			}
		}
	}
	internal static class NextCivilizationPatch
	{
		private static void Prefix(Player __instance)
		{
			NationSelection.Prepare(__instance);
		}
	}
	internal static class PlayerStartedPatch
	{
		private static void Postfix(Player __instance)
		{
			NationSelection.Prepare(__instance);
		}
	}
	internal static class Collection
	{
		internal static bool IsCivilization(InventoryDef? definition)
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			if (definition == (InventoryDef)null)
			{
				return false;
			}
			if (Enum.TryParse<Civilization>(definition.GetProperty(Constants.UNLOCKS_CIVILIZATION), out Civilization result))
			{
				return Array.IndexOf(NationSelection.Civilizations, result) >= 0;
			}
			return false;
		}

		internal static void Refresh(StoreEntry entry)
		{
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)entry == (Object)null) && IsCivilization(entry.Definition))
			{
				if ((Object)(object)entry.ownedVisual != (Object)null)
				{
					entry.ownedVisual.SetActive(true);
				}
				if ((Object)(object)entry.priceLabel != (Object)null)
				{
					((Component)entry.priceLabel).gameObject.SetActive(false);
				}
				if ((Object)(object)entry.mainImage != (Object)null)
				{
					((Graphic)entry.mainImage).color = Color.white;
				}
			}
		}
	}
	internal static class CollectionEntryPatch
	{
		private static void Postfix(StoreEntry __instance)
		{
			try
			{
				Collection.Refresh(__instance);
			}
			catch (Exception error)
			{
				Plugin.Instance.Error(error);
			}
		}
	}
	internal static class CollectionDetailsPatch
	{
		private static void Postfix(Store __instance)
		{
			try
			{
				if (!((Object)(object)__instance == (Object)null) && !((Object)(object)__instance.Selected == (Object)null) && Collection.IsCivilization(__instance.Selected.Definition))
				{
					Collection.Refresh(__instance.Selected);
					if ((Object)(object)__instance.buyGoldButton != (Object)null)
					{
						((Component)__instance.buyGoldButton).gameObject.SetActive(false);
					}
					if ((Object)(object)__instance.buyMoneyButton != (Object)null)
					{
						((Component)__instance.buyMoneyButton).gameObject.SetActive(false);
					}
					if ((Object)(object)__instance.orLabel != (Object)null)
					{
						__instance.orLabel.SetActive(false);
					}
				}
			}
			catch (Exception error)
			{
				Plugin.Instance.Error(error);
			}
		}
	}
}