1+ using System . Text . RegularExpressions ;
2+ using BepInEx ;
3+ using Hachiroku . Novel ;
4+ using Hachiroku . Novel . UI ;
5+ using HarmonyLib ;
6+ using Il2CppSystem . IO ;
7+ using TMPro ;
8+ using UnityEngine ;
9+
10+ namespace IMYSHook ;
11+
12+ public class Patch
13+ {
14+ private static string currentAdvId ;
15+ public static string fontName = "notosanscjktc" ;
16+ public static TMP_FontAsset TMPTranslateFont ;
17+
18+ public static void Initialize ( )
19+ {
20+ Harmony . CreateAndPatchAll ( typeof ( Patch ) ) ;
21+ }
22+
23+ [ HarmonyPostfix ]
24+ [ HarmonyPatch ( typeof ( NovelRoot ) , "Start" ) ]
25+ public static void NovelStart ( ref NovelRoot __instance )
26+ {
27+ if ( ! IMYSConfig . TranslationEnabled ) return ;
28+
29+ if ( TMPTranslateFont == null && File . Exists ( $ "{ Paths . PluginPath } /font/{ fontName } ") )
30+ {
31+ var ab = AssetBundle . LoadFromFile ( $ "{ Paths . PluginPath } /font/{ fontName } ") ;
32+ TMPTranslateFont = ab . LoadAsset < TMP_FontAsset > ( fontName + " SDF" ) ;
33+ ab . Unload ( false ) ;
34+ }
35+
36+ currentAdvId = __instance . Linker . ScenarioId ;
37+
38+ if ( ! Translation . chapterDicts . ContainsKey ( currentAdvId ) ) Translation . FetchChapterTranslation ( currentAdvId ) ;
39+ Plugin . Global . Log . LogInfo ( currentAdvId ) ;
40+ }
41+
42+ // Message
43+ [ HarmonyPrefix ]
44+ [ HarmonyPatch ( typeof ( BurikoParseScript ) , "_SetMssageCommand" ) ]
45+ public static void Novel_SetMssageCommand ( ref int lineNum , ref string line , ref bool isSelectedCaseArea ,
46+ ref int caseCount )
47+ {
48+ if ( ! IMYSConfig . TranslationEnabled ) return ;
49+
50+ if ( Translation . chapterDicts . ContainsKey ( currentAdvId ) && line . Contains ( "「" ) )
51+ {
52+ var idx = line . IndexOf ( '「' ) ;
53+ var name = line . Substring ( 0 , idx ) ;
54+ var text = line . Substring ( idx ) ;
55+
56+ var full = "" ;
57+
58+ string name_replace ;
59+ if ( Translation . nameDicts . TryGetValue ( name , out name_replace ) )
60+ full = name_replace . IsNullOrWhiteSpace ( ) ? text : name_replace ;
61+
62+ string text_replace ;
63+ if ( Translation . chapterDicts [ currentAdvId ] . TryGetValue ( text , out text_replace ) )
64+ full += text_replace . IsNullOrWhiteSpace ( ) ? text : text_replace ;
65+
66+ line = full ;
67+ }
68+ else
69+ {
70+ string text_replace ;
71+ if ( Translation . chapterDicts . ContainsKey ( currentAdvId ) &&
72+ Translation . chapterDicts [ currentAdvId ] . TryGetValue ( line , out text_replace ) )
73+ line = text_replace . IsNullOrWhiteSpace ( ) ? line : text_replace ;
74+ }
75+ }
76+
77+ // Option
78+ [ HarmonyPrefix ]
79+ [ HarmonyPatch ( typeof ( BurikoParseScript ) , "_ToParamList" ) ]
80+ public static void Novel_ToParamList ( ref string param )
81+ {
82+ if ( ! IMYSConfig . TranslationEnabled ) return ;
83+
84+ var re = new Regex ( @"{(.*)}" ) ;
85+ var match = re . Match ( param ) ;
86+ if ( match . Success )
87+ for ( var i = 0 ; i < match . Groups . Count ; i ++ )
88+ {
89+ var options = match . Groups [ i ] . Value . Split ( "," ) ;
90+
91+ for ( var i2 = 0 ; i2 < options . Length ; i2 ++ )
92+ {
93+ string text_replace ;
94+ if ( Translation . chapterDicts . ContainsKey ( currentAdvId ) && Translation . chapterDicts [ currentAdvId ]
95+ . TryGetValue ( options [ i2 ] , out text_replace ) )
96+ {
97+ var option_tr = text_replace . IsNullOrWhiteSpace ( ) ? options [ i2 ] : text_replace ;
98+ param = param . Replace ( options [ i2 ] , option_tr ) ;
99+ }
100+ }
101+ }
102+ }
103+
104+ [ HarmonyPrefix ]
105+ [ HarmonyPatch ( typeof ( MessageScrollView ) , "CreateItem" ) ]
106+ public static void CreateItem ( ref MessageScrollViewItem item )
107+ {
108+ if ( ! IMYSConfig . TranslationEnabled ) return ;
109+
110+ if ( TMPTranslateFont != null )
111+ {
112+ item . _name . font = TMPTranslateFont ;
113+ item . _message . font = TMPTranslateFont ;
114+ }
115+ }
116+
117+ [ HarmonyPostfix ]
118+ [ HarmonyPatch ( typeof ( ChoicesContent ) , "SetChoiceButtonText" ) ]
119+ public static void SetChoiceButtonText ( ref ChoicesContent __instance )
120+ {
121+ if ( ! IMYSConfig . TranslationEnabled ) return ;
122+
123+ if ( TMPTranslateFont != null )
124+ for ( var i = 0 ; i < __instance . choiceTextList . Length ; i ++ )
125+ __instance . choiceTextList [ i ] . font = TMPTranslateFont ;
126+ }
127+
128+ [ HarmonyPostfix ]
129+ [ HarmonyPatch ( typeof ( TextRoot ) , "DeleteRuby" ) ]
130+ public static void DeleteRuby ( ref TextRoot __instance )
131+ {
132+ if ( ! IMYSConfig . TranslationEnabled ) return ;
133+
134+ if ( TMPTranslateFont != null )
135+ {
136+ if ( __instance . CharaName ) __instance . CharaName . font = TMPTranslateFont ;
137+ if ( __instance . Message ) __instance . Message . font = TMPTranslateFont ;
138+ }
139+ }
140+ }
0 commit comments