mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-03-12 02:19:41 +00:00
(svn r21395) -Codechange: move the save and load filter's interface to a header
This commit is contained in:
parent
5ff58155d1
commit
085c693289
@ -734,6 +734,7 @@
|
||||
<ClCompile Include="..\src\saveload\order_sl.cpp" />
|
||||
<ClCompile Include="..\src\saveload\saveload.cpp" />
|
||||
<ClInclude Include="..\src\saveload\saveload.h" />
|
||||
<ClInclude Include="..\src\saveload\saveload_filter.h" />
|
||||
<ClInclude Include="..\src\saveload\saveload_internal.h" />
|
||||
<ClCompile Include="..\src\saveload\signs_sl.cpp" />
|
||||
<ClCompile Include="..\src\saveload\station_sl.cpp" />
|
||||
|
@ -1422,6 +1422,9 @@
|
||||
<ClInclude Include="..\src\saveload\saveload.h">
|
||||
<Filter>Save/Load handlers</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\src\saveload\saveload_filter.h">
|
||||
<Filter>Save/Load handlers</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\src\saveload\saveload_internal.h">
|
||||
<Filter>Save/Load handlers</Filter>
|
||||
</ClInclude>
|
||||
|
@ -2230,6 +2230,10 @@
|
||||
RelativePath=".\..\src\saveload\saveload.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\..\src\saveload\saveload_filter.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\..\src\saveload\saveload_internal.h"
|
||||
>
|
||||
|
@ -2227,6 +2227,10 @@
|
||||
RelativePath=".\..\src\saveload\saveload.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\..\src\saveload\saveload_filter.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\..\src\saveload\saveload_internal.h"
|
||||
>
|
||||
|
@ -492,6 +492,7 @@ saveload/oldloader_sl.cpp
|
||||
saveload/order_sl.cpp
|
||||
saveload/saveload.cpp
|
||||
saveload/saveload.h
|
||||
saveload/saveload_filter.h
|
||||
saveload/saveload_internal.h
|
||||
saveload/signs_sl.cpp
|
||||
saveload/station_sl.cpp
|
||||
|
@ -46,6 +46,7 @@
|
||||
#include "table/strings.h"
|
||||
|
||||
#include "saveload_internal.h"
|
||||
#include "saveload_filter.h"
|
||||
|
||||
/*
|
||||
* Previous savegame versions, the trunk revision where they were
|
||||
@ -246,43 +247,6 @@ enum NeedLength {
|
||||
/** Save in chunks of 128 KiB. */
|
||||
static const size_t MEMORY_CHUNK_SIZE = 128 * 1024;
|
||||
|
||||
/** Interface for filtering a savegame till it is loaded. */
|
||||
struct LoadFilter {
|
||||
/** Chained to the (savegame) filters. */
|
||||
LoadFilter *chain;
|
||||
|
||||
/**
|
||||
* Initialise this filter.
|
||||
* @param chain The next filter in this chain.
|
||||
*/
|
||||
LoadFilter(LoadFilter *chain) : chain(chain)
|
||||
{
|
||||
}
|
||||
|
||||
/** Make sure the writers are properly closed. */
|
||||
virtual ~LoadFilter()
|
||||
{
|
||||
delete this->chain;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a given number of bytes from the savegame.
|
||||
* @param buf The bytes to read.
|
||||
* @param len The number of bytes to read.
|
||||
* @return The number of actually read bytes.
|
||||
*/
|
||||
virtual size_t Read(byte *buf, size_t len) = 0;
|
||||
|
||||
/**
|
||||
* Reset this filter to read from the beginning of the file.
|
||||
*/
|
||||
virtual void Reset()
|
||||
{
|
||||
this->chain->Reset();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/** A buffer for reading (and buffering) savegame data. */
|
||||
struct ReadBuffer {
|
||||
byte buf[MEMORY_CHUNK_SIZE]; ///< Buffer we're going to read from.
|
||||
@ -324,62 +288,6 @@ struct ReadBuffer {
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Instantiator for a load filter.
|
||||
* @param chain The next filter in this chain.
|
||||
* @tparam T The type of load filter to create.
|
||||
*/
|
||||
template <typename T> LoadFilter *CreateLoadFilter(LoadFilter *chain)
|
||||
{
|
||||
return new T(chain);
|
||||
}
|
||||
|
||||
/** Interface for filtering a savegame till it is written. */
|
||||
struct SaveFilter {
|
||||
/** Chained to the (savegame) filters. */
|
||||
SaveFilter *chain;
|
||||
|
||||
/**
|
||||
* Initialise this filter.
|
||||
* @param chain The next filter in this chain.
|
||||
*/
|
||||
SaveFilter(SaveFilter *chain) : chain(chain)
|
||||
{
|
||||
}
|
||||
|
||||
/** Make sure the writers are properly closed. */
|
||||
virtual ~SaveFilter()
|
||||
{
|
||||
delete this->chain;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a given number of bytes into the savegame.
|
||||
* @param buf The bytes to write.
|
||||
* @param len The number of bytes to write.
|
||||
*/
|
||||
virtual void Write(byte *buf, size_t len) = 0;
|
||||
|
||||
/**
|
||||
* Prepare everything to finish writing the savegame.
|
||||
*/
|
||||
virtual void Finish()
|
||||
{
|
||||
if (this->chain != NULL) this->chain->Finish();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Instantiator for a save filter.
|
||||
* @param chain The next filter in this chain.
|
||||
* @param compression_level The requested level of compression.
|
||||
* @tparam T The type of save filter to create.
|
||||
*/
|
||||
template <typename T> SaveFilter *CreateSaveFilter(SaveFilter *chain, byte compression_level)
|
||||
{
|
||||
return new T(chain, compression_level);
|
||||
}
|
||||
|
||||
/** Container for dumping the savegame (quickly) to memory. */
|
||||
struct MemoryDumper {
|
||||
AutoFreeSmallVector<byte *, 16> blocks; ///< Buffer with blocks of allocated memory.
|
||||
@ -2517,6 +2425,23 @@ static SaveOrLoadResult DoSave(SaveFilter *writer, bool threaded)
|
||||
return SL_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the game using a (writer) filter.
|
||||
* @param writer The filter to write the savegame to.
|
||||
* @param threaded Whether to try to perform the saving asynchroniously.
|
||||
* @return Return the result of the action. #SL_OK or #SL_ERROR
|
||||
*/
|
||||
SaveOrLoadResult SaveWithFilter(SaveFilter *writer, bool threaded)
|
||||
{
|
||||
try {
|
||||
_sl.action = SLA_SAVE;
|
||||
return DoSave(writer, threaded);
|
||||
} catch (...) {
|
||||
ClearSaveLoadState();
|
||||
return SL_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Actually perform the loading of a "non-old" savegame.
|
||||
* @param reader The filter to read the savegame from.
|
||||
@ -2659,6 +2584,22 @@ static SaveOrLoadResult DoLoad(LoadFilter *reader, bool load_check)
|
||||
return SL_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the game using a (reader) filter.
|
||||
* @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)
|
||||
{
|
||||
try {
|
||||
_sl.action = SLA_LOAD;
|
||||
return DoLoad(reader, false);
|
||||
} catch (...) {
|
||||
ClearSaveLoadState();
|
||||
return SL_REINIT;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Main Save or Load function where the high-level saveload functions are
|
||||
* handled. It opens the savegame, selects format and checks versions
|
||||
|
@ -55,6 +55,8 @@ SaveOrLoadResult SaveOrLoad(const char *filename, int mode, Subdirectory sb, boo
|
||||
void WaitTillSaved();
|
||||
void DoExitSave();
|
||||
|
||||
SaveOrLoadResult SaveWithFilter(struct SaveFilter *writer, bool threaded);
|
||||
SaveOrLoadResult LoadWithFilter(struct LoadFilter *reader);
|
||||
|
||||
typedef void ChunkSaveLoadProc();
|
||||
typedef void AutolengthProc(void *arg);
|
||||
|
107
src/saveload/saveload_filter.h
Normal file
107
src/saveload/saveload_filter.h
Normal file
@ -0,0 +1,107 @@
|
||||
/* $Id$ */
|
||||
|
||||
/*
|
||||
* This file is part of OpenTTD.
|
||||
* 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.
|
||||
* 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.
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
/** @file saveload_filter.h Declaration of filters used for saving and loading savegames. */
|
||||
|
||||
#ifndef SAVELOAD_FILTER_H
|
||||
#define SAVELOAD_FILTER_H
|
||||
|
||||
/** Interface for filtering a savegame till it is loaded. */
|
||||
struct LoadFilter {
|
||||
/** Chained to the (savegame) filters. */
|
||||
LoadFilter *chain;
|
||||
|
||||
/**
|
||||
* Initialise this filter.
|
||||
* @param chain The next filter in this chain.
|
||||
*/
|
||||
LoadFilter(LoadFilter *chain) : chain(chain)
|
||||
{
|
||||
}
|
||||
|
||||
/** Make sure the writers are properly closed. */
|
||||
virtual ~LoadFilter()
|
||||
{
|
||||
delete this->chain;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a given number of bytes from the savegame.
|
||||
* @param buf The bytes to read.
|
||||
* @param len The number of bytes to read.
|
||||
* @return The number of actually read bytes.
|
||||
*/
|
||||
virtual size_t Read(byte *buf, size_t len) = 0;
|
||||
|
||||
/**
|
||||
* Reset this filter to read from the beginning of the file.
|
||||
*/
|
||||
virtual void Reset()
|
||||
{
|
||||
this->chain->Reset();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Instantiator for a load filter.
|
||||
* @param chain The next filter in this chain.
|
||||
* @tparam T The type of load filter to create.
|
||||
*/
|
||||
template <typename T> LoadFilter *CreateLoadFilter(LoadFilter *chain)
|
||||
{
|
||||
return new T(chain);
|
||||
}
|
||||
|
||||
/** Interface for filtering a savegame till it is written. */
|
||||
struct SaveFilter {
|
||||
/** Chained to the (savegame) filters. */
|
||||
SaveFilter *chain;
|
||||
|
||||
/**
|
||||
* Initialise this filter.
|
||||
* @param chain The next filter in this chain.
|
||||
*/
|
||||
SaveFilter(SaveFilter *chain) : chain(chain)
|
||||
{
|
||||
}
|
||||
|
||||
/** Make sure the writers are properly closed. */
|
||||
virtual ~SaveFilter()
|
||||
{
|
||||
delete this->chain;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a given number of bytes into the savegame.
|
||||
* @param buf The bytes to write.
|
||||
* @param len The number of bytes to write.
|
||||
*/
|
||||
virtual void Write(byte *buf, size_t len) = 0;
|
||||
|
||||
/**
|
||||
* Prepare everything to finish writing the savegame.
|
||||
*/
|
||||
virtual void Finish()
|
||||
{
|
||||
if (this->chain != NULL) this->chain->Finish();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Instantiator for a save filter.
|
||||
* @param chain The next filter in this chain.
|
||||
* @param compression_level The requested level of compression.
|
||||
* @tparam T The type of save filter to create.
|
||||
*/
|
||||
template <typename T> SaveFilter *CreateSaveFilter(SaveFilter *chain, byte compression_level)
|
||||
{
|
||||
return new T(chain, compression_level);
|
||||
}
|
||||
|
||||
#endif /* SAVELOAD_FILTER_H */
|
Loading…
Reference in New Issue
Block a user