OpenTTD Source 20251213-master-g1091fa6071
newgrf_storage.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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#include "stdafx.h"
11#include "newgrf_storage.h"
12#include "core/pool_func.hpp"
13#include "debug.h"
14
15#include "safeguards.h"
16
17PersistentStoragePool _persistent_storage_pool("PersistentStorage");
19
20
22
23bool BasePersistentStorageArray::gameloop;
24bool BasePersistentStorageArray::command;
25bool BasePersistentStorageArray::testmode;
26
34
45
53/* static */ void BasePersistentStorageArray::SwitchMode(PersistentStorageMode mode, [[maybe_unused]] bool ignore_prev_mode)
54{
55 switch (mode) {
57 assert(ignore_prev_mode || !gameloop);
58 assert(!command && !testmode);
59 gameloop = true;
60 break;
61
63 assert(ignore_prev_mode || gameloop);
64 assert(!command && !testmode);
65 gameloop = false;
66 break;
67
69 assert((ignore_prev_mode || !command) && !testmode);
70 command = true;
71 break;
72
74 assert(ignore_prev_mode || command);
75 command = false;
76 break;
77
79 assert(!command && (ignore_prev_mode || !testmode));
80 testmode = true;
81 break;
82
84 assert(ignore_prev_mode || testmode);
85 testmode = false;
86 break;
87
88 default: NOT_REACHED();
89 }
90
91 /* Discard all temporary changes */
92 for (auto &it : *_changed_storage_arrays) {
93 Debug(desync, 2, "warning: discarding persistent storage changes: Feature {}, GrfID {:08X}, Tile {}", it->feature, std::byteswap(it->grfid), it->tile);
94 it->ClearChanges();
95 }
97}
Functions related to debugging.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
void AddChangedPersistentStorage(BasePersistentStorageArray *storage)
Add the changed storage array to the list of changed arrays.
static std::set< BasePersistentStorageArray * > * _changed_storage_arrays
The changed storage arrays.
Functionality related to the temporary and persistent storage arrays for NewGRFs.
PersistentStorageMode
Mode switches to the behaviour of persistent storage array.
@ PSM_ENTER_GAMELOOP
Enter the gameloop, changes will be permanent.
@ PSM_LEAVE_TESTMODE
Leave command test mode, revert to previous mode.
@ PSM_LEAVE_COMMAND
Leave command scope, revert to previous mode.
@ PSM_LEAVE_GAMELOOP
Leave the gameloop, changes will be temporary.
@ PSM_ENTER_COMMAND
Enter command scope, changes will be permanent.
@ PSM_ENTER_TESTMODE
Enter command test mode, changes will be temporary.
Some methods of Pool are placed here in order to reduce compilation time and binary size.
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don't get linker errors.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
Base class for all persistent NewGRF storage arrays.
static void SwitchMode(PersistentStorageMode mode, bool ignore_prev_mode=false)
Clear temporary changes made since the last call to SwitchMode, and set whether subsequent changes sh...
Class for pooled persistent storage of data.
Base class for all pools.