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.

281 lines
8.6KB

  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 << " // If you provide the name of one of the binary resource variables above, this function will" << newLine
  122. << " // return the corresponding data and its size (or a null pointer if the name isn't found)." << newLine
  123. << " const char* getNamedResource (const char* resourceNameUTF8, int& dataSizeInBytes) throw();" << newLine
  124. << "}" << newLine;
  125. return true;
  126. }
  127. bool 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. }
  188. cpp << newLine
  189. << "}" << newLine;
  190. return true;
  191. }
  192. bool ResourceFile::write (Array<File>& filesCreated, const int maxFileSize)
  193. {
  194. const File headerFile (project.getBinaryDataHeaderFile());
  195. {
  196. MemoryOutputStream mo;
  197. if (! (writeHeader (mo) && FileHelpers::overwriteFileWithNewDataIfDifferent (headerFile, mo)))
  198. return false;
  199. filesCreated.add (headerFile);
  200. }
  201. int i = 0;
  202. int fileIndex = 0;
  203. for (;;)
  204. {
  205. File cpp (project.getBinaryDataCppFile (fileIndex));
  206. MemoryOutputStream mo;
  207. if (! (writeCpp (mo, headerFile, i, maxFileSize) && FileHelpers::overwriteFileWithNewDataIfDifferent (cpp, mo)))
  208. return false;
  209. filesCreated.add (cpp);
  210. ++fileIndex;
  211. if (i >= files.size())
  212. break;
  213. }
  214. return true;
  215. }