2013-07-28 06:46:26 +00:00
|
|
|
// Copyright (c) 2012- PPSSPP Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official git repository and contact information can be found at
|
|
|
|
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
// TODO: Remove the Windows-specific code, FILE is fine there too.
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
#include "Core/FileSystems/FileSystem.h"
|
|
|
|
#include "Core/FileSystems/DirectoryFileSystem.h"
|
|
|
|
|
2013-07-28 17:31:42 +00:00
|
|
|
class VirtualDiscFileSystem: public IFileSystem {
|
2013-07-28 06:46:26 +00:00
|
|
|
public:
|
|
|
|
VirtualDiscFileSystem(IHandleAllocator *_hAlloc, std::string _basePath);
|
|
|
|
~VirtualDiscFileSystem();
|
|
|
|
|
2014-12-08 20:14:35 +00:00
|
|
|
void DoState(PointerWrap &p) override;
|
|
|
|
u32 OpenFile(std::string filename, FileAccess access, const char *devicename=NULL) override;
|
|
|
|
size_t SeekFile(u32 handle, s32 position, FileMove type) override;
|
|
|
|
size_t ReadFile(u32 handle, u8 *pointer, s64 size) override;
|
2013-12-28 00:36:51 +00:00
|
|
|
size_t ReadFile(u32 handle, u8 *pointer, s64 size, int &usec) override;
|
2014-12-08 20:14:35 +00:00
|
|
|
void CloseFile(u32 handle) override;
|
|
|
|
PSPFileInfo GetFileInfo(std::string filename) override;
|
|
|
|
bool OwnsHandle(u32 handle) override;
|
|
|
|
int Ioctl(u32 handle, u32 cmd, u32 indataPtr, u32 inlen, u32 outdataPtr, u32 outlen, int &usec) override;
|
|
|
|
int DevType(u32 handle) override;
|
|
|
|
bool GetHostPath(const std::string &inpath, std::string &outpath) override;
|
|
|
|
std::vector<PSPFileInfo> GetDirListing(std::string path) override;
|
|
|
|
int Flags() override { return 0; }
|
2014-11-01 06:48:08 +00:00
|
|
|
u64 FreeSpace(const std::string &path) override { return 0; }
|
2013-07-28 06:46:26 +00:00
|
|
|
|
|
|
|
// unsupported operations
|
2014-12-08 20:14:35 +00:00
|
|
|
size_t WriteFile(u32 handle, const u8 *pointer, s64 size) override;
|
2013-12-28 00:36:51 +00:00
|
|
|
size_t WriteFile(u32 handle, const u8 *pointer, s64 size, int &usec) override;
|
2014-12-08 20:14:35 +00:00
|
|
|
bool MkDir(const std::string &dirname) override;
|
|
|
|
bool RmDir(const std::string &dirname) override;
|
|
|
|
int RenameFile(const std::string &from, const std::string &to) override;
|
|
|
|
bool RemoveFile(const std::string &filename) override;
|
2013-07-28 06:46:26 +00:00
|
|
|
|
|
|
|
private:
|
2013-07-28 09:48:31 +00:00
|
|
|
void LoadFileListIndex();
|
2013-07-28 06:46:26 +00:00
|
|
|
int getFileListIndex(std::string& fileName);
|
2013-07-28 14:56:13 +00:00
|
|
|
int getFileListIndex(u32 accessBlock, u32 accessSize, bool blockMode = false);
|
2013-07-28 06:46:26 +00:00
|
|
|
std::string GetLocalPath(std::string localpath);
|
|
|
|
|
2013-07-28 17:31:42 +00:00
|
|
|
typedef void *HandlerLibrary;
|
|
|
|
typedef int HandlerHandle;
|
|
|
|
typedef s64 HandlerOffset;
|
2013-07-28 20:09:00 +00:00
|
|
|
typedef void (*HandlerLogFunc)(void *arg, HandlerHandle handle, LogTypes::LOG_LEVELS level, const char *msg);
|
2013-07-28 18:49:02 +00:00
|
|
|
|
2013-07-28 20:09:00 +00:00
|
|
|
static void HandlerLogger(void *arg, HandlerHandle handle, LogTypes::LOG_LEVELS level, const char *msg);
|
2013-07-28 17:31:42 +00:00
|
|
|
|
|
|
|
// The primary purpose of handlers is to make it easier to work with large archives.
|
|
|
|
// However, they have other uses as well, such as patching individual files.
|
|
|
|
struct Handler {
|
2013-07-28 20:09:00 +00:00
|
|
|
Handler(const char *filename, VirtualDiscFileSystem *const sys);
|
2013-07-28 17:31:42 +00:00
|
|
|
~Handler();
|
|
|
|
|
2013-07-28 20:09:00 +00:00
|
|
|
typedef bool (*InitFunc)(HandlerLogFunc logger, void *loggerArg);
|
2013-07-28 18:49:02 +00:00
|
|
|
typedef void (*ShutdownFunc)();
|
|
|
|
typedef HandlerHandle (*OpenFunc)(const char *basePath, const char *filename);
|
|
|
|
typedef HandlerOffset (*SeekFunc)(HandlerHandle handle, HandlerOffset offset, FileMove origin);
|
|
|
|
typedef HandlerOffset (*ReadFunc)(HandlerHandle handle, void *data, HandlerOffset size);
|
|
|
|
typedef void (*CloseFunc)(HandlerHandle handle);
|
|
|
|
|
2013-07-28 17:31:42 +00:00
|
|
|
HandlerLibrary library;
|
2013-07-28 18:49:02 +00:00
|
|
|
InitFunc Init;
|
|
|
|
ShutdownFunc Shutdown;
|
|
|
|
OpenFunc Open;
|
|
|
|
SeekFunc Seek;
|
|
|
|
ReadFunc Read;
|
|
|
|
CloseFunc Close;
|
|
|
|
|
2014-12-07 22:00:14 +00:00
|
|
|
bool IsValid() const { return library != NULL; }
|
2013-07-28 17:31:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct HandlerFileHandle {
|
|
|
|
Handler *handler;
|
|
|
|
HandlerHandle handle;
|
|
|
|
|
2013-07-28 18:49:02 +00:00
|
|
|
HandlerFileHandle() : handler(NULL), handle(0) {
|
|
|
|
}
|
|
|
|
HandlerFileHandle(Handler *handler_) : handler(handler_), handle(-1) {
|
|
|
|
}
|
|
|
|
|
2013-07-28 17:31:42 +00:00
|
|
|
bool Open(std::string& basePath, std::string& fileName, FileAccess access) {
|
|
|
|
// Ignore access, read only.
|
|
|
|
handle = handler->Open(basePath.c_str(), fileName.c_str());
|
|
|
|
return handle > 0;
|
|
|
|
}
|
|
|
|
size_t Read(u8 *data, s64 size) {
|
|
|
|
return (size_t)handler->Read(handle, data, size);
|
|
|
|
}
|
|
|
|
size_t Seek(s32 position, FileMove type) {
|
|
|
|
return (size_t)handler->Seek(handle, position, type);
|
|
|
|
}
|
|
|
|
void Close() {
|
|
|
|
handler->Close(handle);
|
|
|
|
}
|
2013-07-28 18:49:02 +00:00
|
|
|
|
|
|
|
bool IsValid() {
|
|
|
|
return handler != NULL && handler->IsValid();
|
|
|
|
}
|
|
|
|
|
|
|
|
HandlerFileHandle &operator =(Handler *_handler) {
|
|
|
|
handler = _handler;
|
|
|
|
return *this;
|
|
|
|
}
|
2013-07-28 17:31:42 +00:00
|
|
|
};
|
|
|
|
|
2013-07-28 06:46:26 +00:00
|
|
|
typedef enum { VFILETYPE_NORMAL, VFILETYPE_LBN, VFILETYPE_ISO } VirtualFileType;
|
|
|
|
|
|
|
|
struct OpenFileEntry {
|
|
|
|
DirectoryFileHandle hFile;
|
2013-07-28 17:31:42 +00:00
|
|
|
HandlerFileHandle handler;
|
2013-07-28 06:46:26 +00:00
|
|
|
VirtualFileType type;
|
|
|
|
u32 fileIndex;
|
2013-08-14 04:05:54 +00:00
|
|
|
u64 curOffset;
|
|
|
|
u64 startOffset; // only used by lbn files
|
|
|
|
u64 size; // only used by lbn files
|
2013-07-28 18:49:02 +00:00
|
|
|
|
|
|
|
bool Open(std::string& basePath, std::string& fileName, FileAccess access) {
|
2014-11-02 20:40:31 +00:00
|
|
|
// Ignored, we're read only.
|
|
|
|
u32 err;
|
2013-07-28 18:49:02 +00:00
|
|
|
if (handler.IsValid()) {
|
|
|
|
return handler.Open(basePath, fileName, access);
|
|
|
|
} else {
|
2014-11-02 20:40:31 +00:00
|
|
|
return hFile.Open(basePath, fileName, access, err);
|
2013-07-28 18:49:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
size_t Read(u8 *data, s64 size) {
|
|
|
|
if (handler.IsValid()) {
|
|
|
|
return handler.Read(data, size);
|
|
|
|
} else {
|
|
|
|
return hFile.Read(data, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
size_t Seek(s32 position, FileMove type) {
|
|
|
|
if (handler.IsValid()) {
|
|
|
|
return handler.Seek(position, type);
|
|
|
|
} else {
|
|
|
|
return hFile.Seek(position, type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void Close() {
|
|
|
|
if (handler.IsValid()) {
|
|
|
|
return handler.Close();
|
|
|
|
} else {
|
|
|
|
return hFile.Close();
|
|
|
|
}
|
|
|
|
}
|
2013-07-28 06:46:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::map<u32, OpenFileEntry> EntryMap;
|
|
|
|
EntryMap entries;
|
|
|
|
IHandleAllocator *hAlloc;
|
|
|
|
std::string basePath;
|
|
|
|
|
2013-07-28 18:49:02 +00:00
|
|
|
struct FileListEntry {
|
2013-07-28 06:46:26 +00:00
|
|
|
std::string fileName;
|
|
|
|
u32 firstBlock;
|
|
|
|
u32 totalSize;
|
2013-07-28 18:49:02 +00:00
|
|
|
Handler *handler;
|
|
|
|
};
|
2013-07-28 06:46:26 +00:00
|
|
|
|
|
|
|
std::vector<FileListEntry> fileList;
|
|
|
|
u32 currentBlockIndex;
|
2013-12-28 00:49:35 +00:00
|
|
|
u32 lastReadBlock_;
|
2013-07-28 17:31:42 +00:00
|
|
|
|
2013-07-28 18:49:02 +00:00
|
|
|
std::map<std::string, Handler *> handlers;
|
2013-07-28 06:46:26 +00:00
|
|
|
};
|