From ab2f03ee4dee948b62a6ff12669c0ee9eed120b2 Mon Sep 17 00:00:00 2001 From: XtraCube <72575280+XtraCube@users.noreply.github.com> Date: Wed, 12 Mar 2025 20:13:20 -0400 Subject: [PATCH] still broken --- MiraAPI/Patches/MainMenuManagerPatch.cs | 8 +--- .../Patches/Options/LobbyViewPanePatches.cs | 2 +- .../Options/NotificationPopperPatch.cs | 4 +- MiraAPI/Roles/CustomRoleManager.cs | 4 +- MiraAPI/Utilities/Assets/SpriteTools.cs | 5 ++- MiraAPI/Utilities/Helpers.cs | 40 +++++++++++++++---- 6 files changed, 43 insertions(+), 20 deletions(-) diff --git a/MiraAPI/Patches/MainMenuManagerPatch.cs b/MiraAPI/Patches/MainMenuManagerPatch.cs index a54efe0..126e1a0 100644 --- a/MiraAPI/Patches/MainMenuManagerPatch.cs +++ b/MiraAPI/Patches/MainMenuManagerPatch.cs @@ -1,9 +1,5 @@ -using System; -using System.Linq; -using HarmonyLib; -using Il2CppInterop.Runtime; +using HarmonyLib; using MiraAPI.Roles; -using MiraAPI.Utilities; using TMPro; namespace MiraAPI.Patches; @@ -16,7 +12,7 @@ public class MainMenuManagerPatch [HarmonyPatch(nameof(MainMenuManager.Start))] public static void StartPatch() { - TMP_Settings.defaultSpriteAsset.fallbackSpriteAssets.Add(CustomRoleManager.SpriteAsset); + TMP_Settings.instance.m_defaultSpriteAsset = CustomRoleManager.SpriteAsset; /*TMP_Text.OnSpriteAssetRequest += DelegateSupport.ConvertDelegate>(new Func((index, str) => { var asset = Helpers.RegisteredSpriteAssets.First(x => x.name.Contains(str)); diff --git a/MiraAPI/Patches/Options/LobbyViewPanePatches.cs b/MiraAPI/Patches/Options/LobbyViewPanePatches.cs index c8e782a..43c5719 100644 --- a/MiraAPI/Patches/Options/LobbyViewPanePatches.cs +++ b/MiraAPI/Patches/Options/LobbyViewPanePatches.cs @@ -331,7 +331,7 @@ public static class LobbyViewPanePatches chancePerGame, 61, customRole.RoleColor, - customRole.Configuration.Icon != null ? customRole.Configuration.Icon.LoadAsset() : MiraAssets.Empty.LoadAsset(), + customRole.Configuration.Icon?.LoadAsset() ?? MiraAssets.Empty.LoadAsset(), true); viewSettingsInfoPanelRoleVariant.iconSprite.transform.localScale = new Vector3(0.365f, 0.365f, 1f); viewSettingsInfoPanelRoleVariant.iconSprite.transform.localPosition = new Vector3(0.7144f, -0.028f, -2); diff --git a/MiraAPI/Patches/Options/NotificationPopperPatch.cs b/MiraAPI/Patches/Options/NotificationPopperPatch.cs index 522bc2b..78a8d38 100644 --- a/MiraAPI/Patches/Options/NotificationPopperPatch.cs +++ b/MiraAPI/Patches/Options/NotificationPopperPatch.cs @@ -23,7 +23,7 @@ public static class NotificationPopperPatch [HarmonyArgument(4)] bool playSound) { var role = CustomRoleManager.CustomRoles.Values.FirstOrDefault(x => x.StringName == key); - if (!role || role is not ICustomRole) + if (!role || role is not ICustomRole customRole) { return true; } @@ -37,7 +37,7 @@ public static class NotificationPopperPatch var item = TranslationController.Instance.GetString( StringNames.LobbyChangeSettingNotificationRole, string.Concat( - $"", + $"", "", text, TranslationController.Instance.GetString(key, Array.Empty()), diff --git a/MiraAPI/Roles/CustomRoleManager.cs b/MiraAPI/Roles/CustomRoleManager.cs index 1484e26..fa49d65 100644 --- a/MiraAPI/Roles/CustomRoleManager.cs +++ b/MiraAPI/Roles/CustomRoleManager.cs @@ -62,7 +62,7 @@ public static class CustomRoleManager pluginInfo.CustomRoles.Add((ushort)role.Role, role); } - SpriteAsset = Helpers.CreateSpriteAsset(_roleIcons, "RoleIcons"); + SpriteAsset = Helpers.CreateSpriteAsset(_roleIcons, "CustomRoleIcons"); } private static RoleBehaviour? RegisterRole(Type roleType, MiraPluginInfo parentMod) @@ -105,7 +105,7 @@ public static class CustomRoleManager { roleBehaviour.RoleIconSolid = asset; roleBehaviour.RoleIconWhite = asset; - _roleIcons.Add(roleBehaviour.NiceName, asset); + _roleIcons.Add(customRole.RoleName, asset); } } diff --git a/MiraAPI/Utilities/Assets/SpriteTools.cs b/MiraAPI/Utilities/Assets/SpriteTools.cs index fa49f12..5d438de 100644 --- a/MiraAPI/Utilities/Assets/SpriteTools.cs +++ b/MiraAPI/Utilities/Assets/SpriteTools.cs @@ -30,6 +30,9 @@ public static class SpriteTools throw new ArgumentException($"Resource not found: {resourcePath}"); } - return Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f), pixelsPerUnit); + tex.name = resourcePath; + var sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f), pixelsPerUnit); + sprite.name = resourcePath; + return sprite; } } diff --git a/MiraAPI/Utilities/Helpers.cs b/MiraAPI/Utilities/Helpers.cs index 7e5e812..0c65c90 100644 --- a/MiraAPI/Utilities/Helpers.cs +++ b/MiraAPI/Utilities/Helpers.cs @@ -6,6 +6,7 @@ using System.Linq; using System.Text; using HarmonyLib; using MiraAPI.Roles; +using Reactor.Utilities; using Reactor.Utilities.Extensions; using TMPro; using UnityEngine; @@ -30,6 +31,23 @@ public static class Helpers internal static readonly List RegisteredSpriteAssets = []; + public static void Dump(this Texture2D texture, string path) => File.WriteAllBytes(path, texture.Decompress().EncodeToPNG()); + + public static Texture2D Decompress(this Texture2D source) + { + var renderTex = RenderTexture.GetTemporary(source.width, source.height, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Linear); + Graphics.Blit(source, renderTex); + var previous = RenderTexture.active; + RenderTexture.active = renderTex; + var readableText = new Texture2D(source.width, source.height); + readableText.ReadPixels(new(0, 0, renderTex.width, renderTex.height), 0, 0); + readableText.Apply(); + RenderTexture.active = previous; + RenderTexture.ReleaseTemporary(renderTex); + readableText.name = source.name; + return readableText; + } + /// /// Allows you to create a custom sprite asset. Normally used for TextMeshPro. /// Ensure you set the TMP object's sprite asset to this one and use the sprite name tag. @@ -44,11 +62,15 @@ public static class Helpers var asset = ScriptableObject.CreateInstance(); var image = new Texture2D(2048, 2048) { name = spriteAssetName }; var rects = image.PackTextures(textures, 2); + Dump(image, $"{spriteAssetName}.png"); for (var i = 0; i < rects.Length; i++) { var rect = rects[i]; var pair = sprites.ElementAt(i); + var sprite = pair.Value; + var spriteRect = sprite.rect; + var pivot = sprite.pivot; var glyph = new TMP_SpriteGlyph { @@ -61,17 +83,19 @@ public static class Helpers }, metrics = new GlyphMetrics { - width = (int)(rect.width * image.width), - height = (int)(rect.height * image.height), - horizontalBearingY = (int)(rect.height * image.height) * 0.75f, - horizontalBearingX = 0, - horizontalAdvance = (int)(rect.width * image.width), + width = spriteRect.width, + height = spriteRect.height, + horizontalBearingX = pivot.x, + horizontalBearingY = spriteRect.height - pivot.y, // Top of the sprite relative to baseline + horizontalAdvance = spriteRect.width, }, index = (uint)i, - sprite = pair.Value, + sprite = sprite, }; - var character = new TMP_SpriteCharacter(0, asset, glyph) + // Assign unique Unicode from the Private Use Area + var unicode = 0xE000 + (uint)i; + var character = new TMP_SpriteCharacter(unicode, asset, glyph) { name = pair.Key, glyphIndex = (uint)i, @@ -84,7 +108,7 @@ public static class Helpers asset.name = spriteAssetName; asset.hashCode = TMP_TextUtilities.GetSimpleHashCode(asset.name); asset.material = new Material(Shader.Find("TextMeshPro/Sprite")); - AccessTools.Property(asset.GetType(), "version").SetValue(asset, "1.1.0"); + asset.version = "1.1.0"; asset.material.mainTexture = asset.spriteSheet = image; asset.UpdateLookupTables(); RegisteredSpriteAssets.Add(asset);