BsMDCodeEditor.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 "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. WString args = L"--no-splash \"" + solutionPath.toWString() + L"\" \"" + filePath.toWString() + L";" + toWString(lineNumber) + L"\"";
  42. #if BS_PLATFORM == BS_PLATFORM_WIN32
  43. WString pathStr = mExecPath.toWString();
  44. ShellExecuteW(0, L"open", pathStr.c_str(), args.c_str(), NULL, SW_HIDE);
  45. #elif BS_PLATFORM == BS_PLATFORM_LINUX
  46. String narrowArgs = UTF8::fromWide(args);
  47. const char* commandPattern = "flatpak run com.xamarin.MonoDevelop %s";
  48. char* commandStr = (char*)bs_stack_alloc((UINT32)narrowArgs.size() + (UINT32)strlen(commandPattern) + 1);
  49. sprintf(commandStr, commandPattern, narrowArgs.c_str());
  50. system(commandStr);
  51. bs_stack_free(commandStr);
  52. #endif
  53. }
  54. void MDCodeEditor::syncSolution(const CodeSolutionData& data, const Path& outputPath) const
  55. {
  56. String solutionString = CSProject::writeSolution(CSProjectVersion::MonoDevelop, data);
  57. solutionString = StringUtil::replaceAll(solutionString, "\n", "\r\n");
  58. Path solutionPath = outputPath;
  59. solutionPath.append(data.name + L".sln");
  60. for (auto& project : data.projects)
  61. {
  62. String projectString = CSProject::writeProject(CSProjectVersion::MonoDevelop, project);
  63. projectString = StringUtil::replaceAll(projectString, "\n", "\r\n");
  64. Path projectPath = outputPath;
  65. projectPath.append(project.name + L".csproj");
  66. SPtr<DataStream> projectStream = FileSystem::createAndOpenFile(projectPath);
  67. projectStream->write(projectString.c_str(), projectString.size() * sizeof(String::value_type));
  68. projectStream->close();
  69. }
  70. SPtr<DataStream> solutionStream = FileSystem::createAndOpenFile(solutionPath);
  71. solutionStream->write(solutionString.c_str(), solutionString.size() * sizeof(String::value_type));
  72. solutionStream->close();
  73. }
  74. MDCodeEditorFactory::MDCodeEditorFactory()
  75. {
  76. #if BS_PLATFORM == BS_PLATFORM_WIN32
  77. #if BS_ARCH_TYPE == BS_ARCHITECTURE_x86_64
  78. BOOL is64bit = 1;
  79. #else
  80. BOOL is64bit = 0;
  81. HANDLE process = GetCurrentProcess();
  82. IsWow64Process(process, (PBOOL)&is64bit);
  83. #endif
  84. WString registryKeyRoot;
  85. if (is64bit)
  86. registryKeyRoot = L"SOFTWARE\\Wow6432Node\\";
  87. else
  88. registryKeyRoot = L"SOFTWARE\\";
  89. WString registryKey = registryKeyRoot + L"\\Xamarin\\XamarinStudio";
  90. HKEY regKey;
  91. LONG result = RegOpenKeyExW(HKEY_LOCAL_MACHINE, registryKey.c_str(), 0, KEY_READ, &regKey);
  92. if (result != ERROR_SUCCESS)
  93. return;
  94. WString installPath;
  95. getRegistryStringValue(regKey, L"Path", installPath, StringUtil::WBLANK);
  96. if (installPath.empty())
  97. return;
  98. mInstallPath = Path(installPath) + Path("XamarinStudio.exe");
  99. #elif BS_PLATFORM == BS_PLATFORM_LINUX
  100. Path monoDevelopDir = LinuxPlatform::getHomeDir();
  101. monoDevelopDir.append(".local/share/flatpak/app/com.xamarin.MonoDevelop");
  102. if(FileSystem::exists(monoDevelopDir))
  103. mInstallPath = monoDevelopDir;
  104. #endif
  105. if (!mInstallPath.isEmpty())
  106. mSupportedEditors.push_back(CodeEditorType::MonoDevelop);
  107. }
  108. CodeEditor* MDCodeEditorFactory::create(CodeEditorType type) const
  109. {
  110. if (mInstallPath.isEmpty())
  111. return nullptr;
  112. if (type != CodeEditorType::MonoDevelop)
  113. return nullptr;
  114. return bs_new<MDCodeEditor>(mInstallPath);
  115. }
  116. }