BsMDCodeEditor.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "CodeEditor/BsMDCodeEditor.h"
  4. #include "FileSystem/BsFileSystem.h"
  5. #include "FileSystem/BsDataStream.h"
  6. #include "String/BsUnicode.h"
  7. #if BS_PLATFORM == BS_PLATFORM_WIN32
  8. #include <Windows.h>
  9. #elif BS_PLATFORM == BS_PLATFORM_LINUX
  10. #include "Private/Linux/BsLinuxPlatform.h"
  11. #endif
  12. namespace bs
  13. {
  14. #if BS_PLATFORM == BS_PLATFORM_WIN32
  15. /**
  16. * Reads a string value from the specified key in the registry.
  17. *
  18. * @param[in] key Registry key to read from.
  19. * @param[in] name Identifier of the value to read from.
  20. * @param[in] value Output value read from the key.
  21. * @param[in] defaultValue Default value to return if the key or identifier doesn't exist.
  22. */
  23. static LONG getRegistryStringValue(HKEY hKey, const WString& name, WString& value, const WString& defaultValue)
  24. {
  25. value = defaultValue;
  26. wchar_t strBuffer[512];
  27. DWORD strBufferSize = sizeof(strBuffer);
  28. ULONG result = RegQueryValueExW(hKey, name.c_str(), 0, nullptr, (LPBYTE)strBuffer, &strBufferSize);
  29. if (result == ERROR_SUCCESS)
  30. value = strBuffer;
  31. return result;
  32. }
  33. #else
  34. #endif
  35. MDCodeEditor::MDCodeEditor(const Path& execPath)
  36. :mExecPath(execPath)
  37. {
  38. }
  39. void MDCodeEditor::openFile(const Path& solutionPath, const Path& filePath, UINT32 lineNumber) const
  40. {
  41. String args = "--no-splash \"" + solutionPath.toString() + "\" \"" + filePath.toString() + ";" + toString(lineNumber) + "\"";
  42. #if BS_PLATFORM == BS_PLATFORM_WIN32
  43. WString pathStr = UTF8::toWide(mExecPath.toString());
  44. WString wideArgs = UTF8::toWide(args);
  45. ShellExecuteW(0, L"open", pathStr.c_str(), wideArgs.c_str(), NULL, SW_HIDE);
  46. #elif BS_PLATFORM == BS_PLATFORM_LINUX
  47. pid_t pid = fork();
  48. if(pid == 0)
  49. {
  50. const char* commandPattern = "flatpak run com.xamarin.MonoDevelop %s";
  51. char* commandStr = (char*) malloc((UINT32) args.size() + (UINT32) strlen(commandPattern) + 1);
  52. sprintf(commandStr, commandPattern, args.c_str());
  53. system(commandStr);
  54. free(commandStr);
  55. exit(1);
  56. }
  57. #endif
  58. }
  59. void MDCodeEditor::syncSolution(const CodeSolutionData& data, const Path& outputPath) const
  60. {
  61. String solutionString = CSProject::writeSolution(CSProjectVersion::MonoDevelop, data);
  62. solutionString = StringUtil::replaceAll(solutionString, "\n", "\r\n");
  63. Path solutionPath = outputPath;
  64. solutionPath.append(data.name + ".sln");
  65. for (auto& project : data.projects)
  66. {
  67. String projectString = CSProject::writeProject(CSProjectVersion::MonoDevelop, project);
  68. projectString = StringUtil::replaceAll(projectString, "\n", "\r\n");
  69. Path projectPath = outputPath;
  70. projectPath.append(project.name + ".csproj");
  71. SPtr<DataStream> projectStream = FileSystem::createAndOpenFile(projectPath);
  72. projectStream->write(projectString.c_str(), projectString.size() * sizeof(String::value_type));
  73. projectStream->close();
  74. }
  75. SPtr<DataStream> solutionStream = FileSystem::createAndOpenFile(solutionPath);
  76. solutionStream->write(solutionString.c_str(), solutionString.size() * sizeof(String::value_type));
  77. solutionStream->close();
  78. }
  79. MDCodeEditorFactory::MDCodeEditorFactory()
  80. {
  81. #if BS_PLATFORM == BS_PLATFORM_WIN32
  82. #if BS_ARCH_TYPE == BS_ARCHITECTURE_x86_64
  83. BOOL is64bit = 1;
  84. #else
  85. BOOL is64bit = 0;
  86. HANDLE process = GetCurrentProcess();
  87. IsWow64Process(process, (PBOOL)&is64bit);
  88. #endif
  89. WString registryKeyRoot;
  90. if (is64bit)
  91. registryKeyRoot = L"SOFTWARE\\Wow6432Node\\";
  92. else
  93. registryKeyRoot = L"SOFTWARE\\";
  94. WString registryKey = registryKeyRoot + L"\\Xamarin\\XamarinStudio";
  95. HKEY regKey;
  96. LONG result = RegOpenKeyExW(HKEY_LOCAL_MACHINE, registryKey.c_str(), 0, KEY_READ, &regKey);
  97. if (result != ERROR_SUCCESS)
  98. return;
  99. WString installPath;
  100. getRegistryStringValue(regKey, L"Path", installPath, StringUtil::WBLANK);
  101. if (installPath.empty())
  102. return;
  103. mInstallPath = Path(UTF8::fromWide(installPath)) + Path("XamarinStudio.exe");
  104. #elif BS_PLATFORM == BS_PLATFORM_LINUX
  105. Path monoDevelopDir = LinuxPlatform::getHomeDir();
  106. monoDevelopDir.append(".local/share/flatpak/app/com.xamarin.MonoDevelop");
  107. if(FileSystem::exists(monoDevelopDir))
  108. mInstallPath = monoDevelopDir;
  109. #endif
  110. if (!mInstallPath.isEmpty())
  111. mSupportedEditors.push_back(CodeEditorType::MonoDevelop);
  112. }
  113. CodeEditor* MDCodeEditorFactory::create(CodeEditorType type) const
  114. {
  115. if (mInstallPath.isEmpty())
  116. return nullptr;
  117. if (type != CodeEditorType::MonoDevelop)
  118. return nullptr;
  119. return bs_new<MDCodeEditor>(mInstallPath);
  120. }
  121. }