aboutsummaryrefslogtreecommitdiff
path: root/engines/sword25/kernel/inputpersistenceblock.cpp
blob: cdce539c310b2dc8f2d2731ffc39bf3a04a3c565 (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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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; either version 2
 * of the License, or (at your option) any later version.

 * 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 for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

/*
 * This code is based on Broken Sword 2.5 engine
 *
 * Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
 *
 * Licensed under GNU GPL v2
 *
 */

#include "common/textconsole.h"

#include "sword25/kernel/inputpersistenceblock.h"

namespace Sword25 {

InputPersistenceBlock::InputPersistenceBlock(const void *data, uint dataLength, int version) :
	_data(static_cast<const byte *>(data), dataLength),
	_errorState(NONE),
	_version(version) {
	_iter = _data.begin();
}

InputPersistenceBlock::~InputPersistenceBlock() {
	if (_iter != _data.end())
		warning("Persistence block was not read to the end.");
}

void InputPersistenceBlock::read(int16 &value) {
	signed int v;
	read(v);
	value = static_cast<int16>(v);
}

void InputPersistenceBlock::read(signed int &value) {
	if (checkMarker(SINT_MARKER)) {
		value = (int32)READ_LE_UINT32(_iter);
		_iter += 4;
	} else {
		value = 0;
	}
}

void InputPersistenceBlock::read(uint &value) {
	if (checkMarker(UINT_MARKER)) {
		value = READ_LE_UINT32(_iter);
		_iter += 4;
	} else {
		value = 0;
	}
}

void InputPersistenceBlock::read(float &value) {
	if (checkMarker(FLOAT_MARKER)) {
		uint32 tmp[1];
		tmp[0] = READ_LE_UINT32(_iter);
		value = ((float *)tmp)[0];
		_iter += 4;
	} else {
		value = 0.0f;
	}
}

void InputPersistenceBlock::read(bool &value) {
	if (checkMarker(BOOL_MARKER)) {
		uint uintBool = READ_LE_UINT32(_iter);
		_iter += 4;
		value = uintBool == 0 ? false : true;
	} else {
		value = false;
	}
}

void InputPersistenceBlock::readString(Common::String &value) {
	value = "";

	if (checkMarker(STRING_MARKER)) {
		uint size;
		read(size);

		if (checkBlockSize(size)) {
			value = Common::String(reinterpret_cast<const char *>(&*_iter), size);
			_iter += size;
		}
	}
}

void InputPersistenceBlock::readByteArray(Common::Array<byte> &value) {
	if (checkMarker(BLOCK_MARKER)) {
		uint size;
		read(size);

		if (checkBlockSize(size)) {
			value = Common::Array<byte>(_iter, size);
			_iter += size;
		}
	}
}

bool InputPersistenceBlock::checkBlockSize(int size) {
	if (_data.end() - _iter >= size) {
		return true;
	} else {
		_errorState = END_OF_DATA;
		error("Unexpected end of persistence block.");
		return false;
	}
}

bool InputPersistenceBlock::checkMarker(byte marker) {
	if (!isGood() || !checkBlockSize(1))
		return false;

	if (*_iter++ == marker) {
		return true;
	} else {
		_errorState = OUT_OF_SYNC;
		error("Wrong type marker found in persistence block.");
		return false;
	}
}

} // End of namespace Sword25