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.

104 lines
2.3KB

  1. #include <stdio.h>
  2. #if defined(WINDOWS)
  3. #include <windows.h>
  4. #elif defined(LINUX) || defined(APPLE)
  5. #include <dlfcn.h>
  6. #include <glob.h>
  7. #endif
  8. #include "5V.hpp"
  9. #include "core/core.hpp"
  10. std::list<Plugin*> gPlugins;
  11. int loadPlugin(const char *path) {
  12. // Load dynamic/shared library
  13. #if defined(WINDOWS)
  14. HINSTANCE handle = LoadLibrary(path);
  15. if (!handle) {
  16. fprintf(stderr, "Failed to load library %s\n", path);
  17. return -1;
  18. }
  19. #elif defined(LINUX) || defined(APPLE)
  20. char ppath[512];
  21. snprintf(ppath, 512, "./%s", path);
  22. void *handle = dlopen(ppath, RTLD_NOW | RTLD_GLOBAL);
  23. if (!handle) {
  24. fprintf(stderr, "Failed to load library %s: %s\n", path, dlerror());
  25. return -1;
  26. }
  27. #endif
  28. // Call plugin init() function
  29. typedef Plugin *(*InitCallback)();
  30. InitCallback initCallback;
  31. #if defined(WINDOWS)
  32. initCallback = (InitCallback) GetProcAddress(handle, "init");
  33. #elif defined(LINUX) || defined(APPLE)
  34. initCallback = (InitCallback) dlsym(handle, "init");
  35. #endif
  36. if (!initCallback) {
  37. fprintf(stderr, "Failed to read init() symbol in %s\n", path);
  38. return -2;
  39. }
  40. // Add plugin to map
  41. Plugin *plugin = initCallback();
  42. if (!plugin) {
  43. fprintf(stderr, "Library %s did not return a plugin\n", path);
  44. return -3;
  45. }
  46. gPlugins.push_back(plugin);
  47. fprintf(stderr, "Loaded plugin %s\n", path);
  48. return 0;
  49. }
  50. void pluginInit() {
  51. // Load core
  52. Plugin *corePlugin = coreInit();
  53. gPlugins.push_back(corePlugin);
  54. // Search for plugin libraries
  55. #if defined(WINDOWS)
  56. // WIN32_FIND_DATA ffd;
  57. // HANDLE hFind = FindFirstFile("plugins/*/plugin.dll", &ffd);
  58. // if (hFind != INVALID_HANDLE_VALUE) {
  59. // do {
  60. // loadPlugin(ffd.cFileName);
  61. // } while (FindNextFile(hFind, &ffd));
  62. // }
  63. // FindClose(hFind);
  64. loadPlugin("plugins/Simple/plugin.dll");
  65. loadPlugin("plugins/AudibleInstruments/plugin.dll");
  66. #elif defined(LINUX) || defined(APPLE)
  67. #if defined(LINUX)
  68. const char *globPath = "plugins/*/plugin.so";
  69. #elif defined(APPLE)
  70. const char *globPath = "plugins/*/plugin.dylib";
  71. #endif
  72. glob_t result;
  73. glob(globPath, GLOB_TILDE, NULL, &result);
  74. for (int i = 0; i < (int) result.gl_pathc; i++) {
  75. loadPlugin(result.gl_pathv[i]);
  76. }
  77. globfree(&result);
  78. #endif
  79. }
  80. void pluginDestroy() {
  81. for (Plugin *plugin : gPlugins) {
  82. delete plugin;
  83. }
  84. gPlugins.clear();
  85. }
  86. Plugin::~Plugin() {
  87. for (Model *model : models) {
  88. delete model;
  89. }
  90. }