summaryrefslogtreecommitdiff
path: root/Source/Core/Common/Src/ChunkFile.h
blob: 92797234d4858cc93dd8ed694c853ff914597c55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Copyright (C) 2003 Dolphin 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.

// 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 SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/

#ifndef _POINTERWRAP_H_
#define _POINTERWRAP_H_

// Extremely simple serialization framework.

// (mis)-features:
// + Super fast
// + Very simple
// + Same code is used for serialization and deserializaition (in most cases)
// - Zero backwards/forwards compatibility
// - Serialization code for anything complex has to be manually written.

#include <stdio.h>

#include <map>
#include <vector>
#include <string>

#include "Common.h"
#include "FileUtil.h"

template <class T>
struct LinkedListItem : public T
{
	LinkedListItem<T> *next;
};

// Wrapper class
class PointerWrap
{
public:
	enum Mode {
		MODE_READ = 1, // load
		MODE_WRITE, // save
		MODE_MEASURE, // calculate size
		MODE_VERIFY, // compare
	};

	u8 **ptr;
	Mode mode;

public:
	PointerWrap(u8 **ptr_, Mode mode_) : ptr(ptr_), mode(mode_) {}
	PointerWrap(unsigned char **ptr_, int mode_) : ptr((u8**)ptr_), mode((Mode)mode_) {}

	void SetMode(Mode mode_) {mode = mode_;}
	Mode GetMode() const {return mode;}
	u8 **GetPPtr() {return ptr;}

	void DoVoid(void *data, int size)
	{
		switch (mode) {
		case MODE_READ:	memcpy(data, *ptr, size); break;
		case MODE_WRITE: memcpy(*ptr, data, size); break;
		case MODE_MEASURE: break;  // MODE_MEASURE - don't need to do anything
		case MODE_VERIFY: for(int i = 0; i < size; i++) _dbg_assert_msg_(COMMON, ((u8*)data)[i] == (*ptr)[i], "Savestate verification failure: %d (0x%X) (at 0x%X) != %d (0x%X) (at 0x%X).\n", ((u8*)data)[i], ((u8*)data)[i], &((u8*)data)[i], (*ptr)[i], (*ptr)[i], &(*ptr)[i]); break;
		default: break;  // throw an error?
		}
		(*ptr) += size;
	}

	template<class T>
	void Do(std::map<unsigned int, T> &x)
	{
		// TODO
		PanicAlert("Do(map<>) does not yet work.");
	}

	void Do(std::map<unsigned int, std::string> &x)
	{
		unsigned int number = (unsigned int)x.size();
		Do(number);
		switch (mode) {
		case MODE_READ:
			{
				x.clear();
				while (number > 0)
				{
					unsigned int first = 0;
					Do(first);
					std::string second;
					Do(second);
					x[first] = second;
					--number;
				}
			}
			break;
		case MODE_WRITE:
		case MODE_MEASURE:
		case MODE_VERIFY:
			{
				std::map<unsigned int, std::string>::iterator itr = x.begin();
				while (number > 0)
				{
					Do(itr->first);
					Do(itr->second);
					--number;
					++itr;
				}
			}
			break;
		}
	}

	// Store vectors.
	template<class T>
	void Do(std::vector<T> &x) {
		// TODO
		PanicAlert("Do(vector<>) does not yet work.");
	}
	
	// Store strings.
	void Do(std::string &x) 
	{
		int stringLen = (int)x.length() + 1;
		Do(stringLen);
		
		switch (mode) {
		case MODE_READ:		x = (char*)*ptr; break;
		case MODE_WRITE:	memcpy(*ptr, x.c_str(), stringLen); break;
		case MODE_MEASURE: break;
		case MODE_VERIFY: _dbg_assert_msg_(COMMON, !strcmp(x.c_str(), (char*)*ptr), "Savestate verification failure: \"%s\" != \"%s\" (at 0x%X).\n", x.c_str(), (char*)*ptr, ptr); break;
		}
		(*ptr) += stringLen;
	}
 
	void DoBuffer(u8** pBuffer, u32& _Size) 
	{
		Do(_Size);	
		
		if (_Size > 0) {
			switch (mode) {
			case MODE_READ:		delete[] *pBuffer; *pBuffer = new u8[_Size]; memcpy(*pBuffer, *ptr, _Size); break;
			case MODE_WRITE:	memcpy(*ptr, *pBuffer, _Size); break;
			case MODE_MEASURE:	break;
			case MODE_VERIFY: if(*pBuffer) for(u32 i = 0; i < _Size; i++) _dbg_assert_msg_(COMMON, (*pBuffer)[i] == (*ptr)[i], "Savestate verification failure: %d (0x%X) (at 0x%X) != %d (0x%X) (at 0x%X).\n", (*pBuffer)[i], (*pBuffer)[i], &(*pBuffer)[i], (*ptr)[i], (*ptr)[i], &(*ptr)[i]); break;
			}
		} else 	{
			*pBuffer = NULL;
		}
		(*ptr) += _Size;
	}
	
    template<class T>
	void DoArray(T *x, int count) {
        DoVoid((void *)x, sizeof(T) * count);
    }
	
	template<class T>
	void Do(T &x) {
		DoVoid((void *)&x, sizeof(x));
	}
	
	template<class T>
	void DoLinkedList(LinkedListItem<T> **list_start) {
		// TODO
		PanicAlert("Do(linked list<>) does not yet work.");
	}
};


class CChunkFileReader
{
public:
	// Load file template
	template<class T>
	static bool Load(const std::string& _rFilename, int _Revision, T& _class) 
	{
		
		INFO_LOG(COMMON, "ChunkReader: Loading %s" , _rFilename.c_str());

		if (! File::Exists(_rFilename.c_str()))
			return false;
				
		// Check file size
		u64 fileSize = File::GetSize(_rFilename.c_str());
		u64 headerSize = sizeof(SChunkHeader);
		if (fileSize < headerSize) {
			ERROR_LOG(COMMON,"ChunkReader: File too small");
			return false;
		}

		FILE* pFile = fopen(_rFilename.c_str(), "rb");
		if (!pFile) { 
			ERROR_LOG(COMMON,"ChunkReader: Can't open file for reading");
			return false;
		}

		// read the header
		SChunkHeader header;
		if (fread(&header, 1, headerSize, pFile) != headerSize) {
			fclose(pFile);
			ERROR_LOG(COMMON,"ChunkReader: Bad header size");
			return false;
		}
		
		// Check revision
		if (header.Revision != _Revision) {
			fclose(pFile);
			ERROR_LOG(COMMON,"ChunkReader: Wrong file revision, got %d expected %d",
					  header.Revision, _Revision);
			return false;
		}
		
		// get size
		int sz = (int)(fileSize - headerSize);
		if (header.ExpectedSize != sz) {
			fclose(pFile);
			ERROR_LOG(COMMON,"ChunkReader: Bad file size, got %d expected %d",
					  sz, header.ExpectedSize);
			return false;
		}
		
		// read the state
		u8* buffer = new u8[sz];
		if ((int)fread(buffer, 1, sz, pFile) != sz) {
			fclose(pFile);
			ERROR_LOG(COMMON,"ChunkReader: Error reading file");
			return false;
		}

		// done reading
		if (fclose(pFile)) {
			ERROR_LOG(COMMON,"ChunkReader: Error closing file! might be corrupted!");
			// should never happen!
			return false;
		}

		u8 *ptr = buffer;
		PointerWrap p(&ptr, PointerWrap::MODE_READ);
		_class.DoState(p);
		delete [] buffer;
		
		INFO_LOG(COMMON, "ChunkReader: Done loading %s" , _rFilename.c_str());
		return true;
	}
	
	// Save file template
	template<class T>
	static bool Save(const std::string& _rFilename, int _Revision, T& _class)
	{
		FILE* pFile;

		INFO_LOG(COMMON, "ChunkReader: Writing %s" , _rFilename.c_str());
		pFile = fopen(_rFilename.c_str(), "wb");
		if (!pFile) {
			ERROR_LOG(COMMON,"ChunkReader: Error opening file for write");
			return false;
		}

		// Get data
		u8 *ptr = 0;
		PointerWrap p(&ptr, PointerWrap::MODE_MEASURE);
		_class.DoState(p);
		size_t sz = (size_t)ptr;
		u8 *buffer = new u8[sz];
		ptr = buffer;
		p.SetMode(PointerWrap::MODE_WRITE);
		_class.DoState(p);
		
		// Create header
		SChunkHeader header;
		header.Compress = 0;
		header.Revision = _Revision;
		header.ExpectedSize = (int)sz;
		
		// Write to file
		if (fwrite(&header, sizeof(SChunkHeader), 1, pFile) != 1) {
			ERROR_LOG(COMMON,"ChunkReader: Failed writing header");
			return false;
		}

		if (fwrite(buffer, sz, 1, pFile) != 1) {
			ERROR_LOG(COMMON,"ChunkReader: Failed writing data");
			return false;
		}

		if (fclose(pFile)) {
			ERROR_LOG(COMMON,"ChunkReader: Error closing file! might be corrupted!");
			return false;
		}
		
		INFO_LOG(COMMON,"ChunkReader: Done writing %s", 
				 _rFilename.c_str());
		return true;
	}
	
private:
	struct SChunkHeader
	{
		int Revision;
		int Compress;
		int ExpectedSize;
	};
};

#endif  // _POINTERWRAP_H_