MissingFiles.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. #include "core/MissingFiles.h"
  2. #include <algorithm>
  3. #include <cctype>
  4. #include <set>
  5. namespace lynx
  6. {
  7. namespace
  8. {
  9. bool isSeparator (char c) { return c == '\\' || c == '/'; }
  10. // lower-case, one kind of separator, no trailing separator: two spellings of the same path compare equal
  11. std::string pathKey (const std::string& path)
  12. {
  13. std::string key;
  14. key.reserve (path.size());
  15. for (const auto c : path)
  16. key.push_back (isSeparator (c) ? '/' : (char) std::tolower ((unsigned char) c));
  17. while (key.size() > 1 && key.back() == '/')
  18. key.pop_back();
  19. return key;
  20. }
  21. std::vector<std::string> pathParts (const std::string& path)
  22. {
  23. std::vector<std::string> parts;
  24. std::string part;
  25. for (const auto c : pathKey (path))
  26. {
  27. if (c == '/')
  28. {
  29. if (! part.empty())
  30. parts.push_back (part);
  31. part.clear();
  32. }
  33. else
  34. part.push_back (c);
  35. }
  36. if (! part.empty())
  37. parts.push_back (part);
  38. return parts;
  39. }
  40. std::string channelUser (const ChannelDef& c)
  41. {
  42. return c.instrument == InstrumentKind::audioClip ? "Audio clip: " + c.name : c.name;
  43. }
  44. bool usesChannelSample (InstrumentKind kind)
  45. {
  46. return kind == InstrumentKind::sampler || kind == InstrumentKind::audioClip || kind == InstrumentKind::granulizer
  47. || kind == InstrumentKind::threeosc;
  48. }
  49. template <typename Fn>
  50. void forEachEffectSlot (std::vector<EffectSlot>& chain, Fn&& fn)
  51. {
  52. for (auto& slot : chain)
  53. {
  54. fn (slot);
  55. for (auto& branch : slot.branches)
  56. forEachEffectSlot (branch.chain, fn);
  57. }
  58. }
  59. } // namespace
  60. std::string pathFileName (const std::string& path)
  61. {
  62. auto end = path.size();
  63. while (end > 0 && isSeparator (path[end - 1]))
  64. --end;
  65. auto start = end;
  66. while (start > 0 && ! isSeparator (path[start - 1]))
  67. --start;
  68. return path.substr (start, end - start);
  69. }
  70. void addMissingFile (std::vector<MissingFile>& list, const std::string& path, const std::string& user)
  71. {
  72. if (path.empty())
  73. return;
  74. const auto key = pathKey (path);
  75. auto it = std::find_if (list.begin(), list.end(), [&] (const MissingFile& m) { return pathKey (m.path) == key; });
  76. if (it == list.end())
  77. {
  78. list.push_back ({ path, {}, {}, false });
  79. it = list.end() - 1;
  80. }
  81. if (! user.empty() && std::find (it->usedBy.begin(), it->usedBy.end(), user) == it->usedBy.end())
  82. it->usedBy.push_back (user);
  83. }
  84. std::vector<MissingFile> findMissingSamples (const Song& song, const std::function<bool (const std::string&)>& fileExists)
  85. {
  86. std::vector<MissingFile> list;
  87. if (! fileExists)
  88. return list;
  89. std::map<std::string, bool> known; // each file is checked once
  90. auto gone = [&] (const std::string& path) {
  91. if (path.empty())
  92. return false;
  93. const auto key = pathKey (path);
  94. if (const auto it = known.find (key); it != known.end())
  95. return it->second;
  96. return known[key] = ! fileExists (path);
  97. };
  98. for (const auto& c : song.channels)
  99. {
  100. if (usesChannelSample (c.instrument) && gone (c.samplePath))
  101. addMissingFile (list, c.samplePath, channelUser (c));
  102. if (c.instrument != InstrumentKind::flkeys)
  103. for (const auto& z : c.zones)
  104. if (gone (z.path))
  105. addMissingFile (list, z.path, c.name);
  106. }
  107. for (std::size_t t = 0; t < song.mixer.size(); ++t)
  108. {
  109. auto chain = song.mixer[t].effects; // (a copy: the walker takes non-const slots)
  110. const auto track = song.mixer[t].name.empty() ? (t == 0 ? std::string ("Master") : "Insert " + std::to_string (t)) : song.mixer[t].name;
  111. forEachEffectSlot (chain, [&] (EffectSlot& slot) {
  112. if (gone (slot.samplePath))
  113. addMissingFile (list, slot.samplePath, track + ": effect");
  114. });
  115. }
  116. return list;
  117. }
  118. int relocationScore (const std::string& missingPath, const std::string& candidate)
  119. {
  120. const auto a = pathParts (missingPath), b = pathParts (candidate);
  121. if (a.empty() || b.empty() || a.back() != b.back())
  122. return 0;
  123. int score = 1;
  124. for (std::size_t i = 2; i <= std::min (a.size(), b.size()) && a[a.size() - i] == b[b.size() - i]; ++i)
  125. ++score;
  126. return score;
  127. }
  128. int relocateByName (std::vector<MissingFile>& missing, const std::vector<std::string>& candidates)
  129. {
  130. std::map<std::string, std::vector<const std::string*>> byName;
  131. for (const auto& c : candidates)
  132. byName[pathKey (pathFileName (c))].push_back (&c);
  133. int found = 0;
  134. for (auto& m : missing)
  135. {
  136. if (! m.found.empty() || m.skipped)
  137. continue;
  138. const auto it = byName.find (pathKey (pathFileName (m.path)));
  139. if (it == byName.end())
  140. continue;
  141. int best = 0;
  142. for (const auto* c : it->second)
  143. if (const auto s = relocationScore (m.path, *c); s > best)
  144. {
  145. best = s;
  146. m.found = *c;
  147. }
  148. found += best > 0 ? 1 : 0;
  149. }
  150. return found;
  151. }
  152. std::vector<std::string> searchFolderForMissing (const std::vector<MissingFile>& missing, const std::string& folder,
  153. const std::function<void (const std::string&, const std::function<bool (const std::string&, bool)>&)>& listFolder,
  154. std::size_t maxEntries, const std::function<bool()>& cancelled)
  155. {
  156. std::set<std::string> wanted;
  157. for (const auto& m : missing)
  158. if (m.found.empty() && ! m.skipped)
  159. wanted.insert (pathKey (pathFileName (m.path)));
  160. std::vector<std::string> hits;
  161. if (wanted.empty() || folder.empty() || ! listFolder)
  162. return hits;
  163. std::vector<std::string> pending { folder };
  164. std::set<std::string> visited;
  165. std::size_t entries = 0;
  166. auto stop = false;
  167. while (! pending.empty() && ! stop)
  168. {
  169. const auto current = pending.back();
  170. pending.pop_back();
  171. if (! visited.insert (pathKey (current)).second)
  172. continue;
  173. listFolder (current, [&] (const std::string& path, bool isFolder) {
  174. if ((maxEntries > 0 && ++entries > maxEntries) || (cancelled && cancelled()))
  175. {
  176. stop = true;
  177. return false;
  178. }
  179. if (isFolder)
  180. pending.push_back (path);
  181. else if (wanted.count (pathKey (pathFileName (path))) != 0)
  182. hits.push_back (path);
  183. return true;
  184. });
  185. }
  186. return hits;
  187. }
  188. std::map<std::string, std::string> relocationMap (const std::vector<MissingFile>& missing)
  189. {
  190. std::map<std::string, std::string> map;
  191. for (const auto& m : missing)
  192. if (! m.found.empty() && ! m.skipped)
  193. map[m.path] = m.found;
  194. return map;
  195. }
  196. std::string relocatedPath (const std::map<std::string, std::string>& relocations, const std::string& path)
  197. {
  198. if (path.empty() || relocations.empty())
  199. return {};
  200. if (const auto it = relocations.find (path); it != relocations.end())
  201. return it->second;
  202. const auto key = pathKey (path);
  203. for (const auto& [from, to] : relocations)
  204. if (pathKey (from) == key)
  205. return to;
  206. return {};
  207. }
  208. int applyRelocations (Song& song, const std::map<std::string, std::string>& relocations)
  209. {
  210. std::map<std::string, std::string> byKey;
  211. for (const auto& [from, to] : relocations)
  212. byKey[pathKey (from)] = to;
  213. int changed = 0;
  214. auto relocate = [&] (std::string& path) {
  215. if (path.empty())
  216. return;
  217. if (const auto it = byKey.find (pathKey (path)); it != byKey.end() && it->second != path)
  218. {
  219. path = it->second;
  220. ++changed;
  221. }
  222. };
  223. for (auto& c : song.channels)
  224. {
  225. if (usesChannelSample (c.instrument))
  226. relocate (c.samplePath);
  227. if (c.instrument != InstrumentKind::flkeys)
  228. for (auto& z : c.zones)
  229. relocate (z.path);
  230. }
  231. for (auto& track : song.mixer)
  232. forEachEffectSlot (track.effects, [&] (EffectSlot& slot) { relocate (slot.samplePath); });
  233. return changed;
  234. }
  235. std::vector<std::string> parseFolderList (const std::string& text)
  236. {
  237. std::vector<std::string> folders;
  238. std::set<std::string> seen;
  239. std::string line;
  240. auto flush = [&] {
  241. while (! line.empty() && (line.back() == '\r' || line.back() == ' ' || line.back() == '\t'))
  242. line.pop_back();
  243. const auto start = line.find_first_not_of (" \t");
  244. line = start == std::string::npos ? std::string() : line.substr (start);
  245. if (! line.empty() && seen.insert (pathKey (line)).second)
  246. folders.push_back (line);
  247. line.clear();
  248. };
  249. for (const auto c : text)
  250. {
  251. if (c == '\n')
  252. flush();
  253. else
  254. line.push_back (c);
  255. }
  256. flush();
  257. return folders;
  258. }
  259. std::string folderListText (const std::vector<std::string>& folders)
  260. {
  261. std::string text;
  262. for (const auto& f : folders)
  263. text += f + "\n";
  264. return text;
  265. }
  266. void rememberFolder (std::vector<std::string>& folders, const std::string& folder, std::size_t limit)
  267. {
  268. if (folder.empty())
  269. return;
  270. const auto key = pathKey (folder);
  271. folders.erase (std::remove_if (folders.begin(), folders.end(), [&] (const std::string& f) { return pathKey (f) == key; }), folders.end());
  272. folders.insert (folders.begin(), folder);
  273. if (limit > 0 && folders.size() > limit)
  274. folders.resize (limit);
  275. }
  276. } // namespace lynx