The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

301 lines
9.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "../jucer_Headers.h"
  18. #include "jucer_ResourceFile.h"
  19. #include "../Application/jucer_OpenDocumentManager.h"
  20. static const char* resourceFileIdentifierString = "JUCER_BINARY_RESOURCE";
  21. //==============================================================================
  22. ResourceFile::ResourceFile (Project& p)
  23. : project (p),
  24. className ("BinaryData")
  25. {
  26. addResourcesFromProjectItem (project.getMainGroup());
  27. }
  28. ResourceFile::~ResourceFile()
  29. {
  30. }
  31. //==============================================================================
  32. void ResourceFile::addResourcesFromProjectItem (const Project::Item& projectItem)
  33. {
  34. if (projectItem.isGroup())
  35. {
  36. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  37. addResourcesFromProjectItem (projectItem.getChild(i));
  38. }
  39. else
  40. {
  41. if (projectItem.shouldBeAddedToBinaryResources())
  42. addFile (projectItem.getFile());
  43. }
  44. }
  45. //==============================================================================
  46. void ResourceFile::setClassName (const String& name)
  47. {
  48. className = name;
  49. }
  50. void ResourceFile::addFile (const File& file)
  51. {
  52. files.add (file);
  53. const String variableNameRoot (CodeHelpers::makeBinaryDataIdentifierName (file));
  54. String variableName (variableNameRoot);
  55. int suffix = 2;
  56. while (variableNames.contains (variableName))
  57. variableName = variableNameRoot + String (suffix++);
  58. variableNames.add (variableName);
  59. }
  60. String ResourceFile::getDataVariableFor (const File& file) const
  61. {
  62. jassert (files.indexOf (file) >= 0);
  63. return variableNames [files.indexOf (file)];
  64. }
  65. String ResourceFile::getSizeVariableFor (const File& file) const
  66. {
  67. jassert (files.indexOf (file) >= 0);
  68. return variableNames [files.indexOf (file)] + "Size";
  69. }
  70. int64 ResourceFile::getTotalDataSize() const
  71. {
  72. int64 total = 0;
  73. for (int i = 0; i < files.size(); ++i)
  74. total += files.getReference(i).getSize();
  75. return total;
  76. }
  77. static String getComment()
  78. {
  79. String comment;
  80. comment << newLine << newLine
  81. << " This is an auto-generated file: Any edits you make may be overwritten!" << newLine
  82. << newLine
  83. << "*/" << newLine
  84. << newLine;
  85. return comment;
  86. }
  87. Result ResourceFile::writeHeader (MemoryOutputStream& header)
  88. {
  89. const String headerGuard ("BINARYDATA_H_" + String (project.getProjectUID().hashCode() & 0x7ffffff) + "_INCLUDED");
  90. header << "/* ========================================================================================="
  91. << getComment()
  92. << "#ifndef " << headerGuard << newLine
  93. << "#define " << headerGuard << newLine
  94. << newLine
  95. << "namespace " << className << newLine
  96. << "{" << newLine;
  97. bool containsAnyImages = false;
  98. for (int i = 0; i < files.size(); ++i)
  99. {
  100. const File& file = files.getReference(i);
  101. if (! file.existsAsFile())
  102. return Result::fail ("Can't open resource file: " + file.getFullPathName());
  103. const int64 dataSize = file.getSize();
  104. const String variableName (variableNames[i]);
  105. FileInputStream fileStream (file);
  106. if (fileStream.openedOk())
  107. {
  108. containsAnyImages = containsAnyImages
  109. || (ImageFileFormat::findImageFormatForStream (fileStream) != nullptr);
  110. header << " extern const char* " << variableName << ";" << newLine;
  111. header << " const int " << variableName << "Size = " << (int) dataSize << ";" << newLine << newLine;
  112. }
  113. }
  114. header << " // Points to the start of a list of resource names." << newLine
  115. << " extern const char* namedResourceList[];" << newLine
  116. << newLine
  117. << " // Number of elements in the namedResourceList array." << newLine
  118. << " const int namedResourceListSize = " << files.size() << ";" << newLine
  119. << newLine
  120. << " // If you provide the name of one of the binary resource variables above, this function will" << newLine
  121. << " // return the corresponding data and its size (or a null pointer if the name isn't found)." << newLine
  122. << " const char* getNamedResource (const char* resourceNameUTF8, int& dataSizeInBytes) throw();" << newLine
  123. << "}" << newLine
  124. << newLine
  125. << "#endif" << newLine;
  126. return Result::ok();
  127. }
  128. Result ResourceFile::writeCpp (MemoryOutputStream& cpp, const File& headerFile, int& i, const int maxFileSize)
  129. {
  130. const bool isFirstFile = (i == 0);
  131. cpp << "/* ==================================== " << resourceFileIdentifierString << " ===================================="
  132. << getComment()
  133. << "namespace " << className << newLine
  134. << "{" << newLine;
  135. bool containsAnyImages = false;
  136. while (i < files.size())
  137. {
  138. const File& file = files.getReference(i);
  139. const String variableName (variableNames[i]);
  140. FileInputStream fileStream (file);
  141. if (fileStream.openedOk())
  142. {
  143. containsAnyImages = containsAnyImages
  144. || (ImageFileFormat::findImageFormatForStream (fileStream) != nullptr);
  145. const String tempVariable ("temp_binary_data_" + String (i));
  146. cpp << newLine << "//================== " << file.getFileName() << " ==================" << newLine
  147. << "static const unsigned char " << tempVariable << "[] =" << newLine;
  148. {
  149. MemoryBlock data;
  150. fileStream.readIntoMemoryBlock (data);
  151. CodeHelpers::writeDataAsCppLiteral (data, cpp, true, true);
  152. }
  153. cpp << newLine << newLine
  154. << "const char* " << variableName << " = (const char*) " << tempVariable << ";" << newLine;
  155. }
  156. ++i;
  157. if (cpp.getPosition() > maxFileSize)
  158. break;
  159. }
  160. if (isFirstFile)
  161. {
  162. if (i < files.size())
  163. {
  164. cpp << newLine
  165. << "}" << newLine
  166. << newLine
  167. << "#include \"" << headerFile.getFileName() << "\"" << newLine
  168. << newLine
  169. << "namespace " << className << newLine
  170. << "{";
  171. }
  172. cpp << newLine
  173. << newLine
  174. << "const char* getNamedResource (const char*, int&) throw();" << newLine
  175. << "const char* getNamedResource (const char* resourceNameUTF8, int& numBytes) throw()" << newLine
  176. << "{" << newLine;
  177. StringArray returnCodes;
  178. for (int j = 0; j < files.size(); ++j)
  179. {
  180. const File& file = files.getReference(j);
  181. const int64 dataSize = file.getSize();
  182. returnCodes.add ("numBytes = " + String (dataSize) + "; return " + variableNames[j] + ";");
  183. }
  184. CodeHelpers::createStringMatcher (cpp, "resourceNameUTF8", variableNames, returnCodes, 4);
  185. cpp << " numBytes = 0;" << newLine
  186. << " return 0;" << newLine
  187. << "}" << newLine
  188. << newLine
  189. << "const char* namedResourceList[] =" << newLine
  190. << "{" << newLine;
  191. for (int j = 0; j < files.size(); ++j)
  192. cpp << " " << variableNames[j].quoted() << (j < files.size() - 1 ? "," : "") << newLine;
  193. cpp << "};" << newLine;
  194. }
  195. cpp << newLine
  196. << "}" << newLine;
  197. return Result::ok();
  198. }
  199. Result ResourceFile::write (Array<File>& filesCreated, const int maxFileSize)
  200. {
  201. const File headerFile (project.getBinaryDataHeaderFile());
  202. {
  203. MemoryOutputStream mo;
  204. Result r (writeHeader (mo));
  205. if (r.failed())
  206. return r;
  207. if (! FileHelpers::overwriteFileWithNewDataIfDifferent (headerFile, mo))
  208. return Result::fail ("Can't write to file: " + headerFile.getFullPathName());
  209. filesCreated.add (headerFile);
  210. }
  211. int i = 0;
  212. int fileIndex = 0;
  213. for (;;)
  214. {
  215. File cpp (project.getBinaryDataCppFile (fileIndex));
  216. MemoryOutputStream mo;
  217. Result r (writeCpp (mo, headerFile, i, maxFileSize));
  218. if (r.failed())
  219. return r;
  220. if (! FileHelpers::overwriteFileWithNewDataIfDifferent (cpp, mo))
  221. return Result::fail ("Can't write to file: " + cpp.getFullPathName());
  222. filesCreated.add (cpp);
  223. ++fileIndex;
  224. if (i >= files.size())
  225. break;
  226. }
  227. return Result::ok();
  228. }