SDL_sysfilesystem.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2020 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 __WINRT__
  22. extern "C" {
  23. #include "SDL_filesystem.h"
  24. #include "SDL_error.h"
  25. #include "SDL_hints.h"
  26. #include "SDL_stdinc.h"
  27. #include "SDL_system.h"
  28. #include "../../core/windows/SDL_windows.h"
  29. }
  30. #include <string>
  31. #include <unordered_map>
  32. using namespace std;
  33. using namespace Windows::Storage;
  34. extern "C" const wchar_t *
  35. SDL_WinRTGetFSPathUNICODE(SDL_WinRT_Path pathType)
  36. {
  37. switch (pathType) {
  38. case SDL_WINRT_PATH_INSTALLED_LOCATION:
  39. {
  40. static wstring path;
  41. if (path.empty()) {
  42. #if defined(NTDDI_WIN10_19H1) && (NTDDI_VERSION >= NTDDI_WIN10_19H1) && (WINAPI_FAMILY == WINAPI_FAMILY_PC_APP) /* Only PC supports mods */
  43. /* Windows 1903 supports mods, via the EffectiveLocation API */
  44. if (Windows::Foundation::Metadata::ApiInformation::IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8, 0)) {
  45. path = Windows::ApplicationModel::Package::Current->EffectiveLocation->Path->Data();
  46. } else {
  47. path = Windows::ApplicationModel::Package::Current->InstalledLocation->Path->Data();
  48. }
  49. #else
  50. path = Windows::ApplicationModel::Package::Current->InstalledLocation->Path->Data();
  51. #endif
  52. }
  53. return path.c_str();
  54. }
  55. case SDL_WINRT_PATH_LOCAL_FOLDER:
  56. {
  57. static wstring path;
  58. if (path.empty()) {
  59. path = ApplicationData::Current->LocalFolder->Path->Data();
  60. }
  61. return path.c_str();
  62. }
  63. #if (WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP) || (NTDDI_VERSION > NTDDI_WIN8)
  64. case SDL_WINRT_PATH_ROAMING_FOLDER:
  65. {
  66. static wstring path;
  67. if (path.empty()) {
  68. path = ApplicationData::Current->RoamingFolder->Path->Data();
  69. }
  70. return path.c_str();
  71. }
  72. case SDL_WINRT_PATH_TEMP_FOLDER:
  73. {
  74. static wstring path;
  75. if (path.empty()) {
  76. path = ApplicationData::Current->TemporaryFolder->Path->Data();
  77. }
  78. return path.c_str();
  79. }
  80. #endif
  81. default:
  82. break;
  83. }
  84. SDL_Unsupported();
  85. return NULL;
  86. }
  87. extern "C" const char *
  88. SDL_WinRTGetFSPathUTF8(SDL_WinRT_Path pathType)
  89. {
  90. typedef unordered_map<SDL_WinRT_Path, string> UTF8PathMap;
  91. static UTF8PathMap utf8Paths;
  92. UTF8PathMap::iterator searchResult = utf8Paths.find(pathType);
  93. if (searchResult != utf8Paths.end()) {
  94. return searchResult->second.c_str();
  95. }
  96. const wchar_t * ucs2Path = SDL_WinRTGetFSPathUNICODE(pathType);
  97. if (!ucs2Path) {
  98. return NULL;
  99. }
  100. char * utf8Path = WIN_StringToUTF8(ucs2Path);
  101. utf8Paths[pathType] = utf8Path;
  102. SDL_free(utf8Path);
  103. return utf8Paths[pathType].c_str();
  104. }
  105. extern "C" char *
  106. SDL_GetBasePath(void)
  107. {
  108. const char * srcPath = SDL_WinRTGetFSPathUTF8(SDL_WINRT_PATH_INSTALLED_LOCATION);
  109. size_t destPathLen;
  110. char * destPath = NULL;
  111. if (!srcPath) {
  112. SDL_SetError("Couldn't locate our basepath: %s", SDL_GetError());
  113. return NULL;
  114. }
  115. destPathLen = SDL_strlen(srcPath) + 2;
  116. destPath = (char *) SDL_malloc(destPathLen);
  117. if (!destPath) {
  118. SDL_OutOfMemory();
  119. return NULL;
  120. }
  121. SDL_snprintf(destPath, destPathLen, "%s\\", srcPath);
  122. return destPath;
  123. }
  124. extern "C" char *
  125. SDL_GetPrefPath(const char *org, const char *app)
  126. {
  127. /* WinRT note: The 'SHGetFolderPath' API that is used in Windows 7 and
  128. * earlier is not available on WinRT or Windows Phone. WinRT provides
  129. * a similar API, but SHGetFolderPath can't be called, at least not
  130. * without violating Microsoft's app-store requirements.
  131. */
  132. const WCHAR * srcPath = NULL;
  133. WCHAR path[MAX_PATH];
  134. char *retval = NULL;
  135. WCHAR* worg = NULL;
  136. WCHAR* wapp = NULL;
  137. size_t new_wpath_len = 0;
  138. BOOL api_result = FALSE;
  139. if (!app) {
  140. SDL_InvalidParamError("app");
  141. return NULL;
  142. }
  143. if (!org) {
  144. org = "";
  145. }
  146. srcPath = SDL_WinRTGetFSPathUNICODE(SDL_WINRT_PATH_LOCAL_FOLDER);
  147. if ( ! srcPath) {
  148. SDL_SetError("Unable to find a source path");
  149. return NULL;
  150. }
  151. if (SDL_wcslen(srcPath) >= MAX_PATH) {
  152. SDL_SetError("Path too long.");
  153. return NULL;
  154. }
  155. SDL_wcslcpy(path, srcPath, SDL_arraysize(path));
  156. worg = WIN_UTF8ToString(org);
  157. if (worg == NULL) {
  158. SDL_OutOfMemory();
  159. return NULL;
  160. }
  161. wapp = WIN_UTF8ToString(app);
  162. if (wapp == NULL) {
  163. SDL_free(worg);
  164. SDL_OutOfMemory();
  165. return NULL;
  166. }
  167. new_wpath_len = SDL_wcslen(worg) + SDL_wcslen(wapp) + SDL_wcslen(path) + 3;
  168. if ((new_wpath_len + 1) > MAX_PATH) {
  169. SDL_free(worg);
  170. SDL_free(wapp);
  171. SDL_SetError("Path too long.");
  172. return NULL;
  173. }
  174. if (*worg) {
  175. SDL_wcslcat(path, L"\\", new_wpath_len + 1);
  176. SDL_wcslcat(path, worg, new_wpath_len + 1);
  177. SDL_free(worg);
  178. }
  179. api_result = CreateDirectoryW(path, NULL);
  180. if (api_result == FALSE) {
  181. if (GetLastError() != ERROR_ALREADY_EXISTS) {
  182. SDL_free(wapp);
  183. WIN_SetError("Couldn't create a prefpath.");
  184. return NULL;
  185. }
  186. }
  187. SDL_wcslcat(path, L"\\", new_wpath_len + 1);
  188. SDL_wcslcat(path, wapp, new_wpath_len + 1);
  189. SDL_free(wapp);
  190. api_result = CreateDirectoryW(path, NULL);
  191. if (api_result == FALSE) {
  192. if (GetLastError() != ERROR_ALREADY_EXISTS) {
  193. WIN_SetError("Couldn't create a prefpath.");
  194. return NULL;
  195. }
  196. }
  197. SDL_wcslcat(path, L"\\", new_wpath_len + 1);
  198. retval = WIN_StringToUTF8(path);
  199. return retval;
  200. }
  201. #endif /* __WINRT__ */
  202. /* vi: set ts=4 sw=4 expandtab: */