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.

336 lines
9.4KB

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