Merge tag '14.0-beta2' into jgrpp

# Conflicts:
#	CMakeLists.txt
#	src/cargotype.cpp
#	src/console_cmds.cpp
#	src/graph_gui.cpp
#	src/industry_cmd.cpp
#	src/industrytype.h
#	src/misc_gui.cpp
#	src/network/network_client.cpp
#	src/newgrf.cpp
#	src/newgrf_town.cpp
#	src/object_cmd.cpp
#	src/openttd.cpp
#	src/pathfinder/water_regions.cpp
#	src/saveload/saveload.cpp
#	src/saveload/saveload.h
#	src/table/build_industry.h
#	src/table/engines.h
#	src/vehicle.cpp
#	src/vehicle_cmd.cpp
#	src/vehicle_gui.cpp
This commit is contained in:
Jonathan G Rennison
2024-02-19 03:17:17 +00:00
53 changed files with 618 additions and 416 deletions

View File

@@ -178,16 +178,16 @@ void MemoryDumper::AllocateBuffer()
* Flush this dumper into a writer.
* @param writer The filter we want to use.
*/
void MemoryDumper::Flush(SaveFilter *writer)
void MemoryDumper::Flush(SaveFilter &writer)
{
this->FinaliseBlock();
size_t block_count = this->blocks.size();
for (size_t i = 0; i < block_count; i++) {
writer->Write(this->blocks[i].data, this->blocks[i].size);
writer.Write(this->blocks[i].data, this->blocks[i].size);
}
writer->Finish();
writer.Finish();
}
void MemoryDumper::StartAutoLength()
@@ -234,11 +234,11 @@ struct SaveLoadParams {
uint32_t current_chunk_id; ///< Current chunk ID
MemoryDumper *dumper; ///< Memory dumper to write the savegame to.
SaveFilter *sf; ///< Filter to write the savegame to.
std::unique_ptr<MemoryDumper> dumper;///< Memory dumper to write the savegame to.
std::shared_ptr<SaveFilter> sf; ///< Filter to write the savegame to.
ReadBuffer *reader; ///< Savegame reading buffer.
LoadFilter *lf; ///< Filter to read the savegame from.
std::unique_ptr<ReadBuffer> reader; ///< Savegame reading buffer.
std::shared_ptr<LoadFilter> lf; ///< Filter to read the savegame from.
StringID error_str; ///< the translatable error message to show
std::string extra_msg; ///< the error message
@@ -251,12 +251,12 @@ static SaveLoadParams _sl; ///< Parameters used for/at saveload.
ReadBuffer *ReadBuffer::GetCurrent()
{
return _sl.reader;
return _sl.reader.get();
}
MemoryDumper *MemoryDumper::GetCurrent()
{
return _sl.dumper;
return _sl.dumper.get();
}
static const std::vector<ChunkHandler> &ChunkHandlers()
@@ -2743,9 +2743,6 @@ struct FileReader : LoadFilter {
{
if (this->file != nullptr) fclose(this->file);
this->file = nullptr;
/* Make sure we don't double free. */
_sl.sf = nullptr;
}
size_t Read(byte *buf, size_t size) override
@@ -2781,9 +2778,6 @@ struct FileWriter : SaveFilter {
~FileWriter()
{
this->Finish();
/* Make sure we don't double free. */
_sl.sf = nullptr;
}
void Write(byte *buf, size_t size) override
@@ -2817,7 +2811,7 @@ struct LZOLoadFilter : LoadFilter {
* Initialise this filter.
* @param chain The next filter in this chain.
*/
LZOLoadFilter(LoadFilter *chain) : LoadFilter(chain)
LZOLoadFilter(std::shared_ptr<LoadFilter> chain) : LoadFilter(std::move(chain))
{
if (lzo_init() != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
}
@@ -2865,7 +2859,7 @@ struct LZOSaveFilter : SaveFilter {
* @param chain The next filter in this chain.
* @param compression_level The requested level of compression.
*/
LZOSaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain)
LZOSaveFilter(std::shared_ptr<SaveFilter> chain, byte compression_level) : SaveFilter(std::move(chain))
{
if (lzo_init() != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
}
@@ -2905,7 +2899,7 @@ struct NoCompLoadFilter : LoadFilter {
* Initialise this filter.
* @param chain The next filter in this chain.
*/
NoCompLoadFilter(LoadFilter *chain) : LoadFilter(chain)
NoCompLoadFilter(std::shared_ptr<LoadFilter> chain) : LoadFilter(std::move(chain))
{
}
@@ -2922,7 +2916,7 @@ struct NoCompSaveFilter : SaveFilter {
* @param chain The next filter in this chain.
* @param compression_level The requested level of compression.
*/
NoCompSaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain)
NoCompSaveFilter(std::shared_ptr<SaveFilter> chain, byte compression_level) : SaveFilter(std::move(chain))
{
}
@@ -2948,7 +2942,7 @@ struct ZlibLoadFilter : LoadFilter {
* Initialise this filter.
* @param chain The next filter in this chain.
*/
ZlibLoadFilter(LoadFilter *chain) : LoadFilter(chain)
ZlibLoadFilter(std::shared_ptr<LoadFilter> chain) : LoadFilter(std::move(chain))
{
memset(&this->z, 0, sizeof(this->z));
if (inflateInit(&this->z) != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
@@ -2993,7 +2987,7 @@ struct ZlibSaveFilter : SaveFilter {
* @param chain The next filter in this chain.
* @param compression_level The requested level of compression.
*/
ZlibSaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain)
ZlibSaveFilter(std::shared_ptr<SaveFilter> chain, byte compression_level) : SaveFilter(std::move(chain))
{
memset(&this->z, 0, sizeof(this->z));
if (deflateInit(&this->z, compression_level) != Z_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
@@ -3077,7 +3071,7 @@ struct LZMALoadFilter : LoadFilter {
* Initialise this filter.
* @param chain The next filter in this chain.
*/
LZMALoadFilter(LoadFilter *chain) : LoadFilter(chain), lzma(_lzma_init)
LZMALoadFilter(std::shared_ptr<LoadFilter> chain) : LoadFilter(std::move(chain)), lzma(_lzma_init)
{
/* Allow saves up to 256 MB uncompressed */
if (lzma_auto_decoder(&this->lzma, 1 << 28, 0) != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
@@ -3121,7 +3115,7 @@ struct LZMASaveFilter : SaveFilter {
* @param chain The next filter in this chain.
* @param compression_level The requested level of compression.
*/
LZMASaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain), lzma(_lzma_init)
LZMASaveFilter(std::shared_ptr<SaveFilter> chain, byte compression_level) : SaveFilter(std::move(chain)), lzma(_lzma_init)
{
if (lzma_easy_encoder(&this->lzma, compression_level, LZMA_CHECK_CRC32) != LZMA_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
}
@@ -3190,7 +3184,7 @@ struct ZSTDLoadFilter : LoadFilter {
* Initialise this filter.
* @param chain The next filter in this chain.
*/
ZSTDLoadFilter(LoadFilter *chain) : LoadFilter(chain)
ZSTDLoadFilter(std::shared_ptr<LoadFilter> chain) : LoadFilter(std::move(chain))
{
this->zstd = ZSTD_createDCtx();
if (!this->zstd) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
@@ -3234,7 +3228,7 @@ struct ZSTDSaveFilter : SaveFilter {
* @param chain The next filter in this chain.
* @param compression_level The requested level of compression.
*/
ZSTDSaveFilter(SaveFilter *chain, byte compression_level) : SaveFilter(chain)
ZSTDSaveFilter(std::shared_ptr<SaveFilter> chain, byte compression_level) : SaveFilter(std::move(chain))
{
this->zstd = ZSTD_createCCtx();
if (!this->zstd) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
@@ -3302,8 +3296,8 @@ struct SaveLoadFormat {
const char *name; ///< name of the compressor/decompressor (debug-only)
uint32_t tag; ///< the 4-letter tag by which it is identified in the savegame
LoadFilter *(*init_load)(LoadFilter *chain); ///< Constructor for the load filter.
SaveFilter *(*init_write)(SaveFilter *chain, byte compression); ///< Constructor for the save filter.
std::shared_ptr<LoadFilter> (*init_load)(std::shared_ptr<LoadFilter> chain); ///< Constructor for the load filter.
std::shared_ptr<SaveFilter> (*init_write)(std::shared_ptr<SaveFilter> chain, byte compression); ///< Constructor for the save filter.
byte min_compression; ///< the minimum compression level of this format
byte default_compression; ///< the default compression level of this format
@@ -3420,20 +3414,11 @@ static void ResetSaveloadData()
*/
static inline void ClearSaveLoadState()
{
delete _sl.dumper;
_sl.dumper = nullptr;
delete _sl.sf;
_sl.sf = nullptr;
delete _sl.reader;
_sl.reader = nullptr;
delete _sl.lf;
_sl.lf = nullptr;
_sl.save_flags = SMF_NONE;
_sl.current_chunk_id = 0;
GamelogStopAnyAction();
@@ -3500,7 +3485,7 @@ static SaveOrLoadResult SaveFileToDisk(bool threaded)
_sl.sf->Write((byte*)hdr, sizeof(hdr));
_sl.sf = fmt->init_write(_sl.sf, compression);
_sl.dumper->Flush(_sl.sf);
_sl.dumper->Flush(*(_sl.sf));
ClearSaveLoadState();
@@ -3542,12 +3527,12 @@ void WaitTillSaved()
* @param threaded Whether to try to perform the saving asynchronously.
* @return Return the result of the action. #SL_OK or #SL_ERROR
*/
static SaveOrLoadResult DoSave(SaveFilter *writer, bool threaded)
static SaveOrLoadResult DoSave(std::shared_ptr<SaveFilter> writer, bool threaded)
{
assert(!_sl.saveinprogress);
_sl.dumper = new MemoryDumper();
_sl.sf = writer;
_sl.dumper = std::make_unique<MemoryDumper>();
_sl.sf = std::move(writer);
_sl_version = SAVEGAME_VERSION;
SlXvSetCurrentState();
@@ -3576,12 +3561,12 @@ static SaveOrLoadResult DoSave(SaveFilter *writer, bool threaded)
* @param flags Save mode flags.
* @return Return the result of the action. #SL_OK or #SL_ERROR
*/
SaveOrLoadResult SaveWithFilter(SaveFilter *writer, bool threaded, SaveModeFlags flags)
SaveOrLoadResult SaveWithFilter(std::shared_ptr<SaveFilter> writer, bool threaded, SaveModeFlags flags)
{
try {
_sl.action = SLA_SAVE;
_sl.save_flags = flags;
return DoSave(writer, threaded);
return DoSave(std::move(writer), threaded);
} catch (...) {
ClearSaveLoadState();
return SL_ERROR;
@@ -3620,7 +3605,7 @@ struct ThreadedLoadFilter : LoadFilter {
* Initialise this filter.
* @param chain The next filter in this chain.
*/
ThreadedLoadFilter(LoadFilter *chain) : LoadFilter(chain)
ThreadedLoadFilter(std::shared_ptr<LoadFilter> chain) : LoadFilter(std::move(chain))
{
std::unique_lock<std::mutex> lk(this->mutex);
if (!StartNewThread(&this->read_thread, "ottd:loadgame", &ThreadedLoadFilter::RunThread, this)) {
@@ -3710,9 +3695,9 @@ struct ThreadedLoadFilter : LoadFilter {
* @param load_check Whether to perform the checking ("preview") or actually load the game.
* @return Return the result of the action. #SL_OK or #SL_REINIT ("unload" the game)
*/
static SaveOrLoadResult DoLoad(LoadFilter *reader, bool load_check)
static SaveOrLoadResult DoLoad(std::shared_ptr<LoadFilter> reader, bool load_check)
{
_sl.lf = reader;
_sl.lf = std::move(reader);
if (load_check) {
/* Clear previous check data */
@@ -3804,11 +3789,11 @@ static SaveOrLoadResult DoLoad(LoadFilter *reader, bool load_check)
SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, err_str);
}
_sl.lf = fmt->init_load(_sl.lf);
_sl.lf = fmt->init_load(std::move(_sl.lf));
if (!(fmt->flags & SLF_NO_THREADED_LOAD)) {
_sl.lf = new ThreadedLoadFilter(_sl.lf);
_sl.lf = std::make_shared<ThreadedLoadFilter>(std::move(_sl.lf));
}
_sl.reader = new ReadBuffer(_sl.lf);
_sl.reader = std::make_unique<ReadBuffer>(_sl.lf);
_next_offs = 0;
upstream_sl::SlResetLoadState();
@@ -3948,11 +3933,11 @@ static SaveOrLoadResult DoLoad(LoadFilter *reader, bool load_check)
* @param reader The filter to read the savegame from.
* @return Return the result of the action. #SL_OK or #SL_REINIT ("unload" the game)
*/
SaveOrLoadResult LoadWithFilter(LoadFilter *reader)
SaveOrLoadResult LoadWithFilter(std::shared_ptr<LoadFilter> reader)
{
try {
_sl.action = SLA_LOAD;
return DoLoad(reader, false);
return DoLoad(std::move(reader), false);
} catch (...) {
ClearSaveLoadState();
@@ -4045,13 +4030,13 @@ SaveOrLoadResult SaveOrLoad(const std::string &filename, SaveLoadOperation fop,
DEBUG(desync, 1, "save: %s; %s", debug_date_dumper().HexDate(), filename.c_str());
if (!_settings_client.gui.threaded_saves) threaded = false;
return DoSave(new FileWriter(fh), threaded);
return DoSave(std::make_shared<FileWriter>(fh), threaded);
}
/* LOAD game */
assert(fop == SLO_LOAD || fop == SLO_CHECK);
DEBUG(desync, 1, "load: %s", filename.c_str());
return DoLoad(new FileReader(fh), fop == SLO_CHECK);
return DoLoad(std::make_shared<FileReader>(fh), fop == SLO_CHECK);
} catch (...) {
/* This code may be executed both for old and new save games. */
ClearSaveLoadState();