BsCodeEditor.cpp 12 KB

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