From 3226bb5ad6adf49feb48aed1eacc533a55a18e92 Mon Sep 17 00:00:00 2001 From: Pyogenics Date: Fri, 6 Mar 2026 23:28:59 +0000 Subject: [PATCH] Add colour table build script --- scripts/build_col.py | 76 ++++++++++++++++++++++++++++++++++++++++++++ scripts/pack_loc.py | 2 +- 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 scripts/build_col.py diff --git a/scripts/build_col.py b/scripts/build_col.py new file mode 100644 index 000000000..55f783360 --- /dev/null +++ b/scripts/build_col.py @@ -0,0 +1,76 @@ +''' +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. +''' + +''' +Colour table builder +-------------------------------------------------- +Usage: build_col.py path/to/HTMLColours.xml path/to/output.col + +For format see Common/Colours/ColourTable.cpp +''' + +from sys import argv +import xml.etree.ElementTree as ET +from struct import pack + + +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_file_path = argv[1] +output_file_path = argv[2] + +colours_file_tree = ET.parse(target_file_path) +colours_root = colours_file_tree.getroot() + +# Read colour data +colours = {} +for colour_data in colours_root: + name = colour_data.attrib["name"] + value = int(colour_data.attrib["value"], 16) + + colours[name] = value +print(f"Processed {len(colours)} from {target_file_path}") + +# Write col file +TARGET_COL_VERSION = 0 +with open(output_file_path, "wb") as col_file: + # Write header + pack_stream(">2I", col_file, + TARGET_COL_VERSION, + len(colours) + ) + + # Write colours + for name, value in colours.items(): + write_string(name, col_file) + pack_stream(">I", col_file, value) + + print(f"Wrote {col_file.tell()} bytes to {output_file_path}") diff --git a/scripts/pack_loc.py b/scripts/pack_loc.py index 7419a6939..936cf5904 100644 --- a/scripts/pack_loc.py +++ b/scripts/pack_loc.py @@ -53,7 +53,7 @@ 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 before strings.lang + 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)