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
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (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, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef __FRAMFS_SAVE_MANAGER__
24 #define __FRAMFS_SAVE_MANAGER__
25 
26 #include <common/savefile.h>
27 #include <common/zlib.h>
28 
29 #include <framfs.h> // N64 FramFS library
30 
31 bool fram_deleteSaveGame(const char *filename);
32 
33 class InFRAMSave : public Common::InSaveFile {
34 private:
35 	FRAMFILE *fd;
36 
37 	uint32 read(void *buf, uint32 cnt);
38 	bool skip(uint32 offset);
39 	bool seek(int32 offs, int whence);
40 
41 public:
InFRAMSave()42 	InFRAMSave() : fd(NULL) { }
43 
~InFRAMSave()44 	~InFRAMSave() {
45 		if (fd != NULL)
46 			framfs_close(fd);
47 	}
48 
eos()49 	bool eos() const {
50 		return framfs_eof(fd);
51 	}
clearErr()52 	void clearErr() {
53 		framfs_clearerr(fd);
54 	}
pos()55 	int32 pos() const {
56 		return framfs_tell(fd);
57 	}
size()58 	int32 size() const {
59 		return fd->size;
60 	}
61 
readSaveGame(const char * filename)62 	bool readSaveGame(const char *filename) {
63 		fd = framfs_open(filename, "r");
64 		return (fd != NULL);
65 	}
66 };
67 
68 class OutFRAMSave : public Common::WriteStream {
69 private:
70 	FRAMFILE *fd;
71 
72 public:
73 	uint32 write(const void *buf, uint32 cnt);
pos()74 	virtual int32 pos() const {
75 		return framfs_tell(fd);
76 	}
77 
OutFRAMSave(const char * _filename)78 	OutFRAMSave(const char *_filename) : fd(NULL) {
79 		fd = framfs_open(_filename, "w");
80 	}
81 
~OutFRAMSave()82 	~OutFRAMSave() {
83 		if (fd != NULL) {
84 			finalize();
85 			framfs_close(fd);
86 		}
87 	}
88 
err()89 	bool err() const {
90 		if (fd)
91 			return (framfs_error(fd) == 1);
92 		else
93 			return true;
94 	}
clearErr()95 	void clearErr() {
96 		framfs_clearerr(fd);
97 	}
finalize()98 	void finalize() {
99 		framfs_flush(fd);
100 	}
101 };
102 
103 class FRAMSaveManager : public Common::SaveFileManager {
104 public:
updateSavefilesList(Common::StringArray & lockedFiles)105 	virtual void updateSavefilesList(Common::StringArray &lockedFiles) {
106 		// this method is used to lock saves while cloud syncing
107 		// as there is no network on N64, this method wouldn't be used
108 		// thus it's not implemtented
109 	}
110 
openRawFile(const Common::String & filename)111 	virtual Common::InSaveFile *openRawFile(const Common::String &filename) {
112 		InFRAMSave *s = new InFRAMSave();
113 		if (s->readSaveGame(filename.c_str())) {
114 			return s;
115 		} else {
116 			delete s;
117 			return 0;
118 		}
119 	}
120 
121 	virtual Common::OutSaveFile *openForSaving(const Common::String &filename, bool compress = true) {
122 		OutFRAMSave *s = new OutFRAMSave(filename.c_str());
123 		if (!s->err()) {
124 			return new Common::OutSaveFile(compress ? Common::wrapCompressedWriteStream(s) : s);
125 		} else {
126 			delete s;
127 			return 0;
128 		}
129 	}
130 
openForLoading(const Common::String & filename)131 	virtual Common::InSaveFile *openForLoading(const Common::String &filename) {
132 		InFRAMSave *s = new InFRAMSave();
133 		if (s->readSaveGame(filename.c_str())) {
134 			return Common::wrapCompressedReadStream(s);
135 		} else {
136 			delete s;
137 			return 0;
138 		}
139 	}
140 
removeSavefile(const Common::String & filename)141 	virtual bool removeSavefile(const Common::String &filename) {
142 		return ::fram_deleteSaveGame(filename.c_str());
143 	}
144 
145 	virtual Common::StringArray listSavefiles(const Common::String &pattern);
146 };
147 
148 
149 #endif
150