Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,10 @@ juce_add_plugin(plugdata_standalone
<dict>
<key>CFBundleIdentifier</key>
<string>com.plugdata.plugdata</string>
<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>
<key>UIFileSharingEnabled</key>
<true/>
</dict>
</plist>]]
)
Expand Down
170 changes: 169 additions & 1 deletion Source/PluginProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
#include <clocale>
#include <memory>
#include <unordered_set>

#include <juce_gui_basics/juce_gui_basics.h>
#include <juce_audio_basics/juce_audio_basics.h>
Expand Down Expand Up @@ -208,6 +209,140 @@ void PluginProcessor::doubleFlushMessageQueue()
unlockAudioThread();
}

#if JUCE_IOS
void PluginProcessor::syncDirectoryFiles(File const& sourceDir, File const& targetDir, Time lastInitTime, bool deleteIfNotExists)
{
if (!sourceDir.exists() || !targetDir.exists()) {
return;
}

// Cache frequently used values
const bool hasValidLastInitTime = lastInitTime.toMilliseconds() > 0;
const String deleteFlag = deleteIfNotExists ? "true" : "false";

// Get all source files once and build a lookup map for efficiency
auto sourceFiles = sourceDir.findChildFiles(juce::File::TypesOfFileToFind::findFiles, true);
std::unordered_set<String> sourceFileSet;
sourceFileSet.reserve(sourceFiles.size());

// Phase 1: Copy files from source to target (update newer files)
for (const auto& sourceFile : sourceFiles) {
const auto relativePath = sourceFile.getRelativePathFrom(sourceDir);
sourceFileSet.insert(relativePath.toStdString());

const auto targetFile = targetDir.getChildFile(relativePath);

// Create directory structure if needed
const auto parentDir = targetFile.getParentDirectory();
if (!parentDir.exists()) {
parentDir.createDirectory();
}

// Copy if target doesn't exist or source is newer
if (!targetFile.exists()) {
sourceFile.copyFileTo(targetFile);
juce::Logger::writeToLog("Created file: " + targetFile.getFullPathName() + ", deleteIfNotExists: " + deleteFlag);
} else {
const auto sourceModTime = sourceFile.getLastModificationTime();
const auto targetModTime = targetFile.getLastModificationTime();
if (sourceModTime > targetModTime) {
sourceFile.copyFileTo(targetFile);
juce::Logger::writeToLog("Updated file: " + targetFile.getFullPathName());
}
}
}

// Phase 2: Delete files in target that don't exist in source (only if deleteIfNotExists is true)
if (deleteIfNotExists) {
auto targetFiles = targetDir.findChildFiles(juce::File::TypesOfFileToFind::findFiles, true);

// Pre-cache directory modification times to prevent updates during deletion
HashMap<String, Time> directoryModTimes;
std::unordered_set<String> uniqueDirPaths;

for (const auto& targetFile : targetFiles) {
const auto parentPath = targetFile.getParentDirectory().getFullPathName();
if (uniqueDirPaths.find(parentPath.toStdString()) == uniqueDirPaths.end()) {
uniqueDirPaths.insert(parentPath.toStdString());
directoryModTimes.set(parentPath, targetFile.getParentDirectory().getLastModificationTime());
}
}

SmallArray<File> directoriesToCleanup;
std::unordered_set<String> cleanupDirPaths; // Prevent duplicates

for (const auto& targetFile : targetFiles) {
const auto relativePath = targetFile.getRelativePathFrom(targetDir);

// Check if file exists in source using our pre-built set
if (sourceFileSet.find(relativePath.toStdString()) == sourceFileSet.end()) {
// File doesn't exist in source - candidate for deletion
bool shouldDelete = true;

if (hasValidLastInitTime) {
const auto parentPath = targetFile.getParentDirectory().getFullPathName();
const auto dirModTime = directoryModTimes[parentPath];

if (dirModTime > lastInitTime) {
shouldDelete = false;
juce::Logger::writeToLog("Will keep file: " + targetFile.getFullPathName());
} else {
juce::Logger::writeToLog("Using directory modification time for: " + targetFile.getFullPathName() +
", dir mod time: " + dirModTime.toString(true, true));
}
}

if (shouldDelete) {
const auto parentDir = targetFile.getParentDirectory();
const auto parentPath = parentDir.getFullPathName();

targetFile.deleteFile();

// Add parent directory to cleanup list (avoid duplicates)
if (cleanupDirPaths.find(parentPath.toStdString()) == cleanupDirPaths.end()) {
cleanupDirPaths.insert(parentPath.toStdString());
directoriesToCleanup.add(parentDir);
}

juce::Logger::writeToLog("Deleted file: " + targetFile.getFullPathName() +
", creationTime: " + targetFile.getCreationTime().toString(true, true) +
", lastInitTime: " + lastInitTime.toString(true, true));
}
}
}

// Clean up empty directories (process from deepest to shallowest)
for (const auto& parentDir : directoriesToCleanup) {
auto currentDir = parentDir;
while (currentDir != targetDir) {
// Check if directory is empty (ignoring hidden files)
const auto filesInDir = currentDir.findChildFiles(juce::File::TypesOfFileToFind::findFiles, false);
bool isEmpty = true;
for (const auto& file : filesInDir) {
if (!file.isHidden()) {
isEmpty = false;
break;
}
}

if (isEmpty) {
const auto parentRelativePath = currentDir.getRelativePathFrom(targetDir);
if (!sourceDir.getChildFile(parentRelativePath).exists()) {
const auto nextParent = currentDir.getParentDirectory();
currentDir.deleteRecursively();
currentDir = nextParent;
} else {
break; // Directory exists in source, keep it
}
} else {
break; // Directory is not empty, stop
}
}
}
}
}
#endif

void PluginProcessor::initialiseFilesystem()
{
auto const& homeDir = ProjectInfo::appDataDir;
Expand Down Expand Up @@ -325,14 +460,47 @@ void PluginProcessor::initialiseFilesystem()
versionDataDir.getChildFile("Documentation").createSymbolicLink(homeDir.getChildFile("Documentation"), true);
versionDataDir.getChildFile("Extra").createSymbolicLink(homeDir.getChildFile("Extra"), true);

auto docsPatchesDir = File::getSpecialLocation(File::SpecialLocationType::userDocumentsDirectory).getChildFile("Patches");
auto docsPatchesDir = homeDir.getChildFile("UserDocuments");
docsPatchesDir.createDirectory();
if (!patchesDir.isSymbolicLink()) {
patchesDir.deleteRecursively();
} else {
patchesDir.deleteFile();
}
docsPatchesDir.createSymbolicLink(patchesDir, true);
if (ProjectInfo::isStandalone) {
// Create shared folder in Documents directory
auto sharedFiles = File::getSpecialLocation(File::SpecialLocationType::userDocumentsDirectory).getChildFile("SharedFiles");
if (!sharedFiles.exists()) {
sharedFiles.createDirectory();
}

// Read last standalone initialization time
auto timestampFile = homeDir.getChildFile(".standalone_last_init");
Time lastInitTime;
if (timestampFile.existsAsFile()) {
auto timestampString = timestampFile.loadFileAsString().trim();
if (timestampString.isNotEmpty()) {
lastInitTime = Time(timestampString.getLargeIntValue());
}
}
else {
// If the file doesn't exist, we assume this is the first run
lastInitTime = Time::getCurrentTime();
// Create the file to store the current time
timestampFile.create();
}

// Sync files both ways: documents -> shared and shared -> documents
// Pass the last init time to prevent deletion of files created by AUv3 apps after standalone was last initialized
syncDirectoryFiles(sharedFiles, docsPatchesDir, lastInitTime, true); // Copy newer files from shared to documents and delete files that don't exist there in shared
syncDirectoryFiles(docsPatchesDir, sharedFiles, lastInitTime); // Copy newer files from documents to shared


// Store current time as the last initialization time for next run...
auto currentTime = Time::getCurrentTime();
timestampFile.replaceWithText(String(currentTime.toMilliseconds()));
}
#else
versionDataDir.getChildFile("Abstractions").createSymbolicLink(homeDir.getChildFile("Abstractions"), true);
versionDataDir.getChildFile("Documentation").createSymbolicLink(homeDir.getChildFile("Documentation"), true);
Expand Down
1 change: 1 addition & 0 deletions Source/PluginProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class PluginProcessor final : public AudioProcessor
void settingsFileReloaded() override;

static void initialiseFilesystem();
static void syncDirectoryFiles(File const& sourceDir, File const& targetDir, Time lastInitTime = Time(), bool deleteIfNotExists = false);
void updateSearchPaths();

void sendMidiBuffer(int device, MidiBuffer& buffer);
Expand Down
5 changes: 5 additions & 0 deletions Source/Sidebar/DocumentationBrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,12 @@ class DocumentationBrowserUpdateThread final : public Thread
static File toolchainDir = ProjectInfo::appDataDir.getChildFile("Toolchain");
static File libraryDir = ProjectInfo::appDataDir.getChildFile("Library");

#if JUCE_IOS
static File userDocumentsDir = ProjectInfo::appDataDir.getChildFile("UserDocuments");
if (threadShouldExit() || directory == versionDataDir || directory == toolchainDir || directory == libraryDir || directory == userDocumentsDir) {
#else
if (threadShouldExit() || directory == versionDataDir || directory == toolchainDir || directory == libraryDir) {
#endif
return {};
}

Expand Down