Merge 14.0-beta3
This commit is contained in:
@@ -89,14 +89,14 @@ struct ReadBuffer {
|
||||
byte buf[MEMORY_CHUNK_SIZE]; ///< Buffer we're going to read from.
|
||||
byte *bufp; ///< Location we're at reading the buffer.
|
||||
byte *bufe; ///< End of the buffer we can read from.
|
||||
LoadFilter *reader; ///< The filter used to actually read.
|
||||
std::shared_ptr<LoadFilter> reader; ///< The filter used to actually read.
|
||||
size_t read; ///< The amount of read bytes so far from the filter.
|
||||
|
||||
/**
|
||||
* Initialise our variables.
|
||||
* @param reader The filter to actually read data.
|
||||
*/
|
||||
ReadBuffer(LoadFilter *reader) : bufp(nullptr), bufe(nullptr), reader(reader), read(0)
|
||||
ReadBuffer(std::shared_ptr<LoadFilter> reader) : bufp(nullptr), bufe(nullptr), reader(reader), read(0)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -163,7 +163,7 @@ struct MemoryDumper {
|
||||
* Flush this dumper into a writer.
|
||||
* @param writer The filter we want to use.
|
||||
*/
|
||||
void Flush(SaveFilter *writer)
|
||||
void Flush(std::shared_ptr<SaveFilter> writer)
|
||||
{
|
||||
uint i = 0;
|
||||
size_t t = this->GetSize();
|
||||
@@ -199,11 +199,11 @@ struct SaveLoadParams {
|
||||
int array_index, last_array_index; ///< in the case of an array, the current and last positions
|
||||
bool expect_table_header; ///< In the case of a table, if the header is saved/loaded.
|
||||
|
||||
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
|
||||
@@ -2176,9 +2176,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
|
||||
@@ -2214,9 +2211,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
|
||||
@@ -2250,7 +2244,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(chain)
|
||||
{
|
||||
if (lzo_init() != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize decompressor");
|
||||
}
|
||||
@@ -2297,7 +2291,7 @@ struct LZOSaveFilter : SaveFilter {
|
||||
* Initialise this filter.
|
||||
* @param chain The next filter in this chain.
|
||||
*/
|
||||
LZOSaveFilter(SaveFilter *chain, byte) : SaveFilter(chain)
|
||||
LZOSaveFilter(std::shared_ptr<SaveFilter> chain, byte) : SaveFilter(chain)
|
||||
{
|
||||
if (lzo_init() != LZO_E_OK) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
|
||||
}
|
||||
@@ -2337,7 +2331,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(chain)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -2353,7 +2347,7 @@ struct NoCompSaveFilter : SaveFilter {
|
||||
* Initialise this filter.
|
||||
* @param chain The next filter in this chain.
|
||||
*/
|
||||
NoCompSaveFilter(SaveFilter *chain, byte) : SaveFilter(chain)
|
||||
NoCompSaveFilter(std::shared_ptr<SaveFilter> chain, byte) : SaveFilter(chain)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -2379,7 +2373,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(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");
|
||||
@@ -2424,7 +2418,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(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");
|
||||
@@ -2508,7 +2502,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(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");
|
||||
@@ -2552,7 +2546,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(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");
|
||||
}
|
||||
@@ -2621,7 +2615,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(chain)
|
||||
{
|
||||
this->zstd = ZSTD_createDCtx();
|
||||
if (!this->zstd) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
|
||||
@@ -2663,7 +2657,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(chain)
|
||||
{
|
||||
this->zstd = ZSTD_createCCtx();
|
||||
if (!this->zstd) SlError(STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR, "cannot initialize compressor");
|
||||
@@ -2720,12 +2714,13 @@ struct ZSTDSaveFilter : SaveFilter {
|
||||
*******************************************/
|
||||
|
||||
/** The format for a reader/writer type of a savegame */
|
||||
// CityMania moved to saveload.h
|
||||
// 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
|
||||
@@ -2943,16 +2938,9 @@ 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;
|
||||
}
|
||||
|
||||
@@ -3065,11 +3053,11 @@ 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, citymania::SavePreset preset)
|
||||
static SaveOrLoadResult DoSave(std::shared_ptr<SaveFilter> writer, bool threaded, citymania::SavePreset preset)
|
||||
{
|
||||
assert(!_sl.saveinprogress);
|
||||
|
||||
_sl.dumper = new MemoryDumper();
|
||||
_sl.dumper = std::make_unique<MemoryDumper>();
|
||||
_sl.sf = writer;
|
||||
|
||||
_sl_version = SAVEGAME_VERSION;
|
||||
@@ -3097,7 +3085,7 @@ static SaveOrLoadResult DoSave(SaveFilter *writer, bool threaded, citymania::Sav
|
||||
* @param threaded Whether to try to perform the saving asynchronously.
|
||||
* @return Return the result of the action. #SL_OK or #SL_ERROR
|
||||
*/
|
||||
SaveOrLoadResult SaveWithFilter(SaveFilter *writer, bool threaded, citymania::SavePreset preset)
|
||||
SaveOrLoadResult SaveWithFilter(std::shared_ptr<SaveFilter> writer, bool threaded, citymania::SavePreset preset)
|
||||
{
|
||||
try {
|
||||
_sl.action = SLA_SAVE;
|
||||
@@ -3114,7 +3102,7 @@ SaveOrLoadResult SaveWithFilter(SaveFilter *writer, bool threaded, citymania::Sa
|
||||
* @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;
|
||||
|
||||
@@ -3178,7 +3166,7 @@ static SaveOrLoadResult DoLoad(LoadFilter *reader, bool load_check)
|
||||
Debug(sl, 2, "Using saveload format '%s'", fmt->name);
|
||||
|
||||
_sl.lf = fmt->init_load(_sl.lf);
|
||||
_sl.reader = new ReadBuffer(_sl.lf);
|
||||
_sl.reader = std::make_unique<ReadBuffer>(_sl.lf);
|
||||
_next_offs = 0;
|
||||
|
||||
if (!load_check) {
|
||||
@@ -3255,7 +3243,7 @@ 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;
|
||||
@@ -3342,13 +3330,13 @@ SaveOrLoadResult SaveOrLoad(const std::string &filename, SaveLoadOperation fop,
|
||||
Debug(desync, 1, "save: {:08x}; {:02x}; {}", TimerGameEconomy::date, TimerGameEconomy::date_fract, filename);
|
||||
if (!_settings_client.gui.threaded_saves) threaded = false;
|
||||
|
||||
return DoSave(new FileWriter(fh), threaded, citymania::GetLocalSavePreset());
|
||||
return DoSave(std::make_shared<FileWriter>(fh), threaded, citymania::GetLocalSavePreset());
|
||||
}
|
||||
|
||||
/* LOAD game */
|
||||
assert(fop == SLO_LOAD || fop == SLO_CHECK);
|
||||
Debug(desync, 1, "load: {}", filename);
|
||||
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();
|
||||
|
||||
Reference in New Issue
Block a user