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.

285 lines
9.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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. bool 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. const int64 dataSize = file.getSize();
  101. const String variableName (variableNames[i]);
  102. FileInputStream fileStream (file);
  103. if (fileStream.openedOk())
  104. {
  105. containsAnyImages = containsAnyImages
  106. || (ImageFileFormat::findImageFormatForStream (fileStream) != nullptr);
  107. header << " extern const char* " << variableName << ";" << newLine;
  108. header << " const int " << variableName << "Size = " << (int) dataSize << ";" << newLine << newLine;
  109. }
  110. }
  111. header << " // Points to the start of a list of resource names." << newLine
  112. << " extern const char* namedResourceList[];" << newLine
  113. << newLine
  114. << " // Number of elements in the namedResourceList array." << newLine
  115. << " const int namedResourceListSize = " << files.size() << ";" << newLine
  116. << newLine
  117. << " // If you provide the name of one of the binary resource variables above, this function will" << newLine
  118. << " // return the corresponding data and its size (or a null pointer if the name isn't found)." << newLine
  119. << " const char* getNamedResource (const char* resourceNameUTF8, int& dataSizeInBytes) throw();" << newLine
  120. << "}" << newLine
  121. << newLine
  122. << "#endif" << newLine;
  123. return true;
  124. }
  125. bool ResourceFile::writeCpp (MemoryOutputStream& cpp, const File& headerFile, int& i, const int maxFileSize)
  126. {
  127. const bool isFirstFile = (i == 0);
  128. cpp << "/* ==================================== " << resourceFileIdentifierString << " ===================================="
  129. << getComment()
  130. << "namespace " << className << newLine
  131. << "{" << newLine;
  132. bool containsAnyImages = false;
  133. while (i < files.size())
  134. {
  135. const File& file = files.getReference(i);
  136. const String variableName (variableNames[i]);
  137. FileInputStream fileStream (file);
  138. if (fileStream.openedOk())
  139. {
  140. containsAnyImages = containsAnyImages
  141. || (ImageFileFormat::findImageFormatForStream (fileStream) != nullptr);
  142. const String tempVariable ("temp_binary_data_" + String (i));
  143. cpp << newLine << "//================== " << file.getFileName() << " ==================" << newLine
  144. << "static const unsigned char " << tempVariable << "[] =" << newLine;
  145. {
  146. MemoryBlock data;
  147. fileStream.readIntoMemoryBlock (data);
  148. CodeHelpers::writeDataAsCppLiteral (data, cpp, true, true);
  149. }
  150. cpp << newLine << newLine
  151. << "const char* " << variableName << " = (const char*) " << tempVariable << ";" << newLine;
  152. }
  153. ++i;
  154. if (cpp.getPosition() > maxFileSize)
  155. break;
  156. }
  157. if (isFirstFile)
  158. {
  159. if (i < files.size())
  160. {
  161. cpp << newLine
  162. << "}" << newLine
  163. << newLine
  164. << "#include \"" << headerFile.getFileName() << "\"" << newLine
  165. << newLine
  166. << "namespace " << className << newLine
  167. << "{";
  168. }
  169. cpp << newLine
  170. << newLine
  171. << "const char* getNamedResource (const char*, int&) throw();" << newLine
  172. << "const char* getNamedResource (const char* resourceNameUTF8, int& numBytes) throw()" << newLine
  173. << "{" << newLine;
  174. StringArray returnCodes;
  175. for (int j = 0; j < files.size(); ++j)
  176. {
  177. const File& file = files.getReference(j);
  178. const int64 dataSize = file.getSize();
  179. returnCodes.add ("numBytes = " + String (dataSize) + "; return " + variableNames[j] + ";");
  180. }
  181. CodeHelpers::createStringMatcher (cpp, "resourceNameUTF8", variableNames, returnCodes, 4);
  182. cpp << " numBytes = 0;" << newLine
  183. << " return 0;" << newLine
  184. << "}" << newLine
  185. << newLine
  186. << "const char* namedResourceList[] =" << newLine
  187. << "{" << newLine;
  188. for (int j = 0; j < files.size(); ++j)
  189. cpp << " " << variableNames[j].quoted() << (j < files.size() - 1 ? "," : "") << newLine;
  190. cpp << "};" << newLine;
  191. }
  192. cpp << newLine
  193. << "}" << newLine;
  194. return true;
  195. }
  196. bool ResourceFile::write (Array<File>& filesCreated, const int maxFileSize)
  197. {
  198. const File headerFile (project.getBinaryDataHeaderFile());
  199. {
  200. MemoryOutputStream mo;
  201. if (! (writeHeader (mo) && FileHelpers::overwriteFileWithNewDataIfDifferent (headerFile, mo)))
  202. return false;
  203. filesCreated.add (headerFile);
  204. }
  205. int i = 0;
  206. int fileIndex = 0;
  207. for (;;)
  208. {
  209. File cpp (project.getBinaryDataCppFile (fileIndex));
  210. MemoryOutputStream mo;
  211. if (! (writeCpp (mo, headerFile, i, maxFileSize) && FileHelpers::overwriteFileWithNewDataIfDifferent (cpp, mo)))
  212. return false;
  213. filesCreated.add (cpp);
  214. ++fileIndex;
  215. if (i >= files.size())
  216. break;
  217. }
  218. return true;
  219. }