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.

297 lines
9.3KB

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