OpenTTD Source  1.11.0-beta2
cheat_sl.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD 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.
4  * OpenTTD 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.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
10 #include "../stdafx.h"
11 #include "../cheat_type.h"
12 
13 #include "saveload.h"
14 
15 #include "../safeguards.h"
16 
20 static void Save_CHTS()
21 {
22  /* Cannot use lengthof because _cheats is of type Cheats, not Cheat */
23  byte count = sizeof(_cheats) / sizeof(Cheat);
24  Cheat *cht = (Cheat*) &_cheats;
25  Cheat *cht_last = &cht[count];
26 
27  SlSetLength(count * 2);
28  for (; cht != cht_last; cht++) {
29  SlWriteByte(cht->been_used);
30  SlWriteByte(cht->value);
31  }
32 }
33 
37 static void Load_CHTS()
38 {
39  Cheat *cht = (Cheat*)&_cheats;
40  size_t count = SlGetFieldLength() / 2;
41  /* Cannot use lengthof because _cheats is of type Cheats, not Cheat */
42  if (count > sizeof(_cheats) / sizeof(Cheat)) SlErrorCorrupt("Too many cheat values");
43 
44  for (uint i = 0; i < count; i++) {
45  cht[i].been_used = (SlReadByte() != 0);
46  cht[i].value = (SlReadByte() != 0);
47  }
48 }
49 
51 extern const ChunkHandler _cheat_chunk_handlers[] = {
52  { 'CHTS', Save_CHTS, Load_CHTS, nullptr, nullptr, CH_RIFF | CH_LAST},
53 };
Load_CHTS
static void Load_CHTS()
Load the cheat values.
Definition: cheat_sl.cpp:37
CH_LAST
@ CH_LAST
Last chunk in this array.
Definition: saveload.h:410
saveload.h
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:379
SlSetLength
void SlSetLength(size_t length)
Sets the length of either a RIFF object or the number of items in an array.
Definition: saveload.cpp:676
SlWriteByte
void SlWriteByte(byte b)
Wrapper for writing a byte to the dumper.
Definition: saveload.cpp:427
Cheat
Info about each of the cheats.
Definition: cheat_type.h:16
_cheat_chunk_handlers
const ChunkHandler _cheat_chunk_handlers[]
Chunk handlers related to cheats.
_cheats
Cheats _cheats
All the cheats.
Definition: cheat.cpp:16
Cheat::value
bool value
tells if the bool cheat is active or not
Definition: cheat_type.h:18
SlGetFieldLength
size_t SlGetFieldLength()
Get the length of the current object.
Definition: saveload.cpp:737
Save_CHTS
static void Save_CHTS()
Save the cheat values.
Definition: cheat_sl.cpp:20
SlErrorCorrupt
void NORETURN SlErrorCorrupt(const char *msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:358
SlReadByte
byte SlReadByte()
Wrapper for reading a byte from the buffer.
Definition: saveload.cpp:418
Cheat::been_used
bool been_used
has this cheat been used before?
Definition: cheat_type.h:17