-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHostObjectImplFiles.cpp
300 lines (226 loc) · 8.1 KB
/
HostObjectImplFiles.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/** $VER: HostObjectImplFiles.cpp (2024.12.02) P. Stuer **/
#include "pch.h"
#include "HostObjectImpl.h"
#include <pathcch.h>
#pragma comment(lib, "pathcch")
#pragma comment(lib, "crypt32")
#include "Support.h"
#include "Resources.h"
#include "Encoding.h"
#include "ProcessLocationsHandler.h"
#include <SDK/titleformat.h>
#include <SDK/playlist.h>
#include <SDK/ui.h>
#include <SDK/contextmenu.h>
#include <pfc/string-conv-lite.h>
#include <pfc/bit_array_impl.h>
/// <summary>
/// Gets the specified artwork of the currently selected item in the current playlist.
/// </summary>
STDMETHODIMP HostObject::getArtwork(BSTR type, BSTR * image)
{
*image = ::SysAllocString(L""); // Return an empty string by default and in case of an error.
if (type == nullptr)
return E_INVALIDARG;
// Verify the requested artwork type.
GUID AlbumArtId;
if (::_wcsicmp(type, L"front") == 0)
{
AlbumArtId = album_art_ids::cover_front;
}
else
if (::_wcsicmp(type, L"back") == 0)
{
AlbumArtId = album_art_ids::cover_back;
}
else
if (::_wcsicmp(type, L"disc") == 0)
{
AlbumArtId = album_art_ids::disc;
}
else
if (::_wcsicmp(type, L"icon") == 0)
{
AlbumArtId = album_art_ids::icon;
}
else
if (::_wcsicmp(type, L"artist") == 0)
{
AlbumArtId = album_art_ids::artist;
}
else
return S_OK;
metadb_handle_ptr Handle;
if (!_PlaybackControl->get_now_playing(Handle))
return S_OK;
static_api_ptr_t<album_art_manager_v3> Manager;
album_art_data::ptr aad;
try
{
album_art_extractor_instance_v2::ptr Extractor = Manager->open_v3(pfc::list_single_ref_t<metadb_handle_ptr>(Handle), pfc::list_single_ref_t<GUID>(AlbumArtId), nullptr, fb2k::noAbort);
if (Extractor.is_empty())
return S_OK;
// Query the external search patterns first.
try
{
album_art_path_list::ptr Paths = Extractor->query_paths(AlbumArtId, fb2k::noAbort);
if (Paths.is_valid())
{
for (size_t i = 0; i < Paths->get_count(); ++i)
{
pfc::string Extension = pfc::io::path::getFileExtension(Paths->get_path(i));
if (!Extension.isEmpty() && ((::_stricmp(Extension.c_str(), ".jpg") == 0) || (::_stricmp(Extension.c_str(), ".png") == 0) || (::_stricmp(Extension.c_str(), ".webp") == 0) || (::_stricmp(Extension.c_str(), ".gif") == 0)))
{
::SysFreeString(*image); // Free the empty string.
*image = ::SysAllocString(::UTF8ToWide(Paths->get_path(i)).c_str());
return S_OK;
}
}
}
}
catch (...)
{
}
// Query the embedded art.
if (!Extractor->query(AlbumArtId, aad, fb2k::noAbort))
{
// Query the stub the stub path.
try
{
Extractor = Manager->open_stub(fb2k::noAbort);
if (!Extractor->query(AlbumArtId, aad, fb2k::noAbort))
return S_OK;
}
catch (std::exception & e)
{
console::print(STR_COMPONENT_BASENAME " failed to query album art stub: ", e.what());
}
}
}
catch (...)
{
// Query the stub the stub path.
try
{
album_art_extractor_instance_v2::ptr Extractor = Manager->open_stub(fb2k::noAbort);
if (!Extractor->query(AlbumArtId, aad, fb2k::noAbort))
return S_OK;
}
catch (std::exception & e)
{
console::print(STR_COMPONENT_BASENAME " failed to query album art stub: ", e.what());
}
}
if (!aad.is_empty())
ToBase64((const BYTE *) aad->data(), (DWORD) aad->size(), image);
return S_OK;
}
#pragma region Files
/// <summary>
/// Reads the specified file and returns it as a string.
/// </summary>
STDMETHODIMP HostObject::readAllText(BSTR filePath, __int32 codePage, BSTR * text)
{
if ((filePath == nullptr) || (text == nullptr))
return E_INVALIDARG;
if (codePage == 0)
codePage = 65001;
HANDLE hFile = ::CreateFileW(filePath, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (hFile == INVALID_HANDLE_VALUE)
return HRESULT_FROM_WIN32(::GetLastError());
LARGE_INTEGER FileSize;
HRESULT hr = S_OK;
if (::GetFileSizeEx(hFile, &FileSize))
{
if ((FileSize.HighPart == 0) && (FileSize.LowPart < 0xFFFFFFFF - 2))
{
std::string Text;
Text.resize((size_t) FileSize.LowPart + 2);
DWORD BytesRead;
if (::ReadFile(hFile, (void *) Text.c_str(), FileSize.LowPart, &BytesRead, nullptr) && (BytesRead == FileSize.LowPart))
*text = ::SysAllocString(::CodePageToWide((uint32_t) codePage, Text).c_str());
else
hr = HRESULT_FROM_WIN32(::GetLastError());
}
else
hr = E_OUTOFMEMORY;
}
else
hr = HRESULT_FROM_WIN32(::GetLastError());
::CloseHandle(hFile);
return hr;
}
/// <summary>
/// Reads the specified file and returns it as a string.
/// </summary>
STDMETHODIMP HostObject::readDirectory(BSTR directoryPath, BSTR searchPattern, BSTR * json)
{
if ((directoryPath == nullptr) || (searchPattern == nullptr) || (json == nullptr))
return E_INVALIDARG;
*json = ::SysAllocString(L""); // Return an empty string by default and in case of an error.
WCHAR PathName[MAX_PATH];
if (!SUCCEEDED(::PathCchCombineEx(PathName, _countof(PathName), directoryPath, searchPattern, PATHCCH_ALLOW_LONG_PATHS)))
return HRESULT_FROM_WIN32(::GetLastError());
WIN32_FIND_DATA fd = {};
HANDLE hFind = ::FindFirstFileW(PathName, &fd);
if (hFind == INVALID_HANDLE_VALUE)
return HRESULT_FROM_WIN32(::GetLastError());
BOOL Success = TRUE;
if (::wcscmp(fd.cFileName, L".") == 0)
{
Success = ::FindNextFileW(hFind, &fd);
if (Success && ::wcscmp(fd.cFileName, L"..") == 0)
Success = ::FindNextFileW(hFind, &fd);
}
std::wstring Result = L"[";
bool IsFirstItem = true;
while (Success)
{
if (!IsFirstItem)
Result.append(L",");
uint64_t FileSize = (((uint64_t) fd.nFileSizeHigh) << 32) + fd.nFileSizeLow;
Result.append(::FormatText(LR"({"name": "%s", "size": %lu, "isDirectory": %s})", fd.cFileName, FileSize, ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? L"true" : L"false")).c_str());
IsFirstItem = false;
Success = ::FindNextFileW(hFind, &fd);
}
Result.append(L"]");
::FindClose(hFind);
*json = ::SysAllocString(Result.c_str());
return S_OK;
}
/// <summary>
/// Reads the specified file and returns it as a string.
/// </summary>
STDMETHODIMP HostObject::readImage(BSTR filePath, BSTR * image)
{
*image = ::SysAllocString(L""); // Return an empty string by default and in case of an error.
if ((filePath == nullptr) || (image == nullptr))
return E_INVALIDARG;
HANDLE hFile = ::CreateFileW(filePath, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (hFile == INVALID_HANDLE_VALUE)
return HRESULT_FROM_WIN32(::GetLastError());
LARGE_INTEGER FileSize;
HRESULT hr = S_OK;
if (::GetFileSizeEx(hFile, &FileSize))
{
if (FileSize.HighPart == 0)
{
BYTE * Data = new BYTE[FileSize.LowPart];
if (Data != nullptr)
{
DWORD BytesRead;
if (::ReadFile(hFile, Data, FileSize.LowPart, &BytesRead, nullptr) && (BytesRead == FileSize.LowPart))
ToBase64(Data, FileSize.LowPart, image);
else
hr = HRESULT_FROM_WIN32(::GetLastError());
delete[] Data;
}
}
else
hr = E_OUTOFMEMORY;
}
else
hr = HRESULT_FROM_WIN32(::GetLastError());
::CloseHandle(hFile);
return S_OK;
}