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.

296 lines
9.3KB

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