BsEditorScriptLibrary.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsEditorScriptLibrary.h"
  4. #include "BsEditorScriptManager.h"
  5. #include "BsEditorApplication.h"
  6. #include "BsScriptObjectManager.h"
  7. #include "FileSystem/BsFileSystem.h"
  8. #include "BsMonoManager.h"
  9. #include "Serialization/BsScriptAssemblyManager.h"
  10. #include "BsMonoAssembly.h"
  11. #include "Scene/BsGameObjectManager.h"
  12. #include "Generated/BsEditorBuiltinReflectableTypesLookup.generated.h"
  13. namespace bs
  14. {
  15. EditorScriptLibrary::EditorScriptLibrary()
  16. :mScriptAssembliesLoaded(false)
  17. { }
  18. void EditorScriptLibrary::initialize()
  19. {
  20. EngineScriptLibrary::initialize();
  21. mEditorTypeMappings.reflectableObjects = EditorBuiltinReflectableTypes::getEntries();
  22. MonoManager::instance().loadAssembly(getEditorAssemblyPath(), EDITOR_ASSEMBLY);
  23. ScriptAssemblyManager::instance().loadAssemblyInfo(EDITOR_ASSEMBLY, mEditorTypeMappings);
  24. EditorScriptManager::startUp();
  25. mQuitRequestedEvent = gEditorApplication().onQuitRequested.connect([]()
  26. {
  27. EditorScriptManager::instance().quitRequested();
  28. });
  29. }
  30. void EditorScriptLibrary::update()
  31. {
  32. EngineScriptLibrary::update();
  33. EditorScriptManager::instance().update();
  34. }
  35. void EditorScriptLibrary::reload()
  36. {
  37. Path engineAssemblyPath = getEngineAssemblyPath();
  38. Path gameAssemblyPath = getGameAssemblyPath();
  39. Path editorAssemblyPath = getEditorAssemblyPath();
  40. Path editorScriptAssemblyPath = getEditorScriptAssemblyPath();
  41. #if BS_DEBUG_MODE
  42. mScriptAssembliesLoaded = true; // Force assembly refresh as an ad hoc unit test in debug mode
  43. #endif
  44. // Do a full refresh if we have already loaded script assemblies
  45. if (mScriptAssembliesLoaded)
  46. {
  47. Vector<AssemblyRefreshInfo> assemblies;
  48. assemblies.push_back(AssemblyRefreshInfo(ENGINE_ASSEMBLY, &engineAssemblyPath, &mEngineTypeMappings));
  49. if (gEditorApplication().isProjectLoaded())
  50. {
  51. if (FileSystem::exists(gameAssemblyPath))
  52. assemblies.push_back(AssemblyRefreshInfo(SCRIPT_GAME_ASSEMBLY, &gameAssemblyPath, &BuiltinTypeMappings::EMPTY));
  53. }
  54. assemblies.push_back(AssemblyRefreshInfo(EDITOR_ASSEMBLY, &editorAssemblyPath, &mEditorTypeMappings));
  55. if (gEditorApplication().isProjectLoaded())
  56. {
  57. if (FileSystem::exists(editorScriptAssemblyPath))
  58. assemblies.push_back(AssemblyRefreshInfo(SCRIPT_EDITOR_ASSEMBLY, &editorScriptAssemblyPath, &BuiltinTypeMappings::EMPTY));
  59. }
  60. ScriptObjectManager::instance().refreshAssemblies(assemblies);
  61. }
  62. else // Otherwise just additively load them
  63. {
  64. ScriptAssemblyManager::instance().clearAssemblyInfo();
  65. MonoManager::instance().loadAssembly(engineAssemblyPath.toString(), ENGINE_ASSEMBLY);
  66. ScriptAssemblyManager::instance().loadAssemblyInfo(ENGINE_ASSEMBLY, mEngineTypeMappings);
  67. if (FileSystem::exists(gameAssemblyPath))
  68. {
  69. MonoManager::instance().loadAssembly(gameAssemblyPath.toString(), SCRIPT_GAME_ASSEMBLY);
  70. ScriptAssemblyManager::instance().loadAssemblyInfo(SCRIPT_GAME_ASSEMBLY, BuiltinTypeMappings());
  71. }
  72. MonoManager::instance().loadAssembly(editorAssemblyPath.toString(), EDITOR_ASSEMBLY);
  73. ScriptAssemblyManager::instance().loadAssemblyInfo(EDITOR_ASSEMBLY, mEditorTypeMappings);
  74. if (FileSystem::exists(editorScriptAssemblyPath))
  75. {
  76. MonoManager::instance().loadAssembly(editorScriptAssemblyPath.toString(), SCRIPT_EDITOR_ASSEMBLY);
  77. ScriptAssemblyManager::instance().loadAssemblyInfo(SCRIPT_EDITOR_ASSEMBLY, BuiltinTypeMappings());
  78. }
  79. mScriptAssembliesLoaded = true;
  80. }
  81. }
  82. void EditorScriptLibrary::destroy()
  83. {
  84. mQuitRequestedEvent.disconnect();
  85. GameObjectManager::instance().destroyQueuedObjects();
  86. unloadAssemblies();
  87. EditorScriptManager::shutDown();
  88. shutdownModules();
  89. }
  90. Path EditorScriptLibrary::getEditorAssemblyPath() const
  91. {
  92. Path assemblyPath = getBuiltinAssemblyFolder();
  93. assemblyPath.append(String(EDITOR_ASSEMBLY) + ".dll");
  94. return assemblyPath;
  95. }
  96. Path EditorScriptLibrary::getEditorScriptAssemblyPath() const
  97. {
  98. Path assemblyPath = getScriptAssemblyFolder();
  99. assemblyPath.append(String(SCRIPT_EDITOR_ASSEMBLY) + ".dll");
  100. return assemblyPath;
  101. }
  102. Path EditorScriptLibrary::getScriptAssemblyFolder() const
  103. {
  104. if (!gEditorApplication().isProjectLoaded())
  105. return Path::BLANK;
  106. Path assemblyFolder = gEditorApplication().getProjectPath();
  107. assemblyFolder.append(INTERNAL_ASSEMBLY_PATH);
  108. return assemblyFolder;
  109. }
  110. }