Update readme.md
[openttd-joker.git] / src / newgrf_storage.cpp
blob99e40f163970464762616b8bcd2224e7dfa46287
1 /* $Id: newgrf_storage.cpp 26371 2014-02-23 22:03:08Z frosch $ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file newgrf_storage.cpp Functionality related to the temporary and persistent storage arrays for NewGRFs. */
12 #include "stdafx.h"
13 #include "newgrf_storage.h"
14 #include "core/pool_func.hpp"
15 #include "core/endian_func.hpp"
16 #include "debug.h"
17 #include <set>
19 #include "safeguards.h"
21 PersistentStoragePool _persistent_storage_pool("PersistentStorage");
22 INSTANTIATE_POOL_METHODS(PersistentStorage)
24 /** The changed storage arrays */
25 static std::set<BasePersistentStorageArray*> *_changed_storage_arrays = new std::set<BasePersistentStorageArray*>;
27 bool BasePersistentStorageArray::gameloop;
28 bool BasePersistentStorageArray::command;
29 bool BasePersistentStorageArray::testmode;
31 /**
32 * Remove references to use.
34 BasePersistentStorageArray::~BasePersistentStorageArray()
36 _changed_storage_arrays->erase(this);
39 /**
40 * Add the changed storage array to the list of changed arrays.
41 * This is done so we only have to revert/save the changed
42 * arrays, which saves quite a few clears, etc. after callbacks.
43 * @param storage the array that has changed
45 void AddChangedPersistentStorage(BasePersistentStorageArray *storage)
47 _changed_storage_arrays->insert(storage);
50 /**
51 * Clear temporary changes made since the last call to SwitchMode, and
52 * set whether subsequent changes shall be persistent or temporary.
54 * @param mode Mode switch affecting temporary/persistent changes.
55 * @param ignore_prev_mode Disable some sanity checks for exceptional call circumstances.
57 /* static */ void BasePersistentStorageArray::SwitchMode(PersistentStorageMode mode, bool ignore_prev_mode)
59 switch (mode) {
60 case PSM_ENTER_GAMELOOP:
61 assert(ignore_prev_mode || !gameloop);
62 assert(!command && !testmode);
63 gameloop = true;
64 break;
66 case PSM_LEAVE_GAMELOOP:
67 assert(ignore_prev_mode || gameloop);
68 assert(!command && !testmode);
69 gameloop = false;
70 break;
72 case PSM_ENTER_COMMAND:
73 assert((ignore_prev_mode || !command) && !testmode);
74 command = true;
75 break;
77 case PSM_LEAVE_COMMAND:
78 assert(ignore_prev_mode || command);
79 command = false;
80 break;
82 case PSM_ENTER_TESTMODE:
83 assert(!command && (ignore_prev_mode || !testmode));
84 testmode = true;
85 break;
87 case PSM_LEAVE_TESTMODE:
88 assert(ignore_prev_mode || testmode);
89 testmode = false;
90 break;
92 default: NOT_REACHED();
95 /* Discard all temporary changes */
96 for (std::set<BasePersistentStorageArray*>::iterator it = _changed_storage_arrays->begin(); it != _changed_storage_arrays->end(); it++) {
97 DEBUG(desync, 1, "Discarding persistent storage changes: Feature %d, GrfID %08X, Tile %d", (*it)->feature, BSWAP32((*it)->grfid), (*it)->tile);
98 (*it)->ClearChanges();
100 _changed_storage_arrays->clear();