-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLoadD.cpp
More file actions
517 lines (448 loc) · 11.7 KB
/
Copy pathLoadD.cpp
File metadata and controls
517 lines (448 loc) · 11.7 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
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
#include "pch.h"
#include "LoadD.h"
#include "afxdialogex.h"
#include <vector>
#include "Lesson.h"
#include "PackerDlg.h"
#include <clocale>
#include "SuccD.h"
using namespace std;
BOOL isThreadAlive=false;
IMPLEMENT_DYNAMIC(LoadD, CDialogEx)
LoadD::LoadD(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_LOADING, pParent)
{
task = 0;
dlgss = (CPackerDlg*)pParent;
}
LoadD::~LoadD()
{
}
void LoadD::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_PROG, progbar);
DDX_Control(pDX, IDC_PT, stat);
}
BEGIN_MESSAGE_MAP(LoadD, CDialogEx)
ON_WM_TIMER()
ON_WM_SHOWWINDOW()
END_MESSAGE_MAP()
#define ST(x) stat->SetWindowTextW(_T(x))
#define STS(x) stat->SetWindowTextW(x)
BOOL inline hasFile(CString path) {
return PathFileExists(path);
}
void GetFiles(CString csDirPath, vector<CString>& vctPath)
{
csDirPath += "\\*";
HANDLE file;
WIN32_FIND_DATA fileData;
file = FindFirstFile(csDirPath.GetBuffer(), &fileData);
if (file != INVALID_HANDLE_VALUE)
{
vctPath.push_back(fileData.cFileName);
while (FindNextFile(file, &fileData))
{
vctPath.push_back(fileData.cFileName);
}
}
}
BOOL GetLines(CString path, vector<CString>& vec) {
FILE* f;
USES_CONVERSION;
int o=fopen_s(&f, W2A(path), "r,ccs=UNICODE");
CStdioFile file(f);
CString strValue = _T("");
while (file.ReadString(strValue))
{
vec.push_back(strValue);
}
file.Close();
return true;
}
BOOL PutLines(CString path, vector<CString> vec) {
FILE* f;
USES_CONVERSION;
int o = fopen_s(&f, W2A(path), "w+,ccs=UNICODE");
CStdioFile file(f);
int s = vec.size();
for (int i = 0; i < s; i++) {
file.WriteString(vec.at(i));
}
file.Close();
return true;
}
CString SelectFolderPath()
{
TCHAR szFolderPath[MAX_PATH] = { 0 };
CString strFolderPath = TEXT("");
BROWSEINFO sInfo;
::ZeroMemory(&sInfo, sizeof(BROWSEINFO));
sInfo.pidlRoot = ILCreateFromPath(_T("\\"));
sInfo.lpszTitle = _T("请选择保存路径");
sInfo.ulFlags = BIF_RETURNONLYFSDIRS | BIF_DONTGOBELOWDOMAIN;
sInfo.lpfn = NULL;
// 显示文件夹选择对话框
LPITEMIDLIST lpidlBrowse = ::SHBrowseForFolder(&sInfo);
if (lpidlBrowse != NULL)
{
if (::SHGetPathFromIDList(lpidlBrowse, szFolderPath))
{
strFolderPath = szFolderPath;
}
::CoTaskMemFree(lpidlBrowse);
}
return strFolderPath;
}
UINT workThread(LPVOID para) {
isThreadAlive = true;
LoadD* ld = (LoadD*)para;
vector<Lesson*>* allLessonsp =ld->dlgss->allLessons;
CProgressCtrl* prog = (CProgressCtrl*)ld->GetDlgItem(IDC_PROG);
CStatic* stat = (CStatic*)ld->GetDlgItem(IDC_PT);
CString path = ld->path;
ST("搜索中");
if (!hasFile(path))
{
ld->MessageBox(_T("目录错误,请检查目录或手动定位至\"AppData\\Roaming\\Seewo\\EasiNote5\\Data\""));
isThreadAlive = false;
return -1;
}
ST("搜索用户中");
vector<CString> users;
vector<CString> ust;
vector<CString> phones;
//vector<Lesson> classes;
GetFiles(path, ust);
int ustsize = ust.size();
int found = 0;
for (int i = 0; i < ustsize; i++)
{
CString tc = ust.at(i);
char fc = tc.GetAt(0);
if (fc >= '0' && fc <= '9' && tc.GetLength()==11)
{
users.push_back(path+_T("\\") + tc+_T("\\Courseware\\"));
phones.push_back(tc);
CString numt;
numt.Format(_T("%d"), ++found);
CString s = _T("搜索到") + numt + _T("个用户");
STS(s);
}
}
int userss = users.size();
if (userss==0)
{
ld->MessageBox(_T("没有发现留下来的课件呢"));
isThreadAlive = false;
return - 1;
}
ST("搜索课件中");
found = 0;
for (int i = 0; i < userss; i++) {
vector<CString> fs;
CString tPhone = phones.at(i);
CString tUser = users.at(i);
GetFiles(users.at(i), fs);
int sfs = fs.size();
for (int j = 0; j < sfs; j++) {
CString tp = tUser+fs.at(j) + _T("\\")+fs.at(j)+_T(".json");
if (hasFile(tp))
{
Lesson* l=new Lesson();
l->path = new CString(tp);
l->phone = new CString(tPhone);
//ld->MessageBox(_T("Found") + tp);
allLessonsp->push_back(l);
CString numt;
numt.Format(_T("%d"), ++found);
STS(_T("搜索到") + numt + _T("个课件"));
//delete& numt;
}
}
}
ST("解析课件中");
int alls = allLessonsp->size();
prog->SetRange(0,alls);
prog->SetStep(1);
char* old_locale = _strdup(setlocale(LC_CTYPE, NULL));
setlocale(LC_CTYPE, "chs");
for (int i = 0; i < alls; i++) {
Lesson* l = allLessonsp->at(i);
STS(_T("正在解析 ")+*l->phone+_T("的课件"));
vector<CString> lines;
GetLines(*l->path, lines);
int sl = lines.size();
vector<Lesson::CSS*>* cssv = new vector<Lesson::CSS*>();
CString fName;
bool inName = false;
for (int j = 0; j < sl; j++)
{
CString tl = lines.at(j);
if (tl.Find(_T("\"Name\""))!=-1)
{
tl.Replace(_T(" "), _T(""));
l->name = new CString(tl.Mid(8, tl.GetLength() - 10));
}
else if (tl.Find(_T("\"FileName\"")) != -1) {
tl.Replace(_T(" "), _T(""));
fName = tl.Mid(12, tl.GetLength() - 14);
inName = true;
}
else if (tl.Find(_T("\"RelativePath\"")) != -1 && inName) {
tl.Replace(_T(" "), _T(""));
Lesson::CSS* css;
css = new Lesson::CSS();
css->rela = new CString(tl.Mid(16, tl.GetLength() - 17));
css->path = new CString(fName);
cssv->push_back(css);
inName = false;
}
else if (tl.Find(_T("\"Author\"")) != -1) {
tl.Replace(_T(" "), _T(""));
l->user= new CString(tl.Mid(10, tl.GetLength() - 12));
}
}
l->files = cssv;
prog->OffsetPos(1);
}
setlocale(LC_CTYPE, old_locale);
free(old_locale);
//TODO Code here
isThreadAlive = false;
return 0;
}
void executeAndWait(CString path,CString para) {
SHELLEXECUTEINFO ShExecInfo = { 0 };
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = path;
ShExecInfo.lpParameters = para;
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_HIDE;
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo);
WaitForSingleObject(ShExecInfo.hProcess, INFINITE);
}
void exEXERes(int resID,CString path) {
HRSRC hRes = FindResource(theApp.m_hInstance, MAKEINTRESOURCE(resID/*IDR_EXE1*/), _T("EXE"));
//获取资源长度
DWORD len = SizeofResource(theApp.m_hInstance, hRes);
HGLOBAL hg = LoadResource(theApp.m_hInstance, hRes);
LPVOID lp = (LPSTR)LockResource(hg);
CFile file;
file.Open(path/**/, CFile::modeCreate | CFile::modeWrite);
char* cp = (char*)lp;
for (int i = 0; i < len; i++)
{
file.Write(cp++, 1);
}
CString filePath = file.GetFilePath();
file.Close();
FreeResource(hg);
}
BOOL DeleteDirectory(CString directory_path){
CFileFind filefind;
CString path = directory_path;
if (path.Right(1) != "\\")//目录的最右边需要“\”字符
path += "\\";
path += "*.*";
BOOL res = filefind.FindFile(path);
while (res)
{
res = filefind.FindNextFile();
//是文件时直接删除
if (!filefind.IsDots() && !filefind.IsDirectory())
DeleteFile(filefind.GetFilePath());//删除文件
else if (filefind.IsDots())
continue;
else if (filefind.IsDirectory())//为目录
{
path = filefind.GetFilePath();
//是目录时继续递归,删除该目录下的文件
DeleteDirectory(path);
//目录为空后删除目录
RemoveDirectory((LPCTSTR)path);
}
}
return TRUE;
}
UINT saveWork(LPVOID para) {
isThreadAlive = true;
LoadD* ld = (LoadD*)para;
vector<Lesson*>* allLessonsp = ld->dlgss->allLessons;
if (!allLessonsp) {
isThreadAlive = false;
return 0;
}
CProgressCtrl* prog = (CProgressCtrl*)ld->GetDlgItem(IDC_PROG);
CStatic* stat = (CStatic*)ld->GetDlgItem(IDC_PT);
CString path = ld->path;
ST("处理中");
vector<Lesson*> ls;
int as = allLessonsp->size();
for (int i = 0; i < as; i++)
{
Lesson* l =allLessonsp->at(i);
if (*l->checked==TRUE)
{
ls.push_back(l);
}
}
int lss = ls.size();
if (lss == 0) {
isThreadAlive = false;
return 0;
}
CString oPath = SelectFolderPath();
if (oPath.Compare(_T("")) == 0) {
isThreadAlive = false;
return 0;
}
prog->SetRange(0, lss);
prog->SetStep(1);
//prog->OffsetPos(1);
for (int i = 0; i < lss; i++) {
CString hint;
hint.Format(_T("正在处理 %d/%d"), i + 1, lss);
STS(hint);
Lesson* l = ls.at(i);
CString orgPath = *l->path + _T("\\..\\");
CString workPath = *l->path + _T("\\..\\Work\\");
CString slPath = workPath + _T("\\Slides\\");
CString resPath = workPath + _T("\\Resources\\");
DeleteDirectory(workPath);
CreateDirectory(workPath, 0);
CreateDirectory(slPath, 0);
CreateDirectory(resPath, 0);
vector<Lesson::CSS*>* csss = l->files;
vector<CString> types;
vector<CString> overrided;
int cssss = csss->size();
for (int j = 0; j < cssss; j++)
{
Lesson::CSS* tcs = csss->at(j);
CString rela = *tcs->rela;
bool hasSub = rela.Find(_T("\\\\")) != -1;
if (hasSub)
{
CString slN;
if (rela.Find(_T("Slides")) != -1) {
slN = CString(rela);
slN = slN.Mid(8, slN.GetLength() - 8);
CopyFile(orgPath + *tcs->path, slPath + slN, true);
}
else if (rela.Find(_T("Resources")) != -1)
{
slN = CString(rela);
slN = slN.Mid(11, slN.GetLength() - 11);
CopyFile(orgPath + *tcs->path, resPath + slN, true);
}
Lesson::CSS* tcs = csss->at(j);
CString kz = *tcs->path;
int pp = kz.Find(_T("."));
if (pp == -1)
{
overrided.push_back(_T("<Override PartName=\"/Resources/\""
+ slN + _T(" ContentType=\"\" />")));
}
else
{
kz = kz.Mid(pp + 1, kz.GetLength() - pp - 1);
CString adds = _T("<Default Extension=\"") + kz
+ _T("\" ContentType=\"\" />");
int typess = types.size();
bool has = false;
for (int k = 0; k < typess; k++) {
if (adds.Compare(types.at(k)) == 0) {
has = true;
break;
}
}
if (!has)
{
types.push_back(adds);
}
}
}
else {
CopyFile(orgPath + *tcs->path, workPath + rela, true);
}
}
vector<CString> lines;
lines.push_back(_T("\xEF\xBB\xBF"));
lines.push_back(_T(
"<?xml version=\"1.0\" encoding=\"utf-8\"?><Types xmlns=\"http://schemas.openxmlformats.org/package/2006/content-types\">"));
lines.insert(lines.end(), types.begin(), types.end());
lines.insert(lines.end(), overrided.begin(), overrided.end());
lines.push_back(_T("</Types>"));
PutLines(workPath + _T("[Content_Types].xml"), lines);
int nRetCode = 0;
CString hint1;
hint1.Format(_T("正在压制 %d/%d"), i + 1, lss);
STS(hint1);
exEXERes(IDR_EXE1, workPath + _T("7z.exe"));
exEXERes(IDR_EXE2, workPath + _T("7z.dll"));
//TODO: To method
CString wp1 = workPath + _T("7z.exe");
CString para1 = CString(_T("a -mx0 -r ") + workPath + _T("\\o.zip ") + workPath + _T("\\* "));
CString para3 = CString(_T("d ") + workPath + _T("\\o.zip 7z.dll"));
CString para2 = CString(_T("d ") + workPath + _T("\\o.zip 7z.exe"));
executeAndWait(wp1, para1);
executeAndWait(wp1, para2);
executeAndWait(wp1, para3);
CString hint2;
hint2.Format(_T("正在保存 %d/%d"), i + 1, lss);
STS(hint2);
MoveFile(workPath + _T("o.zip"), oPath + _T("\\") + *l->name + _T(".enbx"));
CString hint3;
hint3.Format(_T("正在清理 %d/%d"), i + 1, lss);
STS(hint3);
DeleteDirectory(workPath);
prog->OffsetPos(1);
}
ST("完成!");
ld->SetWindowTextW(_T("完成"));
SuccD sd;
sd.path = oPath;
sd.DoModal();
isThreadAlive = false;
return 0;
}
BOOL LoadD::OnInitDialog()
{
CDialogEx::OnInitDialog();
return TRUE;
}
void LoadD::OnTimer(UINT_PTR nIDEvent)
{
switch (nIDEvent)
{
case 1: {
if (!isThreadAlive)
{
EndDialog(0);
}
break;
}
default:
break;
}
CDialogEx::OnTimer(nIDEvent);
}
void LoadD::OnShowWindow(BOOL bShow, UINT nStatus)
{
CDialogEx::OnShowWindow(bShow, nStatus);
if (task == 0) {
HANDLE h = AfxBeginThread(workThread, this, THREAD_PRIORITY_IDLE);
}
else if (task == 1)
{
HANDLE h = AfxBeginThread(saveWork, this, THREAD_PRIORITY_IDLE);
}
SetTimer(1, 1, NULL);
}