comparison DPF-Prymula-audioplugins/dpf/utils/res2c.py @ 3:84e66ea83026

DPF-Prymula-audioplugins-0.231015-2
author prymula <prymula76@outlook.com>
date Mon, 16 Oct 2023 21:53:34 +0200
parents
children
comparison
equal deleted inserted replaced
2:cf2cb71d31dd 3:84e66ea83026
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 # DISTRHO Plugin Framework (DPF)
5 # Copyright (C) 2012-2019 Filipe Coelho <falktx@falktx.com>
6 #
7 # Permission to use, copy, modify, and/or distribute this software for any purpose with
8 # or without fee is hereby granted, provided that the above copyright notice and this
9 # permission notice appear in all copies.
10 #
11 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
12 # TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
13 # NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 # DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
15 # IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16 # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
18 import os, sys
19
20 # -----------------------------------------------------
21
22 def res2c(namespace, filenames):
23
24 fdH = open("%s.hpp" % namespace, "w")
25 fdH.write("/* (Auto-generated binary data file). */\n")
26 fdH.write("\n")
27 fdH.write("#ifndef BINARY_%s_HPP\n" % namespace.upper())
28 fdH.write("#define BINARY_%s_HPP\n" % namespace.upper())
29 fdH.write("\n")
30 fdH.write("namespace %s\n" % namespace)
31 fdH.write("{\n")
32
33 fdC = open("%s.cpp" % namespace, "w")
34 fdC.write("/* (Auto-generated binary data file). */\n")
35 fdC.write("\n")
36 fdC.write("#include \"%s.hpp\"\n" % namespace)
37 fdC.write("\n")
38
39 tempIndex = 1
40
41 for filename in filenames:
42 shortFilename = filename.rsplit(os.sep, 1)[-1].split(".", 1)[0]
43 shortFilename = shortFilename.replace("-", "_").replace("@","_")
44
45 resData = open(filename, 'rb').read()
46
47 print("Generating data for \"%s\"" % (filename))
48
49 fdH.write(" extern const unsigned char* %sData;\n" % shortFilename)
50 fdH.write(" const unsigned int %sDataSize = %i;\n" % (shortFilename, len(resData)))
51
52 if tempIndex != len(filenames):
53 fdH.write("\n")
54
55 fdC.write("static const unsigned char temp_%s_%i[] = {\n" % (shortFilename, tempIndex))
56
57 curColumn = 1
58 fdC.write(" ")
59
60 for data in resData:
61 if curColumn == 0:
62 fdC.write(" ")
63
64 fdC.write(" %3u," % data)
65
66 if curColumn > 20:
67 fdC.write("\n ")
68 curColumn = 1
69 else:
70 curColumn += 1
71
72 fdC.write("};\n")
73 fdC.write("const unsigned char* %s::%sData = (const unsigned char*)temp_%s_%i;\n" % (namespace, shortFilename, shortFilename, tempIndex))
74
75 if tempIndex != len(filenames):
76 fdC.write("\n")
77
78 tempIndex += 1
79
80 fdH.write("}\n")
81 fdH.write("\n")
82 fdH.write("#endif // BINARY_%s_HPP\n" % namespace.upper())
83 fdH.write("\n")
84 fdH.close()
85
86 fdC.write("\n")
87 fdC.close()
88
89 # -----------------------------------------------------
90
91 if __name__ == '__main__':
92 if len(sys.argv) not in (3, 4):
93 print("Usage: %s <namespace> <resource-folder> [output-folder=$CWD]" % sys.argv[0])
94 quit()
95
96 namespace = sys.argv[1].replace("-","_")
97 resFolder = sys.argv[2]
98 outFolder = sys.argv[3] if len(sys.argv) == 4 else None
99
100 if not os.path.exists(resFolder):
101 print("Folder '%s' does not exist" % resFolder)
102 quit()
103
104 if outFolder is not None and not os.path.exists(outFolder):
105 print("Output folder '%s' does not exist" % outFolder)
106 quit()
107
108 # find resource files
109 resFiles = []
110
111 for root, dirs, files in os.walk(resFolder):
112 for name in files:
113 resFiles.append(os.path.abspath(os.path.join(root, name)))
114
115 resFiles.sort()
116
117 if outFolder is not None:
118 os.chdir(outFolder)
119
120 # create code now
121 res2c(namespace, resFiles)