#include "core/MissingFiles.h" #include #include #include 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 pathParts (const std::string& path) { std::vector 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 void forEachEffectSlot (std::vector& 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& 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 findMissingSamples (const Song& song, const std::function& fileExists) { std::vector list; if (! fileExists) return list; std::map 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& missing, const std::vector& candidates) { std::map> 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 searchFolderForMissing (const std::vector& missing, const std::string& folder, const std::function&)>& listFolder, std::size_t maxEntries, const std::function& cancelled) { std::set wanted; for (const auto& m : missing) if (m.found.empty() && ! m.skipped) wanted.insert (pathKey (pathFileName (m.path))); std::vector hits; if (wanted.empty() || folder.empty() || ! listFolder) return hits; std::vector pending { folder }; std::set 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 relocationMap (const std::vector& missing) { std::map 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& 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& relocations) { std::map 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 parseFolderList (const std::string& text) { std::vector folders; std::set 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& folders) { std::string text; for (const auto& f : folders) text += f + "\n"; return text; } void rememberFolder (std::vector& 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