-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXFilePackManager.cpp
More file actions
158 lines (137 loc) · 3.6 KB
/
XFilePackManager.cpp
File metadata and controls
158 lines (137 loc) · 3.6 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
/*******************************************************************************
FILE: XFilePackManager.cpp
DESCRIPTTION:
CREATED BY: YangCao, 2015/01/16
Copyright (C) - All Rights Reserved with Coconat
*******************************************************************************/
#include "XFilePackManager.h"
#include "XLog.h"
#include "XSys.h"
std::vector<std::string> XFilePackManage::pack_names = std::vector<std::string>();
XFilePackManage::XFilePackManage()
{
}
XFilePackManage& XFilePackManage::Get()
{
static XFilePackManage inst;
return inst;
}
XFilePackageEasy* XFilePackManage::FindPack(const char* pack_name)
{
if (!pack_name)
{
return NULL;
}
for (int i = 0; i < (int)file_pack_items.size(); i++)
{
if (0 == strcmp(pack_name, file_pack_items[i]->pack_name.c_str()))
{
return file_pack_items[i]->file_pack_easy;
}
}
return NULL;
}
const char* XFilePackManage::FindPackByFilePath(const char* path, std::string& pathrecord)
{
std::string str_path = path;
size_t find_pos = str_path.find_first_of('/');
if (find_pos != std::string::npos)
{
std::string predir = str_path.substr(0, find_pos);
for (int i = 0; i < (int)pack_names.size(); i++)
{
if (strcmp(predir.c_str(), pack_names[i].c_str()) == 0)
{
//pathrecord = "/";
pathrecord += str_path[find_pos];
return pack_names[i].c_str();
}
}
}
return NULL;
}
bool XFilePackManage::SaveAll()
{
for (int i = 0; i < (int)file_pack_items.size(); i++)
{
FilePackItem* pPackItem = file_pack_items[i];
if (pPackItem && pPackItem->file_pack_easy)
{
pPackItem->file_pack_easy->SavePackageRecords();
pPackItem->file_pack_easy->Flush();
}
}
return true;
}
bool XFilePackManage::InitPackMan(const char* asset_path)
{
for (int i = 0; i < (int)pack_names.size(); i++)
{
std::string pck_path = std::string(asset_path) + pack_names[i];
pck_path += ".fpk";
if (!XSys::XIsFileExist(pck_path.c_str()))
{
XFilePackageEasy fpe;
if (!fpe.CreatePackage(pck_path.c_str()))
{
XLog::Get().LogOutput(true, "debug", "CreatePackage %s.fpk failed in directory %s!", pack_names[i], asset_path);
return false;
}
fpe.SavePackageRecords();
fpe.CloseFile();
}
if( !AddPack(pack_names[i].c_str(), asset_path))
{
XLog::Get().LogOutput(true, "debug", "AddPack %s to package failed! directory is asset_path");
return false;
}
}
return true;
}
bool XFilePackManage::AddPack(const char* pack_name, const char* pack_dir)
{
std::string strpackname = std::string(pack_name) + ".fpk";
std::string strpackpath = std::string(pack_dir) + strpackname;
XFilePackageEasy* new_fpe = new XFilePackageEasy;
if (new_fpe->InitPackage(strpackpath.c_str()))
{
FilePackItem* item = new FilePackItem;
item->pack_name = pack_name;
item->file_pack_easy = new_fpe;
file_pack_items.push_back(item);
return true;
}
else
{
delete new_fpe;
return false;
}
return false;
}
bool XFilePackManage::AddFile(const char* pack_name, const char* file_path, const void* buffer, int length)
{
XFilePackageEasy* fpe = FindPack(pack_name);
if (!fpe)
{
return false;
}
return fpe->AppendFileFromData(file_path, (const unsigned char*)buffer, length);
}
bool XFilePackManage::DelFile(const char* pack_name, const char* file_path)
{
XFilePackageEasy* fpe = FindPack(pack_name);
if (!fpe)
{
return false;
}
return fpe->RemoveFile(file_path);
}
bool XFilePackManage::ModFile(const char* pack_name, const char* file_path, const void* buffer, int length)
{
XFilePackageEasy* fpe = FindPack(pack_name);
if (!fpe)
{
return false;
}
return fpe->RewriteFileFromData(file_path, (const unsigned char*)buffer, length);
}