BsMDCodeEditor.cpp 4.3 KB

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