The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
4.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. #include "../Application/jucer_Headers.h"
  14. #include "jucer_DownloadCompileEngineThread.h"
  15. #include "../LiveBuildEngine/jucer_CompileEngineDLL.h"
  16. #include "../Utility/Helpers/jucer_VersionInfo.h"
  17. //==============================================================================
  18. bool DownloadCompileEngineThread::downloadAndInstall()
  19. {
  20. DownloadCompileEngineThread d;
  21. if (d.runThread())
  22. {
  23. if (d.result.failed())
  24. return withError (d.result.getErrorMessage());
  25. return true;
  26. }
  27. if (d.cancelledByUser)
  28. return false;
  29. return withError (d.result.getErrorMessage());
  30. }
  31. DownloadCompileEngineThread::DownloadCompileEngineThread()
  32. : ThreadWithProgressWindow ("Downloading live-build engine", true, true),
  33. result (Result::ok()), cancelledByUser (false)
  34. {
  35. }
  36. void DownloadCompileEngineThread::threadComplete (bool userPressedCancel)
  37. {
  38. cancelledByUser = userPressedCancel;
  39. }
  40. void DownloadCompileEngineThread::run()
  41. {
  42. setProgress (-1.0);
  43. setStatusMessage ("Downloading...");
  44. MemoryBlock zipData;
  45. result = download (zipData);
  46. if (result.failed())
  47. return;
  48. setStatusMessage ("Installing...");
  49. File installFolder = getInstallFolder();
  50. if (! installFolder.createDirectory())
  51. {
  52. result = Result::fail ("Install error: cannot create target directory");
  53. return;
  54. }
  55. result = install (zipData, installFolder);
  56. }
  57. Result DownloadCompileEngineThread::download (MemoryBlock& dest)
  58. {
  59. auto info = VersionInfo::fetchFromUpdateServer (ProjectInfo::versionString);
  60. if (info == nullptr)
  61. return Result::fail ("Download error: cannot communicate with server");
  62. auto requiredAssetName = []
  63. {
  64. String name ("JUCECompileEngine_");
  65. #if JUCE_MAC
  66. name << "osx_";
  67. #elif JUCE_WINDOWS
  68. name << "windows_";
  69. #else
  70. jassertfalse;
  71. #endif
  72. return name + ProjectInfo::versionString + ".zip";
  73. }();
  74. for (auto& asset : info->assets)
  75. {
  76. if (asset.name == requiredAssetName)
  77. {
  78. int statusCode = 0;
  79. auto in = VersionInfo::createInputStreamForAsset (asset, statusCode);
  80. if (in == nullptr || statusCode != 200)
  81. return Result::fail ("Download error: cannot establish connection");
  82. MemoryOutputStream mo (dest, true);
  83. int64 size = in->getTotalLength();
  84. int64 bytesReceived = -1;
  85. String msg("Downloading... (123)");
  86. for (int64 pos = 0; pos < size; pos += bytesReceived)
  87. {
  88. setStatusMessage (msg.replace ("123", File::descriptionOfSizeInBytes (pos)));
  89. if (threadShouldExit())
  90. return Result::fail ("Download error: operation interrupted");
  91. bytesReceived = mo.writeFromInputStream (*in, 8192);
  92. if (bytesReceived == 0)
  93. return Result::fail ("Download error: lost connection");
  94. }
  95. return Result::ok();
  96. }
  97. }
  98. return Result::fail ("Download error: no downloads available");
  99. }
  100. Result DownloadCompileEngineThread::install (const MemoryBlock& data, File& targetFolder)
  101. {
  102. MemoryInputStream input (data, false);
  103. ZipFile zip (input);
  104. if (zip.getNumEntries() == 0)
  105. return Result::fail ("Install error: downloaded file is corrupt");
  106. if (threadShouldExit())
  107. return Result::fail ("Install error: operation interrupted");
  108. return zip.uncompressTo (targetFolder);
  109. }
  110. File DownloadCompileEngineThread::getInstallFolder()
  111. {
  112. return CompileEngineDLL::getVersionedUserAppSupportFolder();
  113. }
  114. bool DownloadCompileEngineThread::withError(const String& msg)
  115. {
  116. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  117. "Download and install", msg);
  118. return false;
  119. }