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.

290 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());
  58. }
  59. }
  60. //==============================================================================
  61. void ResourceFile::setClassName (const String& name)
  62. {
  63. className = name;
  64. }
  65. void ResourceFile::addFile (const File& file)
  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. variableNames.add (variableName);
  74. }
  75. String ResourceFile::getDataVariableFor (const File& file) const
  76. {
  77. jassert (files.indexOf (file) >= 0);
  78. return variableNames [files.indexOf (file)];
  79. }
  80. String ResourceFile::getSizeVariableFor (const File& file) const
  81. {
  82. jassert (files.indexOf (file) >= 0);
  83. return variableNames [files.indexOf (file)] + "Size";
  84. }
  85. int64 ResourceFile::getTotalDataSize() const
  86. {
  87. int64 total = 0;
  88. for (int i = 0; i < files.size(); ++i)
  89. total += files.getReference(i).getSize();
  90. return total;
  91. }
  92. static String getComment()
  93. {
  94. String comment;
  95. comment << newLine << newLine
  96. << " This is an auto-generated file: Any edits you make may be overwritten!" << newLine
  97. << newLine
  98. << "*/" << newLine
  99. << newLine;
  100. return comment;
  101. }
  102. bool ResourceFile::writeHeader (MemoryOutputStream& header)
  103. {
  104. header << "/* ========================================================================================="
  105. << getComment()
  106. << "namespace " << className << newLine
  107. << "{" << newLine;
  108. bool containsAnyImages = false;
  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]);
  114. FileInputStream fileStream (file);
  115. if (fileStream.openedOk())
  116. {
  117. containsAnyImages = containsAnyImages
  118. || (ImageFileFormat::findImageFormatForStream (fileStream) != nullptr);
  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. }
  123. }
  124. header << " // If you provide the name of one of the binary resource variables above, this function will" << newLine
  125. << " // return the corresponding data and its size (or a null pointer if the name isn't found)." << newLine
  126. << " const char* getNamedResource (const char* resourceNameUTF8, int& dataSizeInBytes) throw();" << newLine
  127. << "}" << newLine;
  128. return true;
  129. }
  130. bool ResourceFile::writeCpp (MemoryOutputStream& cpp, const File& headerFile, int& i)
  131. {
  132. const int maxFileSize = 10 * 1024 * 1024;
  133. const bool isFirstFile = (i == 0);
  134. cpp << "/* ==================================== " << resourceFileIdentifierString << " ===================================="
  135. << getComment()
  136. << "namespace " << className << newLine
  137. << "{" << newLine;
  138. bool containsAnyImages = false;
  139. while (i < files.size())
  140. {
  141. const File& file = files.getReference(i);
  142. const String variableName (variableNames[i]);
  143. FileInputStream fileStream (file);
  144. if (fileStream.openedOk())
  145. {
  146. containsAnyImages = containsAnyImages
  147. || (ImageFileFormat::findImageFormatForStream (fileStream) != nullptr);
  148. const String tempVariable ("temp_" + String::toHexString (file.hashCode()));
  149. cpp << newLine << "//================== " << file.getFileName() << " ==================" << newLine
  150. << "static const unsigned char " << tempVariable << "[] =" << newLine;
  151. {
  152. MemoryBlock data;
  153. fileStream.readIntoMemoryBlock (data);
  154. CodeHelpers::writeDataAsCppLiteral (data, cpp, true, true);
  155. }
  156. cpp << newLine << newLine
  157. << "const char* " << variableName << " = (const char*) " << tempVariable << ";" << newLine;
  158. }
  159. ++i;
  160. if (cpp.getPosition() > maxFileSize)
  161. break;
  162. }
  163. if (isFirstFile)
  164. {
  165. if (i < files.size())
  166. {
  167. cpp << newLine
  168. << "}" << newLine
  169. << newLine
  170. << "#include \"" << headerFile.getFileName() << "\"" << newLine
  171. << newLine
  172. << "namespace " << className << newLine
  173. << "{";
  174. }
  175. cpp << newLine
  176. << newLine
  177. << "const char* getNamedResource (const char*, int&) throw();" << newLine
  178. << "const char* getNamedResource (const char* resourceNameUTF8, int& numBytes) throw()" << newLine
  179. << "{" << newLine;
  180. StringArray returnCodes;
  181. for (int j = 0; j < files.size(); ++j)
  182. {
  183. const File& file = files.getReference(j);
  184. const int64 dataSize = file.getSize();
  185. returnCodes.add ("numBytes = " + String (dataSize) + "; return " + variableNames[j] + ";");
  186. }
  187. CodeHelpers::createStringMatcher (cpp, "resourceNameUTF8", variableNames, returnCodes, 4);
  188. cpp << " numBytes = 0;" << newLine
  189. << " return 0;" << newLine
  190. << "}" << newLine;
  191. }
  192. cpp << newLine
  193. << "}" << newLine;
  194. return true;
  195. }
  196. bool ResourceFile::write (const File& cppFile, Array<File>& filesCreated)
  197. {
  198. const File headerFile (cppFile.withFileExtension (".h"));
  199. {
  200. MemoryOutputStream mo;
  201. if (! (writeHeader (mo) && FileHelpers::overwriteFileWithNewDataIfDifferent (headerFile, mo)))
  202. return false;
  203. filesCreated.add (headerFile);
  204. }
  205. int i = 0;
  206. int fileIndex = 0;
  207. for (;;)
  208. {
  209. File cpp (cppFile);
  210. if (fileIndex > 0)
  211. cpp = cpp.getSiblingFile (cppFile.getFileNameWithoutExtension() + String (fileIndex + 1))
  212. .withFileExtension (cppFile.getFileExtension());
  213. MemoryOutputStream mo;
  214. if (! (writeCpp (mo, headerFile, i) && FileHelpers::overwriteFileWithNewDataIfDifferent (cpp, mo)))
  215. return false;
  216. filesCreated.add (cpp);
  217. ++fileIndex;
  218. if (i >= files.size())
  219. break;
  220. }
  221. return true;
  222. }