forked from osmandapp/OsmAnd-resources
-
Notifications
You must be signed in to change notification settings - Fork 0
/
embed-resources.py
189 lines (161 loc) · 8.74 KB
/
embed-resources.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import glob
import os
import zlib
# =============================================================================
# =============================================================================
# =============================================================================
class OsmAndCoreResourcesPacker(object):
# -------------------------------------------------------------------------
def __init__(self):
return
# -------------------------------------------------------------------------
def writeCppHeader(self, outputFile):
outputFile.write("// AUTOGENERATED FILE\n")
outputFile.write("// Resources bundle for OsmAnd::CoreResourcesEmbeddedBundle\n")
outputFile.write("\n")
outputFile.write("#include <stddef.h>\n")
outputFile.write("#include <stdint.h>\n")
outputFile.write("\n")
outputFile.write("#define ___STRINGIFY2(x) #x\n")
outputFile.write("#define ___STRINGIFY(x) ___STRINGIFY2(x)\n")
outputFile.write("\n")
outputFile.write("#define GETTER_NAME(varname) __get__##varname\\\n")
outputFile.write("\n")
outputFile.write("#if defined(_WIN32) || defined(__CYGWIN__)\n")
outputFile.write("# define BUNDLE_API __declspec(dllexport)\n")
outputFile.write("#elif (defined(__GNUC__) && __GNUC__ >= 4) || defined(__clang__)\n")
outputFile.write("# define BUNDLE_API __attribute__ ((visibility(\"default\"), used))\n")
outputFile.write("#else\n")
outputFile.write("# define BUNDLE_API\n")
outputFile.write("#endif\n")
outputFile.write("\n")
outputFile.write("#if defined(_MSC_VER)\n")
outputFile.write("# define EMIT_GETTER(varname, accessor)\\\n")
outputFile.write(" __pragma( comment ( linker, \"/INCLUDE:_\"___STRINGIFY( __get__##varname ) ) )\\\n")
outputFile.write(" BUNDLE_API const void* __get__##varname() {\\\n")
outputFile.write(" return reinterpret_cast<const void*>(accessor varname);\\\n")
outputFile.write(" }\n")
outputFile.write("#else\n")
outputFile.write("# define EMIT_GETTER(varname, accessor)\\\n")
outputFile.write(" BUNDLE_API const void* __get__##varname() {\\\n")
outputFile.write(" return reinterpret_cast<const void*>(accessor varname);\\\n")
outputFile.write(" }\n")
outputFile.write("#endif // defined(_MSC_VER)\n")
outputFile.write("\n")
outputFile.write("extern \"C\" {\n")
outputFile.write("\n")
# -------------------------------------------------------------------------
def pack(self, workingDir, resourcesPath, resourcesIndex):
cppIndex = 0
idx = 0
for resourcesIndexEntry in resourcesIndex:
cppIndex += 1
outputFilename = workingDir + "/gen/EmbeddedResourcesBundle_" + str(cppIndex) + ".cpp"
# Embedded resources
with open(resourcesIndexEntry.strip(), "r") as resourcesListFile:
resourcesList = resourcesListFile.readlines()
resources = []
for resourcesListEntry in resourcesList:
resourcesListEntryParts = resourcesListEntry.split(':')
originalPath = resourcesListEntryParts[0].strip()
packedPath = resourcesListEntryParts[1].strip()
resources.append((resourcesPath + originalPath, packedPath))
# Check if output directory exists
outputDir = os.path.dirname(outputFilename)
if not os.path.isdir(outputDir):
os.makedirs(outputDir)
# Open file for writing and and write header to it
try:
outputFile = open(outputFilename, "w")
except IOError:
print("Failed to open '%s' for writing" % (outputFilename))
return False
self.writeCppHeader(outputFile)
# For each resource in collection, pack it
for (index, resource) in enumerate(resources):
# if "hdpi" in resource[0]:
# continue
originalSize = os.path.getsize(resource[0])
with open(resource[0], "rb") as resourceFile:
resourceContent = resourceFile.read()
if not resource[0].endswith(".png"):
packedContent = zlib.compress(resourceContent, 9)
else:
packedContent = resourceContent
packedSize = len(packedContent)
outputFile.write("static const char* const __CoreResourcesEmbeddedBundle__ResourceName_%d = \"%s\";\n" % (idx, resource[1]))
outputFile.write("EMIT_GETTER(__CoreResourcesEmbeddedBundle__ResourceName_%d, )\n" % (idx))
outputFile.write("static const uint8_t __CoreResourcesEmbeddedBundle__ResourceData_%d[] = {\n" % (idx))
# Write size header
outputFile.write("\t0x%02x, 0x%02x, 0x%02x, 0x%02x," % (
(originalSize >> 24)&0xff,
(originalSize >> 16)&0xff,
(originalSize >> 8)&0xff,
(originalSize >> 0)&0xff))
# Write content
for (byteIdx, byteValue) in enumerate(packedContent):
if byteIdx % 16 == 0:
outputFile.write("\n\t")
outputFile.write("0x%02x, " % (byteValue))
outputFile.write("\n")
outputFile.write("};\n")
outputFile.write("EMIT_GETTER(__CoreResourcesEmbeddedBundle__ResourceData_%d, )\n" % (idx))
outputFile.write("const size_t __CoreResourcesEmbeddedBundle__ResourceSize_%d = 4 + %d;\n" % (idx, packedSize))
outputFile.write("EMIT_GETTER(__CoreResourcesEmbeddedBundle__ResourceSize_%d, &)\n" % (idx))
outputFile.write("\n")
print("Packed '%s'(%d bytes) as '%s'(4+%d bytes)..." % (resource[0], originalSize, resource[1], packedSize))
idx += 1
# Write footer of the file and close it
outputFile.write("} // extern \"C\"\n")
outputFile.write("\n")
outputFile.flush()
outputFile.close()
outputFilename = workingDir + "/gen/EmbeddedResourcesBundle_total.cpp"
# Open file for writing and and write header to it
try:
outputFile = open(outputFilename, "w")
except IOError:
print("Failed to open '%s' for writing" % (outputFilename))
return False
self.writeCppHeader(outputFile)
# Write footer of the file and close it
outputFile.write("const uint32_t __CoreResourcesEmbeddedBundle__ResourcesCount = %d;\n" % idx)
outputFile.write("EMIT_GETTER(__CoreResourcesEmbeddedBundle__ResourcesCount, &)\n")
outputFile.write("\n")
#outputFile.write("BUNDLE_API void __CoreResourcesEmbeddedBundle__FakeReferences() {\\\n")
#for index in range(1, idx + 1):
# outputFile.write(" GETTER_NAME(__CoreResourcesEmbeddedBundle__ResourceName_%d)();\n" % (index))
# outputFile.write(" GETTER_NAME(__CoreResourcesEmbeddedBundle__ResourceData_%d)();\n" % (index))
# outputFile.write(" GETTER_NAME(__CoreResourcesEmbeddedBundle__ResourceSize_%d)();\n" % (index))
#outputFile.write("}\n")
#outputFile.write("\n")
outputFile.write("} // extern \"C\"\n")
outputFile.write("\n")
outputFile.flush()
outputFile.close()
return True
# =============================================================================
# =============================================================================
# =============================================================================
if __name__=='__main__':
# Get root directory of entire project
rootPath = os.path.realpath(os.path.join(os.path.dirname(os.path.realpath(__file__)), ".."))
print("OsmAnd root path: %s" % (rootPath))
resourcesPath = rootPath + "/resources"
print("OsmAnd resources path: %s" % (resourcesPath))
# Output filename (in current working directory)
workingDir = os.getcwd()
if len(sys.argv) >= 2:
workingDir = sys.argv[1]
print("Working in: %s" % (workingDir))
for f in glob.glob(workingDir + "/gen/EmbeddedResourcesBundle_*"):
os.remove(f)
# Embedded resources
with open(resourcesPath + "/embed-resources.index", "r") as embedResourcesListFileIndex:
embedResourcesListIndex = embedResourcesListFileIndex.readlines()
packer = OsmAndCoreResourcesPacker()
ok = packer.pack(workingDir, resourcesPath, embedResourcesListIndex)
sys.exit(0 if ok else -1)