-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPatch.cs
More file actions
184 lines (160 loc) · 6.31 KB
/
Copy pathPatch.cs
File metadata and controls
184 lines (160 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using BepInEx;
using DMM.OLG.Unity.Engine;
using Hachiroku.Novel;
using Hachiroku.Novel.UI;
using HarmonyLib;
using TMPro;
using UnityEngine;
namespace IMYSHook;
public class Patch
{
private static string currentAdvId;
public static string fontName = "notosanscjktc";
public static TMP_FontAsset TMPTranslateFont;
public static void Initialize()
{
Harmony.CreateAndPatchAll(typeof(Patch));
}
[HarmonyPostfix]
[HarmonyPatch(typeof(NovelRoot), "Start")]
public static void NovelStart(ref NovelRoot __instance)
{
if (!IMYSConfig.TranslationEnabled) return;
if (TMPTranslateFont == null && File.Exists($"{Paths.PluginPath}/font/{fontName}"))
{
var ab = AssetBundle.LoadFromMemory(File.ReadAllBytes($"{Paths.PluginPath}/font/{fontName}"));
TMPTranslateFont = ab.LoadAsset<TMP_FontAsset>(fontName + " SDF");
ab.Unload(false);
}
currentAdvId = __instance.Linker.ScenarioId;
if (!Translation.chapterDicts.ContainsKey(currentAdvId)) Translation.FetchChapterTranslationAsync(currentAdvId).Wait();
Plugin.Global.Log.LogInfo(currentAdvId);
}
// Message
[HarmonyPrefix]
[HarmonyPatch(typeof(BurikoParseScript), "_SetMssageCommand")]
public static void Novel_SetMssageCommand(ref int lineNum, ref string line, ref bool isSelectedCaseArea,
ref int caseCount)
{
if (!IMYSConfig.TranslationEnabled) return;
if (Translation.chapterDicts.ContainsKey(currentAdvId) && line.Contains("「") && line.EndsWith("」"))
{
var idx = line.IndexOf('「');
var name = line.Substring(0, idx);
var text = line.Substring(idx);
var full = "";
string name_replace;
if (Translation.nameDicts.TryGetValue(name, out name_replace))
full = name_replace.IsNullOrWhiteSpace() ? text : name_replace;
string text_replace;
if (Translation.chapterDicts[currentAdvId].TryGetValue(text, out text_replace))
{
text_replace = text_replace.IsNullOrWhiteSpace() ? text : text_replace;
text_replace = text_replace.Substring(1, text_replace.Length - 2);
text_replace = text_replace.Replace("「", "『").Replace("」", "』");
string final_text = "「" + text_replace + "」";
full += final_text;
}
else
{
full += text;
}
line = full;
}
else
{
string text_replace;
if (Translation.chapterDicts.ContainsKey(currentAdvId) &&
Translation.chapterDicts[currentAdvId].TryGetValue(line, out text_replace))
{
text_replace = text_replace.IsNullOrWhiteSpace() ? line : text_replace;
text_replace = text_replace.Replace("「", "『").Replace("」", "』");
line = text_replace;
}
}
}
// Option
[HarmonyPrefix]
[HarmonyPatch(typeof(BurikoParseScript), "_ToParamList")]
public static void Novel_ToParamList(ref string param)
{
if (!IMYSConfig.TranslationEnabled) return;
var re = new Regex(@"{(.*)}");
var match = re.Match(param);
if (match.Success)
for (var i = 0; i < match.Groups.Count; i++)
{
var options = match.Groups[i].Value.Split(",");
for (var i2 = 0; i2 < options.Length; i2++)
{
string text_replace;
if (Translation.chapterDicts.ContainsKey(currentAdvId) && Translation.chapterDicts[currentAdvId]
.TryGetValue(options[i2], out text_replace))
{
var option_tr = text_replace.IsNullOrWhiteSpace() ? options[i2] : text_replace;
param = param.Replace(options[i2], option_tr);
}
}
}
}
[HarmonyPrefix]
[HarmonyPatch(typeof(MessageScrollView), "CreateItem")]
public static void CreateItem(ref MessageScrollViewItem item)
{
if (!IMYSConfig.TranslationEnabled) return;
if (TMPTranslateFont != null)
{
item._name.font = TMPTranslateFont;
item._message.font = TMPTranslateFont;
}
}
[HarmonyPostfix]
[HarmonyPatch(typeof(ChoicesContent), "SetChoiceButtonText")]
public static void SetChoiceButtonText(ref ChoicesContent __instance)
{
if (!IMYSConfig.TranslationEnabled) return;
if (TMPTranslateFont != null)
for (var i = 0; i < __instance.choiceTextList.Length; i++)
__instance.choiceTextList[i].font = TMPTranslateFont;
}
[HarmonyPostfix]
[HarmonyPatch(typeof(TextRoot), "DeleteRuby")]
public static void DeleteRuby(ref TextRoot __instance)
{
if (!IMYSConfig.TranslationEnabled) return;
if (TMPTranslateFont != null)
{
if (__instance.CharaName) __instance.CharaName.font = TMPTranslateFont;
if (__instance.Message) __instance.Message.font = TMPTranslateFont;
}
}
[HarmonyPrefix]
[HarmonyPatch(typeof(LoginResponse), "Parse")]
public static void ParseLoginResp(ref ResponseData res)
{
Plugin.Global.Log.LogInfo("Account created at: " + res.contents["created_at"].ToString());
if (File.Exists($"{Paths.PluginPath}/user.txt") && File.ReadAllText($"{Paths.PluginPath}/user.txt", Encoding.UTF8).IsNullOrWhiteSpace())
{
var token = res.contents["token"].ToString();
Plugin.Global.Log.LogInfo("Account token: " + token);
File.WriteAllText($"{Paths.PluginPath}/user.txt", token);
}
}
[HarmonyPrefix]
[HarmonyPatch(typeof(AdvSoundPlayer), "StopSoundHelperVoice")]
public static bool StopSoundHelperVoice(ref AdvSoundPlayer __instance)
{
if (IMYSConfig.DoNotVoiceCut)
{
var novelRoot = GameObject.FindObjectOfType<NovelRoot>();
if (novelRoot == null) return true;
if (novelRoot._facilitator._LastVoiceName.IsNullOrWhiteSpace())
return false;
return true;
}
else return true;
}
}