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.

300 lines
9.5KB

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