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.

338 lines
9.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. #include "../Application/jucer_Headers.h"
  19. #include "jucer_JucerDocument.h"
  20. //==============================================================================
  21. BinaryResources& BinaryResources::operator= (const BinaryResources& other)
  22. {
  23. for (auto* r : other.resources)
  24. add (r->name, r->originalFilename, r->data);
  25. return *this;
  26. }
  27. void BinaryResources::changed()
  28. {
  29. if (document != nullptr)
  30. {
  31. document->changed();
  32. document->refreshAllPropertyComps();
  33. }
  34. }
  35. //==============================================================================
  36. void BinaryResources::clear()
  37. {
  38. if (resources.size() > 0)
  39. {
  40. resources.clear();
  41. changed();
  42. }
  43. }
  44. StringArray BinaryResources::getResourceNames() const
  45. {
  46. StringArray s;
  47. for (auto* r : resources)
  48. s.add (r->name);
  49. return s;
  50. }
  51. BinaryResources::BinaryResource* BinaryResources::findResource (const String& name) const noexcept
  52. {
  53. for (auto* r : resources)
  54. if (r->name == name)
  55. return r;
  56. return nullptr;
  57. }
  58. const BinaryResources::BinaryResource* BinaryResources::getResource (const String& name) const
  59. {
  60. return findResource (name);
  61. }
  62. const BinaryResources::BinaryResource* BinaryResources::getResourceForFile (const File& file) const
  63. {
  64. for (auto* r : resources)
  65. if (r->originalFilename == file.getFullPathName())
  66. return r;
  67. return nullptr;
  68. }
  69. bool BinaryResources::add (const String& name, const File& file)
  70. {
  71. MemoryBlock mb;
  72. if (! file.loadFileAsData (mb))
  73. return false;
  74. add (name, file.getFullPathName(), mb);
  75. return true;
  76. }
  77. void BinaryResources::add (const String& name, const String& originalFileName, const MemoryBlock& data)
  78. {
  79. auto* r = findResource (name);
  80. if (r == nullptr)
  81. {
  82. resources.add (r = new BinaryResource());
  83. r->name = name;
  84. }
  85. r->originalFilename = originalFileName;
  86. r->data = data;
  87. r->drawable.reset();
  88. changed();
  89. }
  90. bool BinaryResources::reload (const int index)
  91. {
  92. return resources[index] != nullptr
  93. && add (resources [index]->name,
  94. File (resources [index]->originalFilename));
  95. }
  96. void BinaryResources::browseForResource (const String& title,
  97. const String& wildcard,
  98. const File& fileToStartFrom,
  99. const String& resourceToReplace,
  100. std::function<void (String)> callback)
  101. {
  102. chooser = std::make_unique<FileChooser> (title, fileToStartFrom, wildcard);
  103. auto flags = FileBrowserComponent::openMode | FileBrowserComponent::canSelectFiles;
  104. chooser->launchAsync (flags, [this, resourceToReplace, callback] (const FileChooser& fc)
  105. {
  106. if (fc.getResult() == File{})
  107. callback ({});
  108. String name (resourceToReplace);
  109. if (name.isEmpty())
  110. name = findUniqueName (fc.getResult().getFileName());
  111. if (! add (name, fc.getResult()))
  112. {
  113. AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
  114. TRANS("Adding Resource"),
  115. TRANS("Failed to load the file!"));
  116. name.clear();
  117. }
  118. callback (name);
  119. });
  120. }
  121. String BinaryResources::findUniqueName (const String& rootName) const
  122. {
  123. auto nameRoot = build_tools::makeValidIdentifier (rootName, true, true, false);
  124. auto name = nameRoot;
  125. auto names = getResourceNames();
  126. int suffix = 1;
  127. while (names.contains (name))
  128. name = nameRoot + String (++suffix);
  129. return name;
  130. }
  131. void BinaryResources::remove (int i)
  132. {
  133. if (resources[i] != nullptr)
  134. {
  135. resources.remove (i);
  136. changed();
  137. }
  138. }
  139. const Drawable* BinaryResources::getDrawable (const String& name) const
  140. {
  141. if (auto* res = const_cast<BinaryResources::BinaryResource*> (getResource (name)))
  142. {
  143. if (res->drawable == nullptr && res->data.getSize() > 0)
  144. res->drawable = Drawable::createFromImageData (res->data.getData(),
  145. res->data.getSize());
  146. return res->drawable.get();
  147. }
  148. return nullptr;
  149. }
  150. Image BinaryResources::getImageFromCache (const String& name) const
  151. {
  152. if (auto* res = getResource (name))
  153. if (res->data.getSize() > 0)
  154. return ImageCache::getFromMemory (res->data.getData(), (int) res->data.getSize());
  155. return {};
  156. }
  157. void BinaryResources::loadFromCpp (const File& cppFileLocation, const String& cppFile)
  158. {
  159. StringArray cpp;
  160. cpp.addLines (cppFile);
  161. clear();
  162. for (int i = 0; i < cpp.size(); ++i)
  163. {
  164. if (cpp[i].contains ("JUCER_RESOURCE:"))
  165. {
  166. StringArray tokens;
  167. tokens.addTokens (cpp[i].fromFirstOccurrenceOf (":", false, false), ",", "\"'");
  168. tokens.trim();
  169. tokens.removeEmptyStrings();
  170. auto resourceName = tokens[0];
  171. auto resourceSize = tokens[1].getIntValue();
  172. auto originalFileName = cppFileLocation.getSiblingFile (tokens[2].unquoted()).getFullPathName();
  173. jassert (resourceName.isNotEmpty() && resourceSize > 0);
  174. if (resourceName.isNotEmpty() && resourceSize > 0)
  175. {
  176. auto firstLine = i;
  177. while (i < cpp.size())
  178. if (cpp [i++].contains ("}"))
  179. break;
  180. auto dataString = cpp.joinIntoString (" ", firstLine, i - firstLine)
  181. .fromFirstOccurrenceOf ("{", false, false);
  182. MemoryOutputStream out;
  183. String::CharPointerType t (dataString.getCharPointer());
  184. int n = 0;
  185. while (! t.isEmpty())
  186. {
  187. auto c = t.getAndAdvance();
  188. if (c >= '0' && c <= '9')
  189. {
  190. n = n * 10 + (int) (c - '0');
  191. }
  192. else if (c == ',')
  193. {
  194. out.writeByte ((char) n);
  195. n = 0;
  196. }
  197. else if (c == '}')
  198. {
  199. break;
  200. }
  201. }
  202. jassert (resourceSize < (int) out.getDataSize() && resourceSize > (int) out.getDataSize() - 2);
  203. MemoryBlock mb (out.getData(), out.getDataSize());
  204. mb.setSize ((size_t) resourceSize);
  205. add (resourceName, originalFileName, mb);
  206. }
  207. }
  208. }
  209. }
  210. //==============================================================================
  211. void BinaryResources::fillInGeneratedCode (GeneratedCode& code) const
  212. {
  213. if (resources.size() > 0)
  214. {
  215. code.publicMemberDeclarations << "// Binary resources:\n";
  216. MemoryOutputStream defs;
  217. defs << "//==============================================================================\n";
  218. defs << "// Binary resources - be careful not to edit any of these sections!\n\n";
  219. for (auto* r : resources)
  220. {
  221. code.publicMemberDeclarations
  222. << "static const char* "
  223. << r->name
  224. << ";\nstatic const int "
  225. << r->name
  226. << "Size;\n";
  227. auto name = r->name;
  228. auto& mb = r->data;
  229. defs << "// JUCER_RESOURCE: " << name << ", " << (int) mb.getSize()
  230. << ", \""
  231. << File (r->originalFilename)
  232. .getRelativePathFrom (code.document->getCppFile())
  233. .replaceCharacter ('\\', '/')
  234. << "\"\n";
  235. String line1;
  236. line1 << "static const unsigned char resource_"
  237. << code.className << "_" << name << "[] = { ";
  238. defs << line1;
  239. int charsOnLine = line1.length();
  240. for (size_t j = 0; j < mb.getSize(); ++j)
  241. {
  242. auto num = (int) (unsigned char) mb[j];
  243. defs << num << ',';
  244. charsOnLine += 2;
  245. if (num >= 10) ++charsOnLine;
  246. if (num >= 100) ++charsOnLine;
  247. if (charsOnLine >= 200)
  248. {
  249. charsOnLine = 0;
  250. defs << '\n';
  251. }
  252. }
  253. defs
  254. << "0,0};\n\n"
  255. "const char* " << code.className << "::" << name
  256. << " = (const char*) resource_" << code.className << "_" << name
  257. << ";\nconst int "
  258. << code.className << "::" << name << "Size = "
  259. << (int) mb.getSize()
  260. << ";\n\n";
  261. }
  262. code.staticMemberDefinitions << defs.toString();
  263. }
  264. }