-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecentFiles.cpp
More file actions
128 lines (128 loc) · 4.23 KB
/
RecentFiles.cpp
File metadata and controls
128 lines (128 loc) · 4.23 KB
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
#include "RecentFiles.h"
#include "Settings.h"
#include "resource.h"
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")
#include <algorithm>
#include <tchar.h>
HWND RecentFiles::hListBox = NULL;
std::vector<RecentFileInfo> RecentFiles::files;
std::vector<RecentFileInfo> RecentFiles::embeddedFiles;
bool RecentFiles::initialized = false;
void RecentFiles::LoadEmbeddedFiles() {
HRSRC hRes = FindResource(NULL, MAKEINTRESOURCE(IDR_EMBEDDEDSETTINGS), RT_RCDATA);
if (!hRes) return;
HGLOBAL hData = LoadResource(NULL, hRes);
if (!hData) return;
DWORD size = SizeofResource(NULL, hRes);
const BYTE* pData = (const BYTE*)LockResource(hData);
if (!pData || size < 4) return;
DWORD numFiles = *(DWORD*)pData;
const BYTE* ptr = pData + 4;
embeddedFiles.clear();
DWORD loaded = 0;
for (DWORD i = 0; i < numFiles && loaded < 50; i++) {
if (ptr + 4 > pData + size) break;
DWORD nameLen = *(DWORD*)ptr;
ptr += 4;
if (ptr + nameLen * 2 > pData + size) break;
std::basic_string<TCHAR> name;
name.resize(nameLen);
memcpy(&name[0], ptr, nameLen * 2);
ptr += nameLen * 2;
if (ptr + 8 > pData + size) break;
ULONGLONG filetime = *(ULONGLONG*)ptr;
ptr += 8;
RecentFileInfo info;
info.filename = name;
info.fullpath = name;
info.lastWriteTime.dwHighDateTime = (DWORD)(filetime >> 32);
info.lastWriteTime.dwLowDateTime = (DWORD)(filetime & 0xFFFFFFFF);
info.isEmbedded = true;
embeddedFiles.push_back(info);
loaded++;
}
initialized = true;
}
std::basic_string<TCHAR> RecentFiles::GetExecutableDirectory() {
TCHAR path[MAX_PATH];
GetModuleFileName(NULL, path, MAX_PATH);
PathRemoveFileSpec(path);
PathAddBackslash(path);
return std::basic_string<TCHAR>(path);
}
void RecentFiles::PopulateDialogList(HWND hDlg, int comboId) {
LoadEmbeddedFiles();
files.clear();
files = embeddedFiles;
std::sort(files.begin(), files.end(),
[](const RecentFileInfo& a, const RecentFileInfo& b) {
return CompareFileTime(&a.lastWriteTime, &b.lastWriteTime) > 0;
});
HWND hCombo = GetDlgItem(hDlg, comboId);
if (!hCombo) return;
SendMessage(hCombo, WM_SETREDRAW, FALSE, 0);
SendMessage(hCombo, CB_RESETCONTENT, 0, 0);
size_t count = files.size() > 50 ? 50 : files.size();
SendMessage(hCombo, CB_INITSTORAGE, count, count * 64);
for (size_t i = 0; i < count; i++) {
std::basic_string<TCHAR> displayName = files[i].filename;
size_t dotPos = displayName.find_last_of(_T('.'));
if (dotPos != std::basic_string<TCHAR>::npos) {
displayName = displayName.substr(0, dotPos);
}
LRESULT idx = SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM)displayName.c_str());
SendMessage(hCombo, CB_SETITEMDATA, idx, i);
}
SendMessage(hCombo, WM_SETREDRAW, TRUE, 0);
}
void RecentFiles::OnDialogFileSelected(HWND hDlg, int comboId, int index) {
if (index >= 0 && index < (int)files.size()) {
const RecentFileInfo& file = files[index];
if (file.isEmbedded) {
HRSRC hRes = FindResource(NULL, MAKEINTRESOURCE(IDR_EMBEDDEDSETTINGS), RT_RCDATA);
if (hRes) {
HGLOBAL hData = LoadResource(NULL, hRes);
if (hData) {
MHSettings::OpenMHookConfig(hDlg, (TCHAR*)file.fullpath.c_str());
MHSettings::AfterLoad(hDlg);
}
}
} else {
std::basic_string<TCHAR> tryPath = file.fullpath;
std::basic_string<TCHAR> dir = GetExecutableDirectory();
bool fileFound = false;
size_t dotPos = tryPath.find_last_of(_T('.'));
if (dotPos != std::basic_string<TCHAR>::npos) {
std::basic_string<TCHAR> baseName = tryPath.substr(0, dotPos);
std::basic_string<TCHAR> searchPattern = dir + _T("*.mhook");
WIN32_FIND_DATA fd;
HANDLE hFind = FindFirstFile(searchPattern.c_str(), &fd);
if (hFind != INVALID_HANDLE_VALUE) {
do {
std::basic_string<TCHAR> fname = fd.cFileName;
if (fname.find(baseName) == 0) {
std::basic_string<TCHAR> fullPath = dir + fname;
if (GetFileAttributes(fullPath.c_str()) != INVALID_FILE_ATTRIBUTES) {
MHSettings::OpenMHookConfig(hDlg, (TCHAR*)fullPath.c_str());
MHSettings::AfterLoad(hDlg);
fileFound = true;
break;
}
}
} while (FindNextFile(hFind, &fd));
FindClose(hFind);
}
}
}
}
}
void RecentFiles::Shutdown() {
if (hListBox) {
DestroyWindow(hListBox);
hListBox = NULL;
}
files.clear();
embeddedFiles.clear();
initialized = false;
}