still broken

This commit is contained in:
XtraCube committed 2025-03-12 20:13:20 -04:00
1 parent 645d1dad1c
commit ab2f03ee4d
6 files changed
+43 -20

No files matched your search

+2 -6
View File
@@ -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<Il2CppSystem.Func<int, string, TMP_SpriteAsset>>(new Func<int, string, TMP_SpriteAsset>((index, str) =>
{
var asset = Helpers.RegisteredSpriteAssets.First(x => x.name.Contains(str));
@@ -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);
@@ -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(
$"<sprite name=\"{role.NiceName}\">",
$"<sprite name=\"{customRole.RoleName}\">",
"<font=\"Barlow-Black SDF\" material=\"Barlow-Black Outline\">",
text,
TranslationController.Instance.GetString(key, Array.Empty<Object>()),
+2 -2
View File
@@ -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);
}
}
+4 -1
View File
@@ -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;
}
}
+32 -8
View File
@@ -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<TMP_SpriteAsset> 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;
}
/// <summary>
/// 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<TMP_SpriteAsset>();
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);