Skip to content

Commit

Permalink
Add metadata system for store
Browse files Browse the repository at this point in the history
  • Loading branch information
timothyschoen committed Dec 5, 2024
1 parent 4ced7ea commit 8fbbf42
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 24 deletions.
26 changes: 18 additions & 8 deletions Source/Components/WelcomePanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,14 +261,14 @@ class WelcomePanel : public Component
TileType tileType = Patch;

public:
WelcomePanelTile(WelcomePanel& welcomePanel, File& patchFile, float scale, bool favourited, Image const& thumbImage = Image())
WelcomePanelTile(WelcomePanel& welcomePanel, File& patchFile, String patchAuthor, float scale, bool favourited, Image const& thumbImage = Image())
: isFavourited(favourited)
, parent(welcomePanel)
, snapshotScale(scale)
, thumbnailImageData(thumbImage)
{
tileName = patchFile.getFileNameWithoutExtension();

tileSubtitle = patchAuthor;
tileType = Library;

resized();
Expand Down Expand Up @@ -373,11 +373,14 @@ class WelcomePanel : public Component
if (patchFile.existsAsFile())
patchFile.revealToUser();
});
tileMenu.addSeparator();
tileMenu.addItem(isFavourited ? "Remove from favourites" : "Add to favourites", [this]() {
isFavourited = !isFavourited;
onFavourite(isFavourited);
});

if(parent.currentTab == Home) {
tileMenu.addSeparator();
tileMenu.addItem(isFavourited ? "Remove from favourites" : "Add to favourites", [this]() {
isFavourited = !isFavourited;
onFavourite(isFavourited);
});
}
tileMenu.addSeparator();
PopupMenu patchInfoSubMenu;
patchInfoSubMenu.addItem(String("Size: " + fileSizeDescription), false, false, nullptr);
Expand Down Expand Up @@ -877,7 +880,14 @@ class WelcomePanel : public Component
break;
}
}
auto* tile = libraryTiles.add(new WelcomePanelTile(*this, patchFile, scale, false, thumbImage));
auto metaFile = patchFile.getParentDirectory().getChildFile("meta.json");
String author;
if(metaFile.existsAsFile())
{
auto json = JSON::fromString(metaFile.loadFileAsString());
author = json["Author"].toString();
}
auto* tile = libraryTiles.add(new WelcomePanelTile(*this, patchFile, author, scale, false, thumbImage));
tile->onClick = [this, patchFile]() mutable {
if (patchFile.existsAsFile()) {
editor->pd->autosave->checkForMoreRecentAutosave(patchFile, editor, [this, patchFile]() {
Expand Down
44 changes: 28 additions & 16 deletions Source/Dialogs/PatchStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class DownloadPool : public DeletedAtShutdown
struct DownloadListener
{
virtual void downloadProgressed(hash32 hash, float progress) {};
virtual void patchDownloadCompleted(hash32 hash, bool success) {};
virtual void imageDownloadCompleted(hash32 hash, Image const& image) {};
virtual void patchDownloadCompleted(hash32 hash, File const& downloadedPatch) {};
virtual void imageDownloadCompleted(hash32 hash, Image const& downloadedImage) {};
};

~DownloadPool()
Expand Down Expand Up @@ -102,7 +102,7 @@ class DownloadPool : public DeletedAtShutdown
MessageManager::callAsync([this, hash]() mutable {
for(auto& listener : listeners)
{
listener->patchDownloadCompleted(hash, false);
listener->patchDownloadCompleted(hash, {});
}
});
return;
Expand All @@ -119,18 +119,20 @@ class DownloadPool : public DeletedAtShutdown
MemoryInputStream input(data, false);
ZipFile zip(input);

auto result = zip.uncompressTo(ProjectInfo::appDataDir.getChildFile("Patches"), true);
auto patchesDir = ProjectInfo::appDataDir.getChildFile("Patches");
auto result = zip.uncompressTo(patchesDir, true);
auto outputFile = patchesDir.getChildFile(zip.getEntry(0)->filename);

auto macOSTrash = ProjectInfo::appDataDir.getChildFile("Patches").getChildFile("__MACOSX");
if(macOSTrash.isDirectory())
{
macOSTrash.deleteRecursively();
}

MessageManager::callAsync([this, hash, result](){
MessageManager::callAsync([this, hash, outputFile](){
for(auto& listener : listeners)
{
listener->patchDownloadCompleted(hash, result.wasOk());
listener->patchDownloadCompleted(hash, outputFile);
}
});

Expand Down Expand Up @@ -258,6 +260,7 @@ class PatchInfo {
String price;
String thumbnailUrl;
String size;
String json;

PatchInfo() = default;

Expand All @@ -270,6 +273,7 @@ class PatchInfo {
description = jsonData["Description"];
price = jsonData["Price"];
thumbnailUrl = jsonData["StoreThumb"];
json = JSON::toString(jsonData, false);
}
};

Expand Down Expand Up @@ -599,26 +603,34 @@ class PatchFullDisplay : public Component, public DownloadPool::DownloadListener
DownloadPool::getInstance()->removeDownloadListener(this);
}

void downloadProgressed(hash32 hash, float progress) override {
if(hash == patchHash) {
void downloadProgressed(hash32 downloadHash, float progress) override {
if(downloadHash == patchHash) {
downloadButton.setType(LinkButton::Cancel);
downloadProgress = std::max<int>(progress * 100, 1);
repaint();
}
};

void patchDownloadCompleted(hash32 hash, bool success) override {
if(hash == patchHash) {
void patchDownloadCompleted(hash32 downloadHash, File const& downloadedPatch) override {
if(downloadHash == patchHash) {
downloadProgress = 0;
auto fileName = URL(currentPatch.download).getFileName();
auto fileNameWithoutExtension = fileName.upToLastOccurrenceOf(".", false, false);
auto isValid = downloadedPatch.isDirectory() && !downloadedPatch.isRoot();
auto* parent = viewport.getParentComponent();
if(success)
{
Dialogs::showMultiChoiceDialog(&confirmationDialog, parent, "Successfully installed " + fileNameWithoutExtension, [](int){}, { "Dismiss" }, Icons::Checkmark);
if(isValid) {
auto fileName = currentPatch.title.toLowerCase().replace(" ", "-");
auto targetLocation = downloadedPatch.getParentDirectory().getChildFile(fileName + "-" + String::toHexString(hash(currentPatch.author)));
downloadedPatch.moveFileTo(targetLocation);

auto metaFile = targetLocation.getChildFile("meta.json");
if(!metaFile.existsAsFile())
{
metaFile.replaceWithText(currentPatch.json);
}

Dialogs::showMultiChoiceDialog(&confirmationDialog, parent, "Successfully installed " + currentPatch.title, [](int){}, { "Dismiss" }, Icons::Checkmark);
}
else {
Dialogs::showMultiChoiceDialog(&confirmationDialog, parent, "Failed to install " + fileNameWithoutExtension, [](int){}, { "Dismiss" });
Dialogs::showMultiChoiceDialog(&confirmationDialog, parent, "Failed to install " + currentPatch.title, [](int){}, { "Dismiss" });
}

downloadButton.setType(LinkButton::Download);
Expand Down

0 comments on commit 8fbbf42

Please sign in to comment.