Update: Translations from eints
[openttd-github.git] / src / newgrf_storage.cpp
blobff309a66df078b340bdc0a86659bcd72944adcf0
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 */
8 /** @file newgrf_storage.cpp Functionality related to the temporary and persistent storage arrays for NewGRFs. */
10 #include "stdafx.h"
11 #include "newgrf_storage.h"
12 #include "core/pool_func.hpp"
13 #include "core/endian_func.hpp"
14 #include "debug.h"
16 #include "safeguards.h"
18 PersistentStoragePool _persistent_storage_pool("PersistentStorage");
19 INSTANTIATE_POOL_METHODS(PersistentStorage)
21 /** The changed storage arrays */
22 static std::set<BasePersistentStorageArray*> *_changed_storage_arrays = new std::set<BasePersistentStorageArray*>;
24 bool BasePersistentStorageArray::gameloop;
25 bool BasePersistentStorageArray::command;
26 bool BasePersistentStorageArray::testmode;
28 /**
29 * Remove references to use.
31 BasePersistentStorageArray::~BasePersistentStorageArray()
33 _changed_storage_arrays->erase(this);
36 /**
37 * Add the changed storage array to the list of changed arrays.
38 * This is done so we only have to revert/save the changed
39 * arrays, which saves quite a few clears, etc. after callbacks.
40 * @param storage the array that has changed
42 void AddChangedPersistentStorage(BasePersistentStorageArray *storage)
44 _changed_storage_arrays->insert(storage);
47 /**
48 * Clear temporary changes made since the last call to SwitchMode, and
49 * set whether subsequent changes shall be persistent or temporary.
51 * @param mode Mode switch affecting temporary/persistent changes.
52 * @param ignore_prev_mode Disable some sanity checks for exceptional call circumstances.
54 /* static */ void BasePersistentStorageArray::SwitchMode(PersistentStorageMode mode, [[maybe_unused]] bool ignore_prev_mode)
56 switch (mode) {
57 case PSM_ENTER_GAMELOOP:
58 assert(ignore_prev_mode || !gameloop);
59 assert(!command && !testmode);
60 gameloop = true;
61 break;
63 case PSM_LEAVE_GAMELOOP:
64 assert(ignore_prev_mode || gameloop);
65 assert(!command && !testmode);
66 gameloop = false;
67 break;
69 case PSM_ENTER_COMMAND:
70 assert((ignore_prev_mode || !command) && !testmode);
71 command = true;
72 break;
74 case PSM_LEAVE_COMMAND:
75 assert(ignore_prev_mode || command);
76 command = false;
77 break;
79 case PSM_ENTER_TESTMODE:
80 assert(!command && (ignore_prev_mode || !testmode));
81 testmode = true;
82 break;
84 case PSM_LEAVE_TESTMODE:
85 assert(ignore_prev_mode || testmode);
86 testmode = false;
87 break;
89 default: NOT_REACHED();
92 /* Discard all temporary changes */
93 for (auto &it : *_changed_storage_arrays) {
94 Debug(desync, 2, "warning: discarding persistent storage changes: Feature {}, GrfID {:08X}, Tile {}", it->feature, BSWAP32(it->grfid), it->tile);
95 it->ClearChanges();
97 _changed_storage_arrays->clear();