Admit defeat to custom sprite assets

This commit is contained in:
ang-xd committed 2025-03-15 15:04:57 -04:00
1 parent ab2f03ee4d
commit bc7b835dc8
4 files changed
+2 -147

No files matched your search

-22
View File
@@ -1,22 +0,0 @@
using HarmonyLib;
using MiraAPI.Roles;
using TMPro;
namespace MiraAPI.Patches;
[HarmonyPatch(typeof(MainMenuManager))]
public class MainMenuManagerPatch
{
[HarmonyPostfix]
[HarmonyPatch(nameof(MainMenuManager.Start))]
public static void StartPatch()
{
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));
return asset != null ? asset : null;
}));*/
}
}
@@ -2,10 +2,6 @@
using HarmonyLib;
using Il2CppSystem;
using MiraAPI.Roles;
using Reactor.Utilities;
using UnityEngine;
using Action = System.Action;
using Object = Il2CppSystem.Object;
namespace MiraAPI.Patches.Options;
@@ -22,14 +18,11 @@ public static class NotificationPopperPatch
[HarmonyArgument(3)] RoleTeamTypes teamType,
[HarmonyArgument(4)] bool playSound)
{
var role = CustomRoleManager.CustomRoles.Values.FirstOrDefault(x => x.StringName == key);
if (!role || role is not ICustomRole customRole)
if (CustomRoleManager.CustomRoles.Values.FirstOrDefault(x=>x.StringName==key) is not ICustomRole customRole)
{
return true;
}
Logger<MiraApiPlugin>.Error("UPDATING ROLE TEXT");
var text = teamType == RoleTeamTypes.Crewmate
? Palette.CrewmateSettingChangeText.ToTextColor()
: Palette.ImpostorRed.ToTextColor();
@@ -37,7 +30,6 @@ public static class NotificationPopperPatch
var item = TranslationController.Instance.GetString(
StringNames.LobbyChangeSettingNotificationRole,
string.Concat(
$"<sprite name=\"{customRole.RoleName}\">",
"<font=\"Barlow-Black SDF\" material=\"Barlow-Black Outline\">",
text,
TranslationController.Instance.GetString(key, Array.Empty<Object>()),
@@ -47,31 +39,7 @@ public static class NotificationPopperPatch
"<font=\"Barlow-Black SDF\" material=\"Barlow-Black Outline\">" + roleChance + "%"
);
CustomMessageLogic(__instance, role, item, playSound);
__instance.SettingsChangeMessageLogic(key, item, playSound);
return false;
}
private static void CustomMessageLogic(NotificationPopper __instance, RoleBehaviour role, string item, bool playSound)
{
if (__instance.lastMessageKey == (int)role.StringName && __instance.activeMessages.Count > 0)
{
__instance.activeMessages.ToArray()[__instance.activeMessages.Count - 1].UpdateMessage(item);
}
else
{
__instance.lastMessageKey = (int)role.StringName;
var newMessage = UnityEngine.Object.Instantiate(__instance.notificationMessageOrigin, Vector3.zero, Quaternion.identity, __instance.transform);
newMessage.transform.localPosition = new Vector3(0f, 0f, -2f);
newMessage.SetUp(item, __instance.settingsChangeSprite, __instance.settingsChangeColor, new Action(delegate
{
__instance.OnMessageDestroy(newMessage);
}));
__instance.ShiftMessages();
__instance.AddMessageToQueue(newMessage);
}
if (playSound)
{
SoundManager.Instance.PlaySoundImmediate(__instance.settingsChangeSound, false, 1f, 1f, null);
}
}
}
-5
View File
@@ -25,9 +25,7 @@ public static class CustomRoleManager
{
internal static readonly Dictionary<ushort, RoleBehaviour> CustomRoles = [];
internal static readonly Dictionary<Type, ushort> RoleIds = [];
internal static TMP_SpriteAsset SpriteAsset;
private static Dictionary<string, Sprite> _roleIcons = [];
private static ushort _roleId = 100;
private static ushort GetNextRoleId()
@@ -61,8 +59,6 @@ public static class CustomRoleManager
pluginInfo.CustomRoles.Add((ushort)role.Role, role);
}
SpriteAsset = Helpers.CreateSpriteAsset(_roleIcons, "CustomRoleIcons");
}
private static RoleBehaviour? RegisterRole(Type roleType, MiraPluginInfo parentMod)
@@ -105,7 +101,6 @@ public static class CustomRoleManager
{
roleBehaviour.RoleIconSolid = asset;
roleBehaviour.RoleIconWhite = asset;
_roleIcons.Add(customRole.RoleName, asset);
}
}
-86
View File
@@ -29,92 +29,6 @@ public static class Helpers
return PlayerControl.AllPlayerControls.ToArray().Where(x => !x.Data.IsDead).ToList();
}
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.
/// https://docs.unity3d.com/Packages/com.unity.textmeshpro@4.0/manual/RichTextSprite.html
/// </summary>
/// <param name="sprites">The dictionary of sprites you want to add to the asset. String is the name of the sprite you'd like to use for rich text.</param>
/// <param name="spriteAssetName">The name of the sprite asset.</param>
/// <returns>The created sprite asset.</returns>
public static TMP_SpriteAsset CreateSpriteAsset(Dictionary<string, Sprite> sprites, string spriteAssetName)
{
var textures = sprites.Values.Select(x => x.texture).ToArray();
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
{
glyphRect = new GlyphRect
{
x = (int)(rect.x * image.width),
y = (int)(rect.y * image.height),
width = (int)(rect.width * image.width),
height = (int)(rect.height * image.height),
},
metrics = new GlyphMetrics
{
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 = sprite,
};
// 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,
};
asset.spriteGlyphTable.Add(glyph);
asset.spriteCharacterTable.Add(character);
}
asset.name = spriteAssetName;
asset.hashCode = TMP_TextUtilities.GetSimpleHashCode(asset.name);
asset.material = new Material(Shader.Find("TextMeshPro/Sprite"));
asset.version = "1.1.0";
asset.material.mainTexture = asset.spriteSheet = image;
asset.UpdateLookupTables();
RegisteredSpriteAssets.Add(asset);
return asset.DontDestroyOnLoad().DontUnload();
}
/// <summary>
/// Returns an empty coroutine.
/// </summary>