-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdenPackage.cpp
73 lines (60 loc) · 1.6 KB
/
AdenPackage.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
#include "AdenPackage.h"
#include<iostream>
using namespace std;
AdenPackageFile::~AdenPackageFile()
{
fout.close(); fout.clear();
for (auto iter = mapIndex.begin(); iter != mapIndex.end(); iter++)
{
delete[] iter->second->data;
delete iter->second;
}
}
AdenPackageFile::AdenPackageFile(string packagePath)
{
pathName = packagePath;
#ifdef _ADEN_PACKAGE_DEBUG_
fout.open(pathName + ".apf", ios_base::out | ios_base::binary);
#else
ifstream fin(pathName + ".apf", ios_base::in | ios_base::binary);
while (fin.tellg() != EOF)
{
char path[ADEN_MAX_LENGTH] = { '\0' }; size_t length;
fin.read(path, ADEN_MAX_LENGTH);
fin.read((char*)&length, sizeof(length));
char* buffer = new char[length];
fin.read(buffer, length);
AdenData* pData = new AdenData();
pData->data = buffer; pData->length = length;
mapIndex[path] = pData;
}
fin.close(); fin.clear();
#endif
}
AdenData* AdenPackageFile::LoadResource(string dataPath)
{
#ifdef _ADEN_PACKAGE_DEBUG_
ifstream fin(dataPath.c_str(), ios_base::in | ios_base::binary);
if (fin.is_open())
{
fin.seekg(0, fin.end);
size_t length = fin.tellg();
fin.seekg(0, fin.beg);
char* buffer = new char[length];
fin.read(buffer, length);
fin.close(); fin.clear();
AdenData* pData = new AdenData();
pData->data = buffer; pData->length = length;
mapIndex[dataPath] = pData;
char path[ADEN_MAX_LENGTH] = { '\0' };
strcpy_s(path, dataPath.c_str());
fout.write(path, ADEN_MAX_LENGTH);
fout.write((char*)&length, sizeof(length));
fout.write(buffer, length) << flush;
return pData;
}
return nullptr;
#else
return mapIndex[dataPath];
#endif
}