11#include < random>
22
33
4+ class PatchInfo {
5+ public:
6+ String title;
7+ String author;
8+ String releaseDate;
9+ String download;
10+ String description;
11+ String price;
12+ String thumbnailUrl;
13+ String size;
14+ String json;
15+
16+ PatchInfo () = default ;
17+
18+ PatchInfo (var const & jsonData)
19+ {
20+ title = jsonData[" Title" ];
21+ author = jsonData[" Author" ];
22+ releaseDate = jsonData[" Release date" ];
23+ download = jsonData[" Download" ];
24+ description = jsonData[" Description" ];
25+ price = jsonData[" Price" ];
26+ thumbnailUrl = jsonData[" StoreThumb" ];
27+ json = JSON::toString (jsonData, false );
28+ }
29+ };
30+
431class DownloadPool : public DeletedAtShutdown
532{
633public:
734 struct DownloadListener
835 {
936 virtual void downloadProgressed (hash32 hash, float progress) {};
10- virtual void patchDownloadCompleted (hash32 hash, File const & downloadedPatch ) {};
37+ virtual void patchDownloadCompleted (hash32 hash, bool success ) {};
1138 virtual void imageDownloadCompleted (hash32 hash, Image const & downloadedImage) {};
1239 };
1340
@@ -73,14 +100,14 @@ class DownloadPool : public DeletedAtShutdown
73100 });
74101 }
75102
76- void downloadPatch (hash32 hash, URL location )
103+ void downloadPatch (hash32 downloadHash, PatchInfo const & info )
77104 {
78- patchPool.addJob ([this , hash, location ](){
105+ patchPool.addJob ([this , downloadHash, info ](){
79106 MemoryBlock data;
80107
81108 int statusCode = 0 ;
82109
83- auto instream = location .createInputStream (URL::InputStreamOptions (URL::ParameterHandling::inAddress)
110+ auto instream = URL (info. download ) .createInputStream (URL::InputStreamOptions (URL::ParameterHandling::inAddress)
84111 .withConnectionTimeoutMs (10000 ).withStatusCode (&statusCode));
85112 int64 totalBytes = instream->getTotalLength ();
86113 int64 bytesDownloaded = 0 ;
@@ -97,21 +124,21 @@ class DownloadPool : public DeletedAtShutdown
97124
98125 float progress = static_cast <long double >(bytesDownloaded) / static_cast <long double >(totalBytes);
99126
100- if (cancelledDownloads.contains (hash )) {
101- cancelledDownloads.erase (hash );
102- MessageManager::callAsync ([this , hash ]() mutable {
127+ if (cancelledDownloads.contains (downloadHash )) {
128+ cancelledDownloads.erase (downloadHash );
129+ MessageManager::callAsync ([this , downloadHash ]() mutable {
103130 for (auto & listener : listeners)
104131 {
105- listener->patchDownloadCompleted (hash, {} );
132+ listener->patchDownloadCompleted (downloadHash, false );
106133 }
107134 });
108135 return ;
109136 }
110137
111- MessageManager::callAsync ([this , hash , progress]() mutable {
138+ MessageManager::callAsync ([this , downloadHash , progress]() mutable {
112139 for (auto & listener : listeners)
113140 {
114- listener->downloadProgressed (hash , progress);
141+ listener->downloadProgressed (downloadHash , progress);
115142 }
116143 });
117144 }
@@ -121,18 +148,28 @@ class DownloadPool : public DeletedAtShutdown
121148
122149 auto patchesDir = ProjectInfo::appDataDir.getChildFile (" Patches" );
123150 auto result = zip.uncompressTo (patchesDir, true );
124- auto outputFile = patchesDir.getChildFile (zip.getEntry (0 )->filename );
151+ auto downloadedPatch = patchesDir.getChildFile (zip.getEntry (0 )->filename );
152+
153+ auto fileName = info.title .toLowerCase ().replace (" " , " -" );
154+ auto targetLocation = downloadedPatch.getParentDirectory ().getChildFile (fileName + " -" + String::toHexString (hash (info.author )));
155+ downloadedPatch.moveFileTo (targetLocation);
156+
157+ auto metaFile = targetLocation.getChildFile (" meta.json" );
158+ if (!metaFile.existsAsFile ())
159+ {
160+ metaFile.replaceWithText (info.json );
161+ }
125162
126163 auto macOSTrash = ProjectInfo::appDataDir.getChildFile (" Patches" ).getChildFile (" __MACOSX" );
127164 if (macOSTrash.isDirectory ())
128165 {
129166 macOSTrash.deleteRecursively ();
130167 }
131168
132- MessageManager::callAsync ([this , hash, outputFile ](){
169+ MessageManager::callAsync ([this , downloadHash, result ](){
133170 for (auto & listener : listeners)
134171 {
135- listener->patchDownloadCompleted (hash, outputFile );
172+ listener->patchDownloadCompleted (downloadHash, result. wasOk () );
136173 }
137174 });
138175
@@ -250,32 +287,6 @@ class OnlineImage : public Component, public DownloadPool::DownloadListener
250287 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OnlineImage)
251288};
252289
253- class PatchInfo {
254- public:
255- String title;
256- String author;
257- String releaseDate;
258- String download;
259- String description;
260- String price;
261- String thumbnailUrl;
262- String size;
263- String json;
264-
265- PatchInfo () = default ;
266-
267- PatchInfo (var const & jsonData)
268- {
269- title = jsonData[" Title" ];
270- author = jsonData[" Author" ];
271- releaseDate = jsonData[" Release date" ];
272- download = jsonData[" Download" ];
273- description = jsonData[" Description" ];
274- price = jsonData[" Price" ];
275- thumbnailUrl = jsonData[" StoreThumb" ];
276- json = JSON::toString (jsonData, false );
277- }
278- };
279290
280291class PatchDisplay : public Component {
281292public:
@@ -580,7 +591,7 @@ class PatchFullDisplay : public Component, public DownloadPool::DownloadListener
580591 auto fileName = URL (currentPatch.download ).getFileName ();
581592 if (fileName.endsWith (" .zip" ) || fileName.endsWith (" .plugdata" )) {
582593 downloadProgress = 1 ;
583- DownloadPool::getInstance ()->downloadPatch (patchHash, currentPatch. download );
594+ DownloadPool::getInstance ()->downloadPatch (patchHash, currentPatch);
584595 }
585596 else {
586597 URL (currentPatch.download ).launchInDefaultBrowser ();
@@ -611,22 +622,11 @@ class PatchFullDisplay : public Component, public DownloadPool::DownloadListener
611622 }
612623 };
613624
614- void patchDownloadCompleted (hash32 downloadHash, File const & downloadedPatch ) override {
625+ void patchDownloadCompleted (hash32 downloadHash, bool success ) override {
615626 if (downloadHash == patchHash) {
616627 downloadProgress = 0 ;
617- auto isValid = downloadedPatch.isDirectory () && !downloadedPatch.isRoot ();
618628 auto * parent = viewport.getParentComponent ();
619- if (isValid) {
620- auto fileName = currentPatch.title .toLowerCase ().replace (" " , " -" );
621- auto targetLocation = downloadedPatch.getParentDirectory ().getChildFile (fileName + " -" + String::toHexString (hash (currentPatch.author )));
622- downloadedPatch.moveFileTo (targetLocation);
623-
624- auto metaFile = targetLocation.getChildFile (" meta.json" );
625- if (!metaFile.existsAsFile ())
626- {
627- metaFile.replaceWithText (currentPatch.json );
628- }
629-
629+ if (success) {
630630 Dialogs::showMultiChoiceDialog (&confirmationDialog, parent, " Successfully installed " + currentPatch.title , [](int ){}, { " Dismiss" }, Icons::Checkmark);
631631 }
632632 else {
0 commit comments