1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
4 
5   This software is provided 'as-is', without any express or implied
6   warranty.  In no event will the authors be held liable for any damages
7   arising from the use of this software.
8 
9   Permission is granted to anyone to use this software for any purpose,
10   including commercial applications, and to alter it and redistribute it
11   freely, subject to the following restrictions:
12 
13   1. The origin of this software must not be misrepresented; you must not
14      claim that you wrote the original software. If you use this software
15      in a product, an acknowledgment in the product documentation would be
16      appreciated but is not required.
17   2. Altered source versions must be plainly marked as such, and must not be
18      misrepresented as being the original software.
19   3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "../../SDL_internal.h"
22 
23 #ifdef SDL_FILESYSTEM_WINDOWS
24 
25 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
26 /* System dependent filesystem routines                                */
27 
28 #include "../../core/windows/SDL_windows.h"
29 #include <shlobj.h>
30 
31 #include "SDL_assert.h"
32 #include "SDL_error.h"
33 #include "SDL_stdinc.h"
34 #include "SDL_filesystem.h"
35 
36 char *
SDL_GetBasePath(void)37 SDL_GetBasePath(void)
38 {
39     typedef DWORD (WINAPI *GetModuleFileNameExW_t)(HANDLE, HMODULE, LPWSTR, DWORD);
40     GetModuleFileNameExW_t pGetModuleFileNameExW;
41     DWORD buflen = 128;
42     WCHAR *path = NULL;
43     HANDLE psapi = LoadLibrary(L"psapi.dll");
44     char *retval = NULL;
45     DWORD len = 0;
46     int i;
47 
48     if (!psapi) {
49         WIN_SetError("Couldn't load psapi.dll");
50         return NULL;
51     }
52 
53     pGetModuleFileNameExW = (GetModuleFileNameExW_t)GetProcAddress(psapi, "GetModuleFileNameExW");
54     if (!pGetModuleFileNameExW) {
55         WIN_SetError("Couldn't find GetModuleFileNameExW");
56         FreeLibrary(psapi);
57         return NULL;
58     }
59 
60     while (SDL_TRUE) {
61         void *ptr = SDL_realloc(path, buflen * sizeof (WCHAR));
62         if (!ptr) {
63             SDL_free(path);
64             FreeLibrary(psapi);
65             SDL_OutOfMemory();
66             return NULL;
67         }
68 
69         path = (WCHAR *) ptr;
70 
71         len = pGetModuleFileNameExW(GetCurrentProcess(), NULL, path, buflen);
72         if (len != buflen) {
73             break;
74         }
75 
76         /* buffer too small? Try again. */
77         buflen *= 2;
78     }
79 
80     FreeLibrary(psapi);
81 
82     if (len == 0) {
83         SDL_free(path);
84         WIN_SetError("Couldn't locate our .exe");
85         return NULL;
86     }
87 
88     for (i = len-1; i > 0; i--) {
89         if (path[i] == '\\') {
90             break;
91         }
92     }
93 
94     SDL_assert(i > 0); /* Should have been an absolute path. */
95     path[i+1] = '\0';  /* chop off filename. */
96 
97     retval = WIN_StringToUTF8(path);
98     SDL_free(path);
99 
100     return retval;
101 }
102 
103 char *
SDL_GetPrefPath(const char * org,const char * app)104 SDL_GetPrefPath(const char *org, const char *app)
105 {
106     /*
107      * Vista and later has a new API for this, but SHGetFolderPath works there,
108      *  and apparently just wraps the new API. This is the new way to do it:
109      *
110      *     SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_CREATE,
111      *                          NULL, &wszPath);
112      */
113 
114     WCHAR path[MAX_PATH];
115     char *retval = NULL;
116     WCHAR* worg = NULL;
117     WCHAR* wapp = NULL;
118     size_t new_wpath_len = 0;
119     BOOL api_result = FALSE;
120 
121     if (!app) {
122         SDL_InvalidParamError("app");
123         return NULL;
124     }
125     if (!org) {
126         org = "";
127     }
128 
129     if (!SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, path))) {
130         WIN_SetError("Couldn't locate our prefpath");
131         return NULL;
132     }
133 
134     worg = WIN_UTF8ToString(org);
135     if (worg == NULL) {
136         SDL_OutOfMemory();
137         return NULL;
138     }
139 
140     wapp = WIN_UTF8ToString(app);
141     if (wapp == NULL) {
142         SDL_free(worg);
143         SDL_OutOfMemory();
144         return NULL;
145     }
146 
147     new_wpath_len = lstrlenW(worg) + lstrlenW(wapp) + lstrlenW(path) + 3;
148 
149     if ((new_wpath_len + 1) > MAX_PATH) {
150         SDL_free(worg);
151         SDL_free(wapp);
152         WIN_SetError("Path too long.");
153         return NULL;
154     }
155 
156     if (*worg) {
157         lstrcatW(path, L"\\");
158         lstrcatW(path, worg);
159     }
160     SDL_free(worg);
161 
162     api_result = CreateDirectoryW(path, NULL);
163     if (api_result == FALSE) {
164         if (GetLastError() != ERROR_ALREADY_EXISTS) {
165             SDL_free(wapp);
166             WIN_SetError("Couldn't create a prefpath.");
167             return NULL;
168         }
169     }
170 
171     lstrcatW(path, L"\\");
172     lstrcatW(path, wapp);
173     SDL_free(wapp);
174 
175     api_result = CreateDirectoryW(path, NULL);
176     if (api_result == FALSE) {
177         if (GetLastError() != ERROR_ALREADY_EXISTS) {
178             WIN_SetError("Couldn't create a prefpath.");
179             return NULL;
180         }
181     }
182 
183     lstrcatW(path, L"\\");
184 
185     retval = WIN_StringToUTF8(path);
186 
187     return retval;
188 }
189 
190 #endif /* SDL_FILESYSTEM_WINDOWS */
191 
192 /* vi: set ts=4 sw=4 expandtab: */
193