-
Notifications
You must be signed in to change notification settings - Fork 2
/
GameResDown.cpp
254 lines (229 loc) · 5.84 KB
/
GameResDown.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
#include "GameResDown.h"
#include "DownLoadFile.h"
#include "cocos2d.h"
#include "support/zip_support/ZipUtils.h"
#include "Util.h"
USING_NS_CC;
#ifdef WIN32
#define snprintf _snprintf
#endif
#include <stdio.h>
#ifdef _WIN32
#include <io.h>
#include <direct.h>
#else
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#endif
static pthread_mutex_t s_download_mutex;
GameResDown* GameResDown::m_pInstance = NULL;
GameResDown::GameResDown()
{
m_isDownload = false;
pthread_mutex_init(&s_download_mutex, NULL);
DownLoadFile::instance()->addListener(this);
}
GameResDown::~GameResDown()
{
pthread_mutex_destroy(&s_download_mutex);
DownLoadFile::instance()->removeListener(this);
}
GameResDown* GameResDown::instance()
{
if (!m_pInstance)
{
m_pInstance = new GameResDown();
}
return m_pInstance;
}
void GameResDown::downResFile(string url, int gameid, int versionid, string module, string channid)
{
char fileName[64] = {0};
snprintf(fileName,sizeof(fileName) - 1,"R%d_%s_%d_%s.zip",gameid, module.c_str(), versionid, channid.c_str());
m_savePath = getResPath(gameid);
CCLog("savePath:%s, filename:%s", m_savePath.c_str(), fileName);
DownRes downRes;
downRes.gameid = gameid;
downRes.versionid = versionid;
downRes.module = module;
downRes.channid = channid;
downRes.downurl = url;
//判断文件夹是否存在,不存在就创建文件夹
if (!isFileExist(m_savePath))
{
#ifdef _WIN32
mkdir(m_savePath.c_str());
#else
mkdir(m_savePath.c_str(), S_IRWXU);
#endif
}
pthread_mutex_lock(&s_download_mutex); //Get request task from queue
m_downloadReses.push(downRes);
pthread_mutex_unlock(&s_download_mutex);
if (m_isDownload)
{
return;
}
SetRunning(true);
onStart();
}
void GameResDown::onGameEvent(int handle, int eventID, void *param)
{
switch (eventID)
{
case gameDownLoadFailed:
case gameDownLoadCancle:
fireEvent(handle, eventID, param);
break;
case gameDownLoadFinish:
case gameDownLoadFileExist:
fireEvent(handle, eventID, param);
{
string fileFullPath = m_savePath+getFileName(m_downloadRes.gameid, m_downloadRes.versionid, m_downloadRes.module, m_downloadRes.channid);
//解压文件
if (ZipUtils::unZipFileToPath(m_savePath.c_str(), fileFullPath.c_str()))
{
fireEvent(0, gameUnzipFinish, (void*)&m_downloadRes);
}
else
{
fireEvent(0, gameUnzipFailed, (void*)&m_downloadRes);
}
}
break;
case gameDownLoading:
{
fireEvent(0, gameDownLoading,param);
}
break;
}
}
bool GameResDown::isFileExist(string fileFullName)
{
if (access(fileFullName.c_str(), 0) == -1)
{
return false;
}
return true;
}
bool GameResDown::isFileExist(int gameid, int versionid, string module, string channid)
{
string filePath = getResPath(gameid);;
char fileName[64] = {0};
snprintf(fileName,sizeof(fileName) - 1,"R%d_%s_%d_%s.zip",gameid, module.c_str(), versionid, channid.c_str());
return isFileExist(filePath+fileName);
}
string GameResDown::getResPath(int gameid)
{
string filePath = CCFileUtils::sharedFileUtils()->getWriteablePath();
filePath += "V";
filePath += int2string(gameid);
#ifdef _WIN32
filePath += "\\";
#else
filePath += "/";
#endif
return filePath;
}
vector<string> GameResDown::getFiles(string filePath)
{
vector<string> files;
#ifdef _WIN32
//文件句柄
long hFile = 0;
//文件信息
struct _finddata_t fileinfo;
string p;
if((hFile = _findfirst(p.assign(filePath).append("\\*").c_str(),&fileinfo)) != -1)
{
do
{
//如果是目录,迭代之
//如果不是,加入列表
if(!(fileinfo.attrib & _A_SUBDIR))
{
files.push_back(p.assign(filePath).append("\\").append(fileinfo.name) );
}
}while(_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
#else
struct dirent* ent = NULL;
DIR* pDir;
pDir = opendir(filePath.c_str());
while(NULL != (ent = readdir(pDir)))
{
string fullpath = filePath + "/" + ent->d_name;
if(isFile(fullpath))
{
files.push_back(fullpath);
}
}
closedir(pDir);
#endif
return files;
}
void GameResDown::deleteResFile(vector<string> files, string subStr)
{
for (int i=0; i<files.size(); i++)
{
if (files[i].find(subStr) != string::npos)
{
//delete
remove(files[i].c_str());
}
}
}
void GameResDown::Run()
{
pthread_mutex_lock(&s_download_mutex); //Get request task from queue
m_isDownload = true;
while(m_downloadReses.size() > 0)
{
m_downloadRes = m_downloadReses.front();
m_downloadReses.pop();
pthread_mutex_unlock(&s_download_mutex);
string fileName = getFileName(m_downloadRes.gameid, m_downloadRes.versionid, m_downloadRes.module, m_downloadRes.channid);
CCLog(" runaction savePath:%s, filename:%s", m_savePath.c_str(), fileName.c_str());
if (isFileExist(m_savePath+fileName))
{
fireEvent(0, gameDownLoadFileExist, (void*)&m_downloadRes);
//解压文件
if (ZipUtils::unZipFileToPath(m_savePath.c_str(), (m_savePath+fileName).c_str()))
{
fireEvent(0, gameUnzipFinish, (void*)&m_downloadRes);
}
else
{
fireEvent(0, gameUnzipFailed, (void*)&m_downloadRes);
}
return;
}
//删除以前版本的文件
char delFile[32] = {0};
snprintf(delFile, sizeof(delFile)-1, "R%d_%s_", m_downloadRes.gameid, m_downloadRes.module.c_str());
deleteResFile(getFiles(m_savePath), delFile);
//下载文件
DownLoadFile::instance()->downLoadFile(m_downloadRes.downurl+fileName, m_savePath+fileName);
}
m_isDownload = false;
pthread_mutex_unlock(&s_download_mutex);
}
string GameResDown::getFileName(int gameid, int versionid, string module, string channid)
{
char fileName[64] = {0};
snprintf(fileName,sizeof(fileName) - 1,"R%d_%s_%d_%s.zip",gameid, module.c_str(), versionid, channid.c_str());
return string(fileName);
}
bool GameResDown::isFile(const string fileName)
{
#ifdef _WIN32
return true;
#else
struct stat st;
int ret = stat(fileName.c_str(), &st);
return ret>=0 && S_ISREG(st.st_mode);
#endif
}