-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Start.cpp
476 lines (447 loc) · 15.5 KB
/
Start.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
#include "Start.h"
#include <QStringList>
#include <QtWidgets/QMessageBox>
#include <QCoreApplication>
#include <QDebug>
#include <QUrl>
#include <QtConcurrent>
#include <QFileInfo>
#include <qt_windows.h>
#include "file.h"
#include "MD5.h"
#include "Sandefine.h"
#include "Json.h"
#include "mainwindow.h"
Start *tp;
Start::Start(QString dir, QObject *parent)
: QObject(parent)
{
tp=this;
qDebug()<<"Start被创建";
tp->stlog(moduleStart,"Start被创建",NULL);
http = new HTTP("","",NULL);//单线程下载
connect(http, &HTTP::tworkMessageBox, this, &Start::tworkMessageBox);
workProcess = new QThread;
moveToThread(workProcess);
connect(this, &Start::tstart, this, &Start::work);
workProcess->start();
this->dir = dir;
}
Start::~Start()
{
tp->stlog(moduleStart,"Start被销毁",NULL);
qDebug()<<"Start被销毁";
}
void Start::dlworking(LONG64 dlnow,LONG64 dltotal,void *tid,QString path)
{
static QMutex mutex;
mutex.lock();
//qDebug()<<dlnow<<dltotal<<tid<<path;
for(int i=0;i<maxDlThreah;++i)
{
//清理已完成的任务
//if(netspeed[i].dl==netspeed[i].total && netspeed[i].total>0)
if( netspeed[i].isdling && (netspeed[i].dl==netspeed[i].total && netspeed[i].total>0))
{
qDebug()<<&netspeed[i].tid<<"end dl"<<netspeed[i].path;
netspeed[i].isdling=false;
netspeed[i].tid=NULL;
//netspeed[i].dl=0;
//netspeed[i].hisDl=0;
//netspeed[i].total=0;
//netspeed[i].dlt=0;
//netspeed[i].hisDlt=netspeed[i].dlt;
//netspeed[i].path="";
//emit tp->updataDlingmag();//强制更新进度文本
}
}
for(int i=0;i<maxDlThreah;++i)
{
//qDebug()<<i<<netspeed[i].tid<<tid;
if(netspeed[i].isdling==false)
{
//新下载线程开始
qDebug()<<&tid<<"new dl"<<path;
netspeed[i].isdling=true;
netspeed[i].tid=tid;
netspeed[i].dl=dlnow;
netspeed[i].dlt=QDateTime::currentDateTime().toMSecsSinceEpoch();
netspeed[i].hisDl=dlnow;
netspeed[i].hisDlt=netspeed[i].dlt;
netspeed[i].total=dltotal;
if(path!="")netspeed[i].path=path;
break;
}
if(netspeed[i].tid==tid && netspeed[i].isdling)
{
//正在下载的线程
//qDebug()<<"update netspeed"<<tid<<dlnow<<dltotal<<path<<netspeed[i].path;
netspeed[i].dl=dlnow;
netspeed[i].dlt=QDateTime::currentDateTime().toMSecsSinceEpoch();
netspeed[i].total=dltotal;
break;
}
}
mutex.unlock();
}
void Start::stlog(int module,QString str,int mod=NULL)
{
if(tp)
{
emit tp->log(module,str,mod);
tp->newLog(module,str);
}
}
void Start::newLog(int module,QString log)
{
qDebug()<<"llog"<<module<<log;
llog * l = new llog;
l->module = module;
l->log = log;
logList.push_back(l);
}
void Start::dldone()
{
emit tworkProcess(doneFile++,totalFile);
tp->stlog(moduleStart,"下载完成 "+
QString::number(doneFile)+"/"+
QString::number(totalFile)
,NULL);
}
QString Start::tNowWork()
{
qDebug()<<"update speed text";
//return "";
qint64 DETLAms;
int p;
LONG64 s;
QString tem="";
QString tems="0.00B/s";
for(int i=0;i<maxDlThreah;++i)
{
if(netspeed[i].path!="")
{
//qDebug()<<"--------------";
//计算已下载的百分比
DETLAms=netspeed[i].dlt-netspeed[i].hisDlt;
//qDebug().noquote()<<i<<netspeed[i].path<<netspeed[i].dl<<netspeed[i].hisDl<<DETLAms;
p = (int)(100*((double)netspeed[i].dl/(double)netspeed[i].total));
if(p<0)p=0;
//qDebug().noquote()<<p;
//下载速度格式化
//s = (netspeed[i].dl-netspeed[i].hisDl)*2;
s=(int)((double)(netspeed[i].dl-netspeed[i].hisDl)*((double)1000/(double)DETLAms));
if(s>0)tems=conver(s);
//qDebug().noquote()<<tems;
//文本构造
tem+=QString::number(p)+"%\t | "
+QString(tems)+"\t | "
+netspeed[i].path
;
//字节数缓存
netspeed[i].hisDl=netspeed[i].dl;
netspeed[i].hisDlt=netspeed[i].dlt;
p=0;
if(i<2)tem+="\n";
}
tems="0.00B/s";
}
//qDebug().noquote()<<tem;
QCoreApplication::processEvents();
return tem;
}
void Start::updaterErr()
{
tp->stlog(moduleStart,"自动更新失败",NULL);
QString str = "";
int j = 0;
qDebug()<<logList.size();
for(int i = logList.size()-1;i>=0 && j++<10;i--)
{
str += "\n";
switch(logList.at(i)->module)
{
case modulemainWindows:
str += "modulemainWindows";
break;
case moduleStart:
str += "moduleStart";
break;
case moduleHTTP:
str += "moduleHTTP";
break;
case modulefile:
str += "modulefile";
break;
case moduleMD5:
str += "moduleMD5";
break;
case moduleJson:
str += "moduleJson";
break;
}
str += " | ";
str += logList.at(i)->log;
}
emit tworkProcess(0,1);
emit this->changePBText("自动更新失败,请单击重试");
emit tworkFinished(false);
emit this->changeMainPage(1,false);
emit tworkMessageBox(1,
"自动更新失败",
QString(uperr_new)
+"\n"
"最后十条日志"+
str
);
}
void Start::stopWork()
{
tp->stlog(moduleStart,"任务线程被要求终止",NULL);
if(tpoolhttp->activeThreadCount()>0)
{
tp->stlog(moduleStart,"停止下载线程池",NULL);
qDebug()<<"停止线程池";
tpoolhttp->thread()->terminate();
this->thread()->terminate();
}
}
void Start::work()
{
QString path=this->dir;
//Start::updaterErr();
//return;
emit this->changeMainPage(0);
qDebug()<<"工作目标:"<<path;
tp->stlog(moduleStart,"工作目标:"+path,NULL);
//return;
//return;
/*前期工作********************************************************/
/*互斥原神本体路径*/
QFileInfo yuanshen(path+"/YuanShen.exe");
if(yuanshen.exists())
{
qDebug()<<"目标目录找到原神本体";
tp->stlog(moduleStart,"目标目录找到原神本体",NULL);
emit this->changeProgressBarColor(
QString("rgb(255, 0, 0)")
,QString("rgb(255, 0, 0)"));
emit this->changeMainPage0label_Text("?");
emit tworkMessageBox(
1,
"我受过专业的训练",
"但是请你不要把地图装在原神目录"
);
emit this->changeMainPage(1,false);
emit tworkFinished(false);
tp->stlog(moduleStart,"线程主动退出",NULL);
return;
}
/*获取系统环境变量temp*********************************************/
emit this->changeMainPage0label_Text("初始化...");
tp->stlog(moduleStart,"初始化...",NULL);
emit tworkProcess(1,2);
emit this->changeProgressBarColor(
QString("rgb(235, 235, 235)")
,QString("rgb(58, 59, 64)"));
QString tempPath;
tempPath=getTempPath("temp");
qDebug()<<"临时目录:"<<tempPath;
tp->stlog(moduleStart,"临时目录:"+tempPath,NULL);
/*在%temp%创建临时目录*/
tempPath=tempPath+updaterTempDir;
qDebug()<<"临时文件夹:"<<tempPath;
tp->stlog(moduleStart,"创建临时文件夹"+tempPath,NULL);
createFolderSlot(tempPath);
/*在临时目录释放crt证书*/
tp->stlog(moduleStart,"释放crt证书",NULL);
httpcrt();
/*获取在线md5******************************************************/
/* 获取在线文件md5
* url download.yuanshen.site/md5.json
* url dlurl"md5.json"
* path "md5.json"
*/
tp->stlog(moduleStart,"\r\n获取在线文件MD5json",NULL);
emit this->changeMainPage0label_Text("获取在线文件MD5...");
isdling = 1;
http->httpDownLoad(dlurl"md5.json","md5.json");
isdling = 0;
/*读取在线文件md5.json到
* QJson newmd5.json
* 字符串 QString newMD5Str
* 文件数 QSL newFileList
* 文件MD5 QSL newFileMD5
*/
//_sleep(1000);
QString newMD5Str;
QStringList newFileList;
QStringList newFileMD5;
//qDebug()<<tempPath<<"download/md5.json";
tp->stlog(moduleStart,"读入MD5文件",NULL);
newMD5Str = readTXT(tempPath+"download/md5.json");
qDebug()<<"开始转换成QSL";
tp->stlog(moduleStart,"格式化MD5List",NULL);
JSON *json=new JSON();
json->tp=tp;
if(!json->jsonStr2QSL(newMD5Str,newFileList,newFileMD5))
{
emit tworkMessageBox(
1,
"请检查互联网链接",
"rt"
);
emit this->changeMainPage(1,false);
emit tworkFinished(false);
return;
}
// return;
/*按需读取本地文件MD5**************************************************/
emit tworkProcess(1,1000);//进度条归零
QStringList needUpdate;
QStringList needUpdateMD5;
qDebug()<<"按需读取本地文件MD5:"<<path;
emit this->log(moduleStart,"按需读取本地文件MD5",NULL);
QString omd5;
for(int i = 0; i< newFileList.size();++i)
{
//qDebug()<<newFileMD5.at(i)<<this->dir+"/"+newFileList.at(i);
omd5 = getFlieMD5(this->dir+"/"+newFileList.at(i));
//emit log(moduleMD5,"本地文件MD5:"+omd5+"|"+newFileList.at(i),NULL);
emit log(moduleMD5,"本地文件:\t"+newFileList.at(i),NULL);
emit log(moduleMD5,"本地MD5:\t"+omd5,NULL);
emit tworkProcess(i,newFileList.size());
emit this->changeMainPage0label_Text("正在扫描本地文件MD5: "
+QString::number(i)
+"|"
+QString::number(newFileList.size())
+"\n"+newFileList.at(i)
);
if(newFileMD5.at(i) == omd5)
{
emit log(moduleMD5,"云端MD5\t"+newFileMD5.at(i),NULL);
}else{
qDebug()<<"MD5不匹配:"<<dir+"/"+newFileList.at(i);
emit log(moduleMD5,"云端MD5\t"+newFileMD5.at(i)+"\t需要更新",NULL);
needUpdate<<newFileList.at(i);
needUpdateMD5<<newFileMD5.at(i);
}
emit log(moduleMD5,"\r\n",NULL);
}
//return;
/*下载需要更新的文件**********************************************/
/* 下载文件
* 需要更新的文件在 QStringList needUpdater
* 下载文件前需要对字符串做很多工作
* 一是反斜杠转斜杠并删除第一个斜杠
*/
emit tworkProcess(0,1);//进度条归零
emit log(moduleStart,"根据本地文件MD5下载需要更新的文件",NULL);
QString tem;
emit this->changeProgressBarColor(
QString("#3880cc")
,QString("#00c500"));
int retry=0;//多线程下载如何重试QAQ
totalFile=needUpdate.size();
doneFile=0;
//初始libcurl线程池
emit log(moduleStart,"初始libcurl线程池",NULL);
emit log(moduleStart,"设置同时下载任务数为"+QString::number(maxDlThreah),NULL);
tpoolhttp=QThreadPool::globalInstance();
tpoolhttp->setMaxThreadCount(maxDlThreah);
//tpoolhttp->setMaxThreadCount(needUpdate.size());//A8:我tm谢谢你
tpoolhttp->setExpiryTimeout(-1);
isdling = 1;
for(int i = 0; i< needUpdate.size();++i)
{
if(needUpdateMD5.at(i)!=getFlieMD5(tempPath+"download/Map/"+needUpdate.at(i)))
{
qDebug()<<"全新下载";
emit log(moduleStart,"新建下载任务\t"+needUpdate.at(i),NULL);
//构造下载链接
QString url=dlurlMap+QUrl::toPercentEncoding(needUpdate.at(i));
QString dlpath="Map/"+QString(needUpdate.at(i));
thttp = new HTTP(url,dlpath,this);
connect(thttp,&HTTP::dldone
,this,&Start::dldone
,Qt::DirectConnection
);
connect(thttp, &HTTP::tworkMessageBox
, this, &Start::tworkMessageBox
,Qt::DirectConnection
);
tpoolhttp->start(thttp);
}else{
qDebug()<<needUpdate.at(i)<<"已下载";
tp->stlog(moduleStart,"文件已被下载\t"+needUpdate.at(i),NULL);
Start::dldone();
}
}
//tpoolhttp->activeThreadCount();
tpoolhttp->dumpObjectTree();
tpoolhttp->waitForDone(-1);
isdling = 0;
tpoolhttp->clear();
qDebug()<<"下载完成";
tp->stlog(moduleStart,"下载完成",NULL);
//qDebug()<<FindWindowW(NULL,(LPCWSTR)QString("「空荧酒馆」原神地图").unicode());//地图进程窗口
//return;
/*移动文件至目标目录*********************************************/
/*
* 移动文件
* 下载好的地图存在%temp%\download\Map\
* 移动的目标文件夹为path
*/
tp->stlog(moduleStart,"正在移动文件",NULL);
emit this->changeProgressBarColor(
QString("#00c500")
,QString("#078bff"));
int f=0;
for(int i = 0; i< needUpdate.size();++i)
{
//构造新旧文件名
QString oldPath=getTempPath("temp")+
updaterTempDir+
"download/"+"Map/"+
QString(needUpdate.at(i))
;
QString newPath=path+
"/"+
QString(needUpdate.at(i))
;
qDebug()<<QString::number(i+1)+
"|"+
QString::number(needUpdate.size())
;
emit this->changeMainPage0label_Text(
QString::number(i+1)+
"|"+
QString::number(needUpdate.size())+
" 正在移动文件:"+
needUpdate.at(i)
);
QFileInfo info(newPath);
createFolderSlot(info.path());
if(moveFile(oldPath,newPath))
{
qDebug()<<"√(^-^)";
f=0;
}else{
qDebug()<<"移动失败第"<<f<<"次";
f++;
i--;
if(f>1)
{
Start::updaterErr();
return;
}
}
emit tworkProcess(i,needUpdate.size());
}
emit tworkProcess(1,1);
emit this->changeMainPage0label_Text("不存在的看不到这句话的");
emit this->changeMainPage(1,true);
emit tworkFinished(true);
tp->stlog(moduleStart,"更新流程结束",NULL);
//Start::updaterErr();
}