From 4eb685d94e89a2aef61047225c1834a89de01de2 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Wed, 28 Oct 2020 13:13:57 +0200 Subject: Simplify rcc - drop qrc parsing and zstd compression --- rcc_format/__init__.py | 16 +++++++++++-- rcc_format/generate.py | 9 -------- rcc_format/util.py | 62 +++++++++++++++++++++++--------------------------- rcc_format/zstd.py | 43 ---------------------------------- 4 files changed, 43 insertions(+), 87 deletions(-) delete mode 100755 rcc_format/generate.py delete mode 100644 rcc_format/zstd.py (limited to 'rcc_format') diff --git a/rcc_format/__init__.py b/rcc_format/__init__.py index 48ebd70..629bc54 100644 --- a/rcc_format/__init__.py +++ b/rcc_format/__init__.py @@ -1,3 +1,15 @@ from rcc_format.util import * -from rcc_format.generate import generate -from rcc_format.zstd import zstd +from os.path import basename + +def dump(filelist, args): + write_header(args.output, args.namespace) + + items = [] + for f in filelist: + x = resource(basename(f.name), to_variable_name(f.name), f.name) + items.append(x) + write_item(args.output, x.variable, f.read()) + + write_entries(args.output, items) + write_footer(args.output, args.namespace) + diff --git a/rcc_format/generate.py b/rcc_format/generate.py deleted file mode 100755 index f42f6d4..0000000 --- a/rcc_format/generate.py +++ /dev/null @@ -1,9 +0,0 @@ -def generate(filelist, args): - print('', file=args.output) - #echo ' ' - #for f in $@; do - # echo ' '$f''; - #done - #echo ' ' - print('', file=args.output) - diff --git a/rcc_format/util.py b/rcc_format/util.py index 7fcfe34..af6651b 100644 --- a/rcc_format/util.py +++ b/rcc_format/util.py @@ -9,59 +9,55 @@ def to_variable_name(path): name = name[:-5] name = name.replace('-', '_') name = name.replace('.', '_') - return name - -def filelist(file): - root = xml.parse(file).getroot() - if root.tag != 'RCC': - return None - - files = [] - for child in root: - if child.tag == 'qresource': - prefix = child.attrib['prefix'] - for i in child: - alias = prefix + '/' + i.attrib['alias'] - variable = to_variable_name(i.text) - path = i.text - files.append(resource(alias, variable, path)) - - return files + return "r__" + name def write_header(file, namespace): - print("// Autogenerated binary file hexdump", file=file) - print("// This file may get overwritten by the build system\n", file=file) - print("#include \n", file=file) + print("""// Autogenerated header - this file may get overwritten by the build system +#pragma once + +#include +#include +#include +#include +""", file=file) print("namespace {} {{".format(namespace), file=file) def write_item(file, array_name, array_data): - line_items = 0 - - print("constexpr uint8_t {}[] = {{".format(array_name), file=file) + print("constexpr char {}[] = {{".format(array_name), file=file) - for byte in array_data[0:len(array_data)]: + line_items = 0 + for byte in array_data: line_items+=1 if line_items == 16: - print(" 0x{:02X},".format(byte), file=file) + print("0x{:02X},".format(byte), file=file) line_items = 0 else: - print(" 0x{:02X},".format(byte), file=file, end='') - + print("0x{:02X},".format(byte), file=file, end='') print("};", file=file) print("constexpr size_t {}_len = {};\n".format(array_name, len(array_data)), file=file) def write_entries(file, resource_list): print("constexpr std::array entries {", file=file) - for f in resource_list: - print(" \"{}\", ".format(f.alias), file=file) - print("};\n", file=file) - print("constexpr std::array values {", file=file) for f in resource_list: - print(" std::span( {}, {}_len ),".format(f.variable, f.variable), file=file) + print(" std::make_tuple(\"{}\", std::span({}, {}_len))," + .format(f.alias, f.variable, f.variable), file=file) + print("};\n", file=file) def write_footer(file, namespace): + print("""template +constexpr auto get(StrViewLambda name) { + static_assert(std::is_same_v); + + constexpr auto x = std::find_if(icons::entries.begin(), icons::entries.end(), + [=](const auto &tuple) -> bool { + return std::get<0>(tuple) == name(); + }); + static_assert(x != icons::entries.end()); + + return std::get<1>(*x); +}""", file=file) print("\n}} // namespace {}".format(namespace), file=file) diff --git a/rcc_format/zstd.py b/rcc_format/zstd.py deleted file mode 100644 index 34eeb64..0000000 --- a/rcc_format/zstd.py +++ /dev/null @@ -1,43 +0,0 @@ -import subprocess -from rcc_format.util import * - -def zstd(filelist, args): - if args.train is not None: - train(filelist, args.train, args.binary, args.dsize) - return - - write_header(args.output, args.namespace) - - for f in filelist: - with open(f.path, 'rb') as contents: - write_item(args.output, f.variable, compress(contents, args.binary, args.level, dictionary=args.dict)) - - write_entries(args.output, filelist) - if args.dict is not None: - write_item(args.output, 'dict', args.dict.read()) - print("constexpr auto dictionary = std::span(dict, dict_len);", file=args.output) - else: - print("constexpr std::span dictionary {};", file=args.output) - - print("constexpr auto compression = embed::Zstd;", file=args.output) - - write_footer(args.output, args.namespace) - -def train(filelist, output, zstd_bin, maxdict): - cmd = [ zstd_bin, '--train', '--maxdict=' + str(maxdict), '-o', output.name ] - - for f in filelist: - cmd.append(f.path) - - subprocess.run(cmd) - -def compress(file, zstd_bin, level, dictionary=None): - cmd = [ zstd_bin, '--compress', '--stdout', '-' + str(level) ] - - if dictionary is not None: - cmd.append('-D') - cmd.append(dictionary.name) - - cmd.append(file.name) - return subprocess.run(cmd, capture_output=True).stdout - -- cgit v1.2.1