Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/FileSystems/VirtualDiscFileSystem.h
3187 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
#include "Core/FileSystems/DirectoryFileSystem.h"
27
28
extern const std::string INDEX_FILENAME;
29
30
class VirtualDiscFileSystem: public IFileSystem {
31
public:
32
VirtualDiscFileSystem(IHandleAllocator *_hAlloc, const Path &_basePath);
33
~VirtualDiscFileSystem();
34
35
void DoState(PointerWrap &p) override;
36
int OpenFile(std::string filename, FileAccess access, const char *devicename = nullptr) override;
37
size_t SeekFile(u32 handle, s32 position, FileMove type) override;
38
size_t ReadFile(u32 handle, u8 *pointer, s64 size) override;
39
size_t ReadFile(u32 handle, u8 *pointer, s64 size, int &usec) override;
40
void CloseFile(u32 handle) override;
41
PSPFileInfo GetFileInfo(std::string filename) override;
42
PSPFileInfo GetFileInfoByHandle(u32 handle) override;
43
bool OwnsHandle(u32 handle) override;
44
int Ioctl(u32 handle, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, u32 outlen, int &usec) override;
45
PSPDevType DevType(u32 handle) override;
46
std::vector<PSPFileInfo> GetDirListing(const std::string &path, bool *exists = nullptr) override;
47
FileSystemFlags Flags() const override { return FileSystemFlags::UMD; }
48
u64 FreeDiskSpace(const std::string &path) override { return 0; }
49
50
// unsupported operations
51
size_t WriteFile(u32 handle, const u8 *pointer, s64 size) override;
52
size_t WriteFile(u32 handle, const u8 *pointer, s64 size, int &usec) override;
53
bool MkDir(const std::string &dirname) override;
54
bool RmDir(const std::string &dirname) override;
55
int RenameFile(const std::string &from, const std::string &to) override;
56
bool RemoveFile(const std::string &filename) override;
57
58
bool ComputeRecursiveDirSizeIfFast(const std::string &path, int64_t *size) override { return false; }
59
60
void Describe(char *buf, size_t size) const override { snprintf(buf, size, "VirtualDisc: %s", basePath.ToVisualString().c_str()); } // TODO: Ask the fileLoader about the origins
61
62
private:
63
void LoadFileListIndex();
64
// Warning: modifies input string.
65
int getFileListIndex(std::string &fileName);
66
int getFileListIndex(u32 accessBlock, u32 accessSize, bool blockMode = false) const;
67
Path GetLocalPath(std::string localpath) const;
68
69
typedef void *HandlerLibrary;
70
typedef int HandlerHandle;
71
typedef s64 HandlerOffset;
72
typedef void (*HandlerLogFunc)(void *arg, HandlerHandle handle, LogLevel level, const char *msg);
73
74
static void HandlerLogger(void *arg, HandlerHandle handle, LogLevel level, const char *msg);
75
76
// The primary purpose of handlers is to make it easier to work with large archives.
77
// However, they have other uses as well, such as patching individual files.
78
struct Handler {
79
Handler(const char *filename, VirtualDiscFileSystem *const sys);
80
~Handler();
81
82
typedef bool (*InitFunc)(HandlerLogFunc logger, void *loggerArg);
83
typedef void (*ShutdownFunc)();
84
typedef void (*ShutdownV2Func)(void *loggerArg);
85
typedef HandlerHandle (*OpenFunc)(const char *basePath, const char *filename);
86
typedef HandlerOffset (*SeekFunc)(HandlerHandle handle, HandlerOffset offset, FileMove origin);
87
typedef HandlerOffset (*ReadFunc)(HandlerHandle handle, void *data, HandlerOffset size);
88
typedef void (*CloseFunc)(HandlerHandle handle);
89
typedef int (*VersionFunc)();
90
91
HandlerLibrary library;
92
VirtualDiscFileSystem *const sys_;
93
InitFunc Init;
94
ShutdownFunc Shutdown;
95
ShutdownV2Func ShutdownV2;
96
OpenFunc Open;
97
SeekFunc Seek;
98
ReadFunc Read;
99
CloseFunc Close;
100
101
bool IsValid() const { return library != nullptr; }
102
};
103
104
struct HandlerFileHandle {
105
Handler *handler;
106
HandlerHandle handle;
107
108
HandlerFileHandle() : handler(nullptr), handle(0) {}
109
HandlerFileHandle(Handler *handler_) : handler(handler_), handle(-1) {}
110
111
bool Open(const std::string& basePath, const std::string& fileName, FileAccess access) {
112
// Ignore access, read only.
113
handle = handler->Open(basePath.c_str(), fileName.c_str());
114
return handle > 0;
115
}
116
size_t Read(u8 *data, s64 size) {
117
return (size_t)handler->Read(handle, data, size);
118
}
119
size_t Seek(s32 position, FileMove type) {
120
return (size_t)handler->Seek(handle, position, type);
121
}
122
void Close() {
123
handler->Close(handle);
124
}
125
126
bool IsValid() {
127
return handler != nullptr && handler->IsValid();
128
}
129
130
HandlerFileHandle &operator =(Handler *_handler) {
131
handler = _handler;
132
return *this;
133
}
134
};
135
136
typedef enum { VFILETYPE_NORMAL, VFILETYPE_LBN, VFILETYPE_ISO } VirtualFileType;
137
138
struct OpenFileEntry {
139
OpenFileEntry() {}
140
OpenFileEntry(FileSystemFlags fileSystemFlags) {
141
hFile = DirectoryFileHandle(DirectoryFileHandle::SKIP_REPLAY, fileSystemFlags);
142
}
143
144
DirectoryFileHandle hFile;
145
HandlerFileHandle handler;
146
VirtualFileType type = VFILETYPE_NORMAL;
147
u32 fileIndex = 0;
148
u64 curOffset = 0;
149
u64 startOffset = 0; // only used by lbn files
150
u64 size = 0; // only used by lbn files
151
152
bool Open(const Path &basePath, std::string& fileName, FileAccess access) {
153
// Ignored, we're read only.
154
u32 err;
155
if (handler.IsValid()) {
156
return handler.Open(basePath.ToString(), fileName, access);
157
} else {
158
return hFile.Open(basePath, fileName, access, err);
159
}
160
}
161
size_t Read(u8 *data, s64 size) {
162
if (handler.IsValid()) {
163
return handler.Read(data, size);
164
} else {
165
return hFile.Read(data, size);
166
}
167
}
168
size_t Seek(s32 position, FileMove type) {
169
if (handler.IsValid()) {
170
return handler.Seek(position, type);
171
} else {
172
return hFile.Seek(position, type);
173
}
174
}
175
void Close() {
176
if (handler.IsValid()) {
177
return handler.Close();
178
} else {
179
return hFile.Close();
180
}
181
}
182
};
183
184
typedef std::map<u32, OpenFileEntry> EntryMap;
185
186
EntryMap entries;
187
IHandleAllocator *hAlloc;
188
Path basePath;
189
190
struct FileListEntry {
191
std::string fileName;
192
u32 firstBlock;
193
u32 totalSize;
194
Handler *handler;
195
};
196
197
std::vector<FileListEntry> fileList;
198
u32 currentBlockIndex;
199
u32 lastReadBlock_;
200
201
std::map<std::string, Handler *> handlers;
202
};
203
204