forked from lStewieAl/Geck-Extender
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecentlyOpenedForms.cpp
More file actions
276 lines (232 loc) · 5.58 KB
/
RecentlyOpenedForms.cpp
File metadata and controls
276 lines (232 loc) · 5.58 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
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
#include "BetterFloatingFormList.h"
#include "GameTypes.h"
#include "GameObjects.h"
#include "GameAPI.h"
#include "GameData.h"
#include "GeckUtility.h"
#include <unordered_set>
#include "Events/DataLoadEvent.h"
#include "libs/stb_sprintf.h"
#include "libs/nlohmann/json.hpp"
#include <fstream>
#include <string>
using json = nlohmann::json;
extern HWND g_MainHwnd;
namespace RecentlyOpenedForms
{
const char* kRecentFile = "Data\\NVSE\\Plugins\\GeckExtender\\RecentForms.json";
constexpr size_t kMaxRecentForms = 50;
struct RecentFormEntry
{
std::string editorID;
std::time_t lastOpened;
};
std::vector<RecentFormEntry> g_RecentForms;
inline void to_json(json& j, const RecentFormEntry& entry)
{
j =
{
{ "editorID", entry.editorID },
{ "lastOpened", entry.lastOpened }
};
}
inline void from_json(const json& j, RecentFormEntry& entry)
{
j.at("editorID").get_to(entry.editorID);
j.at("lastOpened").get_to(entry.lastOpened);
}
void SaveRecentForms()
{
try
{
json j = g_RecentForms;
std::ofstream file(kRecentFile);
if (file.is_open())
{
file << j.dump(1, '\t');
}
else
{
Console_Print("Failed to open recent forms file for writing");
}
}
catch (std::exception& e)
{
Console_Print("SaveRecentForms exception: %s", e.what());
}
catch (...)
{
Console_Print("SaveRecentForms unknown exception");
}
}
void SortRecentForms()
{
std::sort(
g_RecentForms.begin(),
g_RecentForms.end(),
[](const RecentFormEntry& a, const RecentFormEntry& b)
{
return a.lastOpened < b.lastOpened;
});
}
void LoadRecentForms()
{
try
{
std::ifstream file(kRecentFile);
if (!file.is_open())
return;
json j;
file >> j;
g_RecentForms = j.get<std::vector<RecentFormEntry>>();
SortRecentForms();
if (g_RecentForms.size() > kMaxRecentForms)
{
g_RecentForms.resize(kMaxRecentForms);
}
}
catch (std::exception& e)
{
Console_Print("LoadRecentForms exception: %s", e.what());
}
catch (...)
{
Console_Print("LoadRecentForms unknown exception");
}
}
LRESULT CALLBACK WindowCallback(HWND Hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
if (Message == BSMsg_AcceptsDropType)
{
SetWindowLong(Hwnd, DWL_MSGRESULT, false);
return TRUE;
}
else if (Message == WM_INITDIALOG)
{
SendMessageA(Hwnd, BetterFloatingFormList::BFL_SET_FILTER, 0, 0);
SendMessageA(Hwnd, BetterFloatingFormList::BFL_SET_INITIAL_SORT, 0, 0);
}
auto result = BetterFloatingFormList::BaseWindowCallback(Hwnd, Message, wParam, lParam);
if (Message == WM_INITDIALOG)
{
DragAcceptFiles(Hwnd, FALSE);
}
else if (Message == BetterFloatingFormList::BFL_DELETED_ITEMS)
{
auto formList = (tList<TESForm>*)wParam;
if (formList && formList->Head())
{
std::unordered_set<std::string> deletedIDs;
auto iter = formList->Head();
do
{
if (auto form = iter->data)
{
if (auto edid = form->GetEditorID())
{
deletedIDs.insert(edid);
}
}
} while (iter = iter->next);
g_RecentForms.erase(
std::remove_if(
g_RecentForms.begin(),
g_RecentForms.end(),
[&deletedIDs](const RecentFormEntry& entry)
{
return deletedIDs.contains(entry.editorID);
}),
g_RecentForms.end());
SaveRecentForms();
}
}
return result;
}
void ShowList()
{
HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(nullptr);
static tList<TESForm> recentForms;
recentForms.RemoveAll();
for (const auto& entry : g_RecentForms)
{
TESForm* form = LookupFormByName(entry.editorID.c_str());
if (form)
{
recentForms.Append(form);
}
}
auto hWnd = CreateDialogParamA(
hInstance,
(LPCSTR)189,
g_MainHwnd,
(DLGPROC)WindowCallback,
(LPARAM)((char*)(&recentForms)) - 0xC);
SendMessageA(hWnd, WM_SETTEXT, 0, (LPARAM)"Recently Opened");
}
static std::time_t GetCurrentTimestamp()
{
using namespace std::chrono;
return system_clock::to_time_t(system_clock::now());
}
void StoreForm(TESForm* apForm)
{
if (!apForm)
{
return;
}
const char* sFormName = apForm->GetEditorID();
if (!sFormName || !sFormName[0])
return;
// Remove existing entry if already present
g_RecentForms.erase(
std::remove_if(
g_RecentForms.begin(),
g_RecentForms.end(),
[sFormName](const RecentFormEntry& entry)
{
return _stricmp(entry.editorID.c_str(), sFormName) == 0;
}),
g_RecentForms.end());
g_RecentForms.push_back(
{
sFormName,
GetCurrentTimestamp()
});
// Trim history
if (g_RecentForms.size() > kMaxRecentForms)
{
g_RecentForms.resize(kMaxRecentForms);
}
SaveRecentForms();
}
void PostLoadPlugins()
{
LoadRecentForms();
}
HWND __stdcall OnOpenObjectWindowForm(HINSTANCE hInstance, LPCSTR lpTemplateName, HWND hWndParent, DLGPROC lpDialogFunc, LPARAM dwInitParam)
{
auto pForm = ((TESForm**)(dwInitParam))[1];
StoreForm(pForm);
return CreateDialogParamA(hInstance, lpTemplateName, hWndParent, lpDialogFunc, dwInitParam);
}
void __fastcall OnLoadScriptForm(Script* apScript, void* edx, HWND hWnd)
{
apScript->Unk_56(hWnd);
StoreForm(apScript);
}
void __fastcall OnLoadInteriorCell(TES* apTES, void* edx, TESObjectCELL* apCell)
{
apTES->AddToInteriorBuffer(apCell);
StoreForm(apCell);
}
void InitHooks()
{
DataLoadEvent::RegisterCallback(PostLoadPlugins);
// add forms to list when opening them
WriteRelCall(0x4376A6, UInt32(OnOpenObjectWindowForm));
SafeWrite8(0x4376A6 + 5, 0x90);
SafeWrite16(0x5C48AB, 0xBA90);
SafeWrite32(0x5C48AB + 2, UInt32(OnLoadScriptForm));
WriteRelCall(0x4CB25C, UInt32(OnLoadInteriorCell));
}
}