ComponentManager.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include "ComponentManager.h"
  2. #include "foundation/error.h"
  3. #include "nx/nxuri.h"
  4. int ComponentManager::AddComponent(nx_uri_t filename)
  5. {
  6. if (phase > PHASE_LOADED)
  7. return NErr_Error;
  8. HMODULE hLib = LoadLibraryW(filename->string);
  9. if (hLib)
  10. {
  11. GETCOMPONENT_FUNC pr = (GETCOMPONENT_FUNC)GetProcAddress(hLib, "GetWasabi2Component");
  12. if (pr)
  13. {
  14. ifc_component *component = pr();
  15. if (component)
  16. {
  17. if (component->component_info.wasabi_version != wasabi2_component_version
  18. || component->component_info.nx_api_version != nx_api_version
  19. || component->component_info.nx_platform_guid != nx_platform_guid)
  20. {
  21. FreeLibrary(hLib);
  22. return NErr_IncompatibleVersion;
  23. }
  24. component->component_info.hModule = hLib;
  25. component->component_info.filename = NXURIRetain(filename);
  26. int ret = component->Initialize(service_api);
  27. if (ret != NErr_Success)
  28. {
  29. NXURIRelease(component->component_info.filename);
  30. FreeLibrary(hLib);
  31. return ret;
  32. }
  33. /* if the component was added late, we'll need to run some extra stages */
  34. ret = LateLoad(component);
  35. if (ret != NErr_Success)
  36. {
  37. NXURIRelease(component->component_info.filename);
  38. FreeLibrary(hLib);
  39. return ret;
  40. }
  41. components.push_back(component);
  42. return NErr_Success;
  43. }
  44. }
  45. return NErr_Error;
  46. }
  47. else
  48. {
  49. return NErr_FileNotFound;
  50. }
  51. }
  52. int ComponentManager::AddDirectory(nx_uri_t directory)
  53. {
  54. WIN32_FIND_DATAW find_data = {0};
  55. nx_uri_t directory_mask;
  56. int ret = NXURICreateFromPath(&directory_mask, L"*.w6c", directory);
  57. if (ret != NErr_Success)
  58. return ret;
  59. HANDLE find_handle = FindFirstFileW(directory_mask->string, &find_data);
  60. if (find_handle != INVALID_HANDLE_VALUE)
  61. {
  62. do
  63. {
  64. nx_uri_t w6c_filename;
  65. if (NXURICreateFromPath(&w6c_filename, find_data.cFileName, directory) == NErr_Success)
  66. {
  67. AddComponent(w6c_filename);
  68. NXURIRelease(w6c_filename);
  69. }
  70. }
  71. while (FindNextFileW(find_handle,&find_data));
  72. FindClose(find_handle);
  73. }
  74. return NErr_Success;
  75. }
  76. void ComponentManager::CloseComponent(ifc_component *component)
  77. {
  78. FreeLibrary(component->component_info.hModule);
  79. }