forked from uesp/skyedit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SrLoadDlg.cpp
585 lines (484 loc) · 18.7 KB
/
SrLoadDlg.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/*===========================================================================
*
* File: Srloaddlg.CPP
* Author: Dave Humphrey ([email protected])
* Created On: 26 November 2011
*
* Description
*
*=========================================================================*/
/* Include Files */
#include "stdafx.h"
#include "sredit.h"
#include "srLoadDlg.h"
#include "dialogs/sreditdlghandler.h"
/*===========================================================================
*
* Begin Local Definitions
*
*=========================================================================*/
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CStringArray CSrLoadDlg::s_ExtraFilePaths;
/*===========================================================================
* End of Local Definitions
*=========================================================================*/
/*===========================================================================
*
* Begin Class CSrLoadDlg Message Map
*
*=========================================================================*/
BEGIN_MESSAGE_MAP(CSrLoadDlg, CDialog)
ON_NOTIFY(LVN_COLUMNCLICK, IDC_FILE_LIST, OnColumnclickFileList)
ON_NOTIFY(NM_DBLCLK, IDC_FILE_LIST, OnDblclkFileList)
ON_BN_CLICKED(IDC_SETACTIVE, OnSetactive)
END_MESSAGE_MAP()
/*===========================================================================
* End of Class CSrLoadDlg Message Map
*=========================================================================*/
/*===========================================================================
*
* Function - int l_FileListCompare (lParam1, lParam2, lParamSort);
*
*=========================================================================*/
int CALLBACK l_FileListCompare (LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort) {
srloaddlgfileinfo_t* pData1 = (srloaddlgfileinfo_t *) lParam1;
srloaddlgfileinfo_t* pData2 = (srloaddlgfileinfo_t *) lParam2;
srloadfilesortinfo_t* pSortData = (srloadfilesortinfo_t *) lParamSort;
int Result = 0;
if (pData1 == NULL || pData2 == NULL || pSortData == NULL) return (0);
switch (pSortData->SortType) {
case SRFILELIST_FILENAME:
Result = _tcsicmp(pData2->FindData.cFileName, pData1->FindData.cFileName);
break;
case SRFILELIST_DATE: {
CTime Time1(pData1->FindData.ftLastWriteTime);
CTime Time2(pData2->FindData.ftLastWriteTime);
CTimeSpan TimeDiff = Time2 - Time1;
Result = (int)(TimeDiff.GetTotalSeconds());
break; }
case SRFILELIST_SIZE:
Result = pData2->FindData.nFileSizeHigh - pData1->FindData.nFileSizeHigh;
if (Result == 0) Result = pData1->FindData.nFileSizeLow - pData2->FindData.nFileSizeLow;
break;
case SRFILELIST_DEFAULTSORT: {
if (pData1->IsMaster != pData2->IsMaster) {
Result = pData2->IsMaster - pData1->IsMaster;
}
else {
CTime Time1(pData1->FindData.ftLastWriteTime);
CTime Time2(pData2->FindData.ftLastWriteTime);
CTimeSpan TimeDiff = Time2 - Time1;
Result = (int) (TimeDiff.GetTotalSeconds());
}
break; }
}
if (pSortData->Reverse) return (-Result);
return (Result);
}
/*===========================================================================
* End of Function l_FileListCompare()
*=========================================================================*/
/*===========================================================================
*
* Class CSrLoadDlg Constructor
*
*=========================================================================*/
CSrLoadDlg::CSrLoadDlg(CWnd* pParent) : CDialog(CSrLoadDlg::IDD, pParent)
{
m_IsFileNew = false;
m_LastSortReverse = false;
m_LastSortSubItem = SRFILELIST_FILENAME;
m_pLastActiveFile = NULL;
}
/*===========================================================================
* End of Class CSrLoadDlg Constructor
*=========================================================================*/
/*===========================================================================
*
* Class CSrLoadDlg Method - void AddFile (pPath, FindData);
*
*=========================================================================*/
void CSrLoadDlg::AddFile (const char* pPath, WIN32_FIND_DATA& FindData)
{
srloaddlgfileinfo_t* pNewData;
CString Buffer;
int ListIndex;
/* Create a copy of the find data */
pNewData = new srloaddlgfileinfo_t;
m_FileInfos.Add(pNewData);
pNewData->FindData = FindData;
pNewData->IsActive = false;
pNewData->IsMaster = false;
pNewData->Path = pPath;
/* Add the file to the list control */
ListIndex = m_FileList.InsertItem(0, FindData.cFileName);
if (ListIndex < 0) return;
m_FileList.SetItemData(ListIndex, (DWORD) pNewData);
if (SrCheckExtension(FindData.cFileName, "esm"))
{
pNewData->IsMaster = true;
}
UpdateFile(ListIndex, pNewData);
}
/*===========================================================================
* End of Class Method CSrLoadDlg::AddFile()
*=========================================================================*/
/*===========================================================================
*
* Function - int l_SortLoadInfosByDate (lParam1, lParam2, lParamSort);
*
*=========================================================================*/
int __stdcall l_SortLoadInfosByDate (long lParam1, long lParam2, long lParamSort)
{
srloaddlgfileinfo_t* pFileInfo1 = (srloaddlgfileinfo_t *) lParam1;
srloaddlgfileinfo_t* pFileInfo2 = (srloaddlgfileinfo_t *) lParam2;
/* Special case for Skyrim.esm to ensure it is loaded first */
if (stricmp(pFileInfo1->FindData.cFileName, "Skyrim.esm") == 0) return -1;
if (stricmp(pFileInfo2->FindData.cFileName, "Skyrim.esm") == 0) return 1;
if (pFileInfo1 == NULL || pFileInfo2 == NULL) return (0);
CTime Time1(pFileInfo1->FindData.ftLastWriteTime);
CTime Time2(pFileInfo2->FindData.ftLastWriteTime);
CTimeSpan TimeDiff = Time1 - Time2;
return(int) TimeDiff.GetTotalSeconds();
}
/*===========================================================================
* End of Function l_SortLoadInfosByDate()
*=========================================================================*/
/*===========================================================================
*
* Class CSrLoadDlg Method - void CreateLoadInfo (void);
*
*=========================================================================*/
void CSrLoadDlg::CreateLoadInfo (void)
{
CSrRefLoadDlgFileInfos SortLoadInfos;
srloaddlgfileinfo_t* pFileData;
CSString* pNewString;
dword Index;
int ListIndex;
/* Clear the current load information */
m_LoadInfo.m_MasterFilenames.Destroy();
m_LoadInfo.m_ActiveFilename.Empty();
/* Iterate through all checked items in the list */
for (ListIndex = 0; ListIndex < m_FileList.GetItemCount(); ++ListIndex)
{
if (!m_FileList.GetCheck(ListIndex)) continue;
pFileData = (srloaddlgfileinfo_t *) m_FileList.GetItemData(ListIndex);
if (pFileData == NULL) continue;
if (pFileData->IsActive)
{
//m_LoadInfo.m_ActiveFilename = m_FilePath + pFileData->FindData.cFileName;
m_LoadInfo.m_ActiveFilename = pFileData->Path + pFileData->FindData.cFileName;
}
else if (pFileData->IsMaster)
{
SortLoadInfos.Add(pFileData);
}
else
{
AddSrGeneralError("Cannot add non-master file '%s' as a dependancy!", pFileData->FindData.cFileName);
}
}
/* Sort and load the masters according to date */
SortLoadInfos.Sort(l_SortLoadInfosByDate, 0);
for (Index = 0; Index < SortLoadInfos.GetSize(); ++Index)
{
pNewString = new CSString;
m_LoadInfo.m_MasterFilenames.Add(pNewString);
*pNewString = SortLoadInfos[Index]->Path + SortLoadInfos[Index]->FindData.cFileName;
//SystemLog.Printf("%d) %s", Index, pNewString->c_str());
}
}
/*===========================================================================
* End of Class Method CSrLoadDlg::CreateLoadInfo()
*=========================================================================*/
/*===========================================================================
*
* Class CSrLoadDlg Method - void DoDataExchange (pDX);
*
*=========================================================================*/
void CSrLoadDlg::DoDataExchange (CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_FILE_LIST, m_FileList);
DDX_Control(pDX, IDC_SETACTIVE, m_ActiveButton);
DDX_Control(pDX, IDC_LABEL, m_Label);
}
/*===========================================================================
* End of Class Method CSrLoadDlg::DoDataExchange()
*=========================================================================*/
/*===========================================================================
*
* Class CSrLoadDlg Method - void FillFileList (void);
*
*=========================================================================*/
void CSrLoadDlg::FillFileList (void)
{
CSString DataPath;
CSString FileSpec;
bool Result;
/* Clear the current contents */
m_FileList.DeleteAllItems();
m_FileInfos.Empty();
m_pLastActiveFile = NULL;
/* Attempt to get Skyrim's data path */
Result = GetSrInstallPath(DataPath);
if (Result) DataPath += "data\\";
/* Find all plugins in the game's \DATA path */
if (!m_IsFileNew) FillFileList(DataPath, "*.esp");
FillFileList(DataPath, "*.esm");
/* Find all plugins in the extra paths */
for (INT_PTR i = 0; i < s_ExtraFilePaths.GetSize(); ++i)
{
if (!m_IsFileNew) FillFileList(s_ExtraFilePaths[i], "*.esp");
FillFileList(s_ExtraFilePaths[i], "*.esm");
}
}
/*===========================================================================
* End of Class Method CSrLoadDlg::FillFileList()
*=========================================================================*/
/*===========================================================================
*
* Class CSrLoadDlg Method - int FillFileList (pPath, pFileSpec);
*
*=========================================================================*/
int CSrLoadDlg::FillFileList (const char* pPath, const char* pFileSpec) {
HANDLE hFind;
WIN32_FIND_DATA FindData;
BOOL FindResult;
CString FullFileSpec(pPath);
int Count = 0;
TerminatePathString(FullFileSpec);
FullFileSpec += pFileSpec;
hFind = FindFirstFile(FullFileSpec, &FindData);
FindResult = (hFind != INVALID_HANDLE_VALUE);
while (FindResult) {
++Count;
AddFile(pPath, FindData);
FindResult = FindNextFile(hFind, &FindData);
}
FindClose(hFind);
return (Count);
}
/*===========================================================================
* End of Class Method CSrLoadDlg::FillFileList()
*=========================================================================*/
/*===========================================================================
*
* Class CSrLoadDlg Event - void OnColumnclickFileList (pNMHDR, pResult);
*
*=========================================================================*/
void CSrLoadDlg::OnColumnclickFileList(NMHDR* pNMHDR, LRESULT* pResult) {
NM_LISTVIEW* pNMListView = (NM_LISTVIEW *) pNMHDR;
srloadfilesortinfo_t SortData;
SortData.Reverse = false;
SortData.SortType = pNMListView->iSubItem;
if (pNMListView->iSubItem == m_LastSortSubItem) {
m_LastSortReverse = !m_LastSortReverse;
SortData.Reverse = m_LastSortReverse;
}
else {
m_LastSortSubItem = pNMListView->iSubItem;
m_LastSortReverse = false;
}
m_FileList.SortItems(l_FileListCompare, (DWORD) &SortData);
*pResult = 0;
}
/*===========================================================================
* End of Class Event CSrLoadDlg::OnColumnclickFileList()
*=========================================================================*/
/*===========================================================================
*
* Class CSrLoadDlg Event - void OnDblclkFileList (pNMHDR, pResult);
*
*=========================================================================*/
void CSrLoadDlg::OnDblclkFileList (NMHDR* pNMHDR, LRESULT* pResult)
{
srloaddlgfileinfo_t* pFileData;
POSITION ItemPos;
int ListIndex;
/* Get the first, and only, selected item */
ItemPos = m_FileList.GetFirstSelectedItemPosition();
if (ItemPos == NULL) return;
ListIndex = m_FileList.GetNextSelectedItem(ItemPos);
if (ListIndex < 0) return;
pFileData = (srloaddlgfileinfo_t *) m_FileList.GetItemData(ListIndex);
if (pFileData == NULL) return;
/* Set the check state */
if (m_FileList.GetCheck(ListIndex))
{
m_FileList.SetCheck(ListIndex, FALSE);
if (pFileData->IsActive && !m_IsFileNew)
{
pFileData->IsActive = false;
m_pLastActiveFile = NULL;
}
}
else
{
m_FileList.SetCheck(ListIndex, TRUE);
if (m_pLastActiveFile == NULL && !pFileData->IsMaster && !m_IsFileNew)
{
pFileData->IsActive = true;
m_pLastActiveFile = pFileData;
}
}
UpdateFile(ListIndex, pFileData);
*pResult = 0;
}
/*===========================================================================
* End of Class Event CSrLoadDlg::OnDblclkFileList()
*=========================================================================*/
/*===========================================================================
*
* Class CSrLoadDlg Event - BOOL OnInitDialog ();
*
*=========================================================================*/
BOOL CSrLoadDlg::OnInitDialog()
{
srloadfilesortinfo_t SortData;
CDialog::OnInitDialog();
if (m_IsFileNew)
{
m_ActiveButton.ShowWindow(SW_HIDE);
m_Label.SetWindowTextA("When creating a new plugin you almost always want to select Skyrim.esm as a base master. Note that regular plugin files (ESP) cannot be used as dependants for other plugins.");
SetWindowText("New Plugin");
}
else
{
m_Label.SetWindowTextA("Select a plugin file (ESP) to load along with all of its base master files (ESM). Set a file to active if you wish to save changes to it.");
SetWindowText("Load Plugin");
}
/* Initialize the file list */
m_FileList.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_CHECKBOXES );
m_FileList.InsertColumn(0, _T("Filename"), LVCFMT_LEFT, 240, SRFILELIST_FILENAME);
m_FileList.InsertColumn(1, _T("Date"), LVCFMT_CENTER, 120, SRFILELIST_DATE);
m_FileList.InsertColumn(2, _T("Size (bytes)"), LVCFMT_CENTER, 75, SRFILELIST_SIZE);
m_FileList.InsertColumn(3, _T("Type"), LVCFMT_CENTER, 90, SRFILELIST_DEFAULTSORT);
FillFileList();
/* Set skyrim.esm to load for a new file by default */
if (m_IsFileNew)
{
FindSkyrimMaster(true);
}
else
{ /* Also select skyrim.esm when opening a plugin for now to see how it works */
FindSkyrimMaster(true);
}
SortData.Reverse = false;
SortData.SortType = SRFILELIST_DEFAULTSORT;
m_FileList.SortItems(l_FileListCompare, (DWORD) &SortData);
return (TRUE);
}
/*===========================================================================
* End of Class Event CSrLoadDlg::OnInitDialog()
*=========================================================================*/
/*===========================================================================
*
* Class CSrLoadDlg Event - void OnOK (void);
*
*=========================================================================*/
void CSrLoadDlg::OnOK (void) {
CreateLoadInfo();
CDialog::OnOK();
}
/*===========================================================================
* End of Class Event CSrLoadDlg::OnOK()
*=========================================================================*/
/*===========================================================================
*
* Class CSrLoadDlg Event - void OnSetactive ();
*
*=========================================================================*/
void CSrLoadDlg::OnSetactive()
{
srloaddlgfileinfo_t* pFileData;
POSITION ItemPos;
LVFINDINFO FindInfo;
int ListIndex;
int ActiveIndex;
if (m_IsFileNew) return;
/* Get the first, and only, selected item */
ItemPos = m_FileList.GetFirstSelectedItemPosition();
if (ItemPos == NULL) return;
ListIndex = m_FileList.GetNextSelectedItem(ItemPos);
if (ListIndex < 0) return;
pFileData = (srloaddlgfileinfo_t *) m_FileList.GetItemData(ListIndex);
if (pFileData == NULL) return;
/* Update the previous active file, if any */
if (m_pLastActiveFile != NULL) {
FindInfo.flags = LVFI_PARAM;
FindInfo.psz = NULL;
FindInfo.lParam = (long) m_pLastActiveFile;
FindInfo.vkDirection = 0;
m_pLastActiveFile->IsActive = false;
ActiveIndex = m_FileList.FindItem(&FindInfo, -1);
if (ActiveIndex >= 0) UpdateFile(ActiveIndex, m_pLastActiveFile);
}
if (pFileData->IsActive) {
pFileData->IsActive = false;
m_pLastActiveFile = NULL;
}
else {
m_FileList.SetCheck(ListIndex, TRUE);
pFileData->IsActive = true;
m_pLastActiveFile = pFileData;
UpdateFile(ListIndex, pFileData);
}
}
/*===========================================================================
* End of Class Event CSrLoadDlg::OnSetactive()
*=========================================================================*/
/*===========================================================================
*
* Class CSrLoadDlg Method - void UpdateFile (ListIndex, pFileData);
*
*=========================================================================*/
void CSrLoadDlg::UpdateFile (const int ListIndex, srloaddlgfileinfo_t* pFileData) {
CString Buffer;
int64 FileSize;
m_FileList.SetItemText(ListIndex, SRFILELIST_FILENAME, pFileData->FindData.cFileName);
CTime FileTime(pFileData->FindData.ftLastWriteTime);
m_FileList.SetItemText(ListIndex, SRFILELIST_DATE, FileTime.Format(_T("%c")));
FileSize = (int64)pFileData->FindData.nFileSizeLow + (((int64) pFileData->FindData.nFileSizeHigh) << 32);
Buffer.Format(_T("%I64d"), FileSize);
m_FileList.SetItemText(ListIndex, SRFILELIST_SIZE, Buffer);
if (pFileData->IsActive) {
if (pFileData->IsMaster)
m_FileList.SetItemText(ListIndex, SRFILELIST_DEFAULTSORT, _T("Active Master"));
else
m_FileList.SetItemText(ListIndex, SRFILELIST_DEFAULTSORT, _T("Active"));
}
else if (pFileData->IsMaster) {
m_FileList.SetItemText(ListIndex, SRFILELIST_DEFAULTSORT, _T("Master"));
}
else {
m_FileList.SetItemText(ListIndex, SRFILELIST_DEFAULTSORT, _T(""));
}
}
/*===========================================================================
* End of Class Method CSrLoadDlg::UpdateFile()
*=========================================================================*/
int CSrLoadDlg::DoModalNew (void)
{
m_IsFileNew = true;
return CDialog::DoModal();
}
int CSrLoadDlg::FindSkyrimMaster (const bool SetCheck)
{
for (int i = 0; i < m_FileList.GetItemCount(); ++i)
{
srloaddlgfileinfo_t* pFileData = (srloaddlgfileinfo_t *) m_FileList.GetItemData(i);
if (pFileData == NULL) continue;
if (stricmp(pFileData->FindData.cFileName, "skyrim.esm") == 0)
{
if (SetCheck) m_FileList.SetCheck(i, true);
return i;
}
}
return -1;
}