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.

379 lines
9.6KB

  1. #include <thread>
  2. #include <mutex>
  3. #include <condition_variable>
  4. #include <library.hpp>
  5. #include <settings.hpp>
  6. #include <app/common.hpp>
  7. #include <network.hpp>
  8. #include <system.hpp>
  9. #include <context.hpp>
  10. #include <window/Window.hpp>
  11. #include <asset.hpp>
  12. #include <settings.hpp>
  13. #include <plugin.hpp>
  14. namespace rack {
  15. namespace library {
  16. static std::mutex appUpdateMutex;
  17. static std::mutex updateMutex;
  18. void init() {
  19. if (settings::autoCheckUpdates && !settings::devMode) {
  20. std::thread t([&]() {
  21. system::setThreadName("Library");
  22. // checkAppUpdate();
  23. checkUpdates();
  24. });
  25. t.detach();
  26. }
  27. }
  28. void destroy() {
  29. // Wait until all library threads are finished
  30. {
  31. std::lock_guard<std::mutex> lock(appUpdateMutex);
  32. }
  33. {
  34. std::lock_guard<std::mutex> lock(updateMutex);
  35. }
  36. }
  37. void checkAppUpdate() {
  38. if (!appUpdateMutex.try_lock())
  39. return;
  40. DEFER({appUpdateMutex.unlock();});
  41. std::string versionUrl = API_URL + "/version";
  42. json_t* reqJ = json_object();
  43. json_object_set(reqJ, "edition", json_string(APP_EDITION.c_str()));
  44. DEFER({json_decref(reqJ);});
  45. json_t* resJ = network::requestJson(network::METHOD_GET, versionUrl, reqJ);
  46. if (!resJ) {
  47. WARN("Request for version failed");
  48. return;
  49. }
  50. DEFER({json_decref(resJ);});
  51. json_t* versionJ = json_object_get(resJ, "version");
  52. if (versionJ)
  53. appVersion = json_string_value(versionJ);
  54. json_t* changelogUrlJ = json_object_get(resJ, "changelogUrl");
  55. if (changelogUrlJ)
  56. appChangelogUrl = json_string_value(changelogUrlJ);
  57. json_t* downloadUrlsJ = json_object_get(resJ, "downloadUrls");
  58. if (downloadUrlsJ) {
  59. json_t* downloadUrlJ = json_object_get(downloadUrlsJ, APP_ARCH.c_str());
  60. if (downloadUrlJ)
  61. appDownloadUrl = json_string_value(downloadUrlJ);
  62. }
  63. }
  64. bool isAppUpdateAvailable() {
  65. return (appVersion != "") && (appVersion != APP_VERSION);
  66. }
  67. bool isLoggedIn() {
  68. return settings::token != "";
  69. }
  70. void logIn(std::string email, std::string password) {
  71. if (!updateMutex.try_lock())
  72. return;
  73. DEFER({updateMutex.unlock();});
  74. loginStatus = "Logging in...";
  75. json_t* reqJ = json_object();
  76. json_object_set(reqJ, "email", json_string(email.c_str()));
  77. json_object_set(reqJ, "password", json_string(password.c_str()));
  78. std::string url = API_URL + "/token";
  79. json_t* resJ = network::requestJson(network::METHOD_POST, url, reqJ);
  80. json_decref(reqJ);
  81. if (!resJ) {
  82. loginStatus = "No response from server";
  83. return;
  84. }
  85. DEFER({json_decref(resJ);});
  86. json_t* errorJ = json_object_get(resJ, "error");
  87. if (errorJ) {
  88. const char* errorStr = json_string_value(errorJ);
  89. loginStatus = errorStr;
  90. return;
  91. }
  92. json_t* tokenJ = json_object_get(resJ, "token");
  93. if (!tokenJ) {
  94. loginStatus = "No token in response";
  95. return;
  96. }
  97. const char* tokenStr = json_string_value(tokenJ);
  98. settings::token = tokenStr;
  99. loginStatus = "";
  100. refreshRequested = true;
  101. }
  102. void logOut() {
  103. settings::token = "";
  104. updateInfos.clear();
  105. }
  106. static network::CookieMap getTokenCookies() {
  107. network::CookieMap cookies;
  108. cookies["token"] = settings::token;
  109. return cookies;
  110. }
  111. void checkUpdates() {
  112. if (!updateMutex.try_lock())
  113. return;
  114. DEFER({updateMutex.unlock();});
  115. if (settings::token.empty())
  116. return;
  117. // Refuse to check for updates while updating plugins
  118. if (isSyncing)
  119. return;
  120. updateStatus = "Querying for updates...";
  121. // Check user token
  122. std::string userUrl = API_URL + "/user";
  123. json_t* userResJ = network::requestJson(network::METHOD_GET, userUrl, NULL, getTokenCookies());
  124. if (!userResJ) {
  125. DEBUG("User failed");
  126. return;
  127. }
  128. DEFER({json_decref(userResJ);});
  129. // Get user's plugins list
  130. std::string pluginsUrl = API_URL + "/plugins";
  131. json_t* pluginsResJ = network::requestJson(network::METHOD_GET, pluginsUrl, NULL, getTokenCookies());
  132. if (!pluginsResJ) {
  133. WARN("Request for user's plugins failed");
  134. updateStatus = "Could not query plugins";
  135. return;
  136. }
  137. DEFER({json_decref(pluginsResJ);});
  138. json_t* errorJ = json_object_get(pluginsResJ, "error");
  139. if (errorJ) {
  140. WARN("Request for user's plugins returned an error: %s", json_string_value(errorJ));
  141. updateStatus = "Could not query plugins";
  142. return;
  143. }
  144. // Get library manifests
  145. std::string manifestsUrl = API_URL + "/library/manifests";
  146. json_t* manifestsReq = json_object();
  147. json_object_set(manifestsReq, "version", json_string(APP_VERSION_MAJOR.c_str()));
  148. json_t* manifestsResJ = network::requestJson(network::METHOD_GET, manifestsUrl, manifestsReq);
  149. json_decref(manifestsReq);
  150. if (!manifestsResJ) {
  151. WARN("Request for library manifests failed");
  152. updateStatus = "Could not query updates";
  153. return;
  154. }
  155. DEFER({json_decref(manifestsResJ);});
  156. json_t* manifestsJ = json_object_get(manifestsResJ, "manifests");
  157. json_t* pluginsJ = json_object_get(pluginsResJ, "plugins");
  158. size_t pluginIndex;
  159. json_t* pluginJ;
  160. json_array_foreach(pluginsJ, pluginIndex, pluginJ) {
  161. // Get plugin manifest
  162. std::string slug = json_string_value(pluginJ);
  163. json_t* manifestJ = json_object_get(manifestsJ, slug.c_str());
  164. if (!manifestJ) {
  165. WARN("VCV account has plugin %s but no manifest was found", slug.c_str());
  166. continue;
  167. }
  168. // Don't replace existing UpdateInfo, even if version is newer.
  169. // This keeps things sane and ensures that only one version of each plugin is downloaded to `plugins/` at a time.
  170. auto it = updateInfos.find(slug);
  171. if (it != updateInfos.end()) {
  172. continue;
  173. }
  174. UpdateInfo update;
  175. // Get plugin name
  176. json_t* nameJ = json_object_get(manifestJ, "name");
  177. if (nameJ)
  178. update.name = json_string_value(nameJ);
  179. // Get version
  180. json_t* versionJ = json_object_get(manifestJ, "version");
  181. if (!versionJ) {
  182. // WARN("Plugin %s has no version in manifest", slug.c_str());
  183. continue;
  184. }
  185. update.version = json_string_value(versionJ);
  186. // Reject plugins with ABI mismatch
  187. if (!string::startsWith(update.version, APP_VERSION_MAJOR + ".")) {
  188. continue;
  189. }
  190. // Check if update is needed
  191. plugin::Plugin* p = plugin::getPlugin(slug);
  192. if (p && p->version == update.version)
  193. continue;
  194. // Require that plugin is available
  195. json_t* availableJ = json_object_get(manifestJ, "available");
  196. if (!json_boolean_value(availableJ))
  197. continue;
  198. // Get changelog URL
  199. json_t* changelogUrlJ = json_object_get(manifestJ, "changelogUrl");
  200. if (changelogUrlJ)
  201. update.changelogUrl = json_string_value(changelogUrlJ);
  202. // Add update to updates map
  203. updateInfos[slug] = update;
  204. }
  205. // Get module whitelist
  206. // TODO
  207. // {
  208. // std::string whitelistUrl = API_URL + "/modules";
  209. // json_t* whitelistResJ = network::requestJson(network::METHOD_GET, whitelistUrl, NULL, getTokenCookies());
  210. // if (!whitelistResJ) {
  211. // WARN("Request for module whitelist failed");
  212. // updateStatus = "Could not query updates";
  213. // return;
  214. // }
  215. // DEFER({json_decref(whitelistResJ);});
  216. // std::map<std::string, std::set<std::string>> moduleWhitelist;
  217. // json_t* pluginsJ = json_object_get(whitelistResJ, "plugins");
  218. // // Iterate plugins
  219. // const char* pluginSlug;
  220. // json_t* modulesJ;
  221. // json_object_foreach(pluginsJ, pluginSlug, modulesJ) {
  222. // // Iterate modules in plugin
  223. // size_t moduleIndex;
  224. // json_t* moduleSlugJ;
  225. // json_array_foreach(modulesJ, moduleIndex, moduleSlugJ) {
  226. // std::string moduleSlug = json_string_value(moduleSlugJ);
  227. // // Insert module in whitelist
  228. // moduleWhitelist[pluginSlug].insert(moduleSlug);
  229. // }
  230. // }
  231. // settings::moduleWhitelist = moduleWhitelist;
  232. // }
  233. updateStatus = "";
  234. refreshRequested = true;
  235. }
  236. bool hasUpdates() {
  237. for (auto& pair : updateInfos) {
  238. if (!pair.second.downloaded)
  239. return true;
  240. }
  241. return false;
  242. }
  243. void syncUpdate(std::string slug) {
  244. if (!updateMutex.try_lock())
  245. return;
  246. DEFER({updateMutex.unlock();});
  247. if (settings::token.empty())
  248. return;
  249. isSyncing = true;
  250. DEFER({isSyncing = false;});
  251. // Get the UpdateInfo object
  252. auto it = updateInfos.find(slug);
  253. if (it == updateInfos.end())
  254. return;
  255. UpdateInfo update = it->second;
  256. updateSlug = slug;
  257. DEFER({updateSlug = "";});
  258. // Set progress to 0%
  259. updateProgress = 0.f;
  260. DEFER({updateProgress = 0.f;});
  261. INFO("Downloading plugin %s v%s for %s", slug.c_str(), update.version.c_str(), APP_ARCH.c_str());
  262. // Get download URL
  263. std::string downloadUrl = API_URL + "/download";
  264. downloadUrl += "?slug=" + network::encodeUrl(slug);
  265. downloadUrl += "&version=" + network::encodeUrl(update.version);
  266. downloadUrl += "&arch=" + network::encodeUrl(APP_ARCH);
  267. // Get file path
  268. std::string packageFilename = slug + "-" + update.version + "-" + APP_ARCH + ".vcvplugin";
  269. std::string packagePath = system::join(plugin::pluginsPath, packageFilename);
  270. // Download plugin package
  271. if (!network::requestDownload(downloadUrl, packagePath, &updateProgress, getTokenCookies())) {
  272. WARN("Plugin %s download was unsuccessful", slug.c_str());
  273. return;
  274. }
  275. // updateInfos could possibly change in the checkUpdates() thread, so re-get the UpdateInfo to modify it.
  276. it = updateInfos.find(slug);
  277. if (it == updateInfos.end())
  278. return;
  279. it->second.downloaded = true;
  280. }
  281. void syncUpdates() {
  282. if (settings::token.empty())
  283. return;
  284. // updateInfos could possibly change in the checkUpdates() thread, but checkUpdates() will not execute if syncUpdate() is running, so the chance of the updateInfos map being modified while iterating is rare.
  285. auto updateInfosClone = updateInfos;
  286. for (auto& pair : updateInfosClone) {
  287. syncUpdate(pair.first);
  288. }
  289. restartRequested = true;
  290. }
  291. std::string appVersion;
  292. std::string appDownloadUrl;
  293. std::string appChangelogUrl;
  294. std::string loginStatus;
  295. std::map<std::string, UpdateInfo> updateInfos;
  296. std::string updateStatus;
  297. std::string updateSlug;
  298. float updateProgress = 0.f;
  299. bool isSyncing = false;
  300. bool restartRequested = false;
  301. bool refreshRequested = false;
  302. } // namespace library
  303. } // namespace rack