Translations update
[openttd/fttd.git] / src / newgrf_storage.cpp
blobfba8f2b64f78d4288856c8cf49be67d37405aed9
1 /* $Id$ */
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 template<> PersistentStorage::Pool PersistentStorage::PoolItem::pool ("PersistentStorage");
20 INSTANTIATE_POOL_METHODS(PersistentStorage)
22 /** The changed storage arrays */
23 static std::set<BasePersistentStorageArray*> *_changed_storage_arrays = new std::set<BasePersistentStorageArray*>;
25 bool BasePersistentStorageArray::gameloop;
26 bool BasePersistentStorageArray::command;
27 bool BasePersistentStorageArray::testmode;
29 /**
30 * Remove references to use.
32 BasePersistentStorageArray::~BasePersistentStorageArray()
34 _changed_storage_arrays->erase(this);
37 /**
38 * Add the changed storage array to the list of changed arrays.
39 * This is done so we only have to revert/save the changed
40 * arrays, which saves quite a few clears, etc. after callbacks.
41 * @param storage the array that has changed
43 void AddChangedPersistentStorage(BasePersistentStorageArray *storage)
45 _changed_storage_arrays->insert(storage);
48 /**
49 * Clear temporary changes made since the last call to SwitchMode, and
50 * set whether subsequent changes shall be persistent or temporary.
52 * @param mode Mode switch affecting temporary/persistent changes.
53 * @param ignore_prev_mode Disable some sanity checks for exceptional call circumstances.
55 /* static */ void BasePersistentStorageArray::SwitchMode(PersistentStorageMode mode, bool ignore_prev_mode)
57 switch (mode) {
58 case PSM_ENTER_GAMELOOP:
59 assert(ignore_prev_mode || !gameloop);
60 assert(!command && !testmode);
61 gameloop = true;
62 break;
64 case PSM_LEAVE_GAMELOOP:
65 assert(ignore_prev_mode || gameloop);
66 assert(!command && !testmode);
67 gameloop = false;
68 break;
70 case PSM_ENTER_COMMAND:
71 assert((ignore_prev_mode || !command) && !testmode);
72 command = true;
73 break;
75 case PSM_LEAVE_COMMAND:
76 assert(ignore_prev_mode || command);
77 command = false;
78 break;
80 case PSM_ENTER_TESTMODE:
81 assert(!command && (ignore_prev_mode || !testmode));
82 testmode = true;
83 break;
85 case PSM_LEAVE_TESTMODE:
86 assert(ignore_prev_mode || testmode);
87 testmode = false;
88 break;
90 default: NOT_REACHED();
93 /* Discard all temporary changes */
94 for (std::set<BasePersistentStorageArray*>::iterator it = _changed_storage_arrays->begin(); it != _changed_storage_arrays->end(); it++) {
95 DEBUG(desync, 1, "Discarding persistent storage changes: Feature %d, GrfID %08X, Tile %d", (*it)->feature, BSWAP32((*it)->grfid), (*it)->tile);
96 (*it)->ClearChanges();
98 _changed_storage_arrays->clear();