Repository navigation
Expand file tree
/
Copy pathTactilityFileSystem.cpp
More file actions
285 lines (247 loc) · 7 KB
/
Copy pathTactilityFileSystem.cpp
File metadata and controls
285 lines (247 loc) · 7 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
#include "TactilityFileSystem.h"
#include <cstdio>
#include <cstring>
#include <sys/stat.h>
#include <deki/LogSystem.h>
#if defined(DEKI_TACTILITY_TARGET)
extern "C"
{
#include <app/paths.h>
}
#endif
namespace DekiTactility
{
namespace
{
/// Tactility documents 255 as the maximum path length.
constexpr size_t kMaxPath = 256;
const char* ModeToStdio(Deki::IFileSystem::OpenMode mode)
{
switch (mode)
{
case Deki::IFileSystem::OpenMode::ReadBinary: return "rb";
case Deki::IFileSystem::OpenMode::WriteBinary: return "wb";
case Deki::IFileSystem::OpenMode::ReadText: return "r";
case Deki::IFileSystem::OpenMode::WriteText: return "w";
}
return "rb";
}
int OriginToStdio(Deki::IFileSystem::SeekOrigin origin)
{
switch (origin)
{
case Deki::IFileSystem::SeekOrigin::Begin: return SEEK_SET;
case Deki::IFileSystem::SeekOrigin::Current: return SEEK_CUR;
case Deki::IFileSystem::SeekOrigin::End: return SEEK_END;
}
return SEEK_SET;
}
bool IsFile(const std::string& path)
{
struct stat st;
return stat(path.c_str(), &st) == 0 && S_ISREG(st.st_mode);
}
// Makes the folders on the way to the file `path`, as the desktop does: a
// game writing "F:/saves/slot1.dat" should not have to make "saves" first.
void MakeParents(const std::string& path)
{
#if defined(DEKI_TACTILITY_TARGET)
for (size_t slash = path.find('/', 1); slash != std::string::npos; slash = path.find('/', slash + 1))
{
mkdir(path.substr(0, slash).c_str(), 0775);
}
#else
(void)path; // the host build opens no files
#endif
}
} // namespace
TactilityFileSystem::TactilityFileSystem(const char* appId)
: m_AppId(appId != nullptr ? appId : "")
{
}
TactilityFileSystem::~TactilityFileSystem()
{
Shutdown();
}
std::string TactilityFileSystem::Resolve(const char* virtualPath) const
{
if (virtualPath == nullptr)
{
return {};
}
// Prefixes are matched case-sensitively, as the engine writes them.
if (std::strncmp(virtualPath, "F:/", 3) == 0)
{
const std::string inAssets = m_AssetsPath + "/" + (virtualPath + 3);
return IsFile(inAssets) ? inAssets : m_UserDataPath + "/internal/" + (virtualPath + 3);
}
if (std::strncmp(virtualPath, "S:/", 3) == 0)
{
return m_UserDataPath + "/" + (virtualPath + 3);
}
// No prefix: the engine's Storage::Default. Treated as user data, the only
// place an app can write.
if (virtualPath[0] == '/')
{
return virtualPath; // already absolute
}
return m_UserDataPath + "/" + virtualPath;
}
bool TactilityFileSystem::IsGameData(const char* virtualPath) const
{
return virtualPath != nullptr && std::strncmp(virtualPath, "F:/", 3) == 0 &&
IsFile(m_AssetsPath + "/" + (virtualPath + 3));
}
bool TactilityFileSystem::ConvertPath(const char* virtualPath, char* outBuffer, size_t bufferSize)
{
const std::string resolved = Resolve(virtualPath);
if (resolved.empty() || outBuffer == nullptr || resolved.size() + 1 > bufferSize)
{
return false;
}
std::memcpy(outBuffer, resolved.c_str(), resolved.size() + 1);
return true;
}
#if defined(DEKI_TACTILITY_TARGET)
bool TactilityFileSystem::Initialize()
{
if (m_Initialized)
{
return true;
}
if (m_AppId.empty())
{
DEKI_LOG_ERROR("TactilityFileSystem: no app id; both directories are keyed off it");
return false;
}
char buffer[kMaxPath];
if (app_paths_get_assets_directory(m_AppId.c_str(), buffer, sizeof(buffer)) != ERROR_NONE)
{
DEKI_LOG_ERROR("TactilityFileSystem: could not resolve the assets directory for '%s'", m_AppId.c_str());
return false;
}
m_AssetsPath = buffer;
if (app_paths_get_user_data_directory(m_AppId.c_str(), buffer, sizeof(buffer)) != ERROR_NONE)
{
DEKI_LOG_ERROR("TactilityFileSystem: could not resolve the user data directory for '%s'", m_AppId.c_str());
return false;
}
m_UserDataPath = buffer;
m_Initialized = true;
DEKI_LOG_INFO("TactilityFileSystem: F:/ -> %s, S:/ -> %s", m_AssetsPath.c_str(), m_UserDataPath.c_str());
return true;
}
#else // !DEKI_TACTILITY_TARGET — editor/host build, no Tactility SDK present
bool TactilityFileSystem::Initialize()
{
return false;
}
#endif // DEKI_TACTILITY_TARGET
void TactilityFileSystem::Shutdown()
{
m_AssetsPath.clear();
m_UserDataPath.clear();
m_Initialized = false;
}
Deki::IFileSystem::FileHandle TactilityFileSystem::OpenFile(const char* path, OpenMode mode)
{
if (!m_Initialized)
{
return nullptr;
}
const bool writing = mode == OpenMode::WriteBinary || mode == OpenMode::WriteText;
if (writing && IsGameData(path))
{
// The game's data is what the .app shipped, the same every run.
DEKI_LOG_WARNING("%s is part of the game's data, which is never written; write the game's own files "
"under another name",
path);
return nullptr;
}
const std::string resolved = Resolve(path);
if (resolved.empty())
{
return nullptr;
}
if (writing)
{
MakeParents(resolved);
}
return (FileHandle)std::fopen(resolved.c_str(), ModeToStdio(mode));
}
void TactilityFileSystem::CloseFile(FileHandle handle)
{
if (handle != nullptr)
{
std::fclose((FILE*)handle);
}
}
size_t TactilityFileSystem::ReadFile(FileHandle handle, void* buffer, size_t size)
{
if (handle == nullptr || buffer == nullptr)
{
return 0;
}
return std::fread(buffer, 1, size, (FILE*)handle);
}
size_t TactilityFileSystem::WriteFile(FileHandle handle, const void* buffer, size_t size)
{
if (handle == nullptr || buffer == nullptr)
{
return 0;
}
return std::fwrite(buffer, 1, size, (FILE*)handle);
}
long TactilityFileSystem::SeekFile(FileHandle handle, long offset, SeekOrigin origin)
{
if (handle == nullptr)
{
return -1;
}
return std::fseek((FILE*)handle, offset, OriginToStdio(origin));
}
long TactilityFileSystem::TellFile(FileHandle handle)
{
if (handle == nullptr)
{
return -1;
}
return std::ftell((FILE*)handle);
}
long TactilityFileSystem::GetFileSize(FileHandle handle)
{
if (handle == nullptr)
{
return -1;
}
FILE* file = (FILE*)handle;
const long current = std::ftell(file);
if (current < 0 || std::fseek(file, 0, SEEK_END) != 0)
{
return -1;
}
const long size = std::ftell(file);
// Put the cursor back: callers use this mid-read to size a buffer.
std::fseek(file, current, SEEK_SET);
return size;
}
bool TactilityFileSystem::FileExists(const char* path)
{
if (!m_Initialized)
{
return false;
}
const std::string resolved = Resolve(path);
if (resolved.empty())
{
return false;
}
FILE* file = std::fopen(resolved.c_str(), "rb");
if (file == nullptr)
{
return false;
}
std::fclose(file);
return true;
}
} // namespace DekiTactility