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
|
#include <physfs/physfs.h>
#include "../exceptions/exception.h"
#include "File.h"
#include "FileData.h"
#include "FileSystem.h"
using namespace std;
namespace AsuraEngine
{
namespace IO
{
#ifdef ASURA_WINDOWS
#include <windows.h>
#include <direct.h>
#else
#include <sys/param.h>
#include <unistd.h>
#endif
Filesystem::~Filesystem()
{
if (m_Inited) //PHYSFS_isInit
PHYSFS_deinit();
}
void Filesystem::Init(const char* arg0)
{
if (!PHYSFS_init(arg0))
throw Exception("Failed to initialize filesystem: %s", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
m_Inited = true;
}
bool Filesystem::Mount(const std::string& locpath, const std::string& montpoint/* = "/"*/, bool prepend /*= false*/)
{
if (!m_Inited)
return false;
return PHYSFS_mount(locpath.c_str(), montpoint.c_str(), !prepend);
}
bool Filesystem::Mount(DataBuffer* db, const std::string& archivename, const std::string& mountpoint /*= "/"*/, bool prepend /*= false*/)
{
if (!m_Inited)
return false;
if (PHYSFS_mountMemory(db->GetData(), db->GetSize(), nullptr, archivename.c_str(), mountpoint.c_str(), !prepend))
{
m_MountData[archivename] = db;
return true;
}
return false;
}
bool Filesystem::Unmount(const std::string& locpath)
{
if (!m_Inited)
return false;
// ǹ鵵ӳɾ
auto datait = m_MountData.find(locpath);
if (datait != m_MountData.end() && PHYSFS_unmount(locpath.c_str()) != 0)
{
m_MountData.erase(datait);
return true;
}
return PHYSFS_unmount(locpath.c_str());
}
bool Filesystem::Unmount(DataBuffer* db)
{
for (const auto& dp : m_MountData)
{
if (dp.second == db)
{
std::string archive = dp.first;
return Unmount(archive);
}
}
}
bool Filesystem::GetMountPoint(const std::string& locpath, ASURA_OUT std::string& mountpoint)
{
if (!m_Inited)
return false;
const char* point = PHYSFS_getMountPoint(locpath.c_str());
if (point != nullptr)
{
mountpoint = point;
return true;
}
return false;
}
void Filesystem::SetWriteDirectory(const std::string locpath)
{
if (!m_Inited)
return;
if (!PHYSFS_setWriteDir(locpath.c_str()))
throw Exception("Failed to set write directory %s", locpath.c_str());
}
std::string Filesystem::GetWriteDirectory()
{
return PHYSFS_getWriteDir();
}
File* Filesystem::NewFile(const std::string& name)
{
return new File(name);
}
bool Filesystem::NewDirectory(const std::string& path)
{
if (!m_Inited)
return false;
if (!PHYSFS_getWriteDir())
return false;
if (!PHYSFS_mkdir(path.c_str()))
return false;
return true;
}
bool Filesystem::Write(const std::string& name, ASURA_REF DataBuffer* buffer)
{
File file(name);
file.Open(File::FILE_MODE_WRITE);
if (!file.Write(buffer))
throw Exception("Data could not be written.");
}
bool Filesystem::Append(const std::string& name, ASURA_REF DataBuffer* buffer)
{
File file(name);
file.Open(File::FILE_MODE_APPEND);
if (!file.Write(buffer))
throw Exception("Data could not be append.");
}
FileData* Filesystem::Read(const std::string& name)
{
File file = File(name);
file.Open(File::FILE_MODE_READ);
int size = file.GetSize();
DataBuffer* db = new DataBuffer(size);
if (db)
{
file.ReadAll(db);
FileData* fd = new FileData(name);
fd->BindData(db);
return fd;
}
return nullptr;
}
bool Filesystem::Remove(const std::string& path)
{
if (!m_Inited)
return false;
if (PHYSFS_getWriteDir() == 0)
return false;
if (!PHYSFS_delete(path.c_str()))
return false;
return true;
}
bool Filesystem::GetFileInfo(const std::string& filepath, ASURA_OUT FileInfo* info)
{
if (!m_Inited)
return false;
PHYSFS_Stat stat = {};
if (!PHYSFS_stat(filepath.c_str(), &stat))
return false;
info->size = (int64)stat.filesize;
info->modtime = (int64)stat.modtime;
if (stat.filetype == PHYSFS_FILETYPE_REGULAR)
info->type = FILE_TYPE_FILE;
else if (stat.filetype == PHYSFS_FILETYPE_DIRECTORY)
info->type = FILE_TYPE_DIRECTORY;
else if (stat.filetype == PHYSFS_FILETYPE_SYMLINK)
info->type = FILE_TYPE_SYMLINK;
else
info->type = FILE_TYPE_OTHER;
return true;
}
}
}
|