Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Windows/W32Util/ShellUtil.cpp
3185 views
1
#pragma warning(disable:4091) // workaround bug in VS2015 headers
2
3
#include "Windows/stdafx.h"
4
5
#include <functional>
6
#include <thread>
7
8
#include "Common/Data/Encoding/Utf8.h"
9
#include "Common/File/FileUtil.h"
10
#include "Common/Data/Format/PNGLoad.h"
11
#include "ShellUtil.h"
12
13
#include <shobjidl.h> // For IFileDialog and related interfaces
14
#include <shellapi.h>
15
#include <shlobj.h>
16
#include <commdlg.h>
17
#include <cderr.h>
18
#include <wrl/client.h>
19
20
namespace W32Util {
21
using Microsoft::WRL::ComPtr;
22
23
bool MoveToTrash(const Path &path) {
24
ComPtr<IFileOperation> pFileOp;
25
26
HRESULT hr = CoCreateInstance(CLSID_FileOperation, nullptr, CLSCTX_ALL, IID_PPV_ARGS(&pFileOp));
27
if (FAILED(hr)) {
28
return false;
29
}
30
31
// Set operation flags
32
hr = pFileOp->SetOperationFlags(FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_SILENT);
33
if (FAILED(hr)) {
34
return false;
35
}
36
37
// Create a shell item from the file path
38
ComPtr<IShellItem> pItem;
39
hr = SHCreateItemFromParsingName(path.ToWString().c_str(), nullptr, IID_PPV_ARGS(&pItem));
40
if (SUCCEEDED(hr)) {
41
// Schedule the delete (move to recycle bin)
42
hr = pFileOp->DeleteItem(pItem.Get(), nullptr);
43
if (SUCCEEDED(hr)) {
44
hr = pFileOp->PerformOperations(); // Execute
45
}
46
}
47
48
if (SUCCEEDED(hr)) {
49
INFO_LOG(Log::IO, "Moved file to trash successfully: %s", path.c_str());
50
return true;
51
} else {
52
WARN_LOG(Log::IO, "Failed to move file to trash: %s", path.c_str());
53
return false;
54
}
55
}
56
57
std::string BrowseForFolder2(HWND parent, std::string_view title, std::string_view initialPath) {
58
const std::wstring wtitle = ConvertUTF8ToWString(title);
59
const std::wstring initialDir = ConvertUTF8ToWString(initialPath);
60
61
std::wstring selectedFolder;
62
63
// Create the FileOpenDialog object
64
ComPtr<IFileDialog> pFileDialog;
65
HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pFileDialog));
66
if (!SUCCEEDED(hr)) {
67
return "";
68
}
69
// Set the options to select folders instead of files
70
DWORD dwOptions;
71
hr = pFileDialog->GetOptions(&dwOptions);
72
if (SUCCEEDED(hr)) {
73
hr = pFileDialog->SetOptions(dwOptions | FOS_PICKFOLDERS);
74
} else {
75
return "";
76
}
77
78
// Set the initial directory
79
if (!initialDir.empty()) {
80
ComPtr<IShellItem> pShellItem;
81
hr = SHCreateItemFromParsingName(initialDir.c_str(), nullptr, IID_PPV_ARGS(&pShellItem));
82
if (SUCCEEDED(hr)) {
83
hr = pFileDialog->SetFolder(pShellItem.Get());
84
}
85
}
86
pFileDialog->SetTitle(wtitle.c_str());
87
88
// Show the dialog
89
hr = pFileDialog->Show(parent);
90
if (SUCCEEDED(hr)) {
91
// Get the selected folder
92
ComPtr<IShellItem> pShellItem;
93
hr = pFileDialog->GetResult(&pShellItem);
94
if (SUCCEEDED(hr)) {
95
PWSTR pszFilePath = nullptr;
96
hr = pShellItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);
97
if (SUCCEEDED(hr)) {
98
selectedFolder = pszFilePath;
99
CoTaskMemFree(pszFilePath);
100
}
101
}
102
}
103
104
return ConvertWStringToUTF8(selectedFolder);
105
}
106
107
bool BrowseForFileName(bool _bLoad, HWND _hParent, const wchar_t *_pTitle,
108
const wchar_t *_pInitialFolder, const wchar_t *_pFilter, const wchar_t *_pExtension,
109
std::string &_strFileName) {
110
// Let's hope this is large enough, don't want to trigger the dialog twice...
111
std::wstring filenameBuffer(32768 * 10, '\0');
112
113
OPENFILENAME ofn{ sizeof(OPENFILENAME) };
114
115
auto resetFileBuffer = [&] {
116
ofn.nMaxFile = (DWORD)filenameBuffer.size();
117
ofn.lpstrFile = &filenameBuffer[0];
118
if (!_strFileName.empty())
119
wcsncpy(ofn.lpstrFile, ConvertUTF8ToWString(_strFileName).c_str(), filenameBuffer.size() - 1);
120
};
121
122
resetFileBuffer();
123
ofn.lpstrInitialDir = _pInitialFolder;
124
ofn.lpstrFilter = _pFilter;
125
ofn.lpstrFileTitle = nullptr;
126
ofn.nMaxFileTitle = 0;
127
ofn.lpstrDefExt = _pExtension;
128
ofn.hwndOwner = _hParent;
129
ofn.Flags = OFN_NOCHANGEDIR | OFN_EXPLORER;
130
if (!_bLoad)
131
ofn.Flags |= OFN_HIDEREADONLY;
132
133
int success = _bLoad ? GetOpenFileName(&ofn) : GetSaveFileName(&ofn);
134
if (success == 0 && CommDlgExtendedError() == FNERR_BUFFERTOOSMALL) {
135
size_t sz = *(unsigned short *)&filenameBuffer[0];
136
// Documentation is unclear if this is WCHARs to CHARs.
137
filenameBuffer.resize(filenameBuffer.size() + sz * 2);
138
resetFileBuffer();
139
success = _bLoad ? GetOpenFileName(&ofn) : GetSaveFileName(&ofn);
140
}
141
142
if (success) {
143
_strFileName = ConvertWStringToUTF8(ofn.lpstrFile);
144
return true;
145
}
146
return false;
147
}
148
149
std::vector<std::string> BrowseForFileNameMultiSelect(bool _bLoad, HWND _hParent, const wchar_t *_pTitle,
150
const wchar_t *_pInitialFolder, const wchar_t *_pFilter, const wchar_t *_pExtension) {
151
// Let's hope this is large enough, don't want to trigger the dialog twice...
152
std::wstring filenameBuffer(32768 * 10, '\0');
153
154
OPENFILENAME ofn{ sizeof(OPENFILENAME) };
155
156
auto resetFileBuffer = [&] {
157
ofn.nMaxFile = (DWORD)filenameBuffer.size();
158
ofn.lpstrFile = &filenameBuffer[0];
159
};
160
161
resetFileBuffer();
162
ofn.lpstrInitialDir = _pInitialFolder;
163
ofn.lpstrFilter = _pFilter;
164
ofn.lpstrFileTitle = nullptr;
165
ofn.nMaxFileTitle = 0;
166
ofn.lpstrDefExt = _pExtension;
167
ofn.hwndOwner = _hParent;
168
ofn.Flags = OFN_NOCHANGEDIR | OFN_EXPLORER | OFN_ALLOWMULTISELECT;
169
if (!_bLoad)
170
ofn.Flags |= OFN_HIDEREADONLY;
171
172
std::vector<std::string> files;
173
int success = _bLoad ? GetOpenFileName(&ofn) : GetSaveFileName(&ofn);
174
if (success == 0 && CommDlgExtendedError() == FNERR_BUFFERTOOSMALL) {
175
size_t sz = *(unsigned short *)&filenameBuffer[0];
176
// Documentation is unclear if this is WCHARs to CHARs.
177
filenameBuffer.resize(filenameBuffer.size() + sz * 2);
178
resetFileBuffer();
179
success = _bLoad ? GetOpenFileName(&ofn) : GetSaveFileName(&ofn);
180
}
181
182
if (success) {
183
std::string directory = ConvertWStringToUTF8(ofn.lpstrFile);
184
wchar_t *temp = ofn.lpstrFile;
185
temp += wcslen(temp) + 1;
186
if (*temp == 0) {
187
//we only got one file
188
files.push_back(directory);
189
} else {
190
while (*temp) {
191
files.emplace_back(directory + "\\" + ConvertWStringToUTF8(temp));
192
temp += wcslen(temp) + 1;
193
}
194
}
195
}
196
return files;
197
}
198
199
std::string UserDocumentsPath() {
200
std::string result;
201
HMODULE shell32 = GetModuleHandle(L"shell32.dll");
202
typedef HRESULT(WINAPI *SHGetKnownFolderPath_f)(REFKNOWNFOLDERID rfid, DWORD dwFlags, HANDLE hToken, PWSTR *ppszPath);
203
SHGetKnownFolderPath_f SHGetKnownFolderPath_ = nullptr;
204
if (shell32)
205
SHGetKnownFolderPath_ = (SHGetKnownFolderPath_f)GetProcAddress(shell32, "SHGetKnownFolderPath");
206
if (SHGetKnownFolderPath_) {
207
PWSTR path = nullptr;
208
if (SHGetKnownFolderPath_(FOLDERID_Documents, 0, nullptr, &path) == S_OK) {
209
result = ConvertWStringToUTF8(path);
210
}
211
if (path)
212
CoTaskMemFree(path);
213
} else {
214
wchar_t path[MAX_PATH];
215
if (SHGetFolderPath(nullptr, CSIDL_PERSONAL, nullptr, SHGFP_TYPE_CURRENT, path) == S_OK) {
216
result = ConvertWStringToUTF8(path);
217
}
218
}
219
220
return result;
221
}
222
223
224
// http://msdn.microsoft.com/en-us/library/aa969393.aspx
225
static HRESULT CreateLink(LPCWSTR lpszPathObj, LPCWSTR lpszArguments, LPCWSTR lpszPathLink, LPCWSTR lpszDesc, LPCWSTR lpszIcon, int iconIndex) {
226
HRESULT hres;
227
ComPtr<IShellLink> psl;
228
hres = CoInitializeEx(NULL, COINIT_MULTITHREADED);
229
if (FAILED(hres))
230
return hres;
231
232
// Get a pointer to the IShellLink interface. It is assumed that CoInitialize
233
// has already been called.
234
hres = CoCreateInstance(__uuidof(ShellLink), NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&psl));
235
if (SUCCEEDED(hres) && psl) {
236
ComPtr<IPersistFile> ppf;
237
238
// Set the path to the shortcut target and add the description.
239
psl->SetPath(lpszPathObj);
240
psl->SetArguments(lpszArguments);
241
psl->SetDescription(lpszDesc);
242
if (lpszIcon) {
243
psl->SetIconLocation(lpszIcon, iconIndex);
244
}
245
246
// Query IShellLink for the IPersistFile interface, used for saving the
247
// shortcut in persistent storage.
248
hres = psl.As(&ppf);
249
250
if (SUCCEEDED(hres) && ppf) {
251
// Save the link by calling IPersistFile::Save.
252
hres = ppf->Save(lpszPathLink, TRUE);
253
}
254
}
255
CoUninitialize();
256
257
return hres;
258
}
259
260
bool CreateDesktopShortcut(std::string_view argumentPath, std::string_view gameTitleStr, const Path &icoFile) {
261
// Get the desktop folder
262
wchar_t *pathbuf = new wchar_t[4096];
263
SHGetFolderPath(0, CSIDL_DESKTOPDIRECTORY, NULL, SHGFP_TYPE_CURRENT, pathbuf);
264
265
std::string gameTitle(gameTitleStr);
266
// Sanitize the game title for banned characters.
267
const char bannedChars[] = "<>:\"/\\|?*";
268
for (size_t i = 0; i < gameTitle.size(); i++) {
269
for (char c : bannedChars) {
270
if (gameTitle[i] == c) {
271
gameTitle[i] = '_';
272
break;
273
}
274
}
275
}
276
277
wcscat(pathbuf, L"\\");
278
wcscat(pathbuf, ConvertUTF8ToWString(gameTitle).c_str());
279
wcscat(pathbuf, L".lnk");
280
281
std::wstring moduleFilename;
282
size_t sz;
283
do {
284
moduleFilename.resize(moduleFilename.size() + MAX_PATH);
285
// On failure, this will return the same value as passed in, but success will always be one lower.
286
sz = GetModuleFileName(nullptr, &moduleFilename[0], (DWORD)moduleFilename.size());
287
} while (sz >= moduleFilename.size());
288
moduleFilename.resize(sz);
289
290
// Need to flip the slashes in the filename.
291
292
std::string sanitizedArgument(argumentPath);
293
for (size_t i = 0; i < sanitizedArgument.size(); i++) {
294
if (sanitizedArgument[i] == '/') {
295
sanitizedArgument[i] = '\\';
296
}
297
}
298
299
sanitizedArgument = "\"" + sanitizedArgument + "\"";
300
301
std::wstring icon;
302
if (!icoFile.empty()) {
303
icon = icoFile.ToWString();
304
}
305
306
CreateLink(moduleFilename.c_str(), ConvertUTF8ToWString(sanitizedArgument).c_str(), pathbuf, ConvertUTF8ToWString(gameTitle).c_str(), icon.empty() ? nullptr : icon.c_str(), 0);
307
308
// TODO: Also extract the game icon and convert to .ico, put it somewhere under Memstick, and set it.
309
310
delete[] pathbuf;
311
return false;
312
}
313
314
// Function to create an icon file from PNG image data (these icons require Windows Vista).
315
// The Old New Thing comes to the rescue again! ChatGPT failed miserably.
316
// https://devblogs.microsoft.com/oldnewthing/20101018-00/?p=12513
317
// https://devblogs.microsoft.com/oldnewthing/20101022-00/?p=12473
318
bool CreateICOFromPNGData(const uint8_t *imageData, size_t imageDataSize, const Path &icoPath) {
319
if (imageDataSize <= sizeof(PNGHeaderPeek)) {
320
return false;
321
}
322
// Parse the PNG
323
PNGHeaderPeek pngHeader;
324
memcpy(&pngHeader, imageData, sizeof(PNGHeaderPeek));
325
if (pngHeader.Width() > 256 || pngHeader.Height() > 256) {
326
// Reject the png as an icon.
327
return false;
328
}
329
330
struct IconHeader {
331
uint16_t reservedZero;
332
uint16_t type; // should be 1
333
uint16_t imageCount;
334
};
335
IconHeader hdr{ 0, 1, 1 };
336
struct IconDirectoryEntry {
337
BYTE bWidth;
338
BYTE bHeight;
339
BYTE bColorCount;
340
BYTE bReserved;
341
WORD wPlanes;
342
WORD wBitCount;
343
DWORD dwBytesInRes;
344
DWORD dwImageOffset;
345
};
346
IconDirectoryEntry entry{};
347
entry.bWidth = pngHeader.Width();
348
entry.bHeight = pngHeader.Height();
349
entry.bColorCount = 0;
350
entry.dwBytesInRes = (DWORD)imageDataSize;
351
entry.wPlanes = 1;
352
entry.wBitCount = 32;
353
entry.dwImageOffset = sizeof(hdr) + sizeof(entry);
354
355
FILE *file = File::OpenCFile(icoPath, "wb");
356
if (!file) {
357
return false;
358
}
359
fwrite(&hdr, sizeof(hdr), 1, file);
360
fwrite(&entry, sizeof(entry), 1, file);
361
fwrite(imageData, 1, imageDataSize, file);
362
fclose(file);
363
return true;
364
}
365
366
} // namespace W32Util
367
368