aboutsummaryrefslogtreecommitdiff
path: root/rcc_format/util.py
blob: 2cd6341a99a01e2867e32cba783e3b5b43b6e485 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from collections import namedtuple
import xml.etree.ElementTree as xml

resource = namedtuple('resource', 'alias variable path')

def to_variable_name(path):
    name = path.replace('/', '_')
    if name.endswith('.zstd'):
        name = name[:-5]
    name = name.replace('-', '_')
    name = name.replace('.', '_')
    return "r__" + name

def write_header(file, namespace):
    print("""// Autogenerated header - this file may get overwritten by the build system
#pragma once

#include <algorithm>
#include <span>
#include <string_view>
#include <tuple>
""", file=file)
    print("namespace {} {{".format(namespace), file=file)

def write_item(file, array_name, array_data):
    print("constexpr char {}[] = {{".format(array_name), file=file)

    line_items = 0
    for byte in array_data:
        line_items+=1
        if line_items == 16:
            print("0x{:02X},".format(byte), file=file)
            line_items = 0
        else:
            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("  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<typename StrViewLambda>
constexpr auto get(StrViewLambda name) {
  static_assert(std::is_same_v<std::string_view, decltype(name())>);

  constexpr auto x = std::find_if(entries.begin(), entries.end(),
  [=](const auto &tuple) -> bool {
    return std::get<0>(tuple) == name();
  });
  static_assert(x != entries.end());

  return std::get<1>(*x);
}""", file=file)
    print("\n}} // namespace {}".format(namespace), file=file)