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.

284 lines
8.7KB

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