~ppsspp/ppsspp/ppsspp_1.3.0

« back to all changes in this revision

Viewing changes to Core/FileSystems/VirtualDiscFileSystem.h

  • Committer: Sérgio Benjamim
  • Date: 2017-01-02 00:12:05 UTC
  • Revision ID: sergio_br2@yahoo.com.br-20170102001205-cxbta9za203nmjwm
1.3.0 source (from ppsspp_1.3.0-r160.p5.l1762.a165.t83~56~ubuntu16.04.1.tar.xz).

Show diffs side-by-side

added added

removed removed

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