2003-11-10 03:02:28 +01:00
|
|
|
/*
|
|
|
|
Config.h
|
|
|
|
|
|
|
|
CConfig dynamic data file manager class
|
|
|
|
Mark Thompson (mot20@cam.ac.uk)
|
|
|
|
|
2003-11-22 03:38:03 +01:00
|
|
|
Last modified: 22 November 2003 (Mark Thompson)
|
2003-11-10 03:02:28 +01:00
|
|
|
|
|
|
|
--Overview--
|
|
|
|
|
|
|
|
Maintains a list of data files in use by the engine; reloads any data file
|
|
|
|
altered during execution when Update() is called.
|
|
|
|
|
|
|
|
--Usage--
|
|
|
|
|
|
|
|
Load files by calling CConfig::Register() for each. The files are not loaded
|
|
|
|
until Update() is called; do so at the end of the block.
|
|
|
|
|
|
|
|
Update() compares the 'last-modified' timestamps of all registered files,
|
|
|
|
reloading all that have been altered since the last call (also any newly
|
|
|
|
registered).
|
|
|
|
|
|
|
|
Loader functions passed to Register() must take a CStr argument for a
|
|
|
|
filename, a void* for additional data, and return PS_OK if successful.
|
|
|
|
They must also handle the case where modified data is being reloaded. Ideally,
|
|
|
|
they should release resources allocated to the old version and load the new, at
|
|
|
|
least functions must leave the system in a predictable state. (e.g. if graphics
|
|
|
|
files are changed, but the loader functions cannot reload them, they should
|
|
|
|
do nothing but return PS_FILE_NODYNAMIC)
|
|
|
|
|
|
|
|
--Examples--
|
|
|
|
|
|
|
|
g_Config.Register( "gameParameters.dat", NULL, paramLoader );
|
|
|
|
g_Config.Register( "graphicsParameters7.dat", (void*)7, gfxParamLoader );
|
|
|
|
g_Config.Update();
|
|
|
|
|
|
|
|
--More info--
|
|
|
|
|
|
|
|
TDD at http://forums.wildfiregames.com/0ad
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2004-01-20 02:51:06 +01:00
|
|
|
#ifndef CONFIG_INCLUDED
|
|
|
|
#define CONFIG_INCLUDED
|
2003-11-10 03:02:28 +01:00
|
|
|
|
|
|
|
//--------------------------------------------------------
|
|
|
|
// Includes / Compiler directives
|
|
|
|
//--------------------------------------------------------
|
|
|
|
|
2004-06-01 19:34:12 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2003-11-10 03:02:28 +01:00
|
|
|
#include "Prometheus.h"
|
|
|
|
#include "Singleton.h"
|
|
|
|
#include "CStr.h"
|
|
|
|
#include "LogFile.h"
|
2004-06-01 19:34:12 +02:00
|
|
|
#include "lib.h"
|
2003-11-10 03:02:28 +01:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
//--------------------------------------------------------
|
|
|
|
// Macros
|
|
|
|
//--------------------------------------------------------
|
|
|
|
|
|
|
|
// Get singleton
|
|
|
|
#define g_Config CConfig::GetSingleton()
|
|
|
|
|
|
|
|
//Dummy timestamp value
|
|
|
|
#define TIME_UNREGISTERED 0
|
|
|
|
#define ACCESS_EXISTS 0
|
|
|
|
//The maximum number of files processed in one call to Update()
|
|
|
|
#define CONFIG_SLICE 100
|
|
|
|
|
|
|
|
//--------------------------------------------------------
|
|
|
|
// Declarations
|
|
|
|
//--------------------------------------------------------
|
|
|
|
|
|
|
|
//Possible return codes
|
|
|
|
DECLARE_ERROR( PS_FILE_NOT_FOUND );
|
|
|
|
DECLARE_ERROR( PS_FILE_LOAD_FAILURE );
|
|
|
|
DECLARE_ERROR( PS_FILE_NODYNAMIC );
|
|
|
|
|
|
|
|
//Loader function
|
|
|
|
typedef PS_RESULT (*LoaderFunction)( CStr Filename, void* Data );
|
|
|
|
|
|
|
|
//Internal registration type
|
|
|
|
struct SConfigData
|
|
|
|
{
|
|
|
|
CStr Filename;
|
|
|
|
LoaderFunction DynamicLoader;
|
|
|
|
void* Data;
|
2003-11-22 03:38:03 +01:00
|
|
|
bool Static;
|
2003-11-10 03:02:28 +01:00
|
|
|
time_t Timestamp;
|
2003-11-22 03:38:03 +01:00
|
|
|
SConfigData( CStr _Filename, void* _Data, LoaderFunction _DynamicLoader, bool _Static );
|
2003-11-10 03:02:28 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class CConfig : public Singleton<CConfig>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CConfig();
|
|
|
|
//Register a new file with it's associated loader function
|
2003-11-22 03:38:03 +01:00
|
|
|
PS_RESULT Register( CStr Filename, void* Data, LoaderFunction DynamicLoader, bool Static = false );
|
2003-11-10 03:02:28 +01:00
|
|
|
//Check all registered files, reload as neccessary
|
|
|
|
PS_RESULT Update();
|
2003-11-22 03:38:03 +01:00
|
|
|
//Force an update of all files in the registered and static lists.
|
2003-11-10 03:02:28 +01:00
|
|
|
PS_RESULT ReloadAll();
|
2003-11-22 03:38:03 +01:00
|
|
|
//Erase the entire list of registered and static files
|
2003-11-10 03:02:28 +01:00
|
|
|
void Clear();
|
|
|
|
//Attach or detach a logfile class.
|
|
|
|
void Attach( CLogFile* LogFile );
|
|
|
|
private:
|
2004-06-11 04:23:00 +02:00
|
|
|
std::vector<SConfigData> m_FileList;
|
|
|
|
std::vector<SConfigData>::iterator i;
|
2003-11-10 03:02:28 +01:00
|
|
|
CLogFile* m_LogFile;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|