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.

113 lines
2.7KB

  1. #include <plugin/Plugin.hpp>
  2. #include <plugin/Model.hpp>
  3. #include <plugin.hpp>
  4. #include <string.hpp>
  5. namespace rack {
  6. namespace plugin {
  7. Plugin::~Plugin() {
  8. for (Model *model : models) {
  9. delete model;
  10. }
  11. }
  12. void Plugin::addModel(Model *model) {
  13. // Check that the model is not added to a plugin already
  14. assert(!model->plugin);
  15. // Check model slug
  16. if (!isSlugValid(model->slug)) {
  17. throw UserException(string::f("Module slug \"%s\" is invalid", model->slug.c_str()));
  18. }
  19. model->plugin = this;
  20. models.push_back(model);
  21. }
  22. Model *Plugin::getModel(std::string slug) {
  23. slug = normalizeSlug(slug);
  24. for (Model *model : models) {
  25. if (model->slug == slug) {
  26. return model;
  27. }
  28. }
  29. return NULL;
  30. }
  31. void Plugin::fromJson(json_t *rootJ) {
  32. json_t *slugJ = json_object_get(rootJ, "slug");
  33. if (slugJ)
  34. slug = json_string_value(slugJ);
  35. json_t *versionJ = json_object_get(rootJ, "version");
  36. if (versionJ)
  37. version = json_string_value(versionJ);
  38. json_t *nameJ = json_object_get(rootJ, "name");
  39. if (nameJ)
  40. name = json_string_value(nameJ);
  41. json_t *brandJ = json_object_get(rootJ, "brand");
  42. if (brandJ)
  43. brand = json_string_value(brandJ);
  44. // Use name for brand name by default
  45. if (brand == "")
  46. brand = name;
  47. json_t *authorJ = json_object_get(rootJ, "author");
  48. if (authorJ)
  49. author = json_string_value(authorJ);
  50. json_t *licenseJ = json_object_get(rootJ, "license");
  51. if (licenseJ)
  52. license = json_string_value(licenseJ);
  53. json_t *authorEmailJ = json_object_get(rootJ, "authorEmail");
  54. if (authorEmailJ)
  55. authorEmail = json_string_value(authorEmailJ);
  56. json_t *pluginUrlJ = json_object_get(rootJ, "pluginUrl");
  57. if (pluginUrlJ)
  58. pluginUrl = json_string_value(pluginUrlJ);
  59. json_t *authorUrlJ = json_object_get(rootJ, "authorUrl");
  60. if (authorUrlJ)
  61. authorUrl = json_string_value(authorUrlJ);
  62. json_t *manualUrlJ = json_object_get(rootJ, "manualUrl");
  63. if (manualUrlJ)
  64. manualUrl = json_string_value(manualUrlJ);
  65. json_t *sourceUrlJ = json_object_get(rootJ, "sourceUrl");
  66. if (sourceUrlJ)
  67. sourceUrl = json_string_value(sourceUrlJ);
  68. json_t *donateUrlJ = json_object_get(rootJ, "donateUrl");
  69. if (donateUrlJ)
  70. donateUrl = json_string_value(donateUrlJ);
  71. json_t *modulesJ = json_object_get(rootJ, "modules");
  72. if (modulesJ) {
  73. size_t moduleId;
  74. json_t *moduleJ;
  75. json_array_foreach(modulesJ, moduleId, moduleJ) {
  76. json_t *modelSlugJ = json_object_get(moduleJ, "slug");
  77. if (!modelSlugJ)
  78. continue;
  79. std::string modelSlug = json_string_value(modelSlugJ);
  80. Model *model = getModel(modelSlug);
  81. if (!model) {
  82. throw UserException(string::f("plugin.json of \"%s\" contains module \"%s\" but it is not defined in the plugin", slug.c_str(), modelSlug.c_str()));
  83. }
  84. model->fromJson(moduleJ);
  85. }
  86. }
  87. }
  88. } // namespace plugin
  89. } // namespace rack