4jcraft/scripts/pack_loc.py
2026-03-09 00:22:02 -05:00

142 lines
4.9 KiB
Python

#!/usr/bin/env python3
'''
Copyright (c) 2026 Pyogenics
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''
'''
Localisation packer tool
--------------------------------------------------
Usage: pack_lock.py path/to/lang/dir path/to/output.loc
'''
from sys import argv
import xml.etree.ElementTree as ET
from glob import glob
from os import path
from struct import pack
from io import BytesIO
def pack_stream(format, stream, *data):
packedData = pack(format, *data)
stream.write(packedData)
def write_string(string, stream):
string = string.encode("utf-8")
pack_stream(">H", stream, len(string))
stream.write(string)
target_dir = argv[1]
output_file_path = argv[2]
strings_h_output_file_path = argv[3]
lang_file_paths = glob(f"{target_dir}/*/*.lang")
# Process lang strings
langs = {}
for lang_file_path in lang_file_paths:
lang_name = path.split(lang_file_path)[0] # This will give us the dir path to strings.lang
lang_name = path.split(lang_name)[1] # This will give use just the directory before strings.lang (which should be the lang name)
lang_file_tree = ET.parse(lang_file_path)
lang_root = lang_file_tree.getroot()
strings = {}
for lang_data in lang_root:
string_name = lang_data.attrib["name"]
string = lang_data[0].text
# Some strings have empty values
if string == None:
string = ""
strings[string_name] = string
print(f"Processed {len(strings)} strings from {lang_file_path}")
langs[lang_name] = dict(sorted(strings.items()))
print(f"Processed {len(langs)} languages")
# Write loc file
TARGET_LOC_VERSION = 0
TARGET_LANG_VERSION = 1
TARGET_LANG_STATIC = True
with open(output_file_path, "wb") as loc_file:
# Write header
pack_stream(">2I", loc_file,
TARGET_LOC_VERSION,
len(langs)
)
# Write lang data
lang_sizes = {}
lang_stream = BytesIO()
old_stream_pos = 0
for lang_name, strings in langs.items():
old_stream_pos = lang_stream.tell()
# Write header
pack_stream(">I", lang_stream, TARGET_LANG_VERSION) # version
if TARGET_LANG_VERSION > 0:
pack_stream("?", lang_stream, TARGET_LANG_STATIC)
write_string(lang_name, lang_stream) # lang name
pack_stream(">I", lang_stream, len(strings)) # string count
# Write strings
if TARGET_LANG_STATIC:
for string in strings.values():
write_string(string, lang_stream)
else:
for string_name, string in strings.items():
write_string(string_name, lang_stream)
write_string(string, lang_stream)
lang_sizes[lang_name] = lang_stream.tell() - old_stream_pos
# Write lang size map, this is used by the client to skip to its prefered lang and read it
for lang_name, size in lang_sizes.items():
write_string(lang_name, loc_file)
pack_stream(">I", loc_file, size)
# Write out the lang data now
lang_stream.seek(0, 0)
loc_file.write(lang_stream.read())
print(f"Wrote {loc_file.tell()} bytes to {output_file_path}")
# Generate strings.h
strings_h = []
# XXX: Default to en-EN because the localisation setup in the sources is inconsistent and we have differing ID_ strings, might be problematic if you wanted to build a certain set of languages
for string_name in langs["en-EN"].keys():
strings_h.append(f"#define {string_name} {len(strings_h)}\n")
with open(strings_h_output_file_path, "w") as strings_h_file:
strings_h_file.writelines(
[
"#pragma once\n",
"// DO NOT MODIFY THIS FILE MANUALLY, it was automatically generated by pack_lock.py\n",
"// If you want to edit language strings edit strings.lang in your chosen platform folder (like Minecraft.Client/Platform/Windows64Media/loc)\n",
"\n"
]
)
strings_h_file.writelines(strings_h)
print(f"Generated {strings_h_output_file_path} with {len(strings_h)} defines")