Repository navigation
Expand file tree
/
Copy pathDesktopFileSystem.cpp
More file actions
217 lines (183 loc) · 4.5 KB
/
Copy pathDesktopFileSystem.cpp
File metadata and controls
217 lines (183 loc) · 4.5 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
#include "DesktopFileSystem.h"
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <filesystem>
namespace Deki
{
DesktopFileSystem::DesktopFileSystem()
: m_Initialized(false)
{
}
DesktopFileSystem::~DesktopFileSystem()
{
Shutdown();
}
bool DesktopFileSystem::Initialize()
{
if (m_Initialized)
{
return true;
}
// Paths are relative to the working directory. The simulator mirrors the
// device's two storage mounts:
// "S:/" (SD card) -> "./storage/" (exported/packed assets)
// "F:/" (flash) -> "./flash/" (boot scene + project settings, from the SPIFFS export)
m_BasePath = "./storage/";
m_FlashPath = "./flash/";
m_Initialized = true;
return true;
}
void DesktopFileSystem::Shutdown()
{
m_Initialized = false;
}
std::string DesktopFileSystem::ConvertPathInternal(const char* virtualPath)
{
if (!virtualPath)
{
return "";
}
std::string path(virtualPath);
// "S:/..." -> "./storage/..." (SD card mount)
if (path.length() >= 3 && path.substr(0, 3) == "S:/")
{
path = m_BasePath + path.substr(3);
}
// "F:/..." -> "./flash/..." (flash mount)
else if (path.length() >= 3 && path.substr(0, 3) == "F:/")
{
path = m_FlashPath + path.substr(3);
}
return path;
}
IFileSystem::FileHandle DesktopFileSystem::OpenFile(const char* path, OpenMode mode)
{
if (!m_Initialized || !path)
{
return nullptr;
}
std::string realPath = ConvertPathInternal(path);
const char* modeStr = "";
bool isWrite = false;
switch (mode)
{
case OpenMode::ReadBinary: modeStr = "rb"; break;
case OpenMode::WriteBinary:
modeStr = "wb";
isWrite = true;
break;
case OpenMode::ReadText: modeStr = "r"; break;
case OpenMode::WriteText:
modeStr = "w";
isWrite = true;
break;
}
if (isWrite)
{
std::error_code ec;
std::filesystem::path parent = std::filesystem::path(realPath).parent_path();
if (!parent.empty())
{
std::filesystem::create_directories(parent, ec);
}
}
FILE* file = fopen(realPath.c_str(), modeStr);
return static_cast<FileHandle>(file);
}
void DesktopFileSystem::CloseFile(FileHandle handle)
{
if (handle)
{
fclose(static_cast<FILE*>(handle));
}
}
size_t DesktopFileSystem::ReadFile(FileHandle handle, void* buffer, size_t size)
{
if (!handle || !buffer)
{
return 0;
}
return fread(buffer, 1, size, static_cast<FILE*>(handle));
}
size_t DesktopFileSystem::WriteFile(FileHandle handle, const void* buffer, size_t size)
{
if (!handle || !buffer)
{
return 0;
}
return fwrite(buffer, 1, size, static_cast<FILE*>(handle));
}
long DesktopFileSystem::SeekFile(FileHandle handle, long offset, SeekOrigin origin)
{
if (!handle)
{
return -1;
}
int whence = SEEK_SET;
switch (origin)
{
case SeekOrigin::Begin: whence = SEEK_SET; break;
case SeekOrigin::Current: whence = SEEK_CUR; break;
case SeekOrigin::End: whence = SEEK_END; break;
}
if (fseek(static_cast<FILE*>(handle), offset, whence) == 0)
{
return ftell(static_cast<FILE*>(handle));
}
return -1;
}
long DesktopFileSystem::TellFile(FileHandle handle)
{
if (!handle)
{
return -1;
}
return ftell(static_cast<FILE*>(handle));
}
long DesktopFileSystem::GetFileSize(FileHandle handle)
{
if (!handle)
{
return -1;
}
FILE* file = static_cast<FILE*>(handle);
long currentPos = ftell(file);
if (fseek(file, 0, SEEK_END) != 0)
{
return -1;
}
long size = ftell(file);
fseek(file, currentPos, SEEK_SET);
return size;
}
bool DesktopFileSystem::FileExists(const char* path)
{
if (!m_Initialized || !path)
{
return false;
}
std::string realPath = ConvertPathInternal(path);
FILE* file = fopen(realPath.c_str(), "rb");
if (file)
{
fclose(file);
return true;
}
return false;
}
bool DesktopFileSystem::ConvertPath(const char* virtualPath, char* outBuffer, size_t bufferSize)
{
if (!virtualPath || !outBuffer || bufferSize == 0)
{
return false;
}
std::string converted = ConvertPathInternal(virtualPath);
if (converted.length() >= bufferSize)
{
return false;
}
strcpy(outBuffer, converted.c_str());
return true;
}
} // namespace Deki