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.

241 lines
9.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #include "jucer_ResourceFile.h"
  19. #include "../Project/jucer_ProjectTreeViewBase.h"
  20. #include "../Application/jucer_OpenDocumentManager.h"
  21. static const char* resourceFileIdentifierString = "JUCER_BINARY_RESOURCE";
  22. //==============================================================================
  23. ResourceFile::ResourceFile (Project& project_)
  24. : project (project_),
  25. className ("BinaryData")
  26. {
  27. addResourcesFromProjectItem (project.getMainGroup());
  28. }
  29. ResourceFile::~ResourceFile()
  30. {
  31. }
  32. bool ResourceFile::isResourceFile (const File& file)
  33. {
  34. if (file.hasFileExtension ("cpp;cc;h"))
  35. {
  36. ScopedPointer <InputStream> in (file.createInputStream());
  37. if (in != nullptr)
  38. {
  39. MemoryBlock mb;
  40. in->readIntoMemoryBlock (mb, 256);
  41. return mb.toString().contains (resourceFileIdentifierString);
  42. }
  43. }
  44. return false;
  45. }
  46. //==============================================================================
  47. void ResourceFile::addResourcesFromProjectItem (const Project::Item& projectItem)
  48. {
  49. if (projectItem.isGroup())
  50. {
  51. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  52. addResourcesFromProjectItem (projectItem.getChild(i));
  53. }
  54. else
  55. {
  56. if (projectItem.shouldBeAddedToBinaryResources())
  57. addFile (projectItem.getFile(), projectItem.getImageFileID());
  58. }
  59. }
  60. //==============================================================================
  61. void ResourceFile::setClassName (const String& className_)
  62. {
  63. className = className_;
  64. }
  65. void ResourceFile::addFile (const File& file, const String& imageProviderId)
  66. {
  67. files.add (file);
  68. const String variableNameRoot (CodeHelpers::makeBinaryDataIdentifierName (file));
  69. String variableName (variableNameRoot);
  70. int suffix = 2;
  71. while (variableNames.contains (variableName))
  72. variableName = variableNameRoot + String (suffix++);
  73. if (imageProviderId.isNotEmpty())
  74. variableName << "|" << imageProviderId;
  75. variableNames.add (variableName);
  76. }
  77. String ResourceFile::getDataVariableFor (const File& file) const
  78. {
  79. jassert (files.indexOf (file) >= 0);
  80. return variableNames [files.indexOf (file)];
  81. }
  82. String ResourceFile::getSizeVariableFor (const File& file) const
  83. {
  84. jassert (files.indexOf (file) >= 0);
  85. return variableNames [files.indexOf (file)] + "Size";
  86. }
  87. int64 ResourceFile::getTotalDataSize() const
  88. {
  89. int64 total = 0;
  90. for (int i = 0; i < files.size(); ++i)
  91. total += files.getReference(i).getSize();
  92. return total;
  93. }
  94. bool ResourceFile::write (const File& cppFile, OutputStream& cpp, OutputStream& header)
  95. {
  96. String comment;
  97. comment << newLine << newLine
  98. << " This is an auto-generated file, created by " << JUCEApplication::getInstance()->getApplicationName() << newLine
  99. << " Do not edit anything in this file!" << newLine << newLine
  100. << "*/" << newLine << newLine;
  101. header << "/* ========================================================================================="
  102. << comment;
  103. cpp << "/* ==================================== " << resourceFileIdentifierString << " ===================================="
  104. << comment;
  105. const String namespaceName (className);
  106. cpp << "namespace " << namespaceName << newLine << "{" << newLine;
  107. header << "namespace " << namespaceName << newLine << "{" << newLine;
  108. StringArray returnCodes;
  109. for (int i = 0; i < files.size(); ++i)
  110. {
  111. const File& file = files.getReference(i);
  112. const int64 dataSize = file.getSize();
  113. const String variableName (variableNames[i].upToFirstOccurrenceOf ("|", false, false));
  114. returnCodes.add ("numBytes = " + String (dataSize) + "; return " + variableName + ";");
  115. ScopedPointer <InputStream> fileStream (file.createInputStream());
  116. jassert (fileStream != nullptr);
  117. if (fileStream != nullptr)
  118. {
  119. const String tempVariable ("temp_" + String::toHexString (file.hashCode()));
  120. header << " extern const char* " << variableName << ";" << newLine;
  121. header << " const int " << variableName << "Size = " << (int) dataSize << ";" << newLine << newLine;
  122. cpp << newLine << "//================== " << file.getFileName() << " ==================" << newLine
  123. << "static const unsigned char " << tempVariable << "[] =" << newLine;
  124. {
  125. MemoryBlock data;
  126. fileStream->readIntoMemoryBlock (data);
  127. CodeHelpers::writeDataAsCppLiteral (data, cpp, true, true);
  128. }
  129. cpp << newLine << newLine
  130. << "const char* " << variableName << " = (const char*) " << tempVariable << ";" << newLine;
  131. }
  132. }
  133. cpp << newLine
  134. << newLine
  135. << "const char* getNamedResource (const char*, int&) throw();" << newLine
  136. << "const char* getNamedResource (const char* resourceNameUTF8, int& numBytes) throw()" << newLine
  137. << "{" << newLine;
  138. CodeHelpers::createStringMatcher (cpp, "resourceNameUTF8", variableNames, returnCodes, 4);
  139. cpp << " numBytes = 0;" << newLine
  140. << " return 0;" << newLine
  141. << "}" << newLine
  142. << newLine
  143. << "}" << newLine;
  144. header << " // If you provide the name of one of the binary resource variables above, this function will" << newLine
  145. << " // return the corresponding data and its size (or a null pointer if the name isn't found)." << newLine
  146. << " const char* getNamedResource (const char* resourceNameUTF8, int& dataSizeInBytes) throw();" << newLine
  147. << newLine
  148. << " //==============================================================================" << newLine
  149. << " // This class acts as an ImageProvider that will access the BinaryData images" << newLine
  150. << " class ImageProvider : public juce::ComponentBuilder::ImageProvider" << newLine
  151. << " {" << newLine
  152. << " public:" << newLine
  153. << " ImageProvider() noexcept {}" << newLine
  154. << newLine
  155. << " juce::Image getImageForIdentifier (const juce::var& imageIdentifier)" << newLine
  156. << " {" << newLine
  157. << " int dataSize = 0;" << newLine
  158. << " const char* const data = getNamedResource (imageIdentifier.toString().toUTF8(), dataSize);" << newLine
  159. << newLine
  160. << " if (data != nullptr)" << newLine
  161. << " return juce::ImageCache::getFromMemory (data, dataSize);" << newLine
  162. << newLine
  163. << " return juce::Image();" << newLine
  164. << " }" << newLine
  165. << newLine
  166. << " juce::var getIdentifierForImage (const juce::Image&) { return juce::var(); }" << newLine
  167. << " };" << newLine
  168. << "}" << newLine;
  169. return true;
  170. }
  171. bool ResourceFile::write (const File& cppFile)
  172. {
  173. TemporaryFile tempH (cppFile.withFileExtension (".h"), TemporaryFile::useHiddenFile);
  174. TemporaryFile tempCpp (cppFile, TemporaryFile::useHiddenFile);
  175. ScopedPointer <FileOutputStream> cppOut (tempCpp.getFile().createOutputStream (32768));
  176. ScopedPointer <FileOutputStream> hppOut (tempH.getFile().createOutputStream (32768));
  177. if (cppOut != nullptr && hppOut != nullptr)
  178. {
  179. if (write (cppFile, *cppOut, *hppOut))
  180. {
  181. cppOut = nullptr;
  182. hppOut = nullptr;
  183. return (tempCpp.getFile().hasIdenticalContentTo (tempCpp.getTargetFile()) || tempCpp.overwriteTargetFileWithTemporary())
  184. && (tempH.getFile().hasIdenticalContentTo (tempH.getTargetFile()) || tempH.overwriteTargetFileWithTemporary());
  185. }
  186. }
  187. return false;
  188. }