Hal Canary | a493510 | 2017-12-08 13:35:47 -0500 | [diff] [blame^] | 1 | #!/usr/bin/env python2 |
| 2 | # Copyright 2017 Google Inc. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | import os |
| 7 | import sys |
| 8 | |
| 9 | def get_resources(rdir): |
| 10 | for root, _, files in os.walk(rdir): |
| 11 | for filepath in files: |
| 12 | fullpath = os.path.join(root, filepath) |
| 13 | if os.path.isfile(fullpath): |
| 14 | yield os.path.relpath(fullpath, rdir) |
| 15 | |
| 16 | def main(resource_dir, array_name, filename): |
| 17 | with open(filename, 'w') as o: |
| 18 | o.write('//generated file\n#include "BinaryAsset.h"\n\n'); |
| 19 | names = [] |
| 20 | for n in sorted(get_resources(resource_dir)): |
| 21 | o.write('static const unsigned char x%d[] = {\n' % len(names)) |
| 22 | with open(os.path.join(resource_dir, n), 'rb') as f: |
| 23 | while True: |
| 24 | buf = f.read(20) |
| 25 | if len(buf) == 0: |
| 26 | break |
| 27 | o.write(''.join('%d,' % ord(x) for x in buf) + '\n') |
| 28 | o.write('};\n') |
| 29 | names.append(n) |
| 30 | o.write('\nBinaryAsset %s[] = {\n' % array_name) |
| 31 | for i, n in enumerate(names): |
| 32 | o.write(' {"%s", x%d, sizeof(x%d)},\n' % (n, i, i)) |
| 33 | o.write(' {nullptr, nullptr, 0}\n};\n') |
| 34 | |
| 35 | if __name__ == '__main__': |
| 36 | if len(sys.argv) < 4: |
| 37 | msg = 'usage:\n %s SOURCE_DIRECTORY ARRAY_IDENTIFIER OUTPUT_PATH.cpp\n\n' |
| 38 | sys.stderr.write(msg % sys.argv[0]) |
| 39 | exit(1) |
| 40 | main(*sys.argv[1:4]) |
| 41 | |