Skip to content

Commit c5fd80d

Browse files
committed
generic text 有序 dump
增加文本 hook 测试 (beta版用)
1 parent 3665ae8 commit c5fd80d

File tree

7 files changed

+79
-9
lines changed

7 files changed

+79
-9
lines changed

app/src/main/cpp/GakumasLocalify/Hook.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,12 @@ namespace GakumasLocal::HookMain {
226226
return;
227227
}
228228
Local::DumpI18nItem(key->ToString(), value->ToString());
229-
I18nHelper_SetValue_Orig(_this, key, value);
229+
if (Config::textTest) {
230+
I18nHelper_SetValue_Orig(_this, key, Il2cppString::New("[I18]" + value->ToString()));
231+
}
232+
else {
233+
I18nHelper_SetValue_Orig(_this, key, value);
234+
}
230235
}
231236

232237
void* fontCache = nullptr;
@@ -286,8 +291,12 @@ namespace GakumasLocal::HookMain {
286291
return TMP_Text_set_text_Orig(_this, UnityResolve::UnityType::String::New(transText));
287292
}
288293

289-
// TMP_Text_set_text_Orig(_this, UnityResolve::UnityType::String::New("[TS]" + text->ToString()));
290-
TMP_Text_set_text_Orig(_this, text);
294+
if (Config::textTest) {
295+
TMP_Text_set_text_Orig(_this, UnityResolve::UnityType::String::New("[TS]" + text->ToString()));
296+
}
297+
else {
298+
TMP_Text_set_text_Orig(_this, text);
299+
}
291300

292301
static auto set_font = Il2cppUtils::GetMethod("Unity.TextMeshPro.dll",
293302
"TMPro", "TMP_Text", "set_font");
@@ -308,7 +317,12 @@ namespace GakumasLocal::HookMain {
308317
//Log::InfoFmt("TextMeshProUGUI_Awake: %s", currText->ToString().c_str());
309318
std::string transText;
310319
if (Local::GetGenericText(currText->ToString(), &transText)) {
311-
TMP_Text_set_text_Orig(_this, UnityResolve::UnityType::String::New(transText));
320+
if (Config::textTest) {
321+
TMP_Text_set_text_Orig(_this, UnityResolve::UnityType::String::New("[TA]" + transText));
322+
}
323+
else {
324+
TMP_Text_set_text_Orig(_this, UnityResolve::UnityType::String::New(transText));
325+
}
312326
}
313327
}
314328

@@ -317,7 +331,7 @@ namespace GakumasLocal::HookMain {
317331
TextMeshProUGUI_Awake_Orig(_this, method);
318332
}
319333

320-
// TODO 文本未hook完整 思路:从tips下手...
334+
// TODO 文本未hook完整
321335
DEFINE_HOOK(void, TextField_set_value, (void* _this, Il2cppString* value)) {
322336
Log::DebugFmt("TextField_set_value: %s", value->ToString().c_str());
323337
TextField_set_value_Orig(_this, value);
@@ -326,7 +340,6 @@ namespace GakumasLocal::HookMain {
326340
DEFINE_HOOK(Il2cppString*, OctoCaching_GetResourceFileName, (void* data, void* method)) {
327341
auto ret = OctoCaching_GetResourceFileName_Orig(data, method);
328342
//Log::DebugFmt("OctoCaching_GetResourceFileName: %s", ret->ToString().c_str());
329-
330343
return ret;
331344
}
332345

app/src/main/cpp/GakumasLocalify/Local.cpp

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace GakumasLocal::Local {
1414
std::unordered_map<std::string, std::string> i18nData{};
1515
std::unordered_map<std::string, std::string> i18nDumpData{};
1616
std::unordered_map<std::string, std::string> genericText{};
17-
std::unordered_map<std::string, std::string> genericTextDumpData{};
17+
std::vector<std::string> genericTextDumpData{};
1818
std::unordered_set<std::string> translatedText{};
1919

2020
std::filesystem::path GetBasePath() {
@@ -76,6 +76,36 @@ namespace GakumasLocal::Local {
7676
}
7777
}
7878

79+
void DumpVectorDataToJson(const std::filesystem::path& dumpBasePath, const std::filesystem::path& fileName,
80+
const std::vector<std::string>& vec) {
81+
const auto dumpFilePath = dumpBasePath / fileName;
82+
try {
83+
if (!is_directory(dumpBasePath)) {
84+
std::filesystem::create_directories(dumpBasePath);
85+
}
86+
if (!std::filesystem::exists(dumpFilePath)) {
87+
std::ofstream dumpWriteLrcFile(dumpFilePath, std::ofstream::out);
88+
dumpWriteLrcFile << "{}";
89+
dumpWriteLrcFile.close();
90+
}
91+
92+
std::ifstream dumpLrcFile(dumpFilePath);
93+
std::string fileContent((std::istreambuf_iterator<char>(dumpLrcFile)), std::istreambuf_iterator<char>());
94+
dumpLrcFile.close();
95+
auto fileData = nlohmann::ordered_json::parse(fileContent);
96+
for (const auto& i : vec) {
97+
fileData[i] = i;
98+
}
99+
const auto newStr = fileData.dump(4, 32, false);
100+
std::ofstream dumpWriteLrcFile(dumpFilePath, std::ofstream::out);
101+
dumpWriteLrcFile << newStr.c_str();
102+
dumpWriteLrcFile.close();
103+
}
104+
catch (std::exception& e) {
105+
Log::ErrorFmt("DumpVectorDataToJson %s failed: %s", dumpFilePath.c_str(), e.what());
106+
}
107+
}
108+
79109
void LoadData() {
80110
static auto localizationFile = GetBasePath() / "local-files" / "localization.json";
81111
static auto genericFile = GetBasePath() / "local-files" / "generic.json";
@@ -155,14 +185,18 @@ namespace GakumasLocal::Local {
155185

156186
if (translatedText.contains(origText)) return false;
157187

158-
genericTextDumpData.emplace(origText, origText);
188+
if (std::find(genericTextDumpData.begin(), genericTextDumpData.end(), "") != genericTextDumpData.end()) {
189+
return false;
190+
}
191+
192+
genericTextDumpData.push_back(origText);
159193
static auto dumpBasePath = GetBasePath() / "dump-files";
160194

161195
if (inDumpGeneric) return false;
162196
inDumpGeneric = true;
163197
std::thread([](){
164198
std::this_thread::sleep_for(std::chrono::seconds(5));
165-
DumpMapDataToJson(dumpBasePath, "generic.json", genericTextDumpData);
199+
DumpVectorDataToJson(dumpBasePath, "generic.json", genericTextDumpData);
166200
genericTextDumpData.clear();
167201
inDumpGeneric = false;
168202
}).detach();

app/src/main/cpp/GakumasLocalify/config/Config.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace GakumasLocal::Config {
66
bool isConfigInit = false;
77

88
bool enabled = true;
9+
bool textTest = false;
910
bool enableFreeCamera = false;
1011
int targetFrameRate = 0;
1112
bool unlockAllLive = false;
@@ -29,6 +30,7 @@ namespace GakumasLocal::Config {
2930
#define GetConfigItem(name) if (config.contains(#name)) name = config[#name]
3031

3132
GetConfigItem(enabled);
33+
GetConfigItem(textTest);
3234
GetConfigItem(targetFrameRate);
3335
GetConfigItem(enableFreeCamera);
3436
GetConfigItem(unlockAllLive);

app/src/main/cpp/GakumasLocalify/config/Config.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace GakumasLocal::Config {
66
extern bool isConfigInit;
77

88
extern bool enabled;
9+
extern bool textTest;
910
extern bool enableFreeCamera;
1011
extern int targetFrameRate;
1112
extern bool unlockAllLive;

app/src/main/java/io/github/chinosk/gakumas/localify/MainActivity.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import java.io.File
2020
interface ConfigListener {
2121
fun onClickStartGame()
2222
fun onEnabledChanged(value: Boolean)
23+
fun onTextTestChanged(value: Boolean)
2324
fun onEnableFreeCameraChanged(value: Boolean)
2425
fun onTargetFpsChanged(s: CharSequence, start: Int, before: Int, count: Int)
2526
fun onUnlockAllLiveChanged(value: Boolean)
@@ -92,6 +93,11 @@ class MainActivity : AppCompatActivity(), ConfigListener {
9293
saveConfig()
9394
}
9495

96+
override fun onTextTestChanged(value: Boolean) {
97+
binding.config!!.textTest = value
98+
saveConfig()
99+
}
100+
95101
override fun onEnableFreeCameraChanged(value: Boolean) {
96102
binding.config!!.enableFreeCamera = value
97103
saveConfig()

app/src/main/java/io/github/chinosk/gakumas/localify/models/GakumasConfig.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import androidx.databinding.BaseObservable
44

55
data class GakumasConfig (
66
var enabled: Boolean = true,
7+
var textTest: Boolean = false,
78
var enableFreeCamera: Boolean = false,
89
var targetFrameRate: Int = 0,
910
var unlockAllLive: Boolean = false,

app/src/main/res/layout/activity_main.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,19 @@
6464
android:text="@string/enable_plugin" />
6565
</TableRow>
6666

67+
<TableRow
68+
android:layout_width="match_parent"
69+
android:layout_height="match_parent">
70+
71+
<com.google.android.material.switchmaterial.SwitchMaterial
72+
android:id="@+id/textTestMode"
73+
android:layout_width="match_parent"
74+
android:layout_height="48dp"
75+
android:checked="@={config.textTest}"
76+
android:onCheckedChanged="@{(view, value) -> listener.onTextTestChanged(value)}"
77+
android:text="文本 hook 测试模式" />
78+
</TableRow>
79+
6780
<TableRow
6881
android:layout_width="match_parent"
6982
android:layout_height="match_parent">

0 commit comments

Comments
 (0)