Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/FileSystems/DirectoryFileSystem.h
5656 views
1
// Copyright (c) 2012- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#pragma once
19
20
// TODO: Remove the Windows-specific code, FILE is fine there too.
21
22
#include <map>
23
24
#include "Common/File/Path.h"
25
#include "Core/FileSystems/FileSystem.h"
26
27
#if defined(_WIN32) && !defined(HAVE_LIBRETRO_VFS)
28
typedef void * HANDLE;
29
#endif
30
31
struct DirectoryFileHandle {
32
enum Flags {
33
NORMAL,
34
SKIP_REPLAY,
35
};
36
37
#ifdef HAVE_LIBRETRO_VFS
38
FILE *hFile = nullptr;
39
#elif defined(_WIN32)
40
HANDLE hFile = (HANDLE)-1;
41
#else
42
int hFile = -1;
43
#endif
44
s64 needsTrunc_ = -1;
45
bool replay_ = true;
46
bool inGameDir_ = false;
47
FileSystemFlags fileSystemFlags_ = (FileSystemFlags)0;
48
49
DirectoryFileHandle() {}
50
51
DirectoryFileHandle(Flags flags, FileSystemFlags fileSystemFlags)
52
: replay_(flags != SKIP_REPLAY), fileSystemFlags_(fileSystemFlags) {}
53
54
Path GetLocalPath(const Path &basePath, std::string_view localPath) const;
55
bool Open(const Path &basePath, std::string &fileName, FileAccess access, u32 &err);
56
size_t Read(u8* pointer, s64 size);
57
size_t Write(const u8* pointer, s64 size);
58
size_t Seek(s32 position, FileMove type);
59
void Close();
60
};
61
62
class DirectoryFileSystem : public IFileSystem {
63
public:
64
DirectoryFileSystem(IHandleAllocator *_hAlloc, const Path &_basePath, FileSystemFlags _flags = FileSystemFlags::NONE);
65
~DirectoryFileSystem();
66
67
void CloseAll();
68
69
void DoState(PointerWrap &p) override;
70
std::vector<PSPFileInfo> GetDirListing(const std::string &path, bool *exists = nullptr) override;
71
int OpenFile(std::string filename, FileAccess access, const char *devicename = nullptr) override;
72
void CloseFile(u32 handle) override;
73
size_t ReadFile(u32 handle, u8 *pointer, s64 size) override;
74
size_t ReadFile(u32 handle, u8 *pointer, s64 size, int &usec) override;
75
size_t WriteFile(u32 handle, const u8 *pointer, s64 size) override;
76
size_t WriteFile(u32 handle, const u8 *pointer, s64 size, int &usec) override;
77
size_t SeekFile(u32 handle, s32 position, FileMove type) override;
78
PSPFileInfo GetFileInfo(std::string filename) override;
79
PSPFileInfo GetFileInfoByHandle(u32 handle) override;
80
bool OwnsHandle(u32 handle) override;
81
int Ioctl(u32 handle, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, u32 outlen, int &usec) override;
82
PSPDevType DevType(u32 handle) override;
83
84
bool MkDir(const std::string &dirname) override;
85
bool RmDir(const std::string &dirname) override;
86
int RenameFile(const std::string &from, const std::string &to) override;
87
bool RemoveFile(const std::string &filename) override;
88
FileSystemFlags Flags() const override { return flags; }
89
u64 FreeDiskSpace(const std::string &path) override;
90
91
bool ComputeRecursiveDirSizeIfFast(const std::string &path, int64_t *size) override;
92
void Describe(char *buf, size_t size) const override { snprintf(buf, size, "Dir: %s", basePath.c_str()); }
93
94
private:
95
struct OpenFileEntry {
96
DirectoryFileHandle hFile;
97
std::string guestFilename;
98
FileAccess access = FILEACCESS_NONE;
99
};
100
101
typedef std::map<u32, OpenFileEntry> EntryMap;
102
EntryMap entries;
103
Path basePath;
104
IHandleAllocator *hAlloc;
105
FileSystemFlags flags;
106
107
Path GetLocalPath(std::string_view internalPath) const;
108
};
109
110
// VFSFileSystem: Ability to map in Android APK paths as well! Does not support all features, only meant for fonts.
111
// Very inefficient - always load the whole file on open.
112
class VFSFileSystem : public IFileSystem {
113
public:
114
VFSFileSystem(IHandleAllocator *_hAlloc, std::string _basePath);
115
~VFSFileSystem();
116
117
void DoState(PointerWrap &p) override;
118
std::vector<PSPFileInfo> GetDirListing(const std::string &path, bool *exists = nullptr) override;
119
int OpenFile(std::string filename, FileAccess access, const char *devicename = nullptr) override;
120
void CloseFile(u32 handle) override;
121
size_t ReadFile(u32 handle, u8 *pointer, s64 size) override;
122
size_t ReadFile(u32 handle, u8 *pointer, s64 size, int &usec) override;
123
size_t WriteFile(u32 handle, const u8 *pointer, s64 size) override;
124
size_t WriteFile(u32 handle, const u8 *pointer, s64 size, int &usec) override;
125
size_t SeekFile(u32 handle, s32 position, FileMove type) override;
126
PSPFileInfo GetFileInfo(std::string filename) override;
127
PSPFileInfo GetFileInfoByHandle(u32 handle) override;
128
bool OwnsHandle(u32 handle) override;
129
int Ioctl(u32 handle, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, u32 outlen, int &usec) override;
130
PSPDevType DevType(u32 handle) override;
131
132
bool MkDir(const std::string &dirname) override;
133
bool RmDir(const std::string &dirname) override;
134
int RenameFile(const std::string &from, const std::string &to) override;
135
bool RemoveFile(const std::string &filename) override;
136
FileSystemFlags Flags() const override { return FileSystemFlags::FLASH; }
137
u64 FreeDiskSpace(const std::string &path) override { return 0; }
138
139
bool ComputeRecursiveDirSizeIfFast(const std::string &path, int64_t *size) override { return false; }
140
void Describe(char *buf, size_t size) const override { snprintf(buf, size, "VFS: %s", basePath.c_str()); }
141
142
private:
143
struct OpenFileEntry {
144
u8 *fileData;
145
size_t size;
146
size_t seekPos;
147
};
148
149
typedef std::map<u32, OpenFileEntry> EntryMap;
150
EntryMap entries;
151
std::string basePath;
152
IHandleAllocator *hAlloc;
153
154
std::string GetLocalPath(std::string_view localpath) const;
155
};
156
157