@@ -28,41 +28,73 @@ public static bool AddChineseFont(string path)
2828 TryCastFontAsset . hideFlags |= HideFlags . HideAndDontSave ;
2929 tmpchinesefonts . Add ( TryCastFontAsset ) ;
3030 tmpchinesefontnames . Add ( TryCastFontAsset . name ) ;
31+ Logger . Log ( $ "Loaded Font { TryCastFontAsset . name } ") ;
3132 return true ;
3233 }
3334 }
3435 }
3536 return false ;
3637 }
3738
39+ // 定义支持的字体名称集合
40+ private static readonly HashSet < string > ExactFontNames = new HashSet < string >
41+ {
42+ "ZenAntique-Regular SDF" ,
43+ "Pretendard-Regular SDF" ,
44+ "LiberationSans SDF" ,
45+ "Mikodacs SDF" ,
46+ "BebasKai SDF" ,
47+ "Anton-Regular SDF" ,
48+ //"Nexus_6-FREE SDF",
49+ "JetRunnerR-Regular SDF"
50+ } ;
51+
52+ // 定义支持的字体前缀集合
53+ private static readonly List < string > FontNamePrefixes = new List < string >
54+ {
55+ "HinaMincho-Regular" ,
56+ "HigashiOme-Gothic-C" ,
57+ "SCDream"
58+ } ;
59+
3860 public static bool GetChineseFont ( string fontname , out TMP_FontAsset fontAsset )
3961 {
4062 fontAsset = null ;
4163 if ( tmpchinesefonts . Count == 0 )
64+ {
65+ Logger . Log ( "Failed to Find chinese font." ) ;
4266 return false ;
43- if ( fontname == "ZenAntique-Regular SDF" || fontname . StartsWith ( "HinaMincho-Regular" ) || fontname . StartsWith ( "HigashiOme-Gothic-C" ) || fontname == "Pretendard-Regular SDF" || fontname . StartsWith ( "SCDream" ) || fontname == "LiberationSans SDF" || fontname == "Mikodacs SDF" || fontname == "BebasKai SDF" )
67+ }
68+ // 使用 HashSet 和前缀检查替代复杂的逻辑判断
69+ if ( ExactFontNames . Contains ( fontname ) ||
70+ FontNamePrefixes . Exists ( prefix => fontname . StartsWith ( prefix ) ) )
4471 {
4572 fontAsset = tmpchinesefonts [ 0 ] ;
4673 return true ;
4774 }
75+
76+ Logger . Log ( $ "Unmatched Font { fontname } ") ;
4877 //if (fontname == "ZenAntique-Regular SDF" || fontname.StartsWith("HinaMincho-Regular") || fontname.StartsWith("HigashiOme-Gothic-C") || fontname == "Pretendard-Regular SDF" || fontname.StartsWith("SCDream") || fontname == "LiberationSans SDF" || fontname == "Mikodacs SDF" || fontname == "BebasKai SDF")
4978 return false ;
5079 }
5180
52- static FieldInfo fi = null ;
81+ // static FieldInfo fi = null;
5382 public static TMP_FontAsset GetFontAsset ( TMP_Text txt )
5483 {
84+ return txt . font ;
85+ /*
5586 if (fi == null)
5687 fi = AccessTools.Field(typeof(TMP_Text), "m_fontAsset");
5788 if (fi != null)
5889 return fi.GetValue(txt) as TMP_FontAsset;
5990 return null;
91+ */
6092 }
6193
6294 public static bool IsChineseFont ( TMP_FontAsset fontAsset )
6395 => tmpchinesefontnames . Contains ( fontAsset . name ) ;
64- [ HarmonyPatch ( typeof ( TMP_Text ) , nameof ( TMP_Text . font ) , MethodType . Setter ) ]
65- [ HarmonyPrefix ]
96+ // [HarmonyPatch(typeof(TMP_Text), nameof(TMP_Text.font), MethodType.Setter)]
97+ // [HarmonyPrefix]
6698 private static bool set_font ( TMP_Text __instance , ref TMP_FontAsset value )
6799 {
68100 if ( IsChineseFont ( GetFontAsset ( __instance ) ) )
@@ -72,23 +104,36 @@ private static bool set_font(TMP_Text __instance, ref TMP_FontAsset value)
72104 value = font ;
73105 return true ;
74106 }
75- [ HarmonyPatch ( typeof ( TMP_Text ) , nameof ( TMP_Text . fontMaterial ) , MethodType . Setter ) ]
76- [ HarmonyPrefix ]
107+ // [HarmonyPatch(typeof(TMP_Text), nameof(TMP_Text.fontMaterial), MethodType.Setter)]
108+ // [HarmonyPrefix]
77109 private static void set_fontMaterial ( TMP_Text __instance , ref Material value )
78110 {
79111 if ( IsChineseFont ( GetFontAsset ( __instance ) ) )
80112 value = GetFontAsset ( __instance ) . material ;
81113 }
82114
83115 [ HarmonyPatch ( typeof ( TMP_Text ) , nameof ( TMP_Text . text ) , MethodType . Setter ) ]
84- [ HarmonyPrefix ]
85- private static void set_text ( TMP_Text __instance , ref Material value )
116+ [ HarmonyPostfix ]
117+ private static void set_text ( TMP_Text __instance , ref string value )
86118 {
87- if ( ! IsChineseFont ( GetFontAsset ( __instance ) ) )
119+ try
120+ {
121+ if ( ! IsChineseFont ( GetFontAsset ( __instance ) ) )
122+ {
123+ string fontname = GetFontAsset ( __instance ) . name ;
124+ if ( GetChineseFont ( fontname , out TMP_FontAsset font ) )
125+ {
126+
127+ Logger . Log ( $ "Replaced font { __instance . font . name } ") ;
128+ __instance . font = font ;
129+ __instance . SetAllDirty ( ) ;
130+ __instance . ForceMeshUpdate ( true ) ;
131+ }
132+ }
133+ }
134+ catch ( Exception e )
88135 {
89- string fontname = GetFontAsset ( __instance ) . name ;
90- if ( GetChineseFont ( fontname , out TMP_FontAsset font ) )
91- __instance . font = font ;
136+ Logger . Log ( "Encountered error:" + e . Message ) ;
92137 }
93138 }
94139
0 commit comments