Skip to content
This repository was archived by the owner on Jun 4, 2024. It is now read-only.

Commit 2ebbb38

Browse files
committed
feat: add update-histogram-only option
1 parent b4ed594 commit 2ebbb38

File tree

2 files changed

+143
-1
lines changed

2 files changed

+143
-1
lines changed

examples/bf1chs/__main__.py

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,136 @@ def _save_strings_binary_runner(file_path: str):
924924
f"[bold green]已完成本地化文件更新。导入 Chunk 时请一并修改 BinaryChunkSize 为 [underline]{strings_binary.chunk_size}[/] 。\n"
925925
)
926926

927+
def _update_histogram(self):
928+
"""
929+
Update the histogram chunk file only.
930+
"""
931+
histogram_path = os.path.abspath(self.config["localization.histogramPath"])
932+
if not os.path.exists(histogram_path):
933+
console.print(f"[yellow]码表路径 {histogram_path} 不存在。")
934+
if self._rich_confirm(message="是否创建?"):
935+
os.makedirs(histogram_path)
936+
console.print(
937+
f"[yellow]请将导出后的码表文件放入路径 {histogram_path} 后重新选择本项。\n"
938+
)
939+
return
940+
941+
strings_path = os.path.abspath(self.config["localization.stringsPath"])
942+
if not os.path.exists(strings_path):
943+
console.print(f"[yellow]本地化文件路径 {strings_path} 不存在。")
944+
if self._rich_confirm(message="是否创建?"):
945+
os.makedirs(strings_path)
946+
console.print(
947+
f"[yellow]请将导出后的本地化文件放入路径 {strings_path} 后重新选择本项。\n"
948+
)
949+
return
950+
951+
original_histogram_path = self._rich_fuzzy_select_file(
952+
directory=histogram_path,
953+
types=[".chunk", ".bin"],
954+
message="选择需要更新的原始码表文件",
955+
)
956+
if original_histogram_path is None:
957+
return
958+
959+
extra_chars_path = self._rich_fuzzy_select_file(
960+
directory=histogram_path,
961+
types=[".txt"],
962+
message="选择额外字符列表文件",
963+
)
964+
if extra_chars_path is None:
965+
return
966+
967+
original_strings_path = self._rich_fuzzy_select_file(
968+
directory=strings_path,
969+
types=[".chunk", ".bin"],
970+
message="选择需要更新的原始本地化文件",
971+
)
972+
if original_strings_path is None:
973+
return
974+
975+
histogram = Histogram(original_histogram_path)
976+
977+
console.print("[bold green]已读取原始码表文件。\n")
978+
self._rich_show_object(histogram)
979+
980+
def _create_strings_binary_runner(
981+
original_histogram_path: str, original_strings_path: str
982+
) -> StringsBinary:
983+
return StringsBinary(
984+
Histogram(original_histogram_path), original_strings_path
985+
)
986+
987+
strings_binary: StringsBinary = self._rich_indeterminate_progress(
988+
task_name="读取本地化文件",
989+
short_name="读取",
990+
actor=_create_strings_binary_runner,
991+
original_histogram_path=original_histogram_path,
992+
original_strings_path=original_strings_path,
993+
)
994+
if strings_binary is None:
995+
return
996+
997+
self._rich_show_object(strings_binary)
998+
999+
# Load new chars file
1000+
with open(extra_chars_path, "r", encoding="utf-8") as f:
1001+
extra_chars = f.read().splitlines()
1002+
1003+
added = histogram.add_chars_from_strings([], extra_chars)
1004+
console.print(f"[bold green]已添加 {added} 个字符至码表。\n")
1005+
1006+
new_histogram_path = self._rich_text(
1007+
message="输入新的码表文件名",
1008+
default=f"new-{os.path.basename(original_histogram_path).rsplit('.', 1)[0]}.chunk",
1009+
filter=lambda x: os.path.join(histogram_path, x),
1010+
)
1011+
if os.path.exists(new_histogram_path):
1012+
console.print(f"[yellow]码表文件 {new_histogram_path} 已存在。\n")
1013+
if not self._rich_confirm(message="是否覆盖?"):
1014+
return
1015+
console.print()
1016+
1017+
histogram.save(new_histogram_path)
1018+
1019+
histogram = Histogram(new_histogram_path)
1020+
self._rich_show_object(histogram)
1021+
console.print(
1022+
f"[bold green]已完成码表更新。导入 Chunk 时请一并修改 HistogramChunkSize 为 [underline]{histogram.chunk_size}[/] 。\n"
1023+
)
1024+
1025+
new_strings_path = self._rich_text(
1026+
message="输入新的本地化文件名",
1027+
default=f"new-{os.path.basename(original_strings_path).rsplit('.', 1)[0]}.chunk",
1028+
filter=lambda x: os.path.join(strings_path, x),
1029+
)
1030+
if os.path.exists(new_strings_path):
1031+
console.print(f"[yellow]本地化文件 {new_strings_path} 已存在。\n")
1032+
if not self._rich_confirm(message="是否覆盖?"):
1033+
return
1034+
console.print()
1035+
1036+
def _save_strings_binary_runner(file_path: str):
1037+
strings_binary.update(histogram)
1038+
strings_binary.save(file_path)
1039+
return True
1040+
1041+
if (
1042+
self._rich_indeterminate_progress(
1043+
task_name="保存本地化文件",
1044+
short_name="保存",
1045+
actor=_save_strings_binary_runner,
1046+
file_path=new_strings_path,
1047+
)
1048+
is None
1049+
):
1050+
return
1051+
1052+
self._rich_show_object(strings_binary)
1053+
console.print(
1054+
f"[bold green]已完成本地化文件更新。导入 Chunk 时请一并修改 BinaryChunkSize 为 [underline]{strings_binary.chunk_size}[/] 。\n"
1055+
)
1056+
9271057
def _update_twinkle(self):
9281058
"""
9291059
Generate twinkle dynamic files.
@@ -1275,6 +1405,11 @@ def run(self):
12751405
"desc": "根据替换后的 .json 汉化文件生成码表、更新导出的原始本地化文件,并生成新的本地化文件。",
12761406
"actor": self._update_strings,
12771407
},
1408+
"update-histogram": {
1409+
"name": "生成 Frosty Editor 码表并更新静态本地化 chunk 文件(仅插入新字符)",
1410+
"desc": "将给定的字符列表文件中的字符序列加入码表、更新导出的原始本地化文件,并生成新的本地化文件。本功能仅基于输入的字符列表文件,不会对原有的码表和本地化文件进行修改。",
1411+
"actor": self._update_histogram,
1412+
},
12781413
"twinkle": {
12791414
"name": "更新 Frosty Editor 动态本地化 chunk 文件",
12801415
"desc": "根据替换后的 .json 汉化文件生成可读取的动态本地化文件。",

flamethrower/localization.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,15 @@ def load(self, file_path: str) -> None:
118118
):
119119
self.section.append(chr(char))
120120

121-
def add_chars_from_strings(self, strings: Iterable[str]) -> int:
121+
def add_chars_from_strings(
122+
self, strings: Iterable[str], extra_chars: Optional[Iterable[str]] = None
123+
) -> int:
122124
"""
123125
Add chars to the histogram. Necessary shifts will be added automatically.
124126
125127
Args:
126128
strings (Iterable[str]): The strings containing chars to add.
129+
extra_chars (Optional[Iterable[str]]): Extra chars to add. Each str should only be one char.
127130
128131
Returns:
129132
int: The amount of chars added.
@@ -135,6 +138,10 @@ def add_chars_from_strings(self, strings: Iterable[str]) -> int:
135138
char_set: Set[str] = set()
136139
for value in strings:
137140
char_set.update(value)
141+
142+
if extra_chars:
143+
char_set.update(extra_chars)
144+
138145
chars = list(char_set.difference(self.section))
139146

140147
# Calculate needed indices

0 commit comments

Comments
 (0)