-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBundleReader.cpp
More file actions
333 lines (274 loc) · 6.38 KB
/
BundleReader.cpp
File metadata and controls
333 lines (274 loc) · 6.38 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
#include "StdAfx.h"
#include "BundleReader.h"
#include "./ParamDef.h"
#include <assert.h>
#pragma warning(once:4996)
using namespace thp;
// Bundle 最大的 行=列 瓦片数
const unsigned int nBUNLE_MAX_EDGE = 128;
const unsigned int nFILE_NAME_LENGTH = 18;
BundleReader::BundleReader()
{
memset(m_szBundleFile, 0, BUNDLE_MAX_PATH);
memset(m_szBundlxContents, 0, BUNDLX_CONTENT_SIZE);
m_nIdxPos = 0;
m_nBundleBeginRow = 0;
m_nBundleBeginCol = 0;
#ifdef _DEBUG
m_nValdTileCount = 0;
#endif// _DEBUG
}
BundleReader::BundleReader(const char* szPath)
{
memset(m_szBundleFile, 0, BUNDLE_MAX_PATH);
memset(m_szLastErr, 0, BUNDLE_MAX_PATH);
memset(m_szBundlxContents, 0, BUNDLX_CONTENT_SIZE);
m_nIdxPos = 0;
m_nBundleBeginRow = 0;
m_nBundleBeginCol = 0;
#ifdef _DEBUG
m_nValdTileCount = 0;
#endif// _DEBUG
}
BundleReader::~BundleReader()
{
close();
}
void thp::BundleReader::close()
{
m_nIdxPos = 0;
m_nBundleBeginRow = 0;
m_nBundleBeginCol = 0;
memset(m_szBundlxContents, 0, BUNDLX_CONTENT_SIZE);
#ifdef _DEBUG
m_nValdTileCount = 0;
#endif// _DEBUG
}
int thp::BundleReader::_nextTile(unsigned char*& pOut, unsigned int& nSize)
{
if(m_nIdxPos == BUNDLE_MAX_TILE_NUM)
return -1;
FILE* fpBundle = fopen(m_szBundleFile, "rb");
if( !fpBundle )
{
//LOG(ERROR) << "打开文件出错";
//m_pLogWriter->errorLog("打开文件出错");
return false;
}
unsigned int* pOffset = NULL;
int nRes = 0;
unsigned int nTileSize =0;
size_t nReadCount = 0;
bool isEOF = true;
while(m_nIdxPos < BUNDLE_MAX_TILE_NUM)
{
// bundle 文件内容过大的话 这个位置可能要处理偏移值的生成
pOffset = (unsigned int*)(m_szBundlxContents + m_nIdxPos * BUNDLX_NODE_SIZE);
nRes = fseek((FILE*)fpBundle, *pOffset, SEEK_SET);
if(0 != nRes)
{
sprintf(m_szLastErr, "seek bundle offset failed");
return -1;
}
nReadCount = fread(&nTileSize, 4, 1, fpBundle);
if( 1 != nReadCount)
{
sprintf(m_szLastErr, "read bundle offset failed");
return -1;
}
if(0 == nTileSize)
{
++m_nIdxPos;
continue;// tile 不存在
}
pOut = new unsigned char[nTileSize];
// 检测内存分配
if(0 == pOut)
{
sprintf(m_szLastErr, "memory not enough");
++m_nIdxPos;
return -2;
}
nReadCount = fread(pOut, 1, nTileSize, fpBundle);
if( nReadCount != nTileSize)
{
delete[] pOut;
pOut = NULL;
sprintf(m_szLastErr, "read bundle offset failed");
++m_nIdxPos;
return -1;
}
nSize = nTileSize;
++m_nIdxPos;
isEOF = false;
#ifdef _DEBUG
++m_nValdTileCount;
#endif// _DEBUG
break;
}
if(isEOF)
return -1;
return m_nIdxPos - 1;
}
bool thp::BundleReader::getTileFromFile(int nTileInBundleIndex, unsigned char*& pByteTile, unsigned int& nSize)
{
unsigned int* pOffSet = (unsigned int*)(m_szBundlxContents + nTileInBundleIndex * BUNDLX_NODE_SIZE);
FILE* fpBundle = fopen(m_szBundleFile, "rb");
if( !fpBundle )
{
//LOG(ERROR) << "打开文件出错";
//m_pLogWriter->errorLog("打开文件出错");
return false;
}
int nRes = fseek((FILE*)fpBundle, *pOffSet, SEEK_SET);
if(0 != nRes)
{
//m_pLogWriter->warnLog("IO文件出错");
//LOG(ERROR) << "IO文件出错" << m_szBundleFile;
sprintf(m_szLastErr, "seek bundle offset failed");
fclose(fpBundle);
return false;
}
unsigned int unReadCount = fread(&nSize, 4, 1, fpBundle);
if( 1 != unReadCount)
{
//m_pLogWriter->warnLog("IO文件出错");
//LOG(ERROR) << "IO文件出错" << m_szBundleFile;
sprintf(m_szLastErr, "read bundle offset failed");
fclose(fpBundle);
return false;
}
if(0 == nSize)
{
fclose(fpBundle);
return false;
}
if( nSize > getMaxByte() )
{
fclose(fpBundle);
return false;
}
nRes = fseek((FILE*)fpBundle, *pOffSet + 4, SEEK_SET);
if(0 != nRes)
{
sprintf(m_szLastErr, "seek bundle offset failed");
return false;
}
// 读取内容
pByteTile = new unsigned char[nSize];
if(!pByteTile)
{
sprintf(m_szLastErr, "memory Error");
delete[] pByteTile;
fclose(fpBundle);
return false;
}
unReadCount = fread(pByteTile, 1, nSize, fpBundle);
if( nSize != unReadCount)
{
if ( feof(fpBundle) )
printf("Error reading test.bin: unexpected end of file\n");
else if (ferror(fpBundle))
{
perror("Error reading test.bin");
}
fclose(fpBundle);
sprintf(m_szLastErr, "I/O Error");
delete[] pByteTile;
return false;
}
fclose(fpBundle);
return true;
}
bool thp::BundleReader::copyBundlx(char*& pBlx, int nSize /*= BUNDLX_CONTENT_SIZE*/)
{
pBlx = new char[nSize];
if(!pBlx)
{
// log
sprintf(m_szLastErr, "memory full");
return false;
}
memcpy(pBlx, m_szBundlxContents, nSize);
return true;
}
bool thp::BundleReader::loadBundlx(char* pBlx, int nSize /*= BUNDLX_CONTENT_SIZE*/)
{
assert(pBlx);
memcpy(m_szBundlxContents, pBlx, nSize);
return true;
}
bool BundleReader::_calcWorldRowCol(int nIdxPos, int& nRow, int& nCol)
{
int nInBundleCol = m_nIdxPos/BUNDLE_EDGE;
int nInBundleRow = m_nIdxPos%BUNDLE_EDGE;
nRow = m_nBundleBeginRow + nInBundleRow;
nCol = m_nBundleBeginCol + nInBundleCol;
return true;
}
BundleReader::FetchType BundleReader::nextTile(int& nRow, int& nCol, unsigned char*& pOut, int& nSize/*, std::ofstream* pOsInfo*/)
{
FILE* fpBundle = fopen(m_szBundleFile, "rb");
if( !fpBundle )
{
fclose(fpBundle);
}
if(m_nIdxPos == BUNDLE_MAX_TILE_NUM)
{
fclose(fpBundle);
return FetchType_EOF;
}
unsigned int* pOffset = NULL;
int nRes = 0;
unsigned int nTileSize =0;
size_t nReadCount = 0;
bool isEOF = true;
while(m_nIdxPos < BUNDLE_MAX_TILE_NUM)
{
// bundle 文件内容过大的话 这个位置可能要处理偏移值的生成
pOffset = (unsigned int*)(m_szBundlxContents + m_nIdxPos * BUNDLX_NODE_SIZE);
nRes = fseek(fpBundle, *pOffset, SEEK_SET);
if(0 != nRes)
{
sprintf(m_szLastErr, "seek bundle offset failed");
fclose(fpBundle);
return FetchType_Fail;
}
nReadCount = fread(&nTileSize, 4, 1, fpBundle);
if( 1 != nReadCount)
{
fclose(fpBundle);
return FetchType_Fail;
}
if(0 == nTileSize)
{
++m_nIdxPos;
continue;// tile 不存在
}
pOut = (unsigned char*)malloc(nTileSize);
nReadCount = fread(pOut, 1, nTileSize, fpBundle);
if( nReadCount != nTileSize)
{
free(pOut);
pOut = NULL;
sprintf(m_szLastErr, "read bundle offset failed");
fclose(fpBundle);
return FetchType_Fail;
}
nSize = nTileSize;
_calcWorldRowCol(m_nIdxPos, nRow, nCol);
++m_nIdxPos;
isEOF = false;
#ifdef _DEBUG
++m_nValdTileCount;
#endif// _DEBUG
break;
}
if(isEOF)
{
fclose(fpBundle);
return FetchType_EOF;
}
fclose(fpBundle);
return FetchType_Success;
}