diff --git a/.gitignore b/.gitignore index 4e0f61985a..06f360e9ef 100644 --- a/.gitignore +++ b/.gitignore @@ -45,4 +45,4 @@ tools/trainerproc/trainerproc *.smol *.fastSmol *.smolTM -python_tools + diff --git a/python_tools/src/图鉴.xlsx b/python_tools/src/图鉴.xlsx new file mode 100644 index 0000000000..9fa1754464 Binary files /dev/null and b/python_tools/src/图鉴.xlsx differ diff --git a/python_tools/src/文本.xlsx b/python_tools/src/文本.xlsx new file mode 100644 index 0000000000..7509cad1f7 Binary files /dev/null and b/python_tools/src/文本.xlsx differ diff --git a/python_tools/src/道具.xlsx b/python_tools/src/道具.xlsx new file mode 100644 index 0000000000..82df2356d5 Binary files /dev/null and b/python_tools/src/道具.xlsx differ diff --git a/python_tools/translate_items.py b/python_tools/translate_items.py new file mode 100644 index 0000000000..32d9f346c0 --- /dev/null +++ b/python_tools/translate_items.py @@ -0,0 +1,79 @@ +import os +import re +import pandas as pd +import os + +current_folder = os.path.dirname(os.path.abspath(__file__)) + +def convert_description_to_multiline(description): + if not isinstance(description, str): + return None + # 替换中文字符为英文字符 + split_text = description.split('\\n') + + # 格式化输出为需要的三段,并处理最后一段不加\\n + formatted_text = [f'"{part}\\\\n"' for part in split_text[:-1]] # 所有除最后一段外加\\n + formatted_text.append(f'"{split_text[-1]}"') # 最后一段不加\\n + + # 拼接成最终的描述 + description = '\n\\t\\t\\t'.join(formatted_text) + final=r'.description = COMPOUND_STRING(\n\t\t\t' + description+ '),' + return final + +def query_pokemon_info(name, df): + try: + result = df.loc[name] + return { + '中文名': result['中文名'], + '单独道具说明': result['单独道具说明'], + # 'description': result['description'] + } + except KeyError: + return None + +def replace_item_info(content, df): + pattern = r"\[ITEM_(\w+)\]\s*=\s*\{.*?\n\s*\},?" + matches = list(re.finditer(pattern, content, re.DOTALL)) + + for match in matches: + item = match.group(1) + original_block = match.group(0) + info = query_pokemon_info("[ITEM_"+item+"]", df) + if not info: + log(f"❌ 未找到 {'ITEM_'+item},跳过") + continue + + block = original_block + + # 替换 .moveName + block = re.sub(r'\.name\s*=\s*_\(.*?\),', f'.name = _("{info["中文名"]}"),', block) + + # block = re.sub(r'\.categoryName\s*=\s*_\(.*?\),', f'.categoryName = _("{info["categoryName"]}"),', block) + try: + block = re.sub(r'\.description\s*=\s*COMPOUND_STRING\((?:.|\n)*?\),\s+#endif', convert_description_to_multiline(info["单独道具说明"]), block) + block = re.sub(r'\.description\s*=\s*COMPOUND_STRING\((?:.|\n)*?\),', convert_description_to_multiline(info["单独道具说明"]), block) + except Exception as e: + print(f"Error: {e}") + log(f"❌ {'ITEM_'+item} 没有单独道具说明,跳过") + content = content.replace(original_block, block) + + return content + +def log(message): + with open(current_folder+"\log.txt", "a", encoding="utf-8") as log_file: + log_file.write(message + "\n") + print(message) + +if __name__ == "__main__": + work_file = current_folder +"\..\src\data\items.h" + df = pd.read_excel(current_folder +r'\src\道具.xlsx') + df.set_index('道具', inplace=True) + + + with open(work_file, "r", encoding="utf-8") as f: + content = f.read() + + new_content = replace_item_info(content, df) + + with open(work_file, "w", encoding="utf-8") as f: + f.write(new_content) diff --git a/python_tools/translate_string.py b/python_tools/translate_string.py new file mode 100644 index 0000000000..f83495046d --- /dev/null +++ b/python_tools/translate_string.py @@ -0,0 +1,76 @@ + +import os +import re +import pandas as pd +import os + +current_folder = os.path.dirname(os.path.abspath(__file__)) + +def query_pokemon_info(name, df): + try: + result = df.loc[name] + return { + '汉化文本': result['汉化文本'], + # 'description': result['description'] + } + except KeyError: + return None + +def replace_move_info(content, df): + # const u8 gText_ExpandedPlaceholder_Empty[] = _("") + pattern = r"const u8 gText_(\w+)\[\]\s*=\s*_\(\s*\"([^\"]*)\"\s*\)\s*;" + matches = list(re.finditer(pattern, content, re.DOTALL)) + + for match in matches: + move = match.group(1) + original_block = match.group(0) + print(move, original_block) + info = query_pokemon_info("gText_"+move, df) + if not info: + log(f"❌ 未找到 {'MOVE_'+move},跳过") + continue + try: + block = original_block + escaped_text = info["汉化文本"] + print(info) + block = re.sub(r'const u8 gText_(\w+)\[\]\s*=\s*_\(".*?"\)\s*;', lambda m: f'const u8 gText_{move}[] = _("{escaped_text}")',block) + except Exception as e: + print(f"Error: {e}") + log(f"❌ {'MOVE_'+move} 没有单独技能效果说明,跳过") + continue + # # 替换 .moveName + + # block = re.sub(r'\.name\s*=\s*COMPOUND_STRING\(.*?\),', f'.name = COMPOUND_STRING("{info["中文名"]}"),', block) + + # # block = re.sub(r'\.categoryName\s*=\s*_\(.*?\),', f'.categoryName = _("{info["categoryName"]}"),', block) + # try: + # block = re.sub(r'\.description\s*=\s*COMPOUND_STRING\((?:.|\n)*?\),\s+#endif', convert_description_to_multiline(info["单独技能效果说明"]), block) + # block = re.sub(r'\.description\s*=\s*COMPOUND_STRING\((?:.|\n)*?\),', convert_description_to_multiline(info["单独技能效果说明"]), block) + # except Exception as e: + # print(f"Error: {e}") + # log(f"❌ {'MOVE_'+move} 没有单独技能效果说明,跳过") + content = content.replace(original_block, block) + + return content + +def log(message): + with open(current_folder+"\log.txt", "a", encoding="utf-8") as log_file: + log_file.write(message + "\n") + print(message) +if __name__ == "__main__": + + work_file = current_folder +"\..\src\strings.c" + df = pd.read_excel(current_folder + r'\src\文本.xlsx') + + # 检查并清理重复的“变量名” + df = df.drop_duplicates(subset='变量名', keep='first') + + # 将“变量名”设置为索引 + df.set_index('变量名', inplace=True) + with open(work_file, "r", encoding="utf-8") as f: + content = f.read() + + new_content = replace_move_info(content, df) + + with open(work_file, "w", encoding="utf-8") as f: + f.write(new_content) \ No newline at end of file