| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- #include "core/MissingFiles.h"
- #include <algorithm>
- #include <cctype>
- #include <set>
- namespace lynx
- {
- namespace
- {
- bool isSeparator (char c) { return c == '\\' || c == '/'; }
- // lower-case, one kind of separator, no trailing separator: two spellings of the same path compare equal
- std::string pathKey (const std::string& path)
- {
- std::string key;
- key.reserve (path.size());
- for (const auto c : path)
- key.push_back (isSeparator (c) ? '/' : (char) std::tolower ((unsigned char) c));
- while (key.size() > 1 && key.back() == '/')
- key.pop_back();
- return key;
- }
- std::vector<std::string> pathParts (const std::string& path)
- {
- std::vector<std::string> parts;
- std::string part;
- for (const auto c : pathKey (path))
- {
- if (c == '/')
- {
- if (! part.empty())
- parts.push_back (part);
- part.clear();
- }
- else
- part.push_back (c);
- }
- if (! part.empty())
- parts.push_back (part);
- return parts;
- }
- std::string channelUser (const ChannelDef& c)
- {
- return c.instrument == InstrumentKind::audioClip ? "Audio clip: " + c.name : c.name;
- }
- bool usesChannelSample (InstrumentKind kind)
- {
- return kind == InstrumentKind::sampler || kind == InstrumentKind::audioClip || kind == InstrumentKind::granulizer
- || kind == InstrumentKind::threeosc;
- }
- template <typename Fn>
- void forEachEffectSlot (std::vector<EffectSlot>& chain, Fn&& fn)
- {
- for (auto& slot : chain)
- {
- fn (slot);
- for (auto& branch : slot.branches)
- forEachEffectSlot (branch.chain, fn);
- }
- }
- } // namespace
- std::string pathFileName (const std::string& path)
- {
- auto end = path.size();
- while (end > 0 && isSeparator (path[end - 1]))
- --end;
- auto start = end;
- while (start > 0 && ! isSeparator (path[start - 1]))
- --start;
- return path.substr (start, end - start);
- }
- void addMissingFile (std::vector<MissingFile>& list, const std::string& path, const std::string& user)
- {
- if (path.empty())
- return;
- const auto key = pathKey (path);
- auto it = std::find_if (list.begin(), list.end(), [&] (const MissingFile& m) { return pathKey (m.path) == key; });
- if (it == list.end())
- {
- list.push_back ({ path, {}, {}, false });
- it = list.end() - 1;
- }
- if (! user.empty() && std::find (it->usedBy.begin(), it->usedBy.end(), user) == it->usedBy.end())
- it->usedBy.push_back (user);
- }
- std::vector<MissingFile> findMissingSamples (const Song& song, const std::function<bool (const std::string&)>& fileExists)
- {
- std::vector<MissingFile> list;
- if (! fileExists)
- return list;
- std::map<std::string, bool> known; // each file is checked once
- auto gone = [&] (const std::string& path) {
- if (path.empty())
- return false;
- const auto key = pathKey (path);
- if (const auto it = known.find (key); it != known.end())
- return it->second;
- return known[key] = ! fileExists (path);
- };
- for (const auto& c : song.channels)
- {
- if (usesChannelSample (c.instrument) && gone (c.samplePath))
- addMissingFile (list, c.samplePath, channelUser (c));
- if (c.instrument != InstrumentKind::flkeys)
- for (const auto& z : c.zones)
- if (gone (z.path))
- addMissingFile (list, z.path, c.name);
- }
- for (std::size_t t = 0; t < song.mixer.size(); ++t)
- {
- auto chain = song.mixer[t].effects; // (a copy: the walker takes non-const slots)
- const auto track = song.mixer[t].name.empty() ? (t == 0 ? std::string ("Master") : "Insert " + std::to_string (t)) : song.mixer[t].name;
- forEachEffectSlot (chain, [&] (EffectSlot& slot) {
- if (gone (slot.samplePath))
- addMissingFile (list, slot.samplePath, track + ": effect");
- });
- }
- return list;
- }
- int relocationScore (const std::string& missingPath, const std::string& candidate)
- {
- const auto a = pathParts (missingPath), b = pathParts (candidate);
- if (a.empty() || b.empty() || a.back() != b.back())
- return 0;
- int score = 1;
- for (std::size_t i = 2; i <= std::min (a.size(), b.size()) && a[a.size() - i] == b[b.size() - i]; ++i)
- ++score;
- return score;
- }
- int relocateByName (std::vector<MissingFile>& missing, const std::vector<std::string>& candidates)
- {
- std::map<std::string, std::vector<const std::string*>> byName;
- for (const auto& c : candidates)
- byName[pathKey (pathFileName (c))].push_back (&c);
- int found = 0;
- for (auto& m : missing)
- {
- if (! m.found.empty() || m.skipped)
- continue;
- const auto it = byName.find (pathKey (pathFileName (m.path)));
- if (it == byName.end())
- continue;
- int best = 0;
- for (const auto* c : it->second)
- if (const auto s = relocationScore (m.path, *c); s > best)
- {
- best = s;
- m.found = *c;
- }
- found += best > 0 ? 1 : 0;
- }
- return found;
- }
- std::vector<std::string> searchFolderForMissing (const std::vector<MissingFile>& missing, const std::string& folder,
- const std::function<void (const std::string&, const std::function<bool (const std::string&, bool)>&)>& listFolder,
- std::size_t maxEntries, const std::function<bool()>& cancelled)
- {
- std::set<std::string> wanted;
- for (const auto& m : missing)
- if (m.found.empty() && ! m.skipped)
- wanted.insert (pathKey (pathFileName (m.path)));
- std::vector<std::string> hits;
- if (wanted.empty() || folder.empty() || ! listFolder)
- return hits;
- std::vector<std::string> pending { folder };
- std::set<std::string> visited;
- std::size_t entries = 0;
- auto stop = false;
- while (! pending.empty() && ! stop)
- {
- const auto current = pending.back();
- pending.pop_back();
- if (! visited.insert (pathKey (current)).second)
- continue;
- listFolder (current, [&] (const std::string& path, bool isFolder) {
- if ((maxEntries > 0 && ++entries > maxEntries) || (cancelled && cancelled()))
- {
- stop = true;
- return false;
- }
- if (isFolder)
- pending.push_back (path);
- else if (wanted.count (pathKey (pathFileName (path))) != 0)
- hits.push_back (path);
- return true;
- });
- }
- return hits;
- }
- std::map<std::string, std::string> relocationMap (const std::vector<MissingFile>& missing)
- {
- std::map<std::string, std::string> map;
- for (const auto& m : missing)
- if (! m.found.empty() && ! m.skipped)
- map[m.path] = m.found;
- return map;
- }
- std::string relocatedPath (const std::map<std::string, std::string>& relocations, const std::string& path)
- {
- if (path.empty() || relocations.empty())
- return {};
- if (const auto it = relocations.find (path); it != relocations.end())
- return it->second;
- const auto key = pathKey (path);
- for (const auto& [from, to] : relocations)
- if (pathKey (from) == key)
- return to;
- return {};
- }
- int applyRelocations (Song& song, const std::map<std::string, std::string>& relocations)
- {
- std::map<std::string, std::string> byKey;
- for (const auto& [from, to] : relocations)
- byKey[pathKey (from)] = to;
- int changed = 0;
- auto relocate = [&] (std::string& path) {
- if (path.empty())
- return;
- if (const auto it = byKey.find (pathKey (path)); it != byKey.end() && it->second != path)
- {
- path = it->second;
- ++changed;
- }
- };
- for (auto& c : song.channels)
- {
- if (usesChannelSample (c.instrument))
- relocate (c.samplePath);
- if (c.instrument != InstrumentKind::flkeys)
- for (auto& z : c.zones)
- relocate (z.path);
- }
- for (auto& track : song.mixer)
- forEachEffectSlot (track.effects, [&] (EffectSlot& slot) { relocate (slot.samplePath); });
- return changed;
- }
- std::vector<std::string> parseFolderList (const std::string& text)
- {
- std::vector<std::string> folders;
- std::set<std::string> seen;
- std::string line;
- auto flush = [&] {
- while (! line.empty() && (line.back() == '\r' || line.back() == ' ' || line.back() == '\t'))
- line.pop_back();
- const auto start = line.find_first_not_of (" \t");
- line = start == std::string::npos ? std::string() : line.substr (start);
- if (! line.empty() && seen.insert (pathKey (line)).second)
- folders.push_back (line);
- line.clear();
- };
- for (const auto c : text)
- {
- if (c == '\n')
- flush();
- else
- line.push_back (c);
- }
- flush();
- return folders;
- }
- std::string folderListText (const std::vector<std::string>& folders)
- {
- std::string text;
- for (const auto& f : folders)
- text += f + "\n";
- return text;
- }
- void rememberFolder (std::vector<std::string>& folders, const std::string& folder, std::size_t limit)
- {
- if (folder.empty())
- return;
- const auto key = pathKey (folder);
- folders.erase (std::remove_if (folders.begin(), folders.end(), [&] (const std::string& f) { return pathKey (f) == key; }), folders.end());
- folders.insert (folders.begin(), folder);
- if (limit > 0 && folders.size() > limit)
- folders.resize (limit);
- }
- } // namespace lynx
|