Decompiled source of ModBridge v1.0.0

BepInEx/plugins/DiceModders.ModBridge.dll

Decompiled 9 hours 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 System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using DiceModders.ModCore;
using DiceModders.UiKit;
using DiceModders.UiKit.Native;
using Microsoft.CodeAnalysis;

[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.ModBridge")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+76269a011f9caf506f0c3c7febf2c833bab6bb2c")]
[assembly: AssemblyProduct("ModBridge")]
[assembly: AssemblyTitle("DiceModders.ModBridge")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[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.ModBridge
{
	internal static class CivilizationEntries
	{
		private static readonly Dictionary<int, RulesCivilizationHandle> Entries = new Dictionary<int, RulesCivilizationHandle>();

		private static UiScope _scope;

		public static void Install(UiScope scope)
		{
			_scope = scope;
			foreach (ModCivilization item in Civilizations.All)
			{
				Add(item);
			}
			Civilizations.Added += Add;
			Civilizations.Removed += Remove;
			Texts.LanguageChanged += delegate
			{
				foreach (ModCivilization item2 in Civilizations.All)
				{
					Add(item2);
				}
			};
		}

		private static void Add(ModCivilization civ)
		{
			if (Entries.Remove(civ.Id, out var value))
			{
				((NativeHandle)value).Dispose();
			}
			RulesCivilizationHandle val = _scope.Native.Rules.AddCivilization(civ.Id, civ.DisplayName);
			if (val != null)
			{
				Entries[civ.Id] = val;
			}
		}

		private static void Remove(ModCivilization civ)
		{
			if (Entries.Remove(civ.Id, out var value))
			{
				((NativeHandle)value).Dispose();
			}
		}
	}
	internal static class HotkeysScreen
	{
		private static UiScope _scope;

		private static SettingsBlockHandle _block;

		private static readonly Dictionary<Hotkey, SettingsRowHandle> Rows = new Dictionary<Hotkey, SettingsRowHandle>();

		private static SettingsRowHandle _clashes;

		public static void Install(UiScope scope)
		{
			_scope = scope;
			if (Hotkeys.All.Count > 0)
			{
				Rebuild();
			}
			Hotkeys.Added += delegate
			{
				Rebuild();
			};
			Texts.LanguageChanged += delegate
			{
				if (_block != null)
				{
					Rebuild();
				}
			};
			Hotkeys.Changed += delegate(Hotkey h)
			{
				if (Rows.TryGetValue(h, out var value))
				{
					value.SetText(Caption(h));
				}
				SettingsRowHandle clashes = _clashes;
				if (clashes != null)
				{
					clashes.SetText(ClashText());
				}
			};
		}

		private static void Rebuild()
		{
			SettingsBlockHandle block = _block;
			if (block != null)
			{
				((NativeHandle)block).Dispose();
			}
			Rows.Clear();
			_block = _scope.Native.Settings.AddBlock("modbridge.hotkeys", (SettingsPage)2, Tx.T("hotkeys.block"), 1000);
			if (_block == null)
			{
				return;
			}
			string text = null;
			foreach (Hotkey item in Hotkeys.All)
			{
				if (item.ModId != text)
				{
					text = item.ModId;
					_block.AddHeader(item.ModName);
				}
				Hotkey hotkey = item;
				SettingsRowHandle row = null;
				row = _block.AddKey(item.Title, Caption(item), (Action)delegate
				{
					SettingsRowHandle obj = row;
					if (obj != null)
					{
						obj.SetText("...");
					}
					hotkey.Capture((Action<string>)delegate
					{
						SettingsRowHandle obj2 = row;
						if (obj2 != null)
						{
							obj2.SetText(Caption(hotkey));
						}
					});
				});
				if (row != null)
				{
					Rows[item] = row;
				}
			}
			_block.AddButton(Tx.T("hotkeys.all"), Tx.T("hotkeys.default"), (Action)delegate
			{
				foreach (Hotkey item2 in Hotkeys.All)
				{
					item2.Reset();
				}
			});
			_clashes = _block.AddInfo(ClashText());
		}

		private static string ClashText()
		{
			List<string> list = new List<string>();
			foreach (Hotkey item in Hotkeys.All)
			{
				IReadOnlyList<string> readOnlyList = item.Clashes();
				if (readOnlyList.Count > 0)
				{
					list.Add(Tx.T("hotkeys.clash", item.Keys, item.Title, string.Join(", ", readOnlyList)));
				}
			}
			if (list.Count != 0)
			{
				return string.Join("\n", list);
			}
			return Tx.T("hotkeys.noClash");
		}

		private static string Caption(Hotkey h)
		{
			if (!string.IsNullOrEmpty(h.Keys))
			{
				return h.Keys;
			}
			return "-";
		}
	}
	internal static class LobbyRulesScreen
	{
		private static UiScope _scope;

		private static SettingsBlockHandle _block;

		private static readonly List<Action> Updaters = new List<Action>();

		private static readonly HashSet<ILobbyRule> Subscribed = new HashSet<ILobbyRule>();

		private static bool _refreshing;

		private static bool _subscribedToLobby;

		private static readonly Dictionary<string, bool> Open = new Dictionary<string, bool>();

		public static void Install(UiScope scope)
		{
			_scope = scope;
			if (Rules.All.Count > 0)
			{
				Rebuild();
			}
			Rules.Registered += delegate
			{
				Rebuild();
			};
			Groups.Changed += delegate
			{
				if (Rules.All.Count > 0)
				{
					Rebuild();
				}
			};
			Texts.LanguageChanged += delegate
			{
				if (_block != null)
				{
					Rebuild();
				}
			};
		}

		private static void Rebuild()
		{
			SettingsBlockHandle block = _block;
			if (block != null)
			{
				((NativeHandle)block).Dispose();
			}
			_block = null;
			Updaters.Clear();
			_block = _scope.Native.Settings.AddBlock("modbridge.lobby.rules", (SettingsPage)4, Tx.T("rules.block"), 1000);
			if (_block == null)
			{
				return;
			}
			foreach (IGrouping<string, ILobbyRule> item in from r in Rules.All
				where r.Group == null
				group r by r.ModId)
			{
				List<ILobbyRule> rules = item.ToList();
				SettingsRowHandle header = _block.AddHeader(rules[0].ModName);
				if (header != null)
				{
					Updaters.Add(delegate
					{
						header.SetVisible(rules.Any((ILobbyRule r) => r.Visible));
					});
				}
				foreach (ILobbyRule item2 in rules)
				{
					AddRule(item2, null);
				}
			}
			foreach (LobbyGroup item3 in Groups.All)
			{
				IReadOnlyList<ILobbyRule> rules2 = item3.Rules;
				if (rules2.Count == 0)
				{
					continue;
				}
				LobbyGroup h = item3;
				SettingsRowHandle row = _block.AddButton(h.Title, Tx.T("rules.showHide"), (Action)delegate
				{
					Open[h.Key] = !IsOpen(h);
					Refresh();
				});
				if (row != null)
				{
					Updaters.Add(delegate
					{
						row.SetVisible(rules2.Any((ILobbyRule r) => r.Visible));
					});
				}
				foreach (ILobbyRule item4 in rules2)
				{
					AddRule(item4, h);
				}
			}
			SettingsRowHandle warnings = _block.AddInfo(string.Empty);
			if (warnings != null)
			{
				Updaters.Add(delegate
				{
					warnings.SetText(string.Join("\n", Compatibility.Warnings));
				});
			}
			SubscribeToLobby();
			Refresh();
		}

		private static bool IsOpen(LobbyGroup g)
		{
			if (!Open.TryGetValue(g.Key, out var value))
			{
				return !g.StartsCollapsed;
			}
			return value;
		}

		private static void AddRule(ILobbyRule rule, LobbyGroup heading)
		{
			//IL_0215: Unknown result type (might be due to invalid IL or missing references)
			//IL_021b: Expected O, but got Unknown
			ILobbyRule val = rule;
			LobbyRule<bool> val2 = val as LobbyRule<bool>;
			SettingsRowHandle row;
			if (val2 == null)
			{
				LobbyRule<int> val3 = val as LobbyRule<int>;
				if (val3 == null)
				{
					if (!(val is LobbyRule<string>))
					{
						ManualLogSource log = Plugin.Log;
						bool flag = default(bool);
						BepInExWarningLogInterpolatedStringHandler val4 = new BepInExWarningLogInterpolatedStringHandler(48, 1, ref flag);
						if (flag)
						{
							((BepInExLogInterpolatedStringHandler)val4).AppendLiteral("lobby rule '");
							((BepInExLogInterpolatedStringHandler)val4).AppendFormatted<string>(rule.Key);
							((BepInExLogInterpolatedStringHandler)val4).AppendLiteral("' has a type this plugin cannot show");
						}
						log.LogWarning(val4);
					}
					return;
				}
				if (val3.Choices != null)
				{
					row = _block.AddDropdown(Label((ILobbyRule)(object)val3, heading), (IEnumerable<string>)val3.Choices, val3.Live, (Action<int>)delegate(int i)
					{
						if (!_refreshing)
						{
							val3.TrySet(i);
							Refresh();
						}
					});
					if (row != null)
					{
						Updaters.Add(delegate
						{
							row.SetValue(val3.Live);
						});
					}
					Subscribe((ILobbyRule)(object)val3);
				}
				else
				{
					LobbyRule<int> val5 = val3;
					row = _block.AddSlider(Label((ILobbyRule)(object)val5, heading), (float)val5.Min, (float)val5.Max, (float)val5.Live, (Action<float>)delegate(float v)
					{
						if (!_refreshing)
						{
							val5.TrySet(Math.Clamp(val5.Min + (int)Math.Round((v - (float)val5.Min) / (float)val5.Step) * val5.Step, val5.Min, val5.Max));
							Refresh();
						}
					}, "0", true);
					if (row != null)
					{
						Updaters.Add(delegate
						{
							row.SetValue((float)val5.Live);
						});
					}
					Subscribe((ILobbyRule)(object)val5);
				}
			}
			else
			{
				row = _block.AddToggle(Label((ILobbyRule)(object)val2, heading), val2.Live, (Action<bool>)delegate(bool v)
				{
					if (!_refreshing)
					{
						val2.TrySet(v);
						Refresh();
					}
				});
				if (row != null)
				{
					Updaters.Add(delegate
					{
						row.SetValue(val2.Live);
					});
				}
				Subscribe((ILobbyRule)(object)val2);
			}
			if (row != null)
			{
				Updaters.Add(delegate
				{
					row.SetEnabled(rule.Editable).SetVisible(Shown());
				});
			}
			if (string.IsNullOrWhiteSpace(rule.Description))
			{
				return;
			}
			SettingsRowHandle info = _block.AddInfo(rule.Description);
			if (info != null)
			{
				Updaters.Add(delegate
				{
					info.SetVisible(Shown());
				});
			}
			bool Shown()
			{
				if (rule.Visible)
				{
					if (heading != null)
					{
						return IsOpen(heading);
					}
					return true;
				}
				return false;
			}
		}

		private static string Label(ILobbyRule rule, LobbyGroup heading)
		{
			if (heading != null && !heading.Rules.All((ILobbyRule r) => r.ModId == rule.ModId))
			{
				return rule.Title + " (" + rule.ModName + ")";
			}
			return rule.Title;
		}

		private static void Subscribe(ILobbyRule rule)
		{
			if (!Subscribed.Add(rule))
			{
				return;
			}
			if (!(rule is LobbyRule<bool> val))
			{
				if (rule is LobbyRule<int> val2)
				{
					val2.Changed += delegate
					{
						Refresh();
					};
				}
			}
			else
			{
				val.Changed += delegate
				{
					Refresh();
				};
			}
		}

		private static void SubscribeToLobby()
		{
			if (!_subscribedToLobby)
			{
				_subscribedToLobby = true;
				Lobby.Changed += Refresh;
				Match.Started += Refresh;
				Match.Ended += Refresh;
			}
		}

		private static void Refresh()
		{
			if (_refreshing)
			{
				return;
			}
			_refreshing = true;
			try
			{
				foreach (Action updater in Updaters)
				{
					try
					{
						updater();
					}
					catch (Exception ex)
					{
						Plugin.Log.LogWarning((object)("could not update a lobby rule row: " + ex.Message));
					}
				}
			}
			finally
			{
				_refreshing = false;
			}
		}
	}
	[BepInPlugin("DiceModders.ModBridge", "ModBridge", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BasePlugin
	{
		internal static ManualLogSource Log;

		public override void Load()
		{
			Log = ((BasePlugin)this).Log;
			Tx.Load();
			UiScope val = UiKit.CreateScope("DiceModders.ModBridge");
			if (val == null)
			{
				Log.LogError((object)"UiKit is not available; the lobby rules and civilization entries will not be shown");
				return;
			}
			val.WhenReady((Action<UiScope>)delegate(UiScope s)
			{
				LobbyRulesScreen.Install(s);
				CivilizationEntries.Install(s);
				HotkeysScreen.Install(s);
			});
		}
	}
	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;
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "DiceModders.ModBridge";

		public const string PLUGIN_NAME = "ModBridge";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}