using System; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.Versioning; using System.Security; using System.Security.Permissions; using System.Text; using BepInEx; using BepInEx.Logging; using HarmonyLib; using Microsoft.CodeAnalysis; using UnityEngine; using UnityEngine.UI; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")] [assembly: IgnoresAccessChecksTo("Assembly-CSharp-firstpass")] [assembly: IgnoresAccessChecksTo("Assembly-CSharp")] [assembly: IgnoresAccessChecksTo("FishNet.Runtime")] [assembly: AssemblyCompany("SpongeMods")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyDescription("Shared UI toolkit for Sponge mods.")] [assembly: AssemblyFileVersion("1.0.0.0")] [assembly: AssemblyInformationalVersion("1.0.0+a149c533da6d929b7267867242f5011f9c2152dc")] [assembly: AssemblyProduct("SpongeMods.UILib")] [assembly: AssemblyTitle("SpongeMods.UILib")] [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.Module, AllowMultiple = false, Inherited = false)] internal sealed class RefSafetyRulesAttribute : Attribute { public readonly int Version; public RefSafetyRulesAttribute(int P_0) { Version = P_0; } } } namespace SpongeMods.UILib { public sealed class Column { private readonly RectTransform _host; private float _cursor; public float Spacing = 8f; public float Padding = 10f; public float Inset = 12f; public float Height => Mathf.Max(0f, _cursor - Spacing) + Padding; public Column(RectTransform host) { _host = host; _cursor = Padding; } public float Add(RectTransform child, float height) { //IL_0018: Unknown result type (might be due to invalid IL or missing references) //IL_002d: Unknown result type (might be due to invalid IL or missing references) //IL_0042: Unknown result type (might be due to invalid IL or missing references) //IL_0058: Unknown result type (might be due to invalid IL or missing references) //IL_006f: Unknown result type (might be due to invalid IL or missing references) //IL_0086: Unknown result type (might be due to invalid IL or missing references) //IL_0092: Unknown result type (might be due to invalid IL or missing references) //IL_009d: Unknown result type (might be due to invalid IL or missing references) ((Transform)child).SetParent((Transform)(object)_host, false); child.anchorMin = new Vector2(0f, 1f); child.anchorMax = new Vector2(1f, 1f); child.pivot = new Vector2(0.5f, 1f); child.offsetMin = new Vector2(Inset, 0f); child.offsetMax = new Vector2(0f - Inset, 0f); child.anchoredPosition = new Vector2(0f, 0f - _cursor); child.sizeDelta = new Vector2(child.sizeDelta.x, height); float cursor = _cursor; _cursor += height + Spacing; return cursor; } public void Gap(float height) { _cursor += height; } public void Rewind() { _cursor = Padding; } } public enum Anchor { TopLeft, TopCentre, TopRight, MiddleLeft, Centre, MiddleRight, BottomLeft, BottomCentre, BottomRight } public readonly struct Placement { public readonly Anchor Anchor; public readonly float X; public readonly float Y; public Placement(Anchor anchor, float x, float y) { Anchor = anchor; X = x; Y = y; } public Vector2 Normalised() { //IL_0076: Unknown result type (might be due to invalid IL or missing references) float num; switch (Anchor) { case Anchor.TopLeft: case Anchor.MiddleLeft: case Anchor.BottomLeft: num = 0f; break; case Anchor.TopRight: case Anchor.MiddleRight: case Anchor.BottomRight: num = 1f; break; default: num = 0.5f; break; } float num2 = num; switch (Anchor) { case Anchor.BottomLeft: case Anchor.BottomCentre: case Anchor.BottomRight: num = 0f; break; case Anchor.TopLeft: case Anchor.TopCentre: case Anchor.TopRight: num = 1f; break; default: num = 0.5f; break; } float num3 = num; return new Vector2(num2, num3); } public void ApplyTo(RectTransform rect) { //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_0008: Unknown result type (might be due to invalid IL or missing references) //IL_000f: Unknown result type (might be due to invalid IL or missing references) //IL_0016: Unknown result type (might be due to invalid IL or missing references) //IL_001c: 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_0059: Unknown result type (might be due to invalid IL or missing references) Vector2 val = (rect.pivot = (rect.anchorMax = (rect.anchorMin = Normalised()))); float num = ((val.x > 0.5f) ? (0f - X) : X); float num2 = ((val.y > 0.5f) ? (0f - Y) : Y); rect.anchoredPosition = new Vector2(num, num2); } } public static class Stage { private const string HostName = "SpongeMods.UILib.Root"; private static Canvas _canvas; private static Sprite _pixel; private const int Layer = 200; private static readonly Dictionary _rounded = new Dictionary(); public static Canvas Root { get { //IL_0018: Unknown result type (might be due to invalid IL or missing references) //IL_001d: Unknown result type (might be due to invalid IL or missing references) //IL_0023: Expected O, but got Unknown //IL_0023: Unknown result type (might be due to invalid IL or missing references) //IL_005f: Unknown result type (might be due to invalid IL or missing references) if ((Object)(object)_canvas != (Object)null) { return _canvas; } GameObject val = new GameObject("SpongeMods.UILib.Root"); Object.DontDestroyOnLoad((Object)val); _canvas = val.AddComponent(); _canvas.renderMode = (RenderMode)0; _canvas.sortingOrder = 200; CanvasScaler obj = val.AddComponent(); obj.uiScaleMode = (ScaleMode)1; obj.referenceResolution = new Vector2(1920f, 1080f); obj.matchWidthOrHeight = 1f; return _canvas; } } public static Sprite Pixel { get { //IL_0017: Unknown result type (might be due to invalid IL or missing references) //IL_001c: Unknown result type (might be due to invalid IL or missing references) //IL_0023: Unknown result type (might be due to invalid IL or missing references) //IL_002a: Unknown result type (might be due to invalid IL or missing references) //IL_002d: Unknown result type (might be due to invalid IL or missing references) //IL_0037: Unknown result type (might be due to invalid IL or missing references) //IL_003d: Unknown result type (might be due to invalid IL or missing references) //IL_0043: Expected O, but got Unknown //IL_0057: Unknown result type (might be due to invalid IL or missing references) //IL_0066: Unknown result type (might be due to invalid IL or missing references) //IL_0070: Expected O, but got Unknown if ((Object)(object)_pixel != (Object)null) { return _pixel; } Texture2D val = new Texture2D(1, 1, (TextureFormat)4, false) { wrapMode = (TextureWrapMode)1, filterMode = (FilterMode)1 }; val.SetPixel(0, 0, Color.white); val.Apply(); Object.DontDestroyOnLoad((Object)val); _pixel = Sprite.Create(val, new Rect(0f, 0f, 1f, 1f), new Vector2(0.5f, 0.5f)); Object.DontDestroyOnLoad((Object)(object)_pixel); return _pixel; } } public static Sprite Rounded(int radius) { //IL_002f: Unknown result type (might be due to invalid IL or missing references) //IL_0034: 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_0043: Expected O, but got Unknown //IL_0138: Unknown result type (might be due to invalid IL or missing references) //IL_0147: Unknown result type (might be due to invalid IL or missing references) //IL_015b: Unknown result type (might be due to invalid IL or missing references) //IL_00f0: Unknown result type (might be due to invalid IL or missing references) //IL_00f5: Unknown result type (might be due to invalid IL or missing references) radius = Mathf.Clamp(radius, 2, 64); if (_rounded.TryGetValue(radius, out var value) && (Object)(object)value != (Object)null) { return value; } int num = radius * 2 + 2; Texture2D val = new Texture2D(num, num, (TextureFormat)4, false) { wrapMode = (TextureWrapMode)1, filterMode = (FilterMode)1 }; Color[] array = (Color[])(object)new Color[num * num]; float num2 = radius; for (int i = 0; i < num; i++) { for (int j = 0; j < num; j++) { float num3 = Mathf.Max(0f, Mathf.Max(num2 - ((float)j + 0.5f), (float)j + 0.5f - ((float)num - num2))); float num4 = Mathf.Max(0f, Mathf.Max(num2 - ((float)i + 0.5f), (float)i + 0.5f - ((float)num - num2))); float num5 = Mathf.Sqrt(num3 * num3 + num4 * num4) - num2; float num6 = Mathf.Clamp01(0.5f - num5); array[i * num + j] = new Color(1f, 1f, 1f, num6); } } val.SetPixels(array); val.Apply(); Object.DontDestroyOnLoad((Object)(object)val); Sprite val2 = Sprite.Create(val, new Rect(0f, 0f, (float)num, (float)num), new Vector2(0.5f, 0.5f), 100f, 0u, (SpriteMeshType)0, new Vector4((float)radius, (float)radius, (float)radius, (float)radius)); Object.DontDestroyOnLoad((Object)(object)val2); _rounded[radius] = val2; return val2; } public static Image Fill(string name, RectTransform parent, Color colour, int radius = 10) { //IL_0030: Unknown result type (might be due to invalid IL or missing references) Image obj = ((Component)NewRect(name, parent)).gameObject.AddComponent(); obj.sprite = Rounded(radius); obj.type = (Type)1; obj.pixelsPerUnitMultiplier = 1f; ((Graphic)obj).color = colour; ((Graphic)obj).raycastTarget = false; return obj; } public static void Spread(RectTransform rect, float bleed = 0f) { //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_0019: Unknown result type (might be due to invalid IL or missing references) //IL_0028: Unknown result type (might be due to invalid IL or missing references) rect.anchorMin = Vector2.zero; rect.anchorMax = Vector2.one; rect.offsetMin = new Vector2(bleed, bleed); rect.offsetMax = new Vector2(0f - bleed, 0f - bleed); } public static RectTransform NewRect(string name, RectTransform parent = null) { //IL_0014: 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_0023: Unknown result type (might be due to invalid IL or missing references) //IL_0041: Expected O, but got Unknown RectTransform val = (RectTransform)new GameObject(name, new Type[1] { typeof(RectTransform) }).transform; ((Transform)val).SetParent((Transform)(((Object)(object)parent != (Object)null) ? ((object)parent) : ((object)((Component)Root).transform)), false); return val; } } public static class Focus { private static object _holder; private static Action _onLost; public static bool Held => _holder != null; private static bool GameScreenOpen { get { if (!PauseManager.IsPaused && !PlayerThinking.IsThinking) { return MainMenuManager.IsInMenu; } return true; } } public static bool Available { get { if ((Object)(object)Player.LocalPlayer != (Object)null) { return !GameScreenOpen; } return false; } } public static bool HeldBy(object who) { if (who != null) { return _holder == who; } return false; } public static bool Claim(object who, Action onLost = null) { if (who == null) { return false; } if (Held) { return HeldBy(who); } if (!Available) { return false; } _holder = who; _onLost = onLost; SyncCursor(); return true; } public static void Release(object who) { if (HeldBy(who)) { Drop(tell: false); } } internal static void Step() { if (Held && (GameScreenOpen || (Object)(object)Player.LocalPlayer == (Object)null)) { Drop(tell: true); } } private static void Drop(bool tell) { Action onLost = _onLost; _holder = null; _onLost = null; SyncCursor(); if (tell) { onLost?.Invoke(); } } private static void SyncCursor() { if ((Object)(object)Player.LocalPlayer != (Object)null) { PlayerCamera.ToggleMouse(Held); } } } internal static class GameHooks { [HarmonyPatch(/*Could not decode attribute arguments.*/)] internal static class SuspendWhileOpen { private static void Postfix(Player __instance, ref bool __result) { if (!__result && Focus.Held && __instance == Player.LocalPlayer) { __result = true; } } } [HarmonyPatch(typeof(PlayerCamera), "ToggleMouse")] internal static class HoldCursorFree { private static void Prefix(ref bool unlock) { if (Focus.Held) { unlock = true; } } } } public static class Pointer { private static int _spent = -1; public static Vector2 Position => Vector2.op_Implicit(Input.mousePosition); public static bool Pressed { get { if (Input.GetMouseButtonDown(0)) { return _spent != Time.frameCount; } return false; } } public static bool Down => Input.GetMouseButton(0); public static bool Released => Input.GetMouseButtonUp(0); public static float Wheel => Input.mouseScrollDelta.y; public static void Spend() { _spent = Time.frameCount; } public static bool Over(RectTransform rect) { //IL_0017: Unknown result type (might be due to invalid IL or missing references) if ((Object)(object)rect != (Object)null && ((Component)rect).gameObject.activeInHierarchy) { return RectTransformUtility.RectangleContainsScreenPoint(rect, Position, (Camera)null); } return false; } } public static class Typing { private static object _target; private static readonly StringBuilder _buffer = new StringBuilder(); private static Action _commit; private static Action _cancel; public const int Limit = 64; public static bool Active => _target != null; public static string Text => _buffer.ToString(); public static bool ActiveOn(object who) { if (who != null) { return _target == who; } return false; } public static void Begin(object who, string initial, Action commit, Action cancel = null) { if (who != null) { if (Active && !ActiveOn(who)) { Stop(keep: false); } _target = who; _commit = commit; _cancel = cancel; _buffer.Length = 0; if (!string.IsNullOrEmpty(initial)) { _buffer.Append(initial, 0, Mathf.Min(initial.Length, 64)); } } } public static void Abandon(object who) { if (ActiveOn(who)) { Stop(keep: false); } } public static bool Step(object who) { if (!ActiveOn(who)) { return false; } if (Input.GetKeyDown((KeyCode)27)) { Stop(keep: false); return true; } if (Input.GetKeyDown((KeyCode)13) || Input.GetKeyDown((KeyCode)271)) { Stop(keep: true); return true; } bool result = false; string inputString = Input.inputString; foreach (char c in inputString) { if (c == '\b') { if (_buffer.Length > 0) { _buffer.Length--; result = true; } } else if (c >= ' ' && _buffer.Length < 64) { _buffer.Append(c); result = true; } } return result; } private static void Stop(bool keep) { Action commit = _commit; Action cancel = _cancel; string obj = _buffer.ToString(); _target = null; _commit = null; _cancel = null; _buffer.Length = 0; try { if (keep) { commit?.Invoke(obj); } else { cancel?.Invoke(); } } catch (Exception ex) { UILibPlugin.Log.LogError((object)("A text field rejected its own value: " + ex.Message)); } } } [BepInPlugin("SpongeMods.UILib", "Sponge's UI Library", "1.0.0")] public sealed class UILibPlugin : BaseUnityPlugin { public const string Id = "SpongeMods.UILib"; public const string Version = "1.0.0"; internal static ManualLogSource Log { get; private set; } private void Awake() { //IL_0010: Unknown result type (might be due to invalid IL or missing references) Log = ((BaseUnityPlugin)this).Logger; try { new Harmony("SpongeMods.UILib").PatchAll(); } catch (Exception ex) { Log.LogError((object)("Could not install the input patches; Sponge menus will not take the mouse: " + ex.Message)); } Log.LogInfo((object)"UI library ready."); } private void Update() { Focus.Step(); } } public sealed class Palette { public Color Surface = new Color(0.043f, 0.086f, 0.126f, 0.92f); public Color Accent = new Color(1f, 0.729f, 0.259f, 1f); public Color Alert = new Color(1f, 0.361f, 0.322f, 1f); public Color Ink = new Color(0.949f, 0.961f, 0.976f, 1f); public Color Rim = new Color(0.31f, 0.545f, 0.639f, 0.85f); public static readonly Palette Default = new Palette(); public int PanelRadius = 14; public int BarRadius = 9; public int ControlRadius = 8; public Color Well => Fade(Shade(Surface, -0.03f), Mathf.Min(1f, Surface.a + 0.06f)); public Color Face => Fade(Shade(Surface, 0.09f), 1f); public Color FaceLit => Fade(Shade(Surface, 0.17f), 1f); public Color FaceHeld => Fade(Shade(Surface, -0.04f), 1f); public Color Muted => Fade(Ink, 0.34f); public static Color Gloss => new Color(1f, 1f, 1f, 0.16f); public static Color Shade(Color source, float amount) { //IL_0000: Unknown result type (might be due to invalid IL or missing references) //IL_0016: Unknown result type (might be due to invalid IL or missing references) //IL_001b: 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_0029: Unknown result type (might be due to invalid IL or missing references) float num = default(float); float num2 = default(float); float num3 = default(float); Color.RGBToHSV(source, ref num, ref num2, ref num3); Color result = Color.HSVToRGB(num, num2, Mathf.Clamp01(num3 + amount)); result.a = source.a; return result; } public static Color Fade(Color source, float alpha) { //IL_000d: Unknown result type (might be due to invalid IL or missing references) source.a = Mathf.Clamp01(alpha); return source; } public Color ForFraction(float fraction) { //IL_000e: Unknown result type (might be due to invalid IL or missing references) //IL_0014: Unknown result type (might be due to invalid IL or missing references) //IL_001c: Unknown result type (might be due to invalid IL or missing references) float num = 1f - Mathf.Clamp01(fraction); return Color.Lerp(Accent, Alert, num * num); } public Color FaceFor(bool interactive, bool hovered, bool pressed) { //IL_0004: Unknown result type (might be due to invalid IL or missing references) //IL_000e: Unknown result type (might be due to invalid IL or missing references) //IL_0018: Unknown result type (might be due to invalid IL or missing references) //IL_0022: 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_002c: Unknown result type (might be due to invalid IL or missing references) if (!interactive) { return Fade(Shade(Surface, 0.03f), 0.7f); } if (pressed) { return FaceHeld; } if (!hovered) { return Face; } return FaceLit; } } public sealed class Button : Control { private readonly Image _rim; private readonly Image _face; private readonly Image _sheen; private readonly Label _label; public Action Action; public Color Wash = new Color(0f, 0f, 0f, 0f); public Color Ink = new Color(0f, 0f, 0f, 0f); public string Text { get { return _label.Text; } set { _label.Text = value; } } public float Measure() { return _label.Measure(); } public Button(string text, RectTransform parent, Palette palette, float textSize = 17f, TextAnchor align = (TextAnchor)4) : base(palette) { //IL_0015: 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_0034: 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_0068: Unknown result type (might be due to invalid IL or missing references) //IL_00ab: Unknown result type (might be due to invalid IL or missing references) //IL_00e6: Unknown result type (might be due to invalid IL or missing references) //IL_0116: Unknown result type (might be due to invalid IL or missing references) //IL_012b: Unknown result type (might be due to invalid IL or missing references) //IL_0140: Unknown result type (might be due to invalid IL or missing references) //IL_0154: Unknown result type (might be due to invalid IL or missing references) //IL_0172: Unknown result type (might be due to invalid IL or missing references) //IL_017e: Unknown result type (might be due to invalid IL or missing references) //IL_01b4: 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) base.Rect = Stage.NewRect("Button", parent); _rim = Stage.Fill("Rim", base.Rect, Theme.Rim, Theme.ControlRadius + 2); Stage.Spread(((Graphic)_rim).rectTransform, -1.5f); _face = Stage.Fill("Face", base.Rect, Theme.Face, Theme.ControlRadius); Stage.Spread(((Graphic)_face).rectTransform); _sheen = Stage.Fill("Sheen", base.Rect, Palette.Gloss, Theme.ControlRadius); RectTransform rectTransform = ((Graphic)_sheen).rectTransform; rectTransform.anchorMin = new Vector2(0f, 0.5f); rectTransform.anchorMax = new Vector2(1f, 1f); rectTransform.offsetMin = new Vector2(2f, 0f); rectTransform.offsetMax = new Vector2(-2f, -2f); _label = new Label("Text", base.Rect, textSize, Theme.Ink) { Alignment = align }; Stage.Spread(_label.Rect); _label.Rect.offsetMin = new Vector2(10f, 0f); _label.Rect.offsetMax = new Vector2(-10f, 0f); _label.Text = text; Repaint(); } protected override void Clicked() { if (Action == null) { return; } try { Action(); } catch (Exception ex) { UILibPlugin.Log.LogError((object)("'" + Text + "' failed: " + ex.Message)); } } protected override void Repaint() { //IL_0018: Unknown result type (might be due to invalid IL or missing references) //IL_001d: Unknown result type (might be due to invalid IL or missing references) //IL_004e: 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_0032: Unknown result type (might be due to invalid IL or missing references) //IL_0042: Unknown result type (might be due to invalid IL or missing references) //IL_0047: Unknown result type (might be due to invalid IL or missing references) //IL_0068: Unknown result type (might be due to invalid IL or missing references) //IL_0072: Unknown result type (might be due to invalid IL or missing references) //IL_0094: Unknown result type (might be due to invalid IL or missing references) //IL_0087: Unknown result type (might be due to invalid IL or missing references) //IL_00d9: Unknown result type (might be due to invalid IL or missing references) //IL_00cc: Unknown result type (might be due to invalid IL or missing references) //IL_00c4: Unknown result type (might be due to invalid IL or missing references) Color val = Theme.FaceFor(Interactive, base.Hovered, base.Pressing); if (Wash.a > 0f) { val = Color.Lerp(val, Wash, Wash.a); } ((Graphic)_face).color = val; ((Graphic)_rim).color = ((!Interactive) ? Palette.Fade(Theme.Rim, 0.3f) : (base.Hovered ? Theme.Accent : Theme.Rim)); _label.Colour = ((!Interactive) ? Theme.Muted : ((Ink.a > 0f) ? Ink : Theme.Ink)); ((Behaviour)_sheen).enabled = Interactive && !base.Pressing; } } public sealed class Chooser : Control { private const float Gap = 4f; private const int MostSegments = 4; private const float ArrowWidth = 30f; private readonly RectTransform _row; private readonly RectTransform _strip; private readonly Label _label; private readonly List