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

Commit

Permalink
refactor: remove legacy code
Browse files Browse the repository at this point in the history
  • Loading branch information
zyf722 committed Feb 28, 2024
1 parent 838f0d1 commit 59f6033
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 276 deletions.
202 changes: 0 additions & 202 deletions examples/bf1chs/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,188 +777,6 @@ def _replace_runner(
save_name=file,
)

def _update_histogram_legacy(self):
"""
Legacy method. Might get deprecated in the future.
Update the histogram chunk file.
"""
histogram_path = os.path.abspath(self.config["localization.histogramPath"])
if not os.path.exists(histogram_path):
console.print(f"[yellow]码表路径 {histogram_path} 不存在。")
if self._rich_confirm(message="是否创建?"):
os.makedirs(histogram_path)
console.print(
f"[yellow]请将导出后的码表文件放入路径 {histogram_path} 后重新选择本项。\n"
)
return

original_histogram_path = self._rich_fuzzy_select_file(
directory=histogram_path,
types=[".chunk", ".bin"],
message="选择 Frosty Editor 导出的原始码表文件",
)
if original_histogram_path is None:
return

histogram = Histogram(original_histogram_path)

console.print("[bold green]已读取原始码表文件。\n")
self._rich_show_object(histogram)

char_list_path = self._rich_fuzzy_select_file(
directory=histogram_path,
types=".txt",
message="选择新增字符列表文件",
)
if char_list_path is None:
return

added = histogram.add_chars_from_file(char_list_path)
console.print(f"[bold green]已添加 {added} 个字符。\n")

expand_shifts = self._rich_integer(
message="输入码表位移量个数的扩展量(扩展此值能够使码表支持更多字符)",
default=48,
min_allowed=0,
)
if expand_shifts is None:
return

histogram.expand_shift_range(expand_shifts)

new_histogram_path = self._rich_text(
message="输入新的码表文件名",
default=f"new-{os.path.basename(original_histogram_path).rsplit('.', 1)[0]}.chunk",
filter=lambda x: os.path.join(histogram_path, x),
)
if os.path.exists(new_histogram_path):
console.print(f"[yellow]码表文件 {new_histogram_path} 已存在。\n")
if not self._rich_confirm(message="是否覆盖?"):
return
console.print()

histogram.save(new_histogram_path)

self._rich_show_object(histogram)
console.print(
f"[bold green]已完成码表更新。导入 Chunk 时请一并修改 HistogramChunkSize 为 [underline]{histogram.chunk_size}[/] 。\n"
)

def _update_strings_legacy(self):
"""
Legacy method. Might get deprecated in the future.
Update the strings chunk file.
"""
strings_path = os.path.abspath(self.config["localization.stringsPath"])
if not os.path.exists(strings_path):
console.print(f"[yellow]本地化文件路径 {strings_path} 不存在。")
if self._rich_confirm(message="是否创建?"):
os.makedirs(strings_path)
console.print(
f"[yellow]请将导出后的本地化文件放入路径 {strings_path} 后重新选择本项。\n"
)
return

original_strings_path = self._rich_fuzzy_select_file(
directory=strings_path,
types=[".chunk", ".bin"],
message="选择 Frosty Editor 导出的原始本地化文件",
)
if original_strings_path is None:
return

histogram_path = os.path.abspath(self.config["localization.histogramPath"])
if not os.path.exists(histogram_path):
console.print(f"[yellow]码表路径 {histogram_path} 不存在,请先更新码表。")
return

original_histogram_path = self._rich_fuzzy_select_file(
directory=histogram_path,
types=[".chunk", ".bin"],
message="选择 Frosty Editor 导出的原始码表文件",
)
if original_histogram_path is None:
return

def _create_strings_binary_runner(
original_histogram_path: str, original_strings_path: str
) -> StringsBinary:
return StringsBinary(
Histogram(original_histogram_path), original_strings_path
)

strings_binary: StringsBinary = self._rich_indeterminate_progress(
task_name="读取本地化文件",
short_name="读取",
actor=_create_strings_binary_runner,
original_histogram_path=original_histogram_path,
original_strings_path=original_strings_path,
)

self._rich_show_object(strings_binary)

# Load new strings json file
processed_path = os.path.abspath(self.config["paratranz.processedPath"])
if not os.path.exists(processed_path):
console.print(
f"[bold red]处理后文件目录 {processed_path} 不存在,请先替换汉化文件。"
)
return

with open(
os.path.join(processed_path, "strings-zht.csv.json"),
"r",
encoding="utf-8",
) as new_file:
new_dict = {
int(key, 16): value for key, value in json.load(new_file).items()
}

self._rich_indeterminate_progress(
task_name="导入汉化至本地化文件",
short_name="导入",
actor=strings_binary.import_strings,
strings=new_dict,
)

new_histogram_path = self._rich_fuzzy_select_file(
directory=histogram_path,
types=[".chunk", ".bin"],
message="选择更新后的码表文件",
)
if new_histogram_path is None:
return

new_strings_path = self._rich_text(
message="输入新的本地化文件名",
default=f"new-{os.path.basename(original_strings_path).rsplit('.', 1)[0]}.chunk",
filter=lambda x: os.path.join(strings_path, x),
)
if os.path.exists(new_strings_path):
console.print(f"[yellow]本地化文件 {new_strings_path} 已存在。\n")
if not self._rich_confirm(message="是否覆盖?"):
return
console.print()

def _save_strings_binary_runner(histogram: Histogram, file_path: str):
strings_binary.update(histogram)
strings_binary.save(file_path)

self._rich_indeterminate_progress(
task_name="保存本地化文件",
short_name="保存",
actor=_save_strings_binary_runner,
histogram=Histogram(new_histogram_path),
file_path=new_strings_path,
)

self._rich_show_object(strings_binary)
console.print(
f"[bold green]已完成本地化文件更新。导入 Chunk 时请一并修改 BinaryChunkSize 为 [underline]{strings_binary.chunk_size}[/] 。\n"
)

def _update_strings(self):
"""
Update the strings chunk file.
Expand Down Expand Up @@ -1409,26 +1227,6 @@ def run(self):
"desc": "根据替换后的 .json 汉化文件生成可读取的动态本地化文件。",
"actor": self._update_twinkle,
},
"legacy": {
"name": "旧版操作",
"desc": "此部分包含为操作性或兼容性而保留的旧版功能。",
"actor": BF1ChsToolbox.SelectAction(
title="旧版操作",
desc="此部分包含为操作性或兼容性而保留的旧版功能。",
choices={
"histogram_old": {
"name": "更新 Frosty Editor 码表 chunk 文件(旧版)",
"desc": "向导出的原始码表文件增加新的字符,并生成新的码表文件。",
"actor": self._update_histogram_legacy,
},
"strings_old": {
"name": "更新 Frosty Editor 静态本地化 chunk 文件(旧版)",
"desc": "用替换后的 .json 汉化文件更新导出的原始本地化文件,并生成新的本地化文件。",
"actor": self._update_strings_legacy,
},
},
),
},
},
),
},
Expand Down
74 changes: 0 additions & 74 deletions flamethrower/localization.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,80 +118,6 @@ def load(self, file_path: str) -> None:
):
self.section.append(chr(char))

def add_chars(
self,
chars: Iterable[str],
) -> int:
"""
Add chars to the histogram.
NOTE: Legacy method. Might get deprecated in the future.
Args:
chars (Iterable[str]): The chars to add.
Returns:
int: The amount of chars added.
"""
assert self.fileSize is not None
assert self.dataOffSize is not None
assert self.section is not None

char_set = set(chars)
if "" in char_set:
char_set.remove("")
chars = list(char_set - char_set.intersection(self.section))
self.section[self.dataOffSize - 1 : self.dataOffSize - 1] = chars
self.fileSize += len(chars)
self.dataOffSize += len(chars)
return len(chars)

def add_chars_from_file(
self,
file_path: str,
) -> int:
"""
Add chars from a file to the histogram.
NOTE: Legacy method. Might get deprecated in the future.
Args:
file_path (str): The path to the file containing chars to add.
Returns:
int: The amount of chars added.
"""
with open(file_path, "r", encoding="utf-8") as file:
return self.add_chars([char.strip() for char in file.readlines()])

def expand_shift_range(self, extra_shifts: int) -> None:
"""
Expand the shift range of the histogram.
By doing this, we can allow more chars with larger code points to be used in the game.
NOTE: Legacy method. Might get deprecated in the future.
Args:
extra_shifts (int): The amount of extra shifts to add.
"""
assert self.section is not None

index = 0x40 # This is where section starts
while index < 0xFF: # This is where section that may contain shifts ends
if self.section[index] != "\x00":
break
index += 1

current_shift_end = ord(self.section[index])

# Note that when stepping down, the range should be like (end, start]
extra_shifts_range = range(
current_shift_end + extra_shifts, current_shift_end, -1
)

self.section[index:index] = [chr(shift) for shift in extra_shifts_range]

def add_chars_from_strings(self, strings: Iterable[str]) -> int:
"""
Add chars to the histogram. Necessary shifts will be added automatically.
Expand Down

0 comments on commit 59f6033

Please sign in to comment.