MissingFiles.h 4.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #pragma once
  2. // Missing files of an opened project (like FL Studio's "missing samples" message, interpreted): which samples a
  3. // project uses that aren't on this PC, where they might be now (a search by file name in folders the user picks),
  4. // and how to point the project at the found copies. Does not depend on JUCE so it can be tested in isolation:
  5. // the file system is reached through the callbacks the caller passes.
  6. #include <functional>
  7. #include <map>
  8. #include <string>
  9. #include <vector>
  10. #include "core/Song.h"
  11. namespace lynx
  12. {
  13. // One file a project references that doesn't exist on this PC.
  14. struct MissingFile
  15. {
  16. std::string path; // as the project saved it (FL's variables already resolved)
  17. std::vector<std::string> usedBy; // "Kick", "Audio clip: Vocals", "Insert 3: Newtone" (English, for display)
  18. std::string found; // the copy picked on this PC ("" = still missing)
  19. bool skipped = false; // the user chose to leave it missing
  20. };
  21. // A plugin a project uses that isn't installed (or wasn't scanned) here, and what LYNX put in its place.
  22. struct MissingPlugin
  23. {
  24. std::string name;
  25. std::vector<std::string> usedBy;
  26. std::string replacement; // English, for display ("Built-in synth", "Removed from the mixer"...); may hold "{0}"
  27. std::string replacementArg; // what "{0}" stands for (a LYNX substitute's effect name), not translated
  28. };
  29. // The last component of a path saved on Windows or elsewhere ('\\' and '/' both separate).
  30. std::string pathFileName (const std::string& path);
  31. // Adds `user` to the entry for `path` (one entry per path, case-insensitive on the separators and letters).
  32. void addMissingFile (std::vector<MissingFile>& list, const std::string& path, const std::string& user);
  33. // The user samples a .lynx song references that `fileExists` says are gone: channel samples (sampler, audio clip,
  34. // Granulizer, 3x Osc custom wave), sampler zones (not FL Keys' installation wavetables) and effect samples
  35. // (Newtone/Newtime). FL Studio installation data some engines keep in samplePath (DLLs, wave folders) is left out.
  36. std::vector<MissingFile> findMissingSamples (const Song& song, const std::function<bool (const std::string&)>& fileExists);
  37. // How well a found file matches a missing path: 0 = different file name; otherwise 1 + the number of trailing folder
  38. // names they share ("Drums\Kicks\kick.wav" vs ".../Kicks/kick.wav" = 2). Case-insensitive.
  39. int relocationScore (const std::string& missingPath, const std::string& candidate);
  40. // For each missing file not yet found or skipped, picks the best candidate by `relocationScore` (ties: the first).
  41. // Returns how many were newly found.
  42. int relocateByName (std::vector<MissingFile>& missing, const std::vector<std::string>& candidates);
  43. // Lists, recursively, the files under `folder` whose names match a missing file (the search a dialog runs).
  44. // `listFolder(folder, onEntry)` reports each entry as (path, isFolder); `onEntry` returning false stops the walk.
  45. // `maxEntries` bounds the walk (0 = no bound); `cancelled` is polled between entries.
  46. std::vector<std::string> searchFolderForMissing (const std::vector<MissingFile>& missing, const std::string& folder,
  47. const std::function<void (const std::string&, const std::function<bool (const std::string&, bool)>&)>& listFolder,
  48. std::size_t maxEntries = 0, const std::function<bool()>& cancelled = {});
  49. // original path → found path, for the files that were found.
  50. std::map<std::string, std::string> relocationMap (const std::vector<MissingFile>& missing);
  51. // The new place of `path` in a relocation map (same file whatever the case or separators), "" when not relocated.
  52. std::string relocatedPath (const std::map<std::string, std::string>& relocations, const std::string& path);
  53. // Points every reference to a relocated file at its new place (channel samples, zones, effect samples, also inside
  54. // parallel containers); a channel whose sampler had lost its sample keeps its kind. Returns how many references changed.
  55. int applyRelocations (Song& song, const std::map<std::string, std::string>& relocations);
  56. // The search folders list as saved (one folder per line): duplicates and empty lines dropped, order kept.
  57. std::vector<std::string> parseFolderList (const std::string& text);
  58. std::string folderListText (const std::vector<std::string>& folders);
  59. // Adds `folder` at the front (most recent first), keeping at most `limit` entries.
  60. void rememberFolder (std::vector<std::string>& folders, const std::string& folder, std::size_t limit = 20);
  61. } // namespace lynx