Skip to content

Commit

Permalink
snbt json 支持互转
Browse files Browse the repository at this point in the history
  • Loading branch information
Wulian233 committed Dec 21, 2024
1 parent ac559a9 commit 11957a0
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 102 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: Build snbt to json and upload
name: Build snbt json converter and upload

on:
push:
paths:
- 'snbt2json/**'
- 'snbt_json_converter/**'
workflow_dispatch:

jobs:
Expand All @@ -21,16 +21,16 @@ jobs:
- name: Install dependencies (Windows)
run: |
pip install pyinstaller
pip install -r snbt2json/requirements.txt
pip install -r snbt_json_converter/requirements.txt
- name: Build with PyInstaller (Windows)
run: |
python -m PyInstaller -F -n snbt-to-json-Windows snbt2json/main.py
python -m PyInstaller -F -n snbt-json-converter-Windows snbt_json_converter/main.py
- name: Upload (Windows)
uses: actions/upload-artifact@v4
with:
path: dist/snbt-to-json-Windows.exe
path: dist/snbt-json-converter-Windows.exe

build-macos:
runs-on: macos-latest
Expand All @@ -46,17 +46,17 @@ jobs:
- name: Install dependencies (macOS)
run: |
pip install pyinstaller
pip install -r snbt2json/requirements.txt
pip install -r snbt_json_converter/requirements.txt
- name: Build with PyInstaller (macOS)
run: |
python -m PyInstaller -F -n snbt-to-json-macOS snbt2json/main.py
python -m PyInstaller -F -n snbt-json-converter-macOS snbt_json_converter/main.py
- name: Upload (macOS)
uses: actions/upload-artifact@v4
with:
name: snbt-to-json-macOS
path: dist/snbt-to-json-macOS
name: snbt-json-converter-macOS
path: dist/snbt-json-converter-macOS

build-linux:
runs-on: ubuntu-latest
Expand All @@ -72,14 +72,14 @@ jobs:
- name: Install dependencies (Linux)
run: |
pip install pyinstaller
pip install -r snbt2json/requirements.txt
pip install -r snbt_json_converter/requirements.txt
- name: Build with PyInstaller (Linux)
run: |
python -m PyInstaller -F -n snbt-to-json-Linux snbt2json/main.py
python -m PyInstaller -F -n snbt-json-converter-Linux snbt_json_converter/main.py
- name: Upload (Linux)
uses: actions/upload-artifact@v4
with:
name: snbt-to-json-Linux
path: dist/snbt-to-json-Linux
name: snbt-json-converter-Linux
path: dist/snbt-json-converter-Linux
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@

使用相关功能需要有Paratranz的项目id和有对应权限用户的token,可在Paratranz个人主页查看。

## snbt json 工具
## snbt json 互转工具

用于将FTB任务中的snbt文件转换为json。对于FTB任务在mc1.21+新加入的语言文件,转换后的json可直接在翻译平台导入进行翻译
用于将FTB任务在mc1.21+新加入的snbt语言文件文件与json格式互转,便于导入翻译平台进行翻译

输入一个snbt文件路径或目录,会转换所有snbt文件为json
输入一个文件路径或目录,会按照选择的模式转换所有文件

- 支持彩色提示信息
- 支持将语言文件导入翻译平台
- 支持转换单个或整个目录的snbt文件
- 支持转换单个或整个目录的文件
- 支持 snbt json 互转

## 下载

Expand All @@ -76,11 +77,11 @@ Paratranz译文同步工具:
python -m PyInstaller paratranz_sync/main.spec
```

snbt json 工具
snbt json 互转工具
```bash
pip install pyinstaller
pip install -r snbt2json/requirements.txt
python -m PyInstaller -F -n snbt-to-json snbt2json/main.py
pip install -r snbt_json_converter/requirements.txt
python -m PyInstaller -F -n snbt-json-converter snbt_json_converter/main.py
```

## 星标
Expand Down
82 changes: 0 additions & 82 deletions snbt2json/main.py

This file was deleted.

96 changes: 96 additions & 0 deletions snbt_json_converter/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
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):
return "\\n".join(data).replace("\\\\n", "\\n")
elif isinstance(data, dict):
return {k: convert_list_to_string(v) for k, v in data.items()}
return data


def convert_file(file_path: Path, to_json: bool) -> None:
try:
if to_json:
# SNBT -> JSON
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"
)
else:
# JSON -> SNBT
json_data = json.loads(file_path.read_text(encoding="utf-8"))
snbt_data = snbtlib.dumps(json_data)
output_path = file_path.with_suffix(".snbt")
output_path.write_text(snbt_data, encoding="utf-8")
print(f"{Fore.CYAN}转换成功: {file_path} -> {output_path}")
except Exception as e:
print(f"{Fore.RED}转换失败: {file_path} - 错误: {e}")


def convert_directory(directory_path: Path, to_json: bool) -> None:
for root, _, files in os.walk(directory_path):
for file in files:
if (to_json and file.endswith(".snbt")) or (
not to_json and file.endswith(".json")
):
file_path = Path(root) / file
convert_file(file_path, to_json)


def main() -> None:
print(
Fore.LIGHTGREEN_EX
+ "snbt json 互转工具 [版本 1.1 (2024)]\n作者:Wulian233(捂脸)\n\n"
+ Fore.RESET
+ """VM之禅:
一,即使翻译难易各异,译者应持己见自立。
二,即使遇到词句争议,组员勿必同心共力。
三,即使译途坏垣跌荡,仍应坚守质量保障。
四,即使成果乏人褒奖,仍不计事后短长。
五,即使面临质疑声涌,仍要对正道守望。
六,即使译句翻乱无章,仍应看向前方、重塑文章。
"""
)

path = input("请输入文件或目录路径:").strip()
input_path = Path(path)

action = input("请选择操作:1. SNBT -> JSON 2. JSON -> SNBT\n").strip()
if action not in {"1", "2"}:
print(f"{Fore.RED}无效操作!请输入 1 或 2")
return

to_json = action == "1"

if input_path.is_dir():
convert_directory(input_path, to_json)
elif input_path.is_file() and (
(to_json and input_path.suffix == ".snbt")
or (not to_json and input_path.suffix == ".json")
):
convert_file(input_path, to_json)
else:
print(f"{Fore.RED}输入的路径无效,请输入正确的文件或目录")
return

print(f"{Fore.GREEN}所有文件已成功转换!")
input("按任意键(关机键除外)退出...\n")


if __name__ == "__main__":
main()
File renamed without changes.

0 comments on commit 11957a0

Please sign in to comment.