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.

plugin.cpp 12KB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. #include "plugin.hpp"
  2. #include "app.hpp"
  3. #include "asset.hpp"
  4. #include "util/request.hpp"
  5. #include "osdialog.h"
  6. #include <stdio.h>
  7. #include <assert.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <sys/param.h> // for MAXPATHLEN
  13. #include <fcntl.h>
  14. #include <thread>
  15. #include <stdexcept>
  16. #define ZIP_STATIC
  17. #include <zip.h>
  18. #include <jansson.h>
  19. #if ARCH_WIN
  20. #include <windows.h>
  21. #include <direct.h>
  22. #define mkdir(_dir, _perms) _mkdir(_dir)
  23. #else
  24. #include <dlfcn.h>
  25. #endif
  26. #include <dirent.h>
  27. namespace rack {
  28. std::list<Plugin*> gPlugins;
  29. std::string gToken;
  30. static bool isDownloading = false;
  31. static float downloadProgress = 0.0;
  32. static std::string downloadName;
  33. static std::string loginStatus;
  34. Plugin::~Plugin() {
  35. for (Model *model : models) {
  36. delete model;
  37. }
  38. }
  39. void Plugin::addModel(Model *model) {
  40. assert(!model->plugin);
  41. model->plugin = this;
  42. models.push_back(model);
  43. }
  44. ////////////////////
  45. // private API
  46. ////////////////////
  47. static bool loadPlugin(std::string path) {
  48. std::string libraryFilename;
  49. #if ARCH_LIN
  50. libraryFilename = path + "/" + "plugin.so";
  51. #elif ARCH_WIN
  52. libraryFilename = path + "/" + "plugin.dll";
  53. #elif ARCH_MAC
  54. libraryFilename = path + "/" + "plugin.dylib";
  55. #endif
  56. // Check file existence
  57. if (!systemIsFile(libraryFilename)) {
  58. warn("Plugin file %s does not exist", libraryFilename.c_str());
  59. return false;
  60. }
  61. // Load dynamic/shared library
  62. #if ARCH_WIN
  63. SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS);
  64. HINSTANCE handle = LoadLibrary(libraryFilename.c_str());
  65. SetErrorMode(0);
  66. if (!handle) {
  67. int error = GetLastError();
  68. warn("Failed to load library %s: code %d", libraryFilename.c_str(), error);
  69. return false;
  70. }
  71. #else
  72. void *handle = dlopen(libraryFilename.c_str(), RTLD_NOW);
  73. if (!handle) {
  74. warn("Failed to load library %s: %s", libraryFilename.c_str(), dlerror());
  75. return false;
  76. }
  77. #endif
  78. // Call plugin's init() function
  79. typedef void (*InitCallback)(Plugin *);
  80. InitCallback initCallback;
  81. #if ARCH_WIN
  82. initCallback = (InitCallback) GetProcAddress(handle, "init");
  83. #else
  84. initCallback = (InitCallback) dlsym(handle, "init");
  85. #endif
  86. if (!initCallback) {
  87. warn("Failed to read init() symbol in %s", libraryFilename.c_str());
  88. return false;
  89. }
  90. // Construct and initialize Plugin instance
  91. Plugin *plugin = new Plugin();
  92. plugin->path = path;
  93. plugin->handle = handle;
  94. initCallback(plugin);
  95. // Reject plugin if slug already exists
  96. Plugin *oldPlugin = pluginGetPlugin(plugin->slug);
  97. if (oldPlugin) {
  98. warn("Plugin \"%s\" is already loaded, not attempting to load it again", plugin->slug.c_str());
  99. // TODO
  100. // Fix memory leak with `plugin` here
  101. return false;
  102. }
  103. // Add plugin to list
  104. gPlugins.push_back(plugin);
  105. info("Loaded plugin %s %s from %s", plugin->slug.c_str(), plugin->version.c_str(), libraryFilename.c_str());
  106. return true;
  107. }
  108. static bool syncPlugin(std::string slug, json_t *manifestJ, bool dryRun) {
  109. // Check that "status" is "available"
  110. json_t *statusJ = json_object_get(manifestJ, "status");
  111. if (!statusJ) {
  112. return false;
  113. }
  114. std::string status = json_string_value(statusJ);
  115. if (status != "available") {
  116. return false;
  117. }
  118. // Get latest version
  119. json_t *latestVersionJ = json_object_get(manifestJ, "latestVersion");
  120. if (!latestVersionJ) {
  121. warn("Could not get latest version of plugin %s", slug.c_str());
  122. return false;
  123. }
  124. std::string latestVersion = json_string_value(latestVersionJ);
  125. // Check whether we already have a plugin with the same slug and version
  126. Plugin *plugin = pluginGetPlugin(slug);
  127. if (plugin && plugin->version == latestVersion) {
  128. return false;
  129. }
  130. json_t *nameJ = json_object_get(manifestJ, "name");
  131. std::string name;
  132. if (nameJ) {
  133. name = json_string_value(nameJ);
  134. }
  135. else {
  136. name = slug;
  137. }
  138. #if ARCH_WIN
  139. std::string arch = "win";
  140. #elif ARCH_MAC
  141. std::string arch = "mac";
  142. #elif ARCH_LIN
  143. std::string arch = "lin";
  144. #endif
  145. std::string downloadUrl;
  146. downloadUrl = gApiHost;
  147. downloadUrl += "/download";
  148. if (dryRun) {
  149. downloadUrl += "/available";
  150. }
  151. downloadUrl += "?token=" + requestEscape(gToken);
  152. downloadUrl += "&slug=" + requestEscape(slug);
  153. downloadUrl += "&version=" + requestEscape(latestVersion);
  154. downloadUrl += "&arch=" + requestEscape(arch);
  155. if (dryRun) {
  156. // Check if available
  157. json_t *availableResJ = requestJson(METHOD_GET, downloadUrl, NULL);
  158. if (!availableResJ) {
  159. warn("Could not check whether download is available");
  160. return false;
  161. }
  162. defer({
  163. json_decref(availableResJ);
  164. });
  165. json_t *successJ = json_object_get(availableResJ, "success");
  166. return json_boolean_value(successJ);
  167. }
  168. else {
  169. downloadName = name;
  170. downloadProgress = 0.0;
  171. info("Downloading plugin %s %s %s", slug.c_str(), latestVersion.c_str(), arch.c_str());
  172. // Download zip
  173. std::string pluginDest = assetLocal("plugins/" + slug + ".zip");
  174. if (!requestDownload(downloadUrl, pluginDest, &downloadProgress)) {
  175. warn("Plugin %s download was unsuccessful", slug.c_str());
  176. return false;
  177. }
  178. downloadName = "";
  179. return true;
  180. }
  181. }
  182. static void loadPlugins(std::string path) {
  183. std::string message;
  184. for (std::string pluginPath : systemListEntries(path)) {
  185. if (!systemIsDirectory(pluginPath))
  186. continue;
  187. if (!loadPlugin(pluginPath)) {
  188. message += stringf("Could not load plugin %s\n", pluginPath.c_str());
  189. }
  190. }
  191. if (!message.empty()) {
  192. message += "See log for details.";
  193. osdialog_message(OSDIALOG_WARNING, OSDIALOG_OK, message.c_str());
  194. }
  195. }
  196. /** Returns 0 if successful */
  197. static int extractZipHandle(zip_t *za, const char *dir) {
  198. int err;
  199. for (int i = 0; i < zip_get_num_entries(za, 0); i++) {
  200. zip_stat_t zs;
  201. err = zip_stat_index(za, i, 0, &zs);
  202. if (err) {
  203. warn("zip_stat_index() failed: error %d", err);
  204. return err;
  205. }
  206. int nameLen = strlen(zs.name);
  207. char path[MAXPATHLEN];
  208. snprintf(path, sizeof(path), "%s/%s", dir, zs.name);
  209. if (zs.name[nameLen - 1] == '/') {
  210. if (mkdir(path, 0755)) {
  211. if (errno != EEXIST) {
  212. warn("mkdir(%s) failed: error %d", path, errno);
  213. return errno;
  214. }
  215. }
  216. }
  217. else {
  218. zip_file_t *zf = zip_fopen_index(za, i, 0);
  219. if (!zf) {
  220. warn("zip_fopen_index() failed");
  221. return -1;
  222. }
  223. FILE *outFile = fopen(path, "wb");
  224. if (!outFile)
  225. continue;
  226. while (1) {
  227. char buffer[1<<15];
  228. int len = zip_fread(zf, buffer, sizeof(buffer));
  229. if (len <= 0)
  230. break;
  231. fwrite(buffer, 1, len, outFile);
  232. }
  233. err = zip_fclose(zf);
  234. if (err) {
  235. warn("zip_fclose() failed: error %d", err);
  236. return err;
  237. }
  238. fclose(outFile);
  239. }
  240. }
  241. return 0;
  242. }
  243. /** Returns 0 if successful */
  244. static int extractZip(const char *filename, const char *path) {
  245. int err;
  246. zip_t *za = zip_open(filename, 0, &err);
  247. if (!za) {
  248. warn("Could not open zip %s: error %d", filename, err);
  249. return err;
  250. }
  251. defer({
  252. zip_close(za);
  253. });
  254. err = extractZipHandle(za, path);
  255. return err;
  256. }
  257. static void extractPackages(std::string path) {
  258. std::string message;
  259. for (std::string packagePath : systemListEntries(path)) {
  260. if (stringExtension(packagePath) != "zip")
  261. continue;
  262. info("Extracting package %s", packagePath.c_str());
  263. // Extract package
  264. if (extractZip(packagePath.c_str(), path.c_str())) {
  265. warn("Package %s failed to extract", packagePath.c_str());
  266. message += stringf("Could not extract package %s\n", packagePath.c_str());
  267. continue;
  268. }
  269. // Remove package
  270. if (remove(packagePath.c_str())) {
  271. warn("Could not delete file %s: error %d", packagePath.c_str(), errno);
  272. }
  273. }
  274. if (!message.empty()) {
  275. osdialog_message(OSDIALOG_WARNING, OSDIALOG_OK, message.c_str());
  276. }
  277. }
  278. ////////////////////
  279. // public API
  280. ////////////////////
  281. void pluginInit(bool devMode) {
  282. tagsInit();
  283. // Load core
  284. // This function is defined in core.cpp
  285. Plugin *corePlugin = new Plugin();
  286. init(corePlugin);
  287. gPlugins.push_back(corePlugin);
  288. // Get local plugins directory
  289. std::string localPlugins = assetLocal("plugins");
  290. mkdir(localPlugins.c_str(), 0755);
  291. if (!devMode) {
  292. // Copy Fundamental package to plugins directory if folder does not exist
  293. std::string fundamentalSrc = assetGlobal("Fundamental.zip");
  294. std::string fundamentalDest = assetLocal("plugins/Fundamental.zip");
  295. std::string fundamentalDir = assetLocal("plugins/Fundamental");
  296. if (systemIsFile(fundamentalSrc) && !systemIsFile(fundamentalDest) && !systemIsDirectory(fundamentalDir)) {
  297. systemCopy(fundamentalSrc, fundamentalDest);
  298. }
  299. }
  300. // Extract packages and load plugins
  301. extractPackages(localPlugins);
  302. loadPlugins(localPlugins);
  303. }
  304. void pluginDestroy() {
  305. for (Plugin *plugin : gPlugins) {
  306. // Free library handle
  307. #if ARCH_WIN
  308. if (plugin->handle)
  309. FreeLibrary((HINSTANCE)plugin->handle);
  310. #else
  311. if (plugin->handle)
  312. dlclose(plugin->handle);
  313. #endif
  314. // For some reason this segfaults.
  315. // It might be best to let them leak anyway, because "crash on exit" issues would occur with badly-written plugins.
  316. // delete plugin;
  317. }
  318. gPlugins.clear();
  319. }
  320. bool pluginSync(bool dryRun) {
  321. if (gToken.empty())
  322. return false;
  323. bool available = false;
  324. if (!dryRun) {
  325. isDownloading = true;
  326. downloadProgress = 0.0;
  327. downloadName = "Updating plugins...";
  328. }
  329. defer({
  330. isDownloading = false;
  331. });
  332. // Get user's plugins list
  333. json_t *pluginsReqJ = json_object();
  334. json_object_set(pluginsReqJ, "token", json_string(gToken.c_str()));
  335. json_t *pluginsResJ = requestJson(METHOD_GET, gApiHost + "/plugins", pluginsReqJ);
  336. json_decref(pluginsReqJ);
  337. if (!pluginsResJ) {
  338. warn("Request for user's plugins failed");
  339. return false;
  340. }
  341. defer({
  342. json_decref(pluginsResJ);
  343. });
  344. json_t *errorJ = json_object_get(pluginsResJ, "error");
  345. if (errorJ) {
  346. warn("Request for user's plugins returned an error: %s", json_string_value(errorJ));
  347. return false;
  348. }
  349. // Get community manifests
  350. json_t *manifestsResJ = requestJson(METHOD_GET, gApiHost + "/community/manifests", NULL);
  351. if (!manifestsResJ) {
  352. warn("Request for community manifests failed");
  353. return false;
  354. }
  355. defer({
  356. json_decref(manifestsResJ);
  357. });
  358. // Check each plugin in list of plugin slugs
  359. json_t *pluginsJ = json_object_get(pluginsResJ, "plugins");
  360. if (!pluginsJ) {
  361. warn("No plugins array");
  362. return false;
  363. }
  364. json_t *manifestsJ = json_object_get(manifestsResJ, "manifests");
  365. if (!manifestsJ) {
  366. warn("No manifests object");
  367. return false;
  368. }
  369. size_t slugIndex;
  370. json_t *slugJ;
  371. json_array_foreach(pluginsJ, slugIndex, slugJ) {
  372. std::string slug = json_string_value(slugJ);
  373. // Search for slug in manifests
  374. const char *manifestSlug;
  375. json_t *manifestJ = NULL;
  376. json_object_foreach(manifestsJ, manifestSlug, manifestJ) {
  377. if (slug == std::string(manifestSlug))
  378. break;
  379. }
  380. if (!manifestJ)
  381. continue;
  382. if (syncPlugin(slug, manifestJ, dryRun)) {
  383. available = true;
  384. }
  385. }
  386. return available;
  387. }
  388. void pluginLogIn(std::string email, std::string password) {
  389. json_t *reqJ = json_object();
  390. json_object_set(reqJ, "email", json_string(email.c_str()));
  391. json_object_set(reqJ, "password", json_string(password.c_str()));
  392. json_t *resJ = requestJson(METHOD_POST, gApiHost + "/token", reqJ);
  393. json_decref(reqJ);
  394. if (resJ) {
  395. json_t *errorJ = json_object_get(resJ, "error");
  396. if (errorJ) {
  397. const char *errorStr = json_string_value(errorJ);
  398. loginStatus = errorStr;
  399. }
  400. else {
  401. json_t *tokenJ = json_object_get(resJ, "token");
  402. if (tokenJ) {
  403. const char *tokenStr = json_string_value(tokenJ);
  404. gToken = tokenStr;
  405. loginStatus = "";
  406. }
  407. }
  408. json_decref(resJ);
  409. }
  410. }
  411. void pluginLogOut() {
  412. gToken = "";
  413. }
  414. void pluginCancelDownload() {
  415. // TODO
  416. }
  417. bool pluginIsLoggedIn() {
  418. return gToken != "";
  419. }
  420. bool pluginIsDownloading() {
  421. return isDownloading;
  422. }
  423. float pluginGetDownloadProgress() {
  424. return downloadProgress;
  425. }
  426. std::string pluginGetDownloadName() {
  427. return downloadName;
  428. }
  429. std::string pluginGetLoginStatus() {
  430. return loginStatus;
  431. }
  432. Plugin *pluginGetPlugin(std::string pluginSlug) {
  433. for (Plugin *plugin : gPlugins) {
  434. if (plugin->slug == pluginSlug) {
  435. return plugin;
  436. }
  437. }
  438. return NULL;
  439. }
  440. Model *pluginGetModel(std::string pluginSlug, std::string modelSlug) {
  441. Plugin *plugin = pluginGetPlugin(pluginSlug);
  442. if (plugin) {
  443. for (Model *model : plugin->models) {
  444. if (model->slug == modelSlug) {
  445. return model;
  446. }
  447. }
  448. }
  449. return NULL;
  450. }
  451. } // namespace rack