BsCodeEditor.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include "BsCodeEditor.h"
  2. #include "BsEditorApplication.h"
  3. #include "BsProjectLibrary.h"
  4. #include "BsProjectResourceMeta.h"
  5. #include "BsScriptCodeImportOptions.h"
  6. #include "BsBuildManager.h"
  7. #if BS_PLATFORM == BS_PLATFORM_WIN32
  8. #include "Win32/BsVSCodeEditor.h"
  9. #else
  10. static_assert("Make sure to add implementations for other platforms.");
  11. #endif
  12. namespace BansheeEngine
  13. {
  14. CodeEditorManager::CodeEditorManager()
  15. :mActiveEditor(nullptr), mActiveEditorType(CodeEditorType::None)
  16. {
  17. #if BS_PLATFORM == BS_PLATFORM_WIN32
  18. VSCodeEditorFactory* vsCodeEditorFactory = bs_new<VSCodeEditorFactory>();
  19. Vector<CodeEditorType> vsEditors = vsCodeEditorFactory->getAvailableEditors();
  20. for(auto& editor : vsEditors)
  21. {
  22. mFactoryPerEditor[editor] = vsCodeEditorFactory;
  23. mEditors.push_back(editor);
  24. }
  25. mFactories.push_back(vsCodeEditorFactory);
  26. #else
  27. static_assert("Make sure to add implementations for other platforms.");
  28. #endif
  29. }
  30. CodeEditorManager::~CodeEditorManager()
  31. {
  32. for (auto& factory : mFactories)
  33. bs_delete(factory);
  34. if (mActiveEditor != nullptr)
  35. bs_delete(mActiveEditor);
  36. }
  37. void CodeEditorManager::setActive(CodeEditorType editor)
  38. {
  39. if (mActiveEditor != nullptr)
  40. {
  41. bs_delete(mActiveEditor);
  42. mActiveEditor = nullptr;
  43. }
  44. auto findIter = mFactoryPerEditor.find(editor);
  45. if (findIter == mFactoryPerEditor.end())
  46. return;
  47. mActiveEditor = findIter->second->create(editor);
  48. mActiveEditorType = editor;
  49. }
  50. void CodeEditorManager::openFile(const Path& path, UINT32 lineNumber) const
  51. {
  52. if (mActiveEditor == nullptr)
  53. return;
  54. Path filePath = path;
  55. if (!path.isAbsolute())
  56. filePath.makeAbsolute(gProjectLibrary().getResourcesFolder());
  57. mActiveEditor->openFile(getSolutionPath(), filePath, lineNumber);
  58. }
  59. void CodeEditorManager::syncSolution() const
  60. {
  61. if (mActiveEditor == nullptr)
  62. return;
  63. CodeSolutionData slnData;
  64. slnData.name = gEditorApplication().getProjectName();
  65. Vector<UINT32> scriptTypeIds =
  66. {
  67. TID_ScriptCode, TID_PlainText, TID_Shader, TID_ShaderInclude
  68. };
  69. Vector<ProjectLibrary::LibraryEntry*> libraryEntries = gProjectLibrary().search(L"*", scriptTypeIds);
  70. PlatformType activePlatform = BuildManager::instance().getActivePlatform();
  71. Vector<WString> frameworkAssemblies = BuildManager::instance().getFrameworkAssemblies(activePlatform);
  72. slnData.projects.push_back(CodeProjectData());
  73. slnData.projects.push_back(CodeProjectData());
  74. // Game project
  75. CodeProjectData& gameProject = slnData.projects[0];
  76. gameProject.name = toWString(SCRIPT_GAME_ASSEMBLY);
  77. gameProject.defines = BuildManager::instance().getDefines(activePlatform);
  78. //// Add references
  79. gameProject.assemblyReferences.push_back(CodeProjectReference{ toWString(ENGINE_ASSEMBLY), gApplication().getEngineAssemblyPath() });
  80. for (auto& assemblyName : frameworkAssemblies)
  81. gameProject.assemblyReferences.push_back(CodeProjectReference{ assemblyName, Path::BLANK });
  82. // Editor project
  83. CodeProjectData& editorProject = slnData.projects[1];
  84. editorProject.name = toWString(SCRIPT_EDITOR_ASSEMBLY);
  85. //// Add references
  86. editorProject.assemblyReferences.push_back(CodeProjectReference{ toWString(ENGINE_ASSEMBLY), gApplication().getEngineAssemblyPath() });
  87. editorProject.assemblyReferences.push_back(CodeProjectReference{ toWString(EDITOR_ASSEMBLY), gEditorApplication().getEditorAssemblyPath() });
  88. for (auto& assemblyName : frameworkAssemblies)
  89. gameProject.assemblyReferences.push_back(CodeProjectReference{ assemblyName, Path::BLANK });
  90. editorProject.projectReferences.push_back(CodeProjectReference{ gameProject.name, Path::BLANK });
  91. //// Add files for both projects
  92. for (auto& entry : libraryEntries)
  93. {
  94. if (entry->type != ProjectLibrary::LibraryEntryType::File)
  95. continue;
  96. ProjectLibrary::ResourceEntry* resEntry = static_cast<ProjectLibrary::ResourceEntry*>(entry);
  97. if (resEntry->meta->getTypeID() == TID_ScriptCode)
  98. {
  99. SPtr<ScriptCodeImportOptions> scriptIO = std::static_pointer_cast<ScriptCodeImportOptions>(resEntry->meta->getImportOptions());
  100. bool isEditorScript = false;
  101. if (scriptIO != nullptr)
  102. isEditorScript = scriptIO->isEditorScript();
  103. if (isEditorScript)
  104. editorProject.codeFiles.push_back(resEntry->path);
  105. else
  106. gameProject.codeFiles.push_back(resEntry->path);
  107. }
  108. else
  109. gameProject.nonCodeFiles.push_back(resEntry->path);
  110. }
  111. mActiveEditor->syncSolution(slnData, gEditorApplication().getProjectPath());
  112. }
  113. Path CodeEditorManager::getSolutionPath() const
  114. {
  115. Path path = gEditorApplication().getProjectPath();
  116. path.append(gEditorApplication().getProjectName() + L".sln");
  117. return path;
  118. }
  119. }