BsCodeEditor.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. #include "CodeEditor/BsMDCodeEditor.h"
  10. #if BS_PLATFORM == BS_PLATFORM_WIN32
  11. #include "Win32/BsVSCodeEditor.h"
  12. #else
  13. // Add implementations for code editors on other platforms.
  14. #endif
  15. namespace bs
  16. {
  17. CodeEditorManager::CodeEditorManager()
  18. :mActiveEditor(nullptr), mActiveEditorType(CodeEditorType::None)
  19. {
  20. #if BS_PLATFORM == BS_PLATFORM_WIN32
  21. VSCodeEditorFactory* vsCodeEditorFactory = bs_new<VSCodeEditorFactory>();
  22. Vector<CodeEditorType> vsEditors = vsCodeEditorFactory->getAvailableEditors();
  23. for(auto& editor : vsEditors)
  24. {
  25. mFactoryPerEditor[editor] = vsCodeEditorFactory;
  26. mEditors.push_back(editor);
  27. }
  28. mFactories.push_back(vsCodeEditorFactory);
  29. #else
  30. // Add implementations for code editors on other platforms.
  31. #endif
  32. MDCodeEditorFactory* mdCodeEditorFactory = bs_new<MDCodeEditorFactory>();
  33. Vector<CodeEditorType> mdEditors = mdCodeEditorFactory->getAvailableEditors();
  34. for(auto& editor : mdEditors)
  35. {
  36. mFactoryPerEditor[editor] = mdCodeEditorFactory;
  37. mEditors.push_back(editor);
  38. }
  39. mFactories.push_back(mdCodeEditorFactory);
  40. }
  41. CodeEditorManager::~CodeEditorManager()
  42. {
  43. for (auto& factory : mFactories)
  44. bs_delete(factory);
  45. if (mActiveEditor != nullptr)
  46. bs_delete(mActiveEditor);
  47. }
  48. void CodeEditorManager::setActive(CodeEditorType editor)
  49. {
  50. if (mActiveEditor != nullptr)
  51. {
  52. bs_delete(mActiveEditor);
  53. mActiveEditor = nullptr;
  54. }
  55. auto findIter = mFactoryPerEditor.find(editor);
  56. if (findIter == mFactoryPerEditor.end())
  57. return;
  58. mActiveEditor = findIter->second->create(editor);
  59. mActiveEditorType = editor;
  60. }
  61. void CodeEditorManager::openFile(const Path& path, UINT32 lineNumber) const
  62. {
  63. if (mActiveEditor == nullptr)
  64. return;
  65. Path filePath = path;
  66. if (!path.isAbsolute())
  67. filePath.makeAbsolute(gProjectLibrary().getResourcesFolder());
  68. mActiveEditor->openFile(getSolutionPath(), filePath, lineNumber);
  69. }
  70. void CodeEditorManager::syncSolution() const
  71. {
  72. if (mActiveEditor == nullptr)
  73. return;
  74. CodeSolutionData slnData;
  75. slnData.name = gEditorApplication().getProjectName();
  76. Vector<UINT32> scriptTypeIds =
  77. {
  78. TID_ScriptCode, TID_PlainText, TID_Shader, TID_ShaderInclude
  79. };
  80. Vector<ProjectLibrary::LibraryEntry*> libraryEntries = gProjectLibrary().search("*", scriptTypeIds);
  81. PlatformType activePlatform = BuildManager::instance().getActivePlatform();
  82. Vector<String> frameworkAssemblies = BuildManager::instance().getFrameworkAssemblies(activePlatform);
  83. slnData.projects.push_back(CodeProjectData());
  84. slnData.projects.push_back(CodeProjectData());
  85. // Game project
  86. CodeProjectData& gameProject = slnData.projects[0];
  87. gameProject.name = String(SCRIPT_GAME_ASSEMBLY);
  88. gameProject.defines = BuildManager::instance().getDefines(activePlatform);
  89. //// Add references
  90. gameProject.assemblyReferences.push_back(CodeProjectReference{ String(ENGINE_ASSEMBLY), gApplication().getEngineAssemblyPath() });
  91. for (auto& assemblyName : frameworkAssemblies)
  92. gameProject.assemblyReferences.push_back(CodeProjectReference{ assemblyName, Path::BLANK });
  93. // Editor project
  94. CodeProjectData& editorProject = slnData.projects[1];
  95. editorProject.name = String(SCRIPT_EDITOR_ASSEMBLY);
  96. //// Add references
  97. editorProject.assemblyReferences.push_back(CodeProjectReference{ String(ENGINE_ASSEMBLY), gApplication().getEngineAssemblyPath() });
  98. editorProject.assemblyReferences.push_back(CodeProjectReference{ String(EDITOR_ASSEMBLY), gEditorApplication().getEditorAssemblyPath() });
  99. for (auto& assemblyName : frameworkAssemblies)
  100. gameProject.assemblyReferences.push_back(CodeProjectReference{ assemblyName, Path::BLANK });
  101. editorProject.projectReferences.push_back(CodeProjectReference{ gameProject.name, Path::BLANK });
  102. //// Add files for both projects
  103. for (auto& entry : libraryEntries)
  104. {
  105. if (entry->type != ProjectLibrary::LibraryEntryType::File)
  106. continue;
  107. ProjectLibrary::FileEntry* resEntry = static_cast<ProjectLibrary::FileEntry*>(entry);
  108. if (resEntry->meta->hasTypeId(TID_ScriptCode))
  109. {
  110. SPtr<ScriptCodeImportOptions> scriptIO = std::static_pointer_cast<ScriptCodeImportOptions>(resEntry->meta->getImportOptions());
  111. bool isEditorScript = false;
  112. if (scriptIO != nullptr)
  113. isEditorScript = scriptIO->isEditorScript();
  114. if (isEditorScript)
  115. editorProject.codeFiles.push_back(resEntry->path);
  116. else
  117. gameProject.codeFiles.push_back(resEntry->path);
  118. }
  119. else
  120. gameProject.nonCodeFiles.push_back(resEntry->path);
  121. }
  122. mActiveEditor->syncSolution(slnData, gEditorApplication().getProjectPath());
  123. }
  124. Path CodeEditorManager::getSolutionPath() const
  125. {
  126. Path path = gEditorApplication().getProjectPath();
  127. path.append(gEditorApplication().getProjectName() + ".sln");
  128. return path;
  129. }
  130. const String CSProject::SLN_TEMPLATE =
  131. R"(Microsoft Visual Studio Solution File, Format Version {0}
  132. # Visual Studio 2013
  133. VisualStudioVersion = 12.0.30723.0
  134. MinimumVisualStudioVersion = 10.0.40219.1{1}
  135. Global
  136. GlobalSection(SolutionConfigurationPlatforms) = preSolution
  137. Debug|Any CPU = Debug|Any CPU
  138. Release|Any CPU = Release|Any CPU
  139. EndGlobalSection
  140. GlobalSection(ProjectConfigurationPlatforms) = postSolution{2}
  141. EndGlobalSection
  142. GlobalSection(SolutionProperties) = preSolution
  143. HideSolutionNode = FALSE
  144. EndGlobalSection
  145. EndGlobal
  146. )";
  147. const String CSProject::PROJ_ENTRY_TEMPLATE =
  148. R"(
  149. Project("\{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC\}") = "{0}", "{1}", "\{{2}\}"
  150. EndProject)";
  151. const String CSProject::PROJ_PLATFORM_TEMPLATE =
  152. R"(
  153. \{{0}\}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
  154. \{{0}\}.Debug|Any CPU.Build.0 = Debug|Any CPU
  155. \{{0}\}.Release|Any CPU.ActiveCfg = Release|Any CPU
  156. \{{0}\}.Release|Any CPU.Build.0 = Release|Any CPU)";
  157. const String CSProject::PROJ_TEMPLATE =
  158. R"literal(<?xml version="1.0" encoding="utf-8"?>
  159. <Project ToolsVersion="{0}" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  160. <Import Project="$(MSBuildExtensionsPath)\\$(MSBuildToolsVersion)\\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\\$(MSBuildToolsVersion)\\Microsoft.Common.props')" />
  161. <PropertyGroup>
  162. <Configuration Condition = " '$(Configuration)' == '' ">Debug</Configuration>
  163. <Platform Condition = " '$(Platform)' == '' ">AnyCPU</Platform>
  164. <ProjectGuid>\{{1}\}</ProjectGuid>
  165. <OutputType>Library</OutputType>
  166. <AppDesignerFolder>Properties</AppDesignerFolder>
  167. <RootNamespace></RootNamespace>
  168. <AssemblyName>{2}</AssemblyName>
  169. <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
  170. <FileAlignment>512</FileAlignment>
  171. <BaseDirectory>Resources</BaseDirectory>
  172. <SchemaVersion>2.0</SchemaVersion>
  173. </PropertyGroup>
  174. <PropertyGroup Condition = " '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
  175. <DebugSymbols>true</DebugSymbols>
  176. <DebugType>full</DebugType>
  177. <Optimize>false</Optimize>
  178. <OutputPath>Internal\\Temp\\Assemblies\\Debug\\</OutputPath>
  179. <BaseIntermediateOutputPath>Internal\\Temp\\Assemblies\\</BaseIntermediateOutputPath>
  180. <DefineConstants>DEBUG;TRACE;{3}</DefineConstants>
  181. <ErrorReport>prompt</ErrorReport>
  182. <WarningLevel>4</WarningLevel >
  183. </PropertyGroup>
  184. <PropertyGroup Condition = " '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
  185. <DebugType>pdbonly</DebugType>
  186. <Optimize>true</Optimize>
  187. <OutputPath>Internal\\Temp\\Assemblies\\Release\\</OutputPath>
  188. <BaseIntermediateOutputPath>Internal\\Temp\\Assemblies\\</BaseIntermediateOutputPath>
  189. <DefineConstants>TRACE;{3}</DefineConstants>
  190. <ErrorReport>prompt</ErrorReport>
  191. <WarningLevel>4</WarningLevel>
  192. </PropertyGroup>
  193. <ItemGroup>{4}
  194. </ItemGroup>
  195. <ItemGroup>{5}
  196. </ItemGroup>
  197. <ItemGroup>{6}
  198. </ItemGroup>
  199. <ItemGroup>{7}
  200. </ItemGroup>
  201. <Import Project = "$(MSBuildToolsPath)\\Microsoft.CSharp.targets"/>
  202. </Project>)literal";
  203. const String CSProject::REFERENCE_ENTRY_TEMPLATE =
  204. R"(
  205. <Reference Include="{0}"/>)";
  206. const String CSProject::REFERENCE_PATH_ENTRY_TEMPLATE =
  207. R"(
  208. <Reference Include="{0}">
  209. <HintPath>{1}</HintPath>
  210. </Reference>)";
  211. const String CSProject::REFERENCE_PROJECT_ENTRY_TEMPLATE =
  212. R"(
  213. <ProjectReference Include="{0}.csproj">
  214. <Project>\{{1}\}</Project>
  215. <Name>{0}</Name>
  216. </ProjectReference>)";
  217. const String CSProject::CODE_ENTRY_TEMPLATE =
  218. R"(
  219. <Compile Include="{0}"/>)";
  220. const String CSProject::NON_CODE_ENTRY_TEMPLATE =
  221. R"(
  222. <None Include="{0}"/>)";
  223. /** Generates a C# project GUID from the project name. */
  224. String getProjectGUID(const String& projectName)
  225. {
  226. static const String guidTemplate = "{0}-{1}-{2}-{3}-{4}";
  227. String hash = md5(projectName);
  228. String output = StringUtil::format(guidTemplate, hash.substr(0, 8),
  229. hash.substr(8, 4), hash.substr(12, 4), hash.substr(16, 4), hash.substr(20, 12));
  230. StringUtil::toUpperCase(output);
  231. return output;
  232. }
  233. String CSProject::writeSolution(CSProjectVersion version, const CodeSolutionData& data)
  234. {
  235. struct VersionData
  236. {
  237. String formatVersion;
  238. };
  239. Map<CSProjectVersion, VersionData> versionData =
  240. {
  241. { CSProjectVersion::VS2008, { "10.00" } },
  242. { CSProjectVersion::VS2010, { "11.00" } },
  243. { CSProjectVersion::VS2012, { "12.00" } },
  244. { CSProjectVersion::VS2013, { "12.00" } },
  245. { CSProjectVersion::VS2015, { "12.00" } },
  246. { CSProjectVersion::VS2017, { "12.00" } },
  247. { CSProjectVersion::MonoDevelop, { "12.00" } }
  248. };
  249. StringStream projectEntriesStream;
  250. StringStream projectPlatformsStream;
  251. for (auto& project : data.projects)
  252. {
  253. String guid = getProjectGUID(project.name);
  254. String projectName = project.name;
  255. projectEntriesStream << StringUtil::format(PROJ_ENTRY_TEMPLATE, projectName, projectName + ".csproj", guid);
  256. projectPlatformsStream << StringUtil::format(PROJ_PLATFORM_TEMPLATE, guid);
  257. }
  258. String projectEntries = projectEntriesStream.str();
  259. String projectPlatforms = projectPlatformsStream.str();
  260. return StringUtil::format(SLN_TEMPLATE, versionData[version].formatVersion, projectEntries, projectPlatforms);
  261. }
  262. String CSProject::writeProject(CSProjectVersion version, const CodeProjectData& projectData)
  263. {
  264. struct VersionData
  265. {
  266. String toolsVersion;
  267. };
  268. Map<CSProjectVersion, VersionData> versionData =
  269. {
  270. { CSProjectVersion::VS2008, { "3.5" } },
  271. { CSProjectVersion::VS2010, { "4.0" } },
  272. { CSProjectVersion::VS2012, { "4.0" } },
  273. { CSProjectVersion::VS2013, { "12.0" } },
  274. { CSProjectVersion::VS2015, { "13.0" } },
  275. { CSProjectVersion::VS2017, { "15.0" } },
  276. { CSProjectVersion::MonoDevelop, { "14.0" } }
  277. };
  278. StringStream tempStream;
  279. for (auto& codeEntry : projectData.codeFiles)
  280. tempStream << StringUtil::format(CODE_ENTRY_TEMPLATE, codeEntry.toString());
  281. String codeEntries = tempStream.str();
  282. tempStream.str("");
  283. tempStream.clear();
  284. for (auto& nonCodeEntry : projectData.nonCodeFiles)
  285. tempStream << StringUtil::format(NON_CODE_ENTRY_TEMPLATE, nonCodeEntry.toString());
  286. String nonCodeEntries = tempStream.str();
  287. tempStream.str("");
  288. tempStream.clear();
  289. for (auto& referenceEntry : projectData.assemblyReferences)
  290. {
  291. String referenceName = referenceEntry.name;
  292. if (referenceEntry.path.isEmpty())
  293. tempStream << StringUtil::format(REFERENCE_ENTRY_TEMPLATE, referenceName);
  294. else
  295. tempStream << StringUtil::format(REFERENCE_PATH_ENTRY_TEMPLATE, referenceName, referenceEntry.path.toString());
  296. }
  297. String referenceEntries = tempStream.str();
  298. tempStream.str("");
  299. tempStream.clear();
  300. for (auto& referenceEntry : projectData.projectReferences)
  301. {
  302. String referenceName = referenceEntry.name;
  303. String projectGUID = getProjectGUID(referenceEntry.name);
  304. tempStream << StringUtil::format(REFERENCE_PROJECT_ENTRY_TEMPLATE, referenceName, projectGUID);
  305. }
  306. String projectReferenceEntries = tempStream.str();
  307. tempStream.str("");
  308. tempStream.clear();
  309. tempStream << projectData.defines;
  310. String defines = tempStream.str();
  311. String projectGUID = getProjectGUID(projectData.name);
  312. return StringUtil::format(PROJ_TEMPLATE, versionData[version].toolsVersion, projectGUID,
  313. projectData.name, defines, referenceEntries, projectReferenceEntries, codeEntries, nonCodeEntries);
  314. }
  315. }