BsCodeEditor.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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)
  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. }
  49. void CodeEditorManager::openFile(const Path& path, UINT32 lineNumber) const
  50. {
  51. if (mActiveEditor == nullptr)
  52. return;
  53. Path filePath = path;
  54. if (!path.isAbsolute())
  55. filePath.makeAbsolute(gEditorApplication().getProjectPath());
  56. mActiveEditor->openFile(getSolutionPath(), filePath, lineNumber);
  57. }
  58. void CodeEditorManager::syncSolution() const
  59. {
  60. if (mActiveEditor == nullptr)
  61. return;
  62. CodeSolutionData slnData;
  63. slnData.name = gEditorApplication().getProjectName();
  64. Vector<UINT32> scriptTypeIds =
  65. {
  66. TID_ScriptCode, TID_PlainText, TID_GpuProgram, TID_GpuProgramInclude
  67. };
  68. Vector<ProjectLibrary::LibraryEntry*> libraryEntries = ProjectLibrary::instance().search(L"*", scriptTypeIds);
  69. PlatformType activePlatform = BuildManager::instance().getActivePlatform();
  70. Vector<WString> frameworkAssemblies = BuildManager::instance().getFrameworkAssemblies(activePlatform);
  71. slnData.projects.push_back(CodeProjectData());
  72. slnData.projects.push_back(CodeProjectData());
  73. // Game project
  74. CodeProjectData& gameProject = slnData.projects[0];
  75. gameProject.name = toWString(SCRIPT_GAME_ASSEMBLY);
  76. gameProject.defines = BuildManager::instance().getDefines(activePlatform);
  77. //// Add references
  78. gameProject.assemblyReferences.push_back(CodeProjectReference{ toWString(ENGINE_ASSEMBLY), gApplication().getEngineAssemblyPath() });
  79. for (auto& assemblyName : frameworkAssemblies)
  80. gameProject.assemblyReferences.push_back(CodeProjectReference{ assemblyName, Path::BLANK });
  81. // Editor project
  82. CodeProjectData& editorProject = slnData.projects[1];
  83. editorProject.name = toWString(SCRIPT_EDITOR_ASSEMBLY);
  84. //// Add references
  85. editorProject.assemblyReferences.push_back(CodeProjectReference{ toWString(ENGINE_ASSEMBLY), gApplication().getEngineAssemblyPath() });
  86. editorProject.assemblyReferences.push_back(CodeProjectReference{ toWString(EDITOR_ASSEMBLY), gEditorApplication().getEditorAssemblyPath() });
  87. for (auto& assemblyName : frameworkAssemblies)
  88. gameProject.assemblyReferences.push_back(CodeProjectReference{ assemblyName, Path::BLANK });
  89. editorProject.projectReferences.push_back(CodeProjectReference{ gameProject.name, Path::BLANK });
  90. //// Add files for both projects
  91. for (auto& entry : libraryEntries)
  92. {
  93. if (entry->type != ProjectLibrary::LibraryEntryType::File)
  94. continue;
  95. ProjectLibrary::ResourceEntry* resEntry = static_cast<ProjectLibrary::ResourceEntry*>(entry);
  96. if (resEntry->meta->getTypeID() == TID_ScriptCode)
  97. {
  98. SPtr<ScriptCodeImportOptions> scriptIO = std::static_pointer_cast<ScriptCodeImportOptions>(resEntry->meta->getImportOptions());
  99. bool isEditorScript = false;
  100. if (scriptIO != nullptr)
  101. isEditorScript = scriptIO->isEditorScript();
  102. if (isEditorScript)
  103. editorProject.codeFiles.push_back(resEntry->path);
  104. else
  105. gameProject.codeFiles.push_back(resEntry->path);
  106. }
  107. else
  108. gameProject.nonCodeFiles.push_back(resEntry->path);
  109. }
  110. mActiveEditor->syncSolution(slnData, gEditorApplication().getProjectPath());
  111. }
  112. Path CodeEditorManager::getSolutionPath() const
  113. {
  114. Path path = gEditorApplication().getProjectPath();
  115. path.append(gEditorApplication().getProjectName() + L".sln");
  116. return path;
  117. }
  118. }