From ac559a90cbc7c20b1ad33327042ac1404654e224 Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Sun, 15 Dec 2024 20:09:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Esnbt=20=E8=BD=AC=20json=20?= =?UTF-8?q?=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build_ftb_color_check.yaml | 5 +- .github/workflows/build_snbt2json.yaml | 85 ++++++++++++++++++++ README.md | 33 ++++++-- ftbq_color_check/main.py | 15 ++-- snbt2json/main.py | 82 +++++++++++++++++++ snbt2json/requirements.txt | 2 + 6 files changed, 208 insertions(+), 14 deletions(-) create mode 100644 .github/workflows/build_snbt2json.yaml create mode 100644 snbt2json/main.py create mode 100644 snbt2json/requirements.txt diff --git a/.github/workflows/build_ftb_color_check.yaml b/.github/workflows/build_ftb_color_check.yaml index a8d2239..5f21500 100644 --- a/.github/workflows/build_ftb_color_check.yaml +++ b/.github/workflows/build_ftb_color_check.yaml @@ -2,8 +2,9 @@ name: Build FTBQ Color Check and Upload on: push: - branches: - - main + paths: + - 'ftbq_color_check/**' + workflow_dispatch: jobs: build-windows: diff --git a/.github/workflows/build_snbt2json.yaml b/.github/workflows/build_snbt2json.yaml new file mode 100644 index 0000000..0f021a7 --- /dev/null +++ b/.github/workflows/build_snbt2json.yaml @@ -0,0 +1,85 @@ +name: Build snbt to json and upload + +on: + push: + paths: + - 'snbt2json/**' + workflow_dispatch: + +jobs: + build-windows: + runs-on: windows-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.13' + + - name: Install dependencies (Windows) + run: | + pip install pyinstaller + pip install -r snbt2json/requirements.txt + + - name: Build with PyInstaller (Windows) + run: | + python -m PyInstaller -F -n snbt-to-json-Windows snbt2json/main.py + + - name: Upload (Windows) + uses: actions/upload-artifact@v4 + with: + path: dist/snbt-to-json-Windows.exe + + build-macos: + runs-on: macos-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.13' + + - name: Install dependencies (macOS) + run: | + pip install pyinstaller + pip install -r snbt2json/requirements.txt + + - name: Build with PyInstaller (macOS) + run: | + python -m PyInstaller -F -n snbt-to-json-macOS snbt2json/main.py + + - name: Upload (macOS) + uses: actions/upload-artifact@v4 + with: + name: snbt-to-json-macOS + path: dist/snbt-to-json-macOS + + build-linux: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.13' + + - name: Install dependencies (Linux) + run: | + pip install pyinstaller + pip install -r snbt2json/requirements.txt + + - name: Build with PyInstaller (Linux) + run: | + python -m PyInstaller -F -n snbt-to-json-Linux snbt2json/main.py + + - name: Upload (Linux) + uses: actions/upload-artifact@v4 + with: + name: snbt-to-json-Linux + path: dist/snbt-to-json-Linux diff --git a/README.md b/README.md index 75993d5..a3281f8 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ ## VM翻译辅助工具 -[![Downloads](https://img.shields.io/github/downloads/VM-Chinese-translate-group/translate-tools/total?style=flat-square&logo=github)](https://github.com/VM-Chinese-translate-group/translate-tools/releases/)[![Last Version](https://img.shields.io/github/release/VM-Chinese-translate-group/translate-tools/all.svg?style=flat-square)](https://github.com/VM-Chinese-translate-group/translate-tools/releases/)[![License](https://img.shields.io/github/license/VM-Chinese-translate-group/translate-tools?style=flat-square)](LICENSE) +[![Downloads](https://img.shields.io/github/downloads/VM-Chinese-translate-group/translate-tools/total?style=flat-square&logo=github)](https://github.com/VM-Chinese-translate-group/translate-tools/releases/) +[![Last Version](https://img.shields.io/github/release/VM-Chinese-translate-group/translate-tools/all.svg?style=flat-square)](https://github.com/VM-Chinese-translate-group/translate-tools/releases/) +[![License](https://img.shields.io/github/license/VM-Chinese-translate-group/translate-tools?style=flat-square)](LICENSE) [![官网](https://img.shields.io/badge/官网-介绍-blue?style=flat-square)](https://vmct-cn.top/tools) @@ -10,10 +12,10 @@ 用于检查翻译后的语言文件中是否存在FTB任务&颜色字符后面的数字或字母丢失的问题。这会导致任务无法正常显示。 -输入一个json路径,会检查颜色字符是否合法。 +输入一个json文件路径或目录,会检查所有json文件内的颜色字符是否合法。 -- 支持彩色提示信息。报错为红色,通过为绿色。 -- 新增支持导出错误报告为txt。 +- 支持彩色提示信息。报错为红色,通过为绿色 +- 新增支持导出错误报告为txt - 可选是否在控制台打印详细信息 1. 开启会在控制台显示具体译文内容 2. 关闭会显示行号 @@ -30,8 +32,8 @@ [CNPack\kubejs\assets\ftbquest\lang\zh_cn.json] SyntaxError: Invalid character after '&' at line 2705 ``` -注:无论选择什么,最终保存报错的文件永远为详细版本。 -- 支持检查单个或整个目录的json文件,更方便。 +注:无论选择什么,最终保存报错的文件永远为详细版本 +- 支持检查单个或整个目录的json文件 - 支持检查json本身格式问题。 # Paratranz译文同步工具 @@ -40,9 +42,19 @@ 使用相关功能需要有Paratranz的项目id和有对应权限用户的token,可在Paratranz个人主页查看。 +## snbt 转 json 工具 + +用于将FTB任务中的snbt文件转换为json。对于FTB任务在mc1.21+新加入的语言文件,转换后的json可直接在翻译平台导入进行翻译。 + +输入一个snbt文件路径或目录,会转换所有snbt文件为json + +- 支持彩色提示信息 +- 支持将语言文件导入翻译平台 +- 支持转换单个或整个目录的snbt文件 + ## 下载 -请在Release页面找到下载不同工具的对应页面 +请在[Release页面](https://github.com/VM-Chinese-translate-group/translate-tools/releases)找到下载不同工具的对应页面 ## 构建 @@ -64,6 +76,13 @@ Paratranz译文同步工具: python -m PyInstaller paratranz_sync/main.spec ``` +snbt 转 json 工具: +```bash + pip install pyinstaller + pip install -r snbt2json/requirements.txt + python -m PyInstaller -F -n snbt-to-json snbt2json/main.py +``` + ## 星标 支持开发者的最简单方式是点击页面顶部的星标(⭐)。 diff --git a/ftbq_color_check/main.py b/ftbq_color_check/main.py index 66308cd..a62d7cd 100644 --- a/ftbq_color_check/main.py +++ b/ftbq_color_check/main.py @@ -69,16 +69,21 @@ def check_directory(directory_path: str, verbose: bool) -> list[str]: return errors +def remove_color_codes(text: str) -> str: + # 去除所有 ANSI 转义字符 + return re.sub(r'\x1b\[[0-9;]*m', '', text) + def save_errors_to_file(errors: list[str]) -> None: with open("error_report.txt", "w", encoding="utf-8") as f: - f.write("\n".join(errors)) + cleaned_errors = [remove_color_codes(error) for error in errors] + f.write("\n".join(cleaned_errors)) print("错误信息已保存到 error_report.txt") def main() -> None: print( - Fore.BLUE - + "FTB任务颜色字符合法检查 [版本 1.2 (2024)]\n作者:Wulian233(捂脸)\n\n" + Fore.LIGHTGREEN_EX + + "FTB任务颜色字符合法检查 [版本 1.3 (2024)]\n作者:Wulian233(捂脸)\n\n" + Fore.RESET + """VM之禅: 一,即使翻译难易各异,译者应持己见自立。 @@ -99,13 +104,13 @@ def main() -> None: path, os.path.relpath(path, start=os.path.dirname(path)), verbose ) else: - print(f"❌ {Fore.RED}输入的路径无效,请输入有效的 JSON 文件路径或目录。") + print(f"{Fore.RED}输入的路径无效,请输入有效的 JSON 文件路径或目录。") return if errors: save_errors_to_file(errors) else: - print(f"✅ {Fore.GREEN}文件检查通过,没有发现错误。") + print(f"{Fore.GREEN}文件检查通过,没有发现错误。") input("按任意键(关机键除外)退出...") diff --git a/snbt2json/main.py b/snbt2json/main.py new file mode 100644 index 0000000..78b499f --- /dev/null +++ b/snbt2json/main.py @@ -0,0 +1,82 @@ +import json +import os +from pathlib import Path +from typing import Union + +import snbtlib +from colorama import Fore, init + +init(autoreset=True) + + +def convert_list_to_string(data: Union[list, dict, str]) -> Union[str, dict]: + """ + 将 JSON 中的列表转换为单行字符串 + """ + if isinstance(data, list): + # 将列表内容拼接成字符串并转义换行符 + result = "\\n".join(data) + # 将 \\n 转换为 \n + return result.replace("\\\\n", "\\n") + elif isinstance(data, dict): + # 递归处理字典中的值 + return {k: convert_list_to_string(v) for k, v in data.items()} + else: + return data + + +def convert_snbt_file(file_path: Path) -> None: + try: + snbt_data = file_path.read_text(encoding="utf-8") + json_data = snbtlib.loads(snbt_data) + json_data = convert_list_to_string(json_data) + output_path = file_path.with_suffix(".json") + + output_path.write_text( + json.dumps(json_data, indent=4, ensure_ascii=False), encoding="utf-8" + ) + print(f"{Fore.CYAN}转换成功: {file_path} -> {output_path}") + except Exception as e: + print(f"{Fore.RED}转换失败: {file_path} - 错误: {e}") + + +def convert_snbt_directory(directory_path: Path) -> None: + for root, _, files in os.walk(directory_path): + for file in files: + if file.endswith(".snbt"): + snbt_file = Path(root) / file + convert_snbt_file(snbt_file) + + +def main() -> None: + print( + Fore.LIGHTGREEN_EX + + "snbt 转 json 工具[版本 1.0 (2024)]\n作者:Wulian233(捂脸)\n\n" + + Fore.RESET + + """VM之禅: + 一,即使翻译难易各异,译者应持己见自立。 + 二,即使遇到词句争议,组员勿必同心共力。 + 三,即使译途坏垣跌荡,仍应坚守质量保障。 + 四,即使成果乏人褒奖,仍不计事后短长。 + 五,即使面临质疑声涌,仍要对正道守望。 + 六,即使译句翻乱无章,仍应看向前方、重塑文章。 + """ + ) + + path = input("请输入 snbt 文件或目录路径:").strip() + input_path = Path(path) + + if input_path.is_dir(): + convert_snbt_directory(input_path) + elif input_path.is_file() and input_path.suffix == ".snbt": + convert_snbt_file(input_path) + else: + print(f"{Fore.RED}输入的路径无效,请输入正确的 snbt 文件或目录。") + return + + print(f"{Fore.GREEN}所有 snbt 文件已成功转换为 json!") + input("按任意键(关机键除外)退出...") + + +if __name__ == "__main__": + main() diff --git a/snbt2json/requirements.txt b/snbt2json/requirements.txt new file mode 100644 index 0000000..4e0d88c --- /dev/null +++ b/snbt2json/requirements.txt @@ -0,0 +1,2 @@ +colorama +snbtlib \ No newline at end of file