2
0

BsCodeEditor.cpp 4.8 KB

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