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 AGS_PLUGINS_SERIALIZER_H
24 #define AGS_PLUGINS_SERIALIZER_H
25 
26 #include "ags/shared/api/stream_api.h"
27 #include "ags/plugins/ags_plugin.h"
28 #include "common/serializer.h"
29 
30 namespace AGS3 {
31 namespace Plugins {
32 
33 class Serializer {
34 private:
35 	IAGSEngine *_engine;
36 	long _file;
37 	bool _isLoading;
38 	public:
Serializer(IAGSEngine * engine,long file,bool isLoading)39 	Serializer(IAGSEngine *engine, long file, bool isLoading) :
40 		_engine(engine), _file(file), _isLoading(isLoading) {}
41 
isLoading()42 	bool isLoading() const {
43 		return _isLoading;
44 	}
isSaving()45 	bool isSaving() const {
46 		return !_isLoading;
47 	}
48 
49 	template<typename T>
syncAsInt(T & value)50 	void syncAsInt(T &value) {
51 		byte buf[4];
52 		if (_isLoading) {
53 			_engine->FRead(buf, 4, _file);
54 			value = READ_LE_INT32(buf);
55 		} else {
56 			WRITE_LE_UINT32(buf, value);
57 			_engine->FWrite(buf, 4, _file);
58 		}
59 	}
60 
syncAsBool(bool & value)61 	void syncAsBool(bool &value) {
62 		if (_isLoading)
63 			_engine->FRead(&value, 1, _file);
64 		else
65 			_engine->FWrite(&value, 1, _file);
66 	}
67 
syncAsInt8(int8 & value)68 	void syncAsInt8(int8 &value) {
69 		if (_isLoading)
70 			_engine->FRead(&value, 1, _file);
71 		else
72 			_engine->FWrite(&value, 1, _file);
73 	}
74 
syncAsByte(byte & value)75 	void syncAsByte(byte &value) {
76 		if (_isLoading)
77 			_engine->FRead(&value, 1, _file);
78 		else
79 			_engine->FWrite(&value, 1, _file);
80 	}
81 
syncAsFloat(float & value)82 	void syncAsFloat(float &value) {
83 		if (_isLoading)
84 			_engine->FRead(&value, sizeof(float), _file);
85 		else
86 			_engine->FWrite(&value, sizeof(float), _file);
87 	}
88 
syncAsDouble(double & value)89 	void syncAsDouble(double &value) {
90 		if (_isLoading)
91 			_engine->FRead(&value, sizeof(double), _file);
92 		else
93 			_engine->FWrite(&value, sizeof(double), _file);
94 	}
95 
unreadInt()96 	void unreadInt() {
97 		_engine->FSeek(-4, AGS::Shared::kSeekCurrent, _file);
98 	}
99 };
100 
101 } // namespace Plugins
102 } // namespace AGS3
103 
104 #endif
105