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.

321 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #include "../Application/jucer_Headers.h"
  20. #include "jucer_ResourceFile.h"
  21. static const char* resourceFileIdentifierString = "JUCER_BINARY_RESOURCE";
  22. //==============================================================================
  23. ResourceFile::ResourceFile (Project& p) : project (p)
  24. {
  25. addResourcesFromProjectItem (project.getMainGroup());
  26. }
  27. ResourceFile::~ResourceFile()
  28. {
  29. }
  30. //==============================================================================
  31. void ResourceFile::addResourcesFromProjectItem (const Project::Item& projectItem)
  32. {
  33. if (projectItem.isGroup())
  34. {
  35. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  36. addResourcesFromProjectItem (projectItem.getChild(i));
  37. }
  38. else
  39. {
  40. if (projectItem.shouldBeAddedToBinaryResources())
  41. addFile (projectItem.getFile());
  42. }
  43. }
  44. //==============================================================================
  45. void ResourceFile::setClassName (const String& name)
  46. {
  47. className = name;
  48. }
  49. void ResourceFile::addFile (const File& file)
  50. {
  51. files.add (file);
  52. auto variableNameRoot = CodeHelpers::makeBinaryDataIdentifierName (file);
  53. auto variableName = variableNameRoot;
  54. int suffix = 2;
  55. while (variableNames.contains (variableName))
  56. variableName = variableNameRoot + String (suffix++);
  57. variableNames.add (variableName);
  58. }
  59. String ResourceFile::getDataVariableFor (const File& file) const
  60. {
  61. jassert (files.indexOf (file) >= 0);
  62. return variableNames [files.indexOf (file)];
  63. }
  64. String ResourceFile::getSizeVariableFor (const File& file) const
  65. {
  66. jassert (files.indexOf (file) >= 0);
  67. return variableNames [files.indexOf (file)] + "Size";
  68. }
  69. int64 ResourceFile::getTotalDataSize() const
  70. {
  71. int64 total = 0;
  72. for (int i = 0; i < files.size(); ++i)
  73. total += files.getReference(i).getSize();
  74. return total;
  75. }
  76. static void writeComment (MemoryOutputStream& mo)
  77. {
  78. mo << newLine << newLine
  79. << " This is an auto-generated file: Any edits you make may be overwritten!" << newLine
  80. << newLine
  81. << "*/" << newLine
  82. << newLine;
  83. }
  84. Result ResourceFile::writeHeader (MemoryOutputStream& header)
  85. {
  86. header << "/* =========================================================================================";
  87. writeComment (header);
  88. header << "#pragma once" << newLine
  89. << newLine
  90. << "namespace " << className << newLine
  91. << "{" << newLine;
  92. bool containsAnyImages = false;
  93. for (int i = 0; i < files.size(); ++i)
  94. {
  95. auto& file = files.getReference(i);
  96. if (! file.existsAsFile())
  97. return Result::fail ("Can't open resource file: " + file.getFullPathName());
  98. auto dataSize = file.getSize();
  99. auto variableName = variableNames[i];
  100. FileInputStream fileStream (file);
  101. if (fileStream.openedOk())
  102. {
  103. containsAnyImages = containsAnyImages
  104. || (ImageFileFormat::findImageFormatForStream (fileStream) != nullptr);
  105. header << " extern const char* " << variableName << ";" << newLine;
  106. header << " const int " << variableName << "Size = " << (int) dataSize << ";" << newLine << newLine;
  107. }
  108. }
  109. header << " // Number of elements in the namedResourceList and originalFileNames arrays." << newLine
  110. << " const int namedResourceListSize = " << files.size() << ";" << newLine
  111. << newLine
  112. << " // Points to the start of a list of resource names." << newLine
  113. << " extern const char* namedResourceList[];" << newLine
  114. << newLine
  115. << " // Points to the start of a list of resource filenames." << newLine
  116. << " extern const char* originalFilenames[];" << newLine
  117. << newLine
  118. << " // If you provide the name of one of the binary resource variables above, this function will" << newLine
  119. << " // return the corresponding data and its size (or a null pointer if the name isn't found)." << newLine
  120. << " const char* getNamedResource (const char* resourceNameUTF8, int& dataSizeInBytes);" << newLine
  121. << newLine
  122. << " // If you provide the name of one of the binary resource variables above, this function will" << newLine
  123. << " // return the corresponding original, non-mangled filename (or a null pointer if the name isn't found)." << newLine
  124. << " const char* getNamedResourceOriginalFilename (const char* resourceNameUTF8);" << newLine
  125. << "}" << newLine;
  126. return Result::ok();
  127. }
  128. Result ResourceFile::writeCpp (MemoryOutputStream& cpp, const File& headerFile, int& i, const int maxFileSize)
  129. {
  130. bool isFirstFile = (i == 0);
  131. cpp << "/* ==================================== " << resourceFileIdentifierString << " ====================================";
  132. writeComment (cpp);
  133. cpp << "namespace " << className << newLine
  134. << "{" << newLine;
  135. bool containsAnyImages = false;
  136. while (i < files.size())
  137. {
  138. auto& file = files.getReference(i);
  139. auto variableName = variableNames[i];
  140. FileInputStream fileStream (file);
  141. if (fileStream.openedOk())
  142. {
  143. containsAnyImages = containsAnyImages
  144. || (ImageFileFormat::findImageFormatForStream (fileStream) != nullptr);
  145. auto tempVariable = "temp_binary_data_" + String (i);
  146. cpp << newLine << "//================== " << file.getFileName() << " ==================" << newLine
  147. << "static const unsigned char " << tempVariable << "[] =" << newLine;
  148. {
  149. MemoryBlock data;
  150. fileStream.readIntoMemoryBlock (data);
  151. CodeHelpers::writeDataAsCppLiteral (data, cpp, true, true);
  152. }
  153. cpp << newLine << newLine
  154. << "const char* " << variableName << " = (const char*) " << tempVariable << ";" << newLine;
  155. }
  156. ++i;
  157. if (cpp.getPosition() > maxFileSize)
  158. break;
  159. }
  160. if (isFirstFile)
  161. {
  162. if (i < files.size())
  163. {
  164. cpp << newLine
  165. << "}" << newLine
  166. << newLine
  167. << "#include \"" << headerFile.getFileName() << "\"" << newLine
  168. << newLine
  169. << "namespace " << className << newLine
  170. << "{";
  171. }
  172. cpp << newLine
  173. << newLine
  174. << "const char* getNamedResource (const char* resourceNameUTF8, int& numBytes)" << newLine
  175. << "{" << newLine;
  176. StringArray returnCodes;
  177. for (auto& file : files)
  178. {
  179. auto dataSize = file.getSize();
  180. returnCodes.add ("numBytes = " + String (dataSize) + "; return " + variableNames[files.indexOf (file)] + ";");
  181. }
  182. CodeHelpers::createStringMatcher (cpp, "resourceNameUTF8", variableNames, returnCodes, 4);
  183. cpp << " numBytes = 0;" << newLine
  184. << " return nullptr;" << newLine
  185. << "}" << newLine
  186. << newLine;
  187. cpp << "const char* namedResourceList[] =" << newLine
  188. << "{" << newLine;
  189. for (int j = 0; j < files.size(); ++j)
  190. cpp << " " << variableNames[j].quoted() << (j < files.size() - 1 ? "," : "") << newLine;
  191. cpp << "};" << newLine << newLine;
  192. cpp << "const char* originalFilenames[] =" << newLine
  193. << "{" << newLine;
  194. for (auto& f : files)
  195. cpp << " " << f.getFileName().quoted() << (files.indexOf (f) < files.size() - 1 ? "," : "") << newLine;
  196. cpp << "};" << newLine << newLine;
  197. cpp << "const char* getNamedResourceOriginalFilename (const char* resourceNameUTF8)" << newLine
  198. << "{" << newLine
  199. << " for (unsigned int i = 0; i < (sizeof (namedResourceList) / sizeof (namedResourceList[0])); ++i)" << newLine
  200. << " {" << newLine
  201. << " if (namedResourceList[i] == resourceNameUTF8)" << newLine
  202. << " return originalFilenames[i];" << newLine
  203. << " }" << newLine
  204. << newLine
  205. << " return nullptr;" << newLine
  206. << "}" << newLine
  207. << newLine;
  208. }
  209. cpp << "}" << newLine;
  210. return Result::ok();
  211. }
  212. Result ResourceFile::write (Array<File>& filesCreated, const int maxFileSize)
  213. {
  214. auto projectLineFeed = project.getProjectLineFeed();
  215. auto headerFile = project.getBinaryDataHeaderFile();
  216. {
  217. MemoryOutputStream mo;
  218. mo.setNewLineString (projectLineFeed);
  219. auto r = writeHeader (mo);
  220. if (r.failed())
  221. return r;
  222. if (! FileHelpers::overwriteFileWithNewDataIfDifferent (headerFile, mo))
  223. return Result::fail ("Can't write to file: " + headerFile.getFullPathName());
  224. filesCreated.add (headerFile);
  225. }
  226. int i = 0;
  227. int fileIndex = 0;
  228. for (;;)
  229. {
  230. auto cpp = project.getBinaryDataCppFile (fileIndex);
  231. MemoryOutputStream mo;
  232. mo.setNewLineString (projectLineFeed);
  233. auto r = writeCpp (mo, headerFile, i, maxFileSize);
  234. if (r.failed())
  235. return r;
  236. if (! FileHelpers::overwriteFileWithNewDataIfDifferent (cpp, mo))
  237. return Result::fail ("Can't write to file: " + cpp.getFullPathName());
  238. filesCreated.add (cpp);
  239. ++fileIndex;
  240. if (i >= files.size())
  241. break;
  242. }
  243. return Result::ok();
  244. }