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.

302 lines
9.5KB

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