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.

350 lines
10KB

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