comparison Resources/EmbedStaticAssets.py @ 46:9b2a2fcc9878 nexus

added option to disable Nexus support
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Apr 2024 07:49:45 +0200
parents dd0cd39e6259
children b798387b085c
comparison
equal deleted inserted replaced
45:967f947014ac 46:9b2a2fcc9878
24 import hashlib 24 import hashlib
25 import io 25 import io
26 import os 26 import os
27 import sys 27 import sys
28 28
29 if len(sys.argv) != 3: 29 if len(sys.argv) <= 2:
30 raise Exception('Usage: %s [source folder] [target C++]' % sys.argv[0]) 30 raise Exception('Usage: %s [target C++] [source folders]' % sys.argv[0])
31 31
32 SOURCE = sys.argv[1] 32 SOURCES = sys.argv[2:]
33 TARGET = sys.argv[2] 33 TARGET = sys.argv[1]
34 34
35 if not os.path.isdir(SOURCE): 35 for source in SOURCES:
36 raise Exception('Nonexistent source folder: %s' % SOURCE) 36 if not os.path.isdir(source):
37 raise Exception('Nonexistent source folder: %s' % source)
37 38
38 39
39 def EncodeFileAsCString(f, variable, content): 40 def EncodeFileAsCString(f, variable, content):
40 f.write('static const uint8_t %s[%d] = \n "' % (variable, len(content) + 1)) 41 f.write('static const uint8_t %s[%d] = \n "' % (variable, len(content) + 1))
41 42
92 ''') 93 ''')
93 94
94 index = {} 95 index = {}
95 count = 0 96 count = 0
96 97
97 for root, dirs, files in os.walk(SOURCE): 98 for source in SOURCES:
98 files.sort() 99 for root, dirs, files in os.walk(source):
99 dirs.sort() 100 files.sort()
101 dirs.sort()
100 102
101 for f in files: 103 for f in files:
102 fullPath = os.path.join(root, f) 104 fullPath = os.path.join(root, f)
103 relativePath = os.path.relpath(os.path.join(root, f), SOURCE) 105 relativePath = os.path.relpath(os.path.join(root, f), source)
104 variable = 'data_%06d' % count 106 variable = 'data_%06d' % count
105 107
106 with open(fullPath, 'rb') as source: 108 with open(fullPath, 'rb') as f:
107 content = source.read() 109 content = f.read()
108 110
109 if sys.version_info < (3, 0): 111 if sys.version_info < (3, 0):
110 # Python 2.7 112 # Python 2.7
111 fileobj = io.BytesIO() 113 fileobj = io.BytesIO()
112 gzip.GzipFile(fileobj=fileobj, mode='w', mtime=0).write(content) 114 gzip.GzipFile(fileobj=fileobj, mode='w', mtime=0).write(content)
113 compressed = fileobj.getvalue() 115 compressed = fileobj.getvalue()
114 else: 116 else:
115 # Python 3.x 117 # Python 3.x
116 compressed = gzip.compress(content, mtime=0) 118 compressed = gzip.compress(content, mtime=0)
117 119
118 EncodeFileAsCString(g, variable, compressed) 120 EncodeFileAsCString(g, variable, compressed)
119 WriteChecksum(g, variable + '_md5', content) 121 WriteChecksum(g, variable + '_md5', content)
120 122
121 index[relativePath] = variable 123 index[relativePath] = variable
122 124
123 count += 1 125 count += 1
124 126
125 g.write('void ReadStaticAsset(std::string& target, const std::string& path)\n') 127 g.write('void ReadStaticAsset(std::string& target, const std::string& path)\n')
126 g.write('{\n') 128 g.write('{\n')
127 for (path, variable) in sorted(index.items()): 129 for (path, variable) in sorted(index.items()):
128 g.write(' if (path == "%s")\n' % path) 130 g.write(' if (path == "%s")\n' % path)