BsEditorScriptLibrary.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. EditorScriptManager::instance().update();
  33. }
  34. void EditorScriptLibrary::reload()
  35. {
  36. Path engineAssemblyPath = getEngineAssemblyPath();
  37. Path gameAssemblyPath = getGameAssemblyPath();
  38. Path editorAssemblyPath = getEditorAssemblyPath();
  39. Path editorScriptAssemblyPath = getEditorScriptAssemblyPath();
  40. #if BS_DEBUG_MODE
  41. mScriptAssembliesLoaded = true; // Force assembly refresh as an ad hoc unit test in debug mode
  42. #endif
  43. // Do a full refresh if we have already loaded script assemblies
  44. if (mScriptAssembliesLoaded)
  45. {
  46. Vector<AssemblyRefreshInfo> assemblies;
  47. assemblies.push_back(AssemblyRefreshInfo(ENGINE_ASSEMBLY, &engineAssemblyPath, &mEngineTypeMappings));
  48. if (gEditorApplication().isProjectLoaded())
  49. {
  50. if (FileSystem::exists(gameAssemblyPath))
  51. assemblies.push_back(AssemblyRefreshInfo(SCRIPT_GAME_ASSEMBLY, &gameAssemblyPath, &BuiltinTypeMappings::EMPTY));
  52. }
  53. assemblies.push_back(AssemblyRefreshInfo(EDITOR_ASSEMBLY, &editorAssemblyPath, &mEditorTypeMappings));
  54. if (gEditorApplication().isProjectLoaded())
  55. {
  56. if (FileSystem::exists(editorScriptAssemblyPath))
  57. assemblies.push_back(AssemblyRefreshInfo(SCRIPT_EDITOR_ASSEMBLY, &editorScriptAssemblyPath, &BuiltinTypeMappings::EMPTY));
  58. }
  59. ScriptObjectManager::instance().refreshAssemblies(assemblies);
  60. }
  61. else // Otherwise just additively load them
  62. {
  63. ScriptAssemblyManager::instance().clearAssemblyInfo();
  64. MonoManager::instance().loadAssembly(engineAssemblyPath.toString(), ENGINE_ASSEMBLY);
  65. ScriptAssemblyManager::instance().loadAssemblyInfo(ENGINE_ASSEMBLY, mEngineTypeMappings);
  66. if (FileSystem::exists(gameAssemblyPath))
  67. {
  68. MonoManager::instance().loadAssembly(gameAssemblyPath.toString(), SCRIPT_GAME_ASSEMBLY);
  69. ScriptAssemblyManager::instance().loadAssemblyInfo(SCRIPT_GAME_ASSEMBLY, BuiltinTypeMappings());
  70. }
  71. MonoManager::instance().loadAssembly(editorAssemblyPath.toString(), EDITOR_ASSEMBLY);
  72. ScriptAssemblyManager::instance().loadAssemblyInfo(EDITOR_ASSEMBLY, mEditorTypeMappings);
  73. if (FileSystem::exists(editorScriptAssemblyPath))
  74. {
  75. MonoManager::instance().loadAssembly(editorScriptAssemblyPath.toString(), SCRIPT_EDITOR_ASSEMBLY);
  76. ScriptAssemblyManager::instance().loadAssemblyInfo(SCRIPT_EDITOR_ASSEMBLY, BuiltinTypeMappings());
  77. }
  78. mScriptAssembliesLoaded = true;
  79. }
  80. }
  81. void EditorScriptLibrary::destroy()
  82. {
  83. mQuitRequestedEvent.disconnect();
  84. GameObjectManager::instance().destroyQueuedObjects();
  85. unloadAssemblies();
  86. EditorScriptManager::shutDown();
  87. shutdownModules();
  88. }
  89. Path EditorScriptLibrary::getEditorAssemblyPath() const
  90. {
  91. Path assemblyPath = getBuiltinAssemblyFolder();
  92. assemblyPath.append(String(EDITOR_ASSEMBLY) + ".dll");
  93. return assemblyPath;
  94. }
  95. Path EditorScriptLibrary::getEditorScriptAssemblyPath() const
  96. {
  97. Path assemblyPath = getScriptAssemblyFolder();
  98. assemblyPath.append(String(SCRIPT_EDITOR_ASSEMBLY) + ".dll");
  99. return assemblyPath;
  100. }
  101. Path EditorScriptLibrary::getScriptAssemblyFolder() const
  102. {
  103. if (!gEditorApplication().isProjectLoaded())
  104. return Path::BLANK;
  105. Path assemblyFolder = gEditorApplication().getProjectPath();
  106. assemblyFolder.append(INTERNAL_ASSEMBLY_PATH);
  107. return assemblyFolder;
  108. }
  109. }