load_plugin_src.cxx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Filename: load_plugin_src.cxx
  2. // Created by: drose (19Jun09)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) Carnegie Mellon University. All rights reserved.
  8. //
  9. // All use of this software is subject to the terms of the revised BSD
  10. // license. You should have received a copy of this license along
  11. // with this source code in a file named "LICENSE."
  12. //
  13. ////////////////////////////////////////////////////////////////////
  14. // This code is used in the plugin_standalone directory, and also in
  15. // the plugin_npapi directory. To facilitate that code re-use with
  16. // minimal structural overhead, it is designed to be simply #included
  17. // into the different source files.
  18. #ifndef _WIN32
  19. #include <dlfcn.h>
  20. #endif
  21. #ifdef _WIN32
  22. static const string dll_ext = ".dll";
  23. #elif defined(__APPLE__)
  24. static const string dll_ext = ".dylib";
  25. #else
  26. static const string dll_ext = ".so";
  27. #endif
  28. static const string default_plugin_filename = "libp3d_plugin";
  29. P3D_initialize_func *P3D_initialize;
  30. P3D_free_string_func *P3D_free_string;
  31. P3D_create_instance_func *P3D_create_instance;
  32. P3D_instance_finish_func *P3D_instance_finish;
  33. P3D_instance_setup_window_func *P3D_instance_setup_window;
  34. P3D_instance_has_property_func *P3D_instance_has_property;
  35. P3D_instance_get_property_func *P3D_instance_get_property;
  36. P3D_instance_set_property_func *P3D_instance_set_property;
  37. P3D_instance_get_request_func *P3D_instance_get_request;
  38. P3D_check_request_func *P3D_check_request;
  39. P3D_request_finish_func *P3D_request_finish;
  40. P3D_instance_feed_url_stream_func *P3D_instance_feed_url_stream;
  41. #ifdef _WIN32
  42. static HMODULE module;
  43. #endif
  44. static void
  45. unload_plugin() {
  46. #ifdef _WIN32
  47. FreeLibrary(module);
  48. module = NULL;
  49. #else
  50. // TODO: unload_dso
  51. #endif
  52. P3D_initialize = NULL;
  53. P3D_free_string = NULL;
  54. P3D_create_instance = NULL;
  55. P3D_instance_finish = NULL;
  56. P3D_instance_has_property = NULL;
  57. P3D_instance_get_property = NULL;
  58. P3D_instance_set_property = NULL;
  59. P3D_instance_get_request = NULL;
  60. P3D_check_request = NULL;
  61. P3D_request_finish = NULL;
  62. P3D_instance_feed_url_stream = NULL;
  63. }
  64. static bool
  65. load_plugin(const string &p3d_plugin_filename) {
  66. string filename = p3d_plugin_filename;
  67. if (filename.empty()) {
  68. // Look for the plugin along the path.
  69. filename = default_plugin_filename + dll_ext;
  70. }
  71. #ifdef _WIN32
  72. module = LoadLibrary(filename.c_str());
  73. if (module == NULL) {
  74. // Couldn't load the DLL.
  75. return false;
  76. }
  77. // Now get all of the function pointers.
  78. P3D_initialize = (P3D_initialize_func *)GetProcAddress(module, "P3D_initialize");
  79. P3D_free_string = (P3D_free_string_func *)GetProcAddress(module, "P3D_free_string");
  80. P3D_create_instance = (P3D_create_instance_func *)GetProcAddress(module, "P3D_create_instance");
  81. P3D_instance_finish = (P3D_instance_finish_func *)GetProcAddress(module, "P3D_instance_finish");
  82. P3D_instance_setup_window = (P3D_instance_setup_window_func *)GetProcAddress(module, "P3D_instance_setup_window");
  83. P3D_instance_has_property = (P3D_instance_has_property_func *)GetProcAddress(module, "P3D_instance_has_property");
  84. P3D_instance_get_property = (P3D_instance_get_property_func *)GetProcAddress(module, "P3D_instance_get_property");
  85. P3D_instance_set_property = (P3D_instance_set_property_func *)GetProcAddress(module, "P3D_instance_set_property");
  86. P3D_instance_get_request = (P3D_instance_get_request_func *)GetProcAddress(module, "P3D_instance_get_request");
  87. P3D_check_request = (P3D_check_request_func *)GetProcAddress(module, "P3D_check_request");
  88. P3D_request_finish = (P3D_request_finish_func *)GetProcAddress(module, "P3D_request_finish");
  89. P3D_instance_feed_url_stream = (P3D_instance_feed_url_stream_func *)GetProcAddress(module, "P3D_instance_feed_url_stream");
  90. #else // _WIN32
  91. // Posix case.
  92. void *module = dlopen(filename.c_str(), RTLD_NOW | RTLD_LOCAL);
  93. if (module == NULL) {
  94. // Couldn't load the .so.
  95. return false;
  96. }
  97. // Now get all of the function pointers.
  98. P3D_initialize = (P3D_initialize_func *)dlsym(module, "P3D_initialize");
  99. P3D_free_string = (P3D_free_string_func *)dlsym(module, "P3D_free_string");
  100. P3D_create_instance = (P3D_create_instance_func *)dlsym(module, "P3D_create_instance");
  101. P3D_instance_finish = (P3D_instance_finish_func *)dlsym(module, "P3D_instance_finish");
  102. P3D_instance_setup_window = (P3D_instance_setup_window_func *)dlsym(module, "P3D_instance_setup_window");
  103. P3D_instance_has_property = (P3D_instance_has_property_func *)dlsym(module, "P3D_instance_has_property");
  104. P3D_instance_get_property = (P3D_instance_get_property_func *)dlsym(module, "P3D_instance_get_property");
  105. P3D_instance_set_property = (P3D_instance_set_property_func *)dlsym(module, "P3D_instance_set_property");
  106. P3D_instance_get_request = (P3D_instance_get_request_func *)dlsym(module, "P3D_instance_get_request");
  107. P3D_check_request = (P3D_check_request_func *)dlsym(module, "P3D_check_request");
  108. P3D_request_finish = (P3D_request_finish_func *)dlsym(module, "P3D_request_finish");
  109. P3D_instance_feed_url_stream = (P3D_instance_feed_url_stream_func *)dlsym(module, "P3D_instance_feed_url_stream");
  110. #endif // _WIN32
  111. // Ensure that all of the function pointers have been found.
  112. if (P3D_initialize == NULL ||
  113. P3D_free_string == NULL ||
  114. P3D_create_instance == NULL ||
  115. P3D_instance_finish == NULL ||
  116. P3D_instance_setup_window == NULL ||
  117. P3D_instance_has_property == NULL ||
  118. P3D_instance_get_property == NULL ||
  119. P3D_instance_set_property == NULL ||
  120. P3D_instance_get_request == NULL ||
  121. P3D_check_request == NULL ||
  122. P3D_request_finish == NULL ||
  123. P3D_instance_feed_url_stream == NULL) {
  124. return false;
  125. }
  126. // Successfully loaded.
  127. #ifdef _WIN32
  128. string logfilename = "c:/cygwin/home/drose/t0.log";
  129. #else
  130. string logfilename = "/Users/drose/t0.log";
  131. #endif
  132. if (!P3D_initialize(P3D_API_VERSION, logfilename.c_str())) {
  133. // Oops, failure to initialize.
  134. unload_plugin();
  135. return false;
  136. }
  137. return true;
  138. }