ScummVM API documentation
pakfs_save_manager.h
1/* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#ifndef __PAKFS_SAVE_MANAGER__
23#define __PAKFS_SAVE_MANAGER__
24
25#include <common/savefile.h>
26#include <common/compression/deflate.h>
27
28#include <pakfs.h> // N64 PakFS library
29
30bool pakfs_deleteSaveGame(const char *filename);
31
33private:
34 PAKFILE *fd;
35
36 uint32 read(void *buf, uint32 cnt) override;
37 bool skip(uint32 offset) override;
38 bool seek(int64 offs, int whence) override;
39
40public:
41 InPAKSave() : fd(NULL) { }
42
43 ~InPAKSave() {
44 if (fd != NULL)
45 pakfs_close(fd);
46 }
47
48 bool eos() const override {
49 return pakfs_eof(fd);
50 }
51 void clearErr() override {
52 pakfs_clearerr(fd);
53 }
54 int64 pos() const override {
55 return pakfs_tell(fd);
56 }
57 int64 size() const override {
58 return fd->size;
59 }
60
61 bool readSaveGame(const char *filename) {
62 fd = pakfs_open(filename, "r");
63 return (fd != NULL);
64 }
65};
66
68private:
69 PAKFILE *fd;
70
71public:
72 uint32 write(const void *buf, uint32 cnt);
73
74 virtual int64 pos() const {
75 return pakfs_tell(fd);
76 }
77
78 OutPAKSave(const char *_filename) : fd(NULL) {
79 fd = pakfs_open(_filename, "w");
80 }
81
82 ~OutPAKSave() {
83 if (fd != NULL) {
84 finalize();
85 pakfs_close(fd);
86 flushCurrentPakData();
87 }
88 }
89
90 bool err() const {
91 if (fd)
92 return (pakfs_error(fd) == 1);
93 else
94 return true;
95 }
96 void clearErr() {
97 pakfs_clearerr(fd);
98 }
99 void finalize() {
100 pakfs_flush(fd);
101 }
102};
103
105public:
106 void updateSavefilesList(Common::StringArray &lockedFiles) override {
107 // this method is used to lock saves while cloud syncing
108 // as there is no network on N64, this method wouldn't be used
109 // thus it's not implemtented
110 }
111
112 Common::InSaveFile *openRawFile(const Common::String &filename) override {
113 InPAKSave *s = new InPAKSave();
114 if (s->readSaveGame(filename.c_str())) {
115 return s;
116 } else {
117 delete s;
118 return NULL;
119 }
120 }
121
122 Common::OutSaveFile *openForSaving(const Common::String &filename, bool compress = true) override {
123 OutPAKSave *s = new OutPAKSave(filename.c_str());
124 if (!s->err()) {
125 return new Common::OutSaveFile(compress ? Common::wrapCompressedWriteStream(s) : s);
126 } else {
127 delete s;
128 return NULL;
129 }
130 }
131
133 InPAKSave *s = new InPAKSave();
134 if (s->readSaveGame(filename.c_str())) {
136 } else {
137 delete s;
138 return NULL;
139 }
140 }
141
142 bool removeSavefile(const Common::String &filename) override {
143 return ::pakfs_deleteSaveGame(filename.c_str());
144 }
145
147
148 bool exists(const Common::String &filename) override {
149 return InPAKSave().readSaveGame(filename.c_str());
150 }
151};
152
153
154#endif
Definition: savefile.h:54
Definition: savefile.h:142
Definition: stream.h:745
Definition: str.h:59
Definition: stream.h:77
Definition: pakfs_save_manager.h:32
int64 pos() const override
Definition: pakfs_save_manager.h:54
void clearErr() override
Definition: pakfs_save_manager.h:51
int64 size() const override
Definition: pakfs_save_manager.h:57
bool eos() const override
Definition: pakfs_save_manager.h:48
Definition: pakfs_save_manager.h:67
void clearErr()
Definition: pakfs_save_manager.h:96
bool err() const
Definition: pakfs_save_manager.h:90
void finalize()
Definition: pakfs_save_manager.h:99
virtual int64 pos() const
Definition: pakfs_save_manager.h:74
uint32 write(const void *buf, uint32 cnt)
Definition: pakfs_save_manager.h:104
bool removeSavefile(const Common::String &filename) override
Definition: pakfs_save_manager.h:142
Common::StringArray listSavefiles(const Common::String &pattern) override
void updateSavefilesList(Common::StringArray &lockedFiles) override
Definition: pakfs_save_manager.h:106
Common::InSaveFile * openForLoading(const Common::String &filename) override
Definition: pakfs_save_manager.h:132
Common::InSaveFile * openRawFile(const Common::String &filename) override
Definition: pakfs_save_manager.h:112
bool exists(const Common::String &filename) override
Definition: pakfs_save_manager.h:148
Common::OutSaveFile * openForSaving(const Common::String &filename, bool compress=true) override
Definition: pakfs_save_manager.h:122
SeekableReadStream * wrapCompressedReadStream(SeekableReadStream *toBeWrapped, DisposeAfterUse::Flag disposeParent=DisposeAfterUse::YES, uint64 knownSize=0)
WriteStream * wrapCompressedWriteStream(WriteStream *toBeWrapped)