Skip to content

Commit

Permalink
[+] Some font features
Browse files Browse the repository at this point in the history
  • Loading branch information
clansty committed Oct 2, 2024
1 parent bae5a7c commit ac375ab
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 0 deletions.
2 changes: 2 additions & 0 deletions AquaMai/AquaMai.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ DEBUG</DefineConstants>
<Compile Include="Fix\ExtendNotesPool.cs" />
<Compile Include="Fix\FixCharaCrash.cs" />
<Compile Include="Fix\FixCheckAuth.cs" />
<Compile Include="Fix\FontFix.cs" />
<Compile Include="Fix\ForceAsServer.cs" />
<Compile Include="Fix\ForceFreePlay.cs" />
<Compile Include="Fix\ForcePaidPlay.cs" />
Expand Down Expand Up @@ -332,6 +333,7 @@ DEBUG</DefineConstants>
<Compile Include="Utils\PractiseModeUI.cs" />
<Compile Include="Utils\SelectionDetail.cs" />
<Compile Include="Utils\ShowNetErrorDetail.cs" />
<Compile Include="UX\CustomFont.cs" />
<Compile Include="UX\CustomPlaceName.cs" />
<Compile Include="UX\CustomVersionString.cs" />
<Compile Include="UX\DemoMaster.cs" />
Expand Down
6 changes: 6 additions & 0 deletions AquaMai/AquaMai.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ CustomPlaceName=""
# In the song selection screen, press the Service button or the "7" key (the round button in the middle of the arrow keys in the default ADX firmware) to toggle the display of self-made charts.
# A directory is considered to contain self-made charts if it does not have DataConfig.xml or OfficialChartsMark.txt in the Axxx directory.
HideSelfMadeCharts=true
# Place font.ttf in the LocalAssets directory to replace the game's global font
# Cannot be used together with FontFix
CustomFont=false

[Fix]
# Allow login with higher data version
Expand All @@ -64,6 +67,9 @@ ForcePaidPlay=false
ExtendNotesPool=128
# Force the frame rate limit to 60 FPS and disable vSync. Do not use if your game has no issues
FrameRateLock=false
# Use Microsoft YaHei Bold to display characters not in the font library
# Cannot be used together with CustomFont
FontFix=true

[Utils]
# Log user ID on login
Expand Down
6 changes: 6 additions & 0 deletions AquaMai/AquaMai.zh.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ CustomPlaceName=""
# 选歌界面按下 Service 键或者键盘上的 “7” 键(ADX 默认固件下箭头键中间的圆形按键)切换自制谱的显示和隐藏
# 是否是自制谱的判断方式是 Axxx 目录里没有 DataConfig.xml 或 OfficialChartsMark.txt 就认为这个目录里是自制谱
HideSelfMadeCharts=true
# 在 LocalAssets 目录下放置 font.ttf 可以替换游戏的全局字体
# 不可以和 FontFix 一起使用
CustomFont=false

# ===================================
# 修复一些潜在的问题
Expand All @@ -80,6 +83,9 @@ ForcePaidPlay=false
ExtendNotesPool=128
# 强制设置帧率上限为 60 帧并关闭垂直同步。如果你的游戏没有问题,请不要使用
FrameRateLock=false
# 在显示字库里没有的字时使用微软雅黑 Bold 显示
# 不可以和 CustomFont 一起使用
FontFix=true

[Utils]
# 登录时将 UserID 输出到日志
Expand Down
2 changes: 2 additions & 0 deletions AquaMai/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class UXConfig
public bool LoadLocalBga { get; set; }
public bool TestProof { get; set; }
public bool HideSelfMadeCharts { get; set; }
public bool CustomFont { get; set; }
public string CustomVersionString { get; set; } = "";
public string CustomPlaceName { get; set; } = "";
public string ExecOnIdle { get; set; } = "";
Expand All @@ -53,6 +54,7 @@ public class FixConfig
public bool ForcePaidPlay { get; set; }
public int ExtendNotesPool { get; set; }
public bool FrameRateLock { get; set; }
public bool FontFix { get; set; }
}

public class UtilsConfig
Expand Down
32 changes: 32 additions & 0 deletions AquaMai/Fix/FontFix.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Collections.Generic;
using HarmonyLib;
using MelonLoader;
using TMPro;
using UnityEngine;
using UnityEngine.TextCore.LowLevel;

namespace AquaMai.Fix;

public class FontFix
{
private static TMP_FontAsset fontAsset;
private static List<TMP_FontAsset> fixedFonts = [];

public static void DoCustomPatch(HarmonyLib.Harmony h)
{
var font = new Font(@"C:\Windows\Fonts\msyhbd.ttc");
fontAsset = TMP_FontAsset.CreateFontAsset(font, 90, 9, GlyphRenderMode.SDFAA, 8192, 8192);
}

[HarmonyPatch(typeof(TextMeshProUGUI), "Awake")]
[HarmonyPostfix]
public static void PostFix(TextMeshProUGUI __instance)
{
if (fixedFonts.Contains(__instance.font)) return;
# if DEBUG
MelonLogger.Msg($"[FontFix] Fixing font: {__instance.font.name}");
# endif
__instance.font.fallbackFontAssetTable.Add(fontAsset);
fixedFonts.Add(__instance.font);
}
}
36 changes: 36 additions & 0 deletions AquaMai/UX/CustomFont.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.IO;
using HarmonyLib;
using MelonLoader;
using TMPro;
using UnityEngine;
using UnityEngine.TextCore.LowLevel;

namespace AquaMai.UX;

public class CustomFont
{
private static TMP_FontAsset fontAsset;

public static void DoCustomPatch(HarmonyLib.Harmony h)
{
var fontPath = Path.Combine(Environment.CurrentDirectory, "LocalAssets", "font.ttf");
if (!File.Exists(fontPath)) return;

var font = new Font(fontPath);

// 不设置成 8192 的话,贴图会用完,剩下的字显示不出来
fontAsset = TMP_FontAsset.CreateFontAsset(font, 90, 9, GlyphRenderMode.SDFAA, 8192, 8192);
}

[HarmonyPatch(typeof(TextMeshProUGUI), "Awake")]
[HarmonyPostfix]
public static void PostFix(TextMeshProUGUI __instance)
{
if (fontAsset is null) return;
# if DEBUG
MelonLogger.Msg($"{__instance.font.name} {__instance.text}");
# endif
__instance.font = fontAsset;
}
}

0 comments on commit ac375ab

Please sign in to comment.