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 18import os, sys 19 20# ----------------------------------------------------- 21 22def 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("-", "_") 44 45 resData = open(filename, 'rb').read() 46 47 print("Generating data for \"%s\"" % (filename)) 48 49 fdH.write(" extern const 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 char* %s::%sData = (const 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 91if __name__ == '__main__': 92 if len(sys.argv) != 3: 93 print("Usage: %s <namespace> <resource-folder>" % sys.argv[0]) 94 quit() 95 96 namespace = sys.argv[1].replace("-","_") 97 resFolder = sys.argv[2] 98 99 if not os.path.exists(resFolder): 100 print("Folder '%s' does not exist" % resFolder) 101 quit() 102 103 # find resource files 104 resFiles = [] 105 106 for root, dirs, files in os.walk(resFolder): 107 for name in files: 108 resFiles.append(os.path.join(root, name)) 109 110 resFiles.sort() 111 112 # create code now 113 res2c(namespace, resFiles) 114