SDL_sysfilesystem.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. /* TODO, WinRT: remove the need to compile this with C++/CX (/ZW) extensions, and if possible, without C++ at all
  20. */
  21. #ifdef SDL_PLATFORM_WINRT
  22. extern "C" {
  23. #include "../../core/windows/SDL_windows.h"
  24. }
  25. #include <string>
  26. #include <unordered_map>
  27. using namespace std;
  28. using namespace Windows::Storage;
  29. static const wchar_t *SDL_WinRTGetFSPathUNICODE(SDL_WinRT_Path pathType)
  30. {
  31. switch (pathType) {
  32. case SDL_WINRT_PATH_INSTALLED_LOCATION:
  33. {
  34. static wstring path;
  35. if (path.empty()) {
  36. #if defined(NTDDI_WIN10_19H1) && (NTDDI_VERSION >= NTDDI_WIN10_19H1) && (WINAPI_FAMILY == WINAPI_FAMILY_PC_APP) /* Only PC supports mods */
  37. /* Windows 1903 supports mods, via the EffectiveLocation API */
  38. if (Windows::Foundation::Metadata::ApiInformation::IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8, 0)) {
  39. path = Windows::ApplicationModel::Package::Current->EffectiveLocation->Path->Data();
  40. } else {
  41. path = Windows::ApplicationModel::Package::Current->InstalledLocation->Path->Data();
  42. }
  43. #else
  44. path = Windows::ApplicationModel::Package::Current->InstalledLocation->Path->Data();
  45. #endif
  46. }
  47. return path.c_str();
  48. }
  49. case SDL_WINRT_PATH_LOCAL_FOLDER:
  50. {
  51. static wstring path;
  52. if (path.empty()) {
  53. path = ApplicationData::Current->LocalFolder->Path->Data();
  54. }
  55. return path.c_str();
  56. }
  57. #if !SDL_WINAPI_FAMILY_PHONE || NTDDI_VERSION > NTDDI_WIN8
  58. case SDL_WINRT_PATH_ROAMING_FOLDER:
  59. {
  60. static wstring path;
  61. if (path.empty()) {
  62. path = ApplicationData::Current->RoamingFolder->Path->Data();
  63. }
  64. return path.c_str();
  65. }
  66. case SDL_WINRT_PATH_TEMP_FOLDER:
  67. {
  68. static wstring path;
  69. if (path.empty()) {
  70. path = ApplicationData::Current->TemporaryFolder->Path->Data();
  71. }
  72. return path.c_str();
  73. }
  74. #endif
  75. default:
  76. break;
  77. }
  78. SDL_Unsupported();
  79. return NULL;
  80. }
  81. extern "C" const char *SDL_WinRTGetFSPath(SDL_WinRT_Path pathType)
  82. {
  83. typedef unordered_map<SDL_WinRT_Path, string> UTF8PathMap;
  84. static UTF8PathMap utf8Paths;
  85. UTF8PathMap::iterator searchResult = utf8Paths.find(pathType);
  86. if (searchResult != utf8Paths.end()) {
  87. return searchResult->second.c_str();
  88. }
  89. const wchar_t *ucs2Path = SDL_WinRTGetFSPathUNICODE(pathType);
  90. if (!ucs2Path) {
  91. return NULL;
  92. }
  93. char *utf8Path = WIN_StringToUTF8(ucs2Path);
  94. utf8Paths[pathType] = utf8Path;
  95. SDL_free(utf8Path);
  96. return utf8Paths[pathType].c_str();
  97. }
  98. extern "C" char *SDL_GetBasePath(void)
  99. {
  100. const char *srcPath = SDL_WinRTGetFSPath(SDL_WINRT_PATH_INSTALLED_LOCATION);
  101. size_t destPathLen;
  102. char *destPath = NULL;
  103. if (!srcPath) {
  104. SDL_SetError("Couldn't locate our basepath: %s", SDL_GetError());
  105. return NULL;
  106. }
  107. destPathLen = SDL_strlen(srcPath) + 2;
  108. destPath = (char *)SDL_malloc(destPathLen);
  109. if (!destPath) {
  110. return NULL;
  111. }
  112. SDL_snprintf(destPath, destPathLen, "%s\\", srcPath);
  113. return destPath;
  114. }
  115. extern "C" char *SDL_GetPrefPath(const char *org, const char *app)
  116. {
  117. /* WinRT note: The 'SHGetFolderPath' API that is used in Windows 7 and
  118. * earlier is not available on WinRT or Windows Phone. WinRT provides
  119. * a similar API, but SHGetFolderPath can't be called, at least not
  120. * without violating Microsoft's app-store requirements.
  121. */
  122. const WCHAR *srcPath = NULL;
  123. WCHAR path[MAX_PATH];
  124. char *retval = NULL;
  125. WCHAR *worg = NULL;
  126. WCHAR *wapp = NULL;
  127. size_t new_wpath_len = 0;
  128. BOOL api_result = FALSE;
  129. if (!app) {
  130. SDL_InvalidParamError("app");
  131. return NULL;
  132. }
  133. if (!org) {
  134. org = "";
  135. }
  136. srcPath = SDL_WinRTGetFSPathUNICODE(SDL_WINRT_PATH_LOCAL_FOLDER);
  137. if (!srcPath) {
  138. SDL_SetError("Unable to find a source path");
  139. return NULL;
  140. }
  141. if (SDL_wcslen(srcPath) >= MAX_PATH) {
  142. SDL_SetError("Path too long.");
  143. return NULL;
  144. }
  145. SDL_wcslcpy(path, srcPath, SDL_arraysize(path));
  146. worg = WIN_UTF8ToString(org);
  147. if (!worg) {
  148. return NULL;
  149. }
  150. wapp = WIN_UTF8ToString(app);
  151. if (!wapp) {
  152. SDL_free(worg);
  153. return NULL;
  154. }
  155. new_wpath_len = SDL_wcslen(worg) + SDL_wcslen(wapp) + SDL_wcslen(path) + 3;
  156. if ((new_wpath_len + 1) > MAX_PATH) {
  157. SDL_free(worg);
  158. SDL_free(wapp);
  159. SDL_SetError("Path too long.");
  160. return NULL;
  161. }
  162. if (*worg) {
  163. SDL_wcslcat(path, L"\\", new_wpath_len + 1);
  164. SDL_wcslcat(path, worg, new_wpath_len + 1);
  165. SDL_free(worg);
  166. }
  167. api_result = CreateDirectoryW(path, NULL);
  168. if (api_result == FALSE) {
  169. if (GetLastError() != ERROR_ALREADY_EXISTS) {
  170. SDL_free(wapp);
  171. WIN_SetError("Couldn't create a prefpath.");
  172. return NULL;
  173. }
  174. }
  175. SDL_wcslcat(path, L"\\", new_wpath_len + 1);
  176. SDL_wcslcat(path, wapp, new_wpath_len + 1);
  177. SDL_free(wapp);
  178. api_result = CreateDirectoryW(path, NULL);
  179. if (api_result == FALSE) {
  180. if (GetLastError() != ERROR_ALREADY_EXISTS) {
  181. WIN_SetError("Couldn't create a prefpath.");
  182. return NULL;
  183. }
  184. }
  185. SDL_wcslcat(path, L"\\", new_wpath_len + 1);
  186. retval = WIN_StringToUTF8(path);
  187. return retval;
  188. }
  189. char *SDL_GetUserFolder(SDL_Folder folder)
  190. {
  191. wstring wpath;
  192. switch (folder) {
  193. #define CASEPATH(sym, var) case sym: wpath = Windows::Storage::UserDataPaths::GetDefault()->var->Data(); break
  194. CASEPATH(SDL_FOLDER_HOME, Profile);
  195. CASEPATH(SDL_FOLDER_DESKTOP, Desktop);
  196. CASEPATH(SDL_FOLDER_DOCUMENTS, Documents);
  197. CASEPATH(SDL_FOLDER_DOWNLOADS, Downloads);
  198. CASEPATH(SDL_FOLDER_MUSIC, Music);
  199. CASEPATH(SDL_FOLDER_PICTURES, Pictures);
  200. CASEPATH(SDL_FOLDER_SCREENSHOTS, Screenshots);
  201. CASEPATH(SDL_FOLDER_TEMPLATES, Templates);
  202. CASEPATH(SDL_FOLDER_VIDEOS, Videos);
  203. #undef CASEPATH
  204. #define UNSUPPPORTED_CASEPATH(sym) SDL_SetError("The %s folder is unsupported on WinRT", #sym); return NULL;
  205. UNSUPPPORTED_CASEPATH(SDL_FOLDER_PUBLICSHARE);
  206. UNSUPPPORTED_CASEPATH(SDL_FOLDER_SAVEDGAMES);
  207. #undef UNSUPPPORTED_CASEPATH
  208. default:
  209. SDL_SetError("Invalid SDL_Folder: %d", (int)folder);
  210. return NULL;
  211. };
  212. return WIN_StringToUTF8(wpath.c_str());
  213. }
  214. #endif /* SDL_PLATFORM_WINRT */