UICommon.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2014 Dolphin Emulator Project
  2. // Licensed under GPLv2+
  3. // Refer to the license.txt file included.
  4. #ifdef _WIN32
  5. #include <shlobj.h> // for SHGetFolderPath
  6. #endif
  7. #include "Common/CommonPaths.h"
  8. #include "Common/FileUtil.h"
  9. #include "Common/Logging/LogManager.h"
  10. #include "Core/ConfigManager.h"
  11. #if defined(__LIBUSB__) || defined (_WIN32)
  12. #include "Core/HW/SI_GCAdapter.h"
  13. #endif
  14. #include "Core/HW/Wiimote.h"
  15. #include "UICommon/UICommon.h"
  16. #include "VideoCommon/VideoBackendBase.h"
  17. namespace UICommon
  18. {
  19. void Init()
  20. {
  21. LogManager::Init();
  22. SConfig::Init();
  23. VideoBackend::PopulateList();
  24. WiimoteReal::LoadSettings();
  25. #if defined(__LIBUSB__) || defined (_WIN32)
  26. SI_GCAdapter::Init();
  27. #endif
  28. VideoBackend::ActivateBackend(SConfig::GetInstance().m_LocalCoreStartupParameter.m_strVideoBackend);
  29. SetEnableAlert(SConfig::GetInstance().m_LocalCoreStartupParameter.bUsePanicHandlers);
  30. }
  31. void Shutdown()
  32. {
  33. #if defined(__LIBUSB__) || defined (_WIN32)
  34. SI_GCAdapter::Shutdown();
  35. #endif
  36. WiimoteReal::Shutdown();
  37. VideoBackend::ClearList();
  38. SConfig::Shutdown();
  39. LogManager::Shutdown();
  40. }
  41. void CreateDirectories()
  42. {
  43. // Copy initial Wii NAND data from Sys to User.
  44. File::CopyDir(File::GetSysDirectory() + WII_USER_DIR,
  45. File::GetUserPath(D_WIIROOT_IDX));
  46. File::CreateFullPath(File::GetUserPath(D_USER_IDX));
  47. File::CreateFullPath(File::GetUserPath(D_CACHE_IDX));
  48. File::CreateFullPath(File::GetUserPath(D_CONFIG_IDX));
  49. File::CreateFullPath(File::GetUserPath(D_DUMPDSP_IDX));
  50. File::CreateFullPath(File::GetUserPath(D_DUMPTEXTURES_IDX));
  51. File::CreateFullPath(File::GetUserPath(D_GAMESETTINGS_IDX));
  52. File::CreateFullPath(File::GetUserPath(D_GCUSER_IDX));
  53. File::CreateFullPath(File::GetUserPath(D_GCUSER_IDX) + USA_DIR DIR_SEP);
  54. File::CreateFullPath(File::GetUserPath(D_GCUSER_IDX) + EUR_DIR DIR_SEP);
  55. File::CreateFullPath(File::GetUserPath(D_GCUSER_IDX) + JAP_DIR DIR_SEP);
  56. File::CreateFullPath(File::GetUserPath(D_HIRESTEXTURES_IDX));
  57. File::CreateFullPath(File::GetUserPath(D_MAILLOGS_IDX));
  58. File::CreateFullPath(File::GetUserPath(D_MAPS_IDX));
  59. File::CreateFullPath(File::GetUserPath(D_SCREENSHOTS_IDX));
  60. File::CreateFullPath(File::GetUserPath(D_SHADERS_IDX));
  61. File::CreateFullPath(File::GetUserPath(D_SHADERS_IDX) + ANAGLYPH_DIR DIR_SEP);
  62. File::CreateFullPath(File::GetUserPath(D_STATESAVES_IDX));
  63. File::CreateFullPath(File::GetUserPath(D_THEMES_IDX));
  64. }
  65. void SetUserDirectory(const std::string& custom_path)
  66. {
  67. if (!custom_path.empty())
  68. {
  69. File::CreateFullPath(custom_path + DIR_SEP);
  70. File::SetUserPath(D_USER_IDX, custom_path + DIR_SEP);
  71. return;
  72. }
  73. std::string user_path = "";
  74. #ifdef _WIN32
  75. // Detect where the User directory is. There are five different cases (on top of the
  76. // command line flag, which overrides all this):
  77. // 1. GetExeDirectory()\portable.txt exists
  78. // -> Use GetExeDirectory()\User
  79. // 2. HKCU\Software\Dolphin Emulator\LocalUserConfig exists and is true
  80. // -> Use GetExeDirectory()\User
  81. // 3. HKCU\Software\Dolphin Emulator\UserConfigPath exists
  82. // -> Use this as the user directory path
  83. // 4. My Documents exists
  84. // -> Use My Documents\Dolphin Emulator as the User directory path
  85. // 5. Default
  86. // -> Use GetExeDirectory()\User
  87. // Check our registry keys
  88. HKEY hkey;
  89. DWORD local = 0;
  90. TCHAR configPath[MAX_PATH] = {0};
  91. if (RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Software\\Dolphin Emulator"), 0, KEY_QUERY_VALUE, &hkey) == ERROR_SUCCESS)
  92. {
  93. DWORD size = 4;
  94. if (RegQueryValueEx(hkey, TEXT("LocalUserConfig"), nullptr, nullptr, reinterpret_cast<LPBYTE>(&local), &size) != ERROR_SUCCESS)
  95. local = 0;
  96. size = MAX_PATH;
  97. if (RegQueryValueEx(hkey, TEXT("UserConfigPath"), nullptr, nullptr, (LPBYTE)configPath, &size) != ERROR_SUCCESS)
  98. configPath[0] = 0;
  99. RegCloseKey(hkey);
  100. }
  101. local = local || File::Exists(File::GetExeDirectory() + DIR_SEP "portable.txt");
  102. // Get Program Files path in case we need it.
  103. TCHAR my_documents[MAX_PATH];
  104. bool my_documents_found = SUCCEEDED(SHGetFolderPath(nullptr, CSIDL_MYDOCUMENTS, nullptr, SHGFP_TYPE_CURRENT, my_documents));
  105. if (local) // Case 1-2
  106. user_path = File::GetExeDirectory() + DIR_SEP USERDATA_DIR DIR_SEP;
  107. else if (configPath[0]) // Case 3
  108. user_path = TStrToUTF8(configPath);
  109. else if (my_documents_found) // Case 4
  110. user_path = TStrToUTF8(my_documents) + DIR_SEP "Dolphin Emulator" DIR_SEP;
  111. else // Case 5
  112. user_path = File::GetExeDirectory() + DIR_SEP USERDATA_DIR DIR_SEP;
  113. // Prettify the path: it will be displayed in some places, we don't want a mix of \ and /.
  114. user_path = ReplaceAll(user_path, "\\", DIR_SEP);
  115. // Make sure it ends in DIR_SEP.
  116. if (*user_path.rbegin() != DIR_SEP_CHR)
  117. user_path += DIR_SEP;
  118. #else
  119. if (File::Exists(ROOT_DIR DIR_SEP USERDATA_DIR))
  120. user_path = ROOT_DIR DIR_SEP USERDATA_DIR DIR_SEP;
  121. else
  122. user_path = std::string(getenv("HOME") ?
  123. getenv("HOME") : getenv("PWD") ?
  124. getenv("PWD") : "") + DIR_SEP DOLPHIN_DATA_DIR DIR_SEP;
  125. #endif
  126. File::SetUserPath(D_USER_IDX, user_path);
  127. }
  128. } // namespace UICommon