using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using RTM.UISystem;
using UnityEngine;
using UnityEngine.UI;
using Voxels.TowerDefense.UI;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyCompany("BadNorth.ModMenu")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("BadNorth.ModMenu")]
[assembly: AssemblyTitle("BadNorth.ModMenu")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace BadNorth.ModMenu;
[BepInPlugin("local.badnorth.mod-menu", "Bad North Mod Menu", "1.1.1")]
public sealed class ModMenuPlugin : BaseUnityPlugin
{
private ConfigEntry<KeyboardShortcut> shortcut;
private MainMenu installedIn;
private NativeModDialog dialog;
private void Awake()
{
//IL_001c: Unknown result type (might be due to invalid IL or missing references)
shortcut = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Menu", "Shortcut", new KeyboardShortcut((KeyCode)291, (KeyCode[])(object)new KeyCode[0]), "Open or close the local mod menu from the main menu.");
dialog = new NativeModDialog(((BaseUnityPlugin)this).Logger);
}
private void Update()
{
//IL_0041: Unknown result type (might be due to invalid IL or missing references)
//IL_0046: Unknown result type (might be due to invalid IL or missing references)
UIManager instance = Singleton<UIManager>.instance;
if (!Object.op_Implicit((Object)(object)instance))
{
return;
}
UIMenu activeMenu = instance.activeMenu;
MainMenu val = (MainMenu)(object)((activeMenu is MainMenu) ? activeMenu : null);
if (val != null && Object.op_Implicit((Object)(object)val) && (Object)(object)installedIn != (Object)(object)val)
{
InstallButton(val);
}
KeyboardShortcut value = shortcut.Value;
if (((KeyboardShortcut)(ref value)).IsDown())
{
if (dialog.IsOpen)
{
dialog.Close();
}
else if (instance.activeMenu is MainMenu)
{
dialog.Open();
}
}
}
private void InstallButton(MainMenu main)
{
installedIn = main;
ButtonWidget val = ((IEnumerable<ButtonWidget>)((Component)main).GetComponentsInChildren<ButtonWidget>(true)).FirstOrDefault((Func<ButtonWidget, bool>)((ButtonWidget button) => Object.op_Implicit((Object)(object)((Widget)button).localize) && ((Widget)button).localize.Term == "UI/COMMON/SETTINGS"));
ButtonWidget val2 = ((GeneratedMenu)main).AddButton("UI/COMMON/SETTINGS", (Func<bool>)dialog.Open, (Transform)null, (ButtonWidget)null);
((Object)((Component)val2).gameObject).name = "Local Mods";
((Widget)val2).SetNonLocalizedLabel("模组").SetSuccessAudio(FabricEventReference.op_Implicit("UI/Menu/Settings")).SetHoverAudio(FabricID.uiHover);
if (Object.op_Implicit((Object)(object)val) && (Object)(object)((Component)val2).transform.parent == (Object)(object)((Component)val).transform.parent)
{
((Component)val2).transform.SetSiblingIndex(((Component)val).transform.GetSiblingIndex() + 1);
}
TransformExtensions.ForceChildLayoutUpdates(((Component)main).transform, false);
((BaseUnityPlugin)this).Logger.LogInfo((object)"Added Local Mods to the main menu.");
}
}
internal sealed class NativeModDialog
{
private const BindingFlags PrivateInstance = BindingFlags.Instance | BindingFlags.NonPublic;
private static readonly FieldInfo ModalMessage = typeof(ModalOverlay).GetField("message", BindingFlags.Instance | BindingFlags.NonPublic);
private static readonly FieldInfo TogglePrefab = typeof(GeneratedMenu).GetField("multiSelectWidgetPrefab", BindingFlags.Instance | BindingFlags.NonPublic);
private static readonly FieldInfo SettingsInstance = typeof(UserSettingsMenu).GetField("_instance", BindingFlags.Static | BindingFlags.NonPublic);
private readonly ManualLogSource log;
private ModalOverlay modal;
private RectTransform body;
private Text notice;
private Dictionary<string, bool> saved;
private Dictionary<string, bool> draft;
public bool IsOpen
{
get
{
if (Object.op_Implicit((Object)(object)modal))
{
return ((UIMenu)modal).isOpen;
}
return false;
}
}
public NativeModDialog(ManualLogSource log)
{
this.log = log;
}
public bool Open()
{
//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
//IL_00d3: Expected O, but got Unknown
if (IsOpen)
{
return true;
}
object? obj = SettingsInstance?.GetValue(null);
UserSettingsMenu val = (UserSettingsMenu)((obj is UserSettingsMenu) ? obj : null);
MultiSelectWidget val2 = (MultiSelectWidget)(Object.op_Implicit((Object)(object)val) ? /*isinst with value type is only supported in some contexts*/: null);
if (!ModMenuBootstrap.CatalogueReady || !Object.op_Implicit((Object)(object)val2) || (object)ModalMessage == null)
{
ModalOverlay.GetInstance().InitializeNonLocalized("模组", "无法读取本地模组。请查看 BepInEx 日志。", false).AddOKButton((Func<bool>)null);
return true;
}
try
{
saved = PluginSelection.Read();
draft = new Dictionary<string, bool>(saved, StringComparer.OrdinalIgnoreCase);
modal = ModalOverlay.GetInstance();
modal.InitializeNonLocalized("模组", "保存后重启游戏,开关才会生效。", true);
Text message = (Text)ModalMessage.GetValue(modal);
AddBody(message, val2);
((Widget)modal.AddOKButton((Func<bool>)Save)).SetNonLocalizedLabel("保存");
TransformExtensions.ForceChildLayoutUpdates(((Component)modal).transform, false);
return true;
}
catch (Exception ex)
{
log.LogError((object)ex);
Close();
ModalOverlay.GetInstance().InitializeNonLocalized("模组", "无法打开模组设置。请查看 BepInEx 日志。", false).AddOKButton((Func<bool>)null);
return true;
}
}
public void Close()
{
if (IsOpen)
{
modal.Dismiss();
}
}
private void AddBody(Text message, MultiSelectWidget prefab)
{
//IL_0091: Unknown result type (might be due to invalid IL or missing references)
//IL_0096: Unknown result type (might be due to invalid IL or missing references)
//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
//IL_00bb: Expected O, but got Unknown
//IL_0155: Unknown result type (might be due to invalid IL or missing references)
//IL_015a: Unknown result type (might be due to invalid IL or missing references)
//IL_0180: Unknown result type (might be due to invalid IL or missing references)
//IL_018f: Unknown result type (might be due to invalid IL or missing references)
//IL_01a4: Unknown result type (might be due to invalid IL or missing references)
//IL_01d3: Unknown result type (might be due to invalid IL or missing references)
//IL_01df: Unknown result type (might be due to invalid IL or missing references)
//IL_01f5: Unknown result type (might be due to invalid IL or missing references)
//IL_0201: Unknown result type (might be due to invalid IL or missing references)
//IL_0227: Unknown result type (might be due to invalid IL or missing references)
//IL_0231: Expected O, but got Unknown
//IL_02bf: Unknown result type (might be due to invalid IL or missing references)
//IL_02d4: Unknown result type (might be due to invalid IL or missing references)
//IL_02e9: Unknown result type (might be due to invalid IL or missing references)
//IL_0308: Unknown result type (might be due to invalid IL or missing references)
LayoutElement layout = ((Component)message).GetComponent<LayoutElement>();
bool added = !Object.op_Implicit((Object)(object)layout);
if (added)
{
layout = ((Component)message).gameObject.AddComponent<LayoutElement>();
}
float previousWidth = layout.preferredWidth;
float previousHeight = layout.preferredHeight;
int previousPriority = layout.layoutPriority;
TextAnchor previousAlignment = message.alignment;
LocalMod[] array = ModMenuBootstrap.Plugins.ToArray();
float num = Mathf.Max(64f, LayoutUtility.GetPreferredHeight((RectTransform)((Component)prefab).transform));
layout.preferredWidth = 864f;
layout.preferredHeight = 88f + Mathf.Clamp((float)array.Length * (num + 8f) + 8f, 96f, 440f);
layout.layoutPriority = 10;
message.alignment = (TextAnchor)1;
modal.SetCloseAction((Action)delegate
{
//IL_008c: Unknown result type (might be due to invalid IL or missing references)
if (Object.op_Implicit((Object)(object)body))
{
((Component)body).gameObject.SetActive(false);
Object.Destroy((Object)(object)((Component)body).gameObject);
}
if (added)
{
Object.Destroy((Object)(object)layout);
}
else
{
layout.preferredWidth = previousWidth;
layout.preferredHeight = previousHeight;
layout.layoutPriority = previousPriority;
}
message.alignment = previousAlignment;
body = null;
notice = null;
modal = null;
});
body = AddRect("Local Mod Settings", (Transform)(object)((Graphic)message).rectTransform);
Stretch(body, Vector2.zero, Vector2.zero);
RectTransform val = AddRect("Installed Mod List", (Transform)(object)body);
Stretch(val, new Vector2(0f, 34f), new Vector2(0f, -54f));
((Graphic)((Component)val).gameObject.AddComponent<Image>()).color = Color.clear;
((Component)val).gameObject.AddComponent<RectMask2D>();
RectTransform val2 = AddRect("Mod Rows", (Transform)(object)val);
val2.anchorMin = new Vector2(0f, 1f);
val2.anchorMax = Vector2.one;
val2.pivot = new Vector2(0.5f, 1f);
val2.sizeDelta = Vector2.zero;
VerticalLayoutGroup obj = ((Component)val2).gameObject.AddComponent<VerticalLayoutGroup>();
((HorizontalOrVerticalLayoutGroup)obj).spacing = 8f;
((LayoutGroup)obj).padding = new RectOffset(4, 4, 4, 4);
((HorizontalOrVerticalLayoutGroup)obj).childControlWidth = true;
((HorizontalOrVerticalLayoutGroup)obj).childControlHeight = true;
((HorizontalOrVerticalLayoutGroup)obj).childForceExpandWidth = true;
((HorizontalOrVerticalLayoutGroup)obj).childForceExpandHeight = false;
((Component)val2).gameObject.AddComponent<ContentSizeFitter>().verticalFit = (FitMode)2;
ScrollRect val3 = ((Component)val).gameObject.AddComponent<ScrollRect>();
val3.viewport = val;
val3.content = val2;
val3.horizontal = false;
val3.movementType = (MovementType)2;
val3.scrollSensitivity = 32f;
notice = AddLabel(string.Empty, message, (Transform)(object)body);
((Graphic)notice).rectTransform.anchorMin = Vector2.zero;
((Graphic)notice).rectTransform.anchorMax = Vector2.right;
((Graphic)notice).rectTransform.offsetMin = Vector2.zero;
((Graphic)notice).rectTransform.offsetMax = new Vector2(0f, 30f);
LocalMod[] array2 = array;
foreach (LocalMod mod in array2)
{
AddRow(mod, prefab, val2, num, val3);
}
RefreshNotice();
}
private void AddRow(LocalMod mod, MultiSelectWidget prefab, RectTransform parent, float minHeight, ScrollRect scroll)
{
MultiSelectWidget row = Object.Instantiate<MultiSelectWidget>(prefab, (Transform)(object)parent, false);
((ValueWidget<int>)(object)row.SetOnOff()).Initialize("UI/COMMON/SETTINGS", (Func<int>)(() => NextEnabled(draft, mod) ? 1 : 0), (Func<int, bool>)delegate(int value)
{
draft[mod.Key] = value != 0;
RefreshNotice();
return true;
});
bool flag = Chainloader.PluginInfos.Values.Any((PluginInfo plugin) => string.Equals(plugin.Location, mod.ActivePath, StringComparison.OrdinalIgnoreCase));
string text = (mod.ConflictingFiles ? "文件冲突" : (mod.ChangeFailed ? "切换失败" : (flag ? "已加载" : "未加载")));
((Widget)row).SetNonLocalizedLabel(mod.Name + "(" + text + ")");
LayoutElement val = ((Component)row).GetComponent<LayoutElement>();
if (!Object.op_Implicit((Object)(object)val))
{
val = ((Component)row).gameObject.AddComponent<LayoutElement>();
}
val.minHeight = Mathf.Max(val.minHeight, minHeight);
val.preferredHeight = Mathf.Max(val.preferredHeight, minHeight);
if (mod.IsMenu || mod.ConflictingFiles)
{
UIInteractable[] componentsInChildren = ((Component)row).GetComponentsInChildren<UIInteractable>(true);
for (int num = 0; num < componentsInChildren.Length; num++)
{
componentsInChildren[num].disabled = true;
}
}
((Widget)row).ForceUpdate();
UINavigable componentInChildren = ((Component)row).GetComponentInChildren<UINavigable>(true);
if (!Object.op_Implicit((Object)(object)componentInChildren))
{
return;
}
componentInChildren.onFocusChanged += delegate(bool focused)
{
//IL_0014: Unknown result type (might be due to invalid IL or missing references)
//IL_001e: Expected O, but got Unknown
if (focused)
{
RevealRow(scroll, (RectTransform)((Component)row).transform);
}
};
}
private void RefreshNotice()
{
bool flag = ModMenuBootstrap.Plugins.Any((LocalMod mod) => mod.IsSaveCompatibility && !NextEnabled(draft, mod));
notice.text = (flag ? "关闭存档兼容补丁可能让旧档再次无法读取。" : string.Empty);
}
private bool Save()
{
if (!ModMenuBootstrap.Plugins.Any((LocalMod mod) => NextEnabled(saved, mod) != NextEnabled(draft, mod)))
{
return true;
}
try
{
PluginSelection.Save(draft);
log.LogInfo((object)"Saved local mod selection for the next launch.");
return true;
}
catch (Exception ex)
{
notice.text = "保存失败。请查看 BepInEx 日志。";
log.LogError((object)ex);
return false;
}
}
private static bool NextEnabled(Dictionary<string, bool> selection, LocalMod mod)
{
if (!selection.TryGetValue(mod.Key, out var value))
{
return mod.IsEnabled;
}
return value;
}
private static RectTransform AddRect(string name, Transform parent)
{
//IL_0014: Unknown result type (might be due to invalid IL or missing references)
RectTransform component = new GameObject(name, new Type[1] { typeof(RectTransform) }).GetComponent<RectTransform>();
((Component)component).gameObject.layer = ((Component)parent).gameObject.layer;
((Transform)component).SetParent(parent, false);
return component;
}
private static void Stretch(RectTransform rect, Vector2 min, Vector2 max)
{
//IL_0001: Unknown result type (might be due to invalid IL or missing references)
//IL_000c: 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_001e: Unknown result type (might be due to invalid IL or missing references)
rect.anchorMin = Vector2.zero;
rect.anchorMax = Vector2.one;
rect.offsetMin = min;
rect.offsetMax = max;
}
private static Text AddLabel(string value, Text style, Transform parent)
{
//IL_002f: Unknown result type (might be due to invalid IL or missing references)
Text obj = ((Component)AddRect("Text", parent)).gameObject.AddComponent<Text>();
obj.font = style.font;
obj.fontSize = style.fontSize;
((Graphic)obj).color = ((Graphic)style).color;
((Graphic)obj).raycastTarget = false;
obj.alignment = (TextAnchor)4;
obj.text = value;
return obj;
}
private static void RevealRow(ScrollRect scroll, RectTransform row)
{
//IL_0006: Unknown result type (might be due to invalid IL or missing references)
//IL_000b: Unknown result type (might be due to invalid IL or missing references)
//IL_001a: Unknown result type (might be due to invalid IL or missing references)
//IL_001f: Unknown result type (might be due to invalid IL or missing references)
//IL_0043: Unknown result type (might be due to invalid IL or missing references)
//IL_004f: 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_0063: Unknown result type (might be due to invalid IL or missing references)
//IL_0073: 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_0088: Unknown result type (might be due to invalid IL or missing references)
//IL_008d: Unknown result type (might be due to invalid IL or missing references)
Rect rect = scroll.content.rect;
float height = ((Rect)(ref rect)).height;
rect = scroll.viewport.rect;
float num = height - ((Rect)(ref rect)).height;
if (!(num <= 0f))
{
float num2 = (1f - scroll.verticalNormalizedPosition) * num;
float num3 = 0f - row.anchoredPosition.y;
rect = row.rect;
float num4 = num3 - ((Rect)(ref rect)).height * (1f - row.pivot.y);
rect = row.rect;
float num5 = num4 + ((Rect)(ref rect)).height;
rect = scroll.viewport.rect;
float num6 = num5 - ((Rect)(ref rect)).height;
scroll.verticalNormalizedPosition = 1f - Mathf.Clamp01(Mathf.Clamp(num2, num6, num4) / num);
}
}
}