|
5 | 5 | import sys |
6 | 6 | import platform |
7 | 7 | import zipfile |
| 8 | +import binascii |
8 | 9 |
|
9 | 10 | #Parse arguments |
10 | 11 | value_mappings = { |
@@ -93,6 +94,101 @@ def replaceTextInFolder(folder_path, old_string, new_string): |
93 | 94 | for dir_name in dirs: |
94 | 95 | replaceTextInFolder(os.path.join(root, dir_name), old_string, new_string) |
95 | 96 |
|
| 97 | +def expand_glob_list(file_patterns): |
| 98 | + expanded_files = [] |
| 99 | + for pattern in file_patterns: |
| 100 | + expanded_files.extend(glob.glob(pattern)) |
| 101 | + return expanded_files |
| 102 | + |
| 103 | +def hash_resource_name(resource_name): |
| 104 | + # Hashes a resource name using the same algorithm as in the C++ code. |
| 105 | + hash_val = 0 |
| 106 | + for char in resource_name: |
| 107 | + hash_val = 31 * hash_val + ord(char) |
| 108 | + return hash_val & 0xFFFFFFFF # Limit to 32-bit hash |
| 109 | + |
| 110 | +def generate_binary_data(output_dir, file_list): |
| 111 | + os.makedirs(output_dir, exist_ok=True) # Ensure output directory exists |
| 112 | + file_data = [] # To store metadata for each file |
| 113 | + |
| 114 | + # Gather file information and binary content |
| 115 | + for index, filepath in enumerate(file_list): |
| 116 | + filename = os.path.basename(filepath) |
| 117 | + if os.path.isfile(filepath): |
| 118 | + with open(filepath, "rb") as file: |
| 119 | + binary_content = file.read() |
| 120 | + |
| 121 | + var_name = filename.replace('.', '_').replace('-', '_') |
| 122 | + temp_name = f"temp_binary_data_{index}" |
| 123 | + data = { |
| 124 | + "filename": filename, |
| 125 | + "var_name": var_name, |
| 126 | + "temp_name": temp_name, |
| 127 | + "size": len(binary_content), |
| 128 | + "binary": list(binary_content), |
| 129 | + "index": str(index), |
| 130 | + "hash": hash_resource_name(var_name) |
| 131 | + } |
| 132 | + file_data.append(data) |
| 133 | + |
| 134 | + # Generate header file |
| 135 | + with open(output_dir + "/BinaryData.h", "w") as header: |
| 136 | + header.write("namespace BinaryData\n{\n") |
| 137 | + for data in file_data: |
| 138 | + header.write(f" extern const char* {data['var_name']};\n") |
| 139 | + header.write(f" const int {data['var_name']}Size = {data['size']};\n") |
| 140 | + |
| 141 | + header.write("\n const int namedResourceListSize = {};\n".format(len(file_data))) |
| 142 | + header.write(" extern const char* namedResourceList[];\n") |
| 143 | + header.write(" extern const char* originalFilenames[];\n") |
| 144 | + header.write(" const char* getNamedResource (const char* resourceNameUTF8, int& dataSizeInBytes);\n") |
| 145 | + header.write("}\n") |
| 146 | + |
| 147 | + with open(output_dir + "/BinaryData.cpp", "w") as source: |
| 148 | + source.write("#include \"BinaryData.h\"\n\n") |
| 149 | + source.write("namespace BinaryData\n{\n\n") |
| 150 | + |
| 151 | + source.write("\n const char* namedResourceList[] = {\n") |
| 152 | + for data in file_data: |
| 153 | + source.write(f" \"{data['var_name']}\",\n") |
| 154 | + source.write(" };\n\n") |
| 155 | + |
| 156 | + source.write("const char* getNamedResource (const char* resourceNameUTF8, int& numBytes)\n") |
| 157 | + source.write("{\n") |
| 158 | + source.write(" unsigned int hash = 0;\n") |
| 159 | + source.write(" if (resourceNameUTF8 != nullptr)\n") |
| 160 | + source.write(" while (*resourceNameUTF8 != 0)\n") |
| 161 | + source.write(" hash = 31 * hash + (unsigned int) *resourceNameUTF8++;\n\n") |
| 162 | + |
| 163 | + source.write(" switch (hash)\n {\n") |
| 164 | + for data in file_data: |
| 165 | + source.write(f" case 0x{data['hash']:08x}: numBytes = {data['size']}; return {data['var_name']};\n") |
| 166 | + source.write(" default: break;\n") |
| 167 | + source.write(" }\n\n") |
| 168 | + |
| 169 | + source.write(" numBytes = 0;\n") |
| 170 | + source.write(" return nullptr;\n") |
| 171 | + source.write("}\n") |
| 172 | + source.write("}\n") |
| 173 | + |
| 174 | + # Generate .cpp files for each binary resource |
| 175 | + for data in file_data: |
| 176 | + cpp_filename = output_dir + "/BinaryData_" + data["index"] + ".cpp" |
| 177 | + with open(cpp_filename, "w") as cpp_file: |
| 178 | + cpp_file.write("namespace BinaryData\n{\n") |
| 179 | + cpp_file.write(f"//================== {data['filename']} ==================\n") |
| 180 | + cpp_file.write(f"static const unsigned char {data['temp_name']}[] =\n{{\n") |
| 181 | + |
| 182 | + # Write binary data in readable hex format |
| 183 | + binary_chunks = [data['binary'][i:i+12] for i in range(0, len(data['binary']), 12)] |
| 184 | + for chunk in binary_chunks: |
| 185 | + hex_values = ', '.join(f"0x{binascii.hexlify(byte.to_bytes(1, 'big')).decode('utf-8').upper()}" for byte in chunk) |
| 186 | + cpp_file.write(f" {hex_values},\n") |
| 187 | + |
| 188 | + cpp_file.write("};\n\n") |
| 189 | + cpp_file.write(f"const char* {data['var_name']} = (const char*) {data['temp_name']};\n") |
| 190 | + cpp_file.write("}\n") |
| 191 | + |
96 | 192 |
|
97 | 193 | if existsAsFile("../Filesystem.zip"): |
98 | 194 | removeFile("../Filesystem.zip") |
@@ -204,3 +300,19 @@ def replaceTextInFolder(folder_path, old_string, new_string): |
204 | 300 |
|
205 | 301 | splitFile("./Filesystem.zip", output_dir + "/Filesystem_%i.zip", 12) |
206 | 302 | removeFile("./Filesystem.zip") |
| 303 | + |
| 304 | +generate_binary_data("../BinaryData", expand_glob_list({ |
| 305 | + project_root + "/Resources/Fonts/IconFont.ttf", |
| 306 | + project_root + "/Resources/Fonts/InterTabular.ttf", |
| 307 | + project_root + "/Resources/Fonts/InterBold.ttf", |
| 308 | + project_root + "/Resources/Fonts/InterSemiBold.ttf", |
| 309 | + project_root + "/Resources/Fonts/InterThin.ttf", |
| 310 | + project_root + "/Resources/Fonts/InterVariable.ttf", |
| 311 | + project_root + "/Resources/Fonts/InterRegular.ttf", |
| 312 | + project_root + "/Resources/Fonts/RobotoMono-Regular.ttf", |
| 313 | + project_root + "/Resources/Icons/plugdata_large_logo.png", |
| 314 | + project_root + "/Resources/Icons/plugdata_logo.png", |
| 315 | + "Documentation.bin", |
| 316 | + "InterUnicode_*.ttf", |
| 317 | + "Filesystem_*.zip" |
| 318 | +})) |
0 commit comments