Skip to content

Commit

Permalink
Refactor|libdeng2: Use Refuge in Config
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Feb 20, 2014
1 parent a67cf9e commit dfed485
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 38 deletions.
27 changes: 12 additions & 15 deletions doomsday/libdeng2/include/de/core/config.h
Expand Up @@ -30,18 +30,20 @@ namespace de {
class ArrayValue;

/**
* Stores the configuration of everything. The application owns a Config.
* The default configuration is produced by executing the .de scripts
* in the config directories. The resulting namespace is serialized for
* storage, and is restored from the serialized version directly before the
* config scripts are run.
* Stores the configuration of everything.
*
* The version of the engine is stored in the serialized config namespace.
* This is for actions needed when upgrading: the config script can check
* the previous version and apply changes accordingly.
* The application owns a Config. The default configuration is produced by executing the
* .de scripts in the config directories. The resulting namespace is serialized for
* storage, and is restored from the serialized version directly before the config
* scripts are run.
*
* In practice, Config is a specialized script namespace stored in a Record. It
* gets written to the application's persistent data store (persist.pack).
* The version of the engine is stored in the serialized config namespace. This is for
* actions needed when upgrading: the config script can check the previous version and
* apply changes accordingly.
*
* In practice, Config is a specialized script namespace stored in a Record. It gets
* written to the application's persistent data store (persist.pack) using a Refuge. The
* Config is automatically written persistently before being destroyed.
*/
class DENG2_PUBLIC Config
{
Expand All @@ -57,11 +59,6 @@ class DENG2_PUBLIC Config
*/
Config(Path const &path);

/**
* Destructor. The configuration is automatically saved.
*/
virtual ~Config();

/// Read configuration from files.
void read();

Expand Down
2 changes: 2 additions & 0 deletions doomsday/libdeng2/include/de/data/iblock.h
Expand Up @@ -27,6 +27,8 @@ namespace de {
/**
* Interface for a resizable block of memory that provides direct access
* to the bytes.
*
* @ingroup data
*/
class IBlock
{
Expand Down
34 changes: 11 additions & 23 deletions doomsday/libdeng2/src/core/config.cpp
Expand Up @@ -20,6 +20,7 @@
#include "de/Config"
#include "de/App"
#include "de/Archive"
#include "de/Refuge"
#include "de/Log"
#include "de/Folder"
#include "de/ArrayValue"
Expand All @@ -36,8 +37,8 @@ DENG2_PIMPL_NOREF(Config)
/// Configuration file name.
Path configPath;

/// Path where the configuration is written (inside persist.pack).
Path persistentPath;
/// Saved configuration data (inside persist.pack).
Refuge refuge;

/// The configuration namespace.
Process config;
Expand All @@ -46,8 +47,9 @@ DENG2_PIMPL_NOREF(Config)
Version oldVersion;

Instance(Path const &path)
: configPath(path),
persistentPath("modules/Config")
: configPath(path)
, refuge("modules/Config")
, config(&refuge.names())
{}

void setOldVersion(Value const &old)
Expand All @@ -68,19 +70,6 @@ DENG2_PIMPL_NOREF(Config)
Config::Config(Path const &path) : d(new Instance(path))
{}

Config::~Config()
{
LOG_AS("~Config");
try
{
write();
}
catch(Error const &err)
{
LOG_ERROR("") << err.asText();
}
}

void Config::read()
{
LOG_AS("Config::read");
Expand All @@ -99,8 +88,7 @@ void Config::read()
try
{
// If we already have a saved copy of the config, read it.
Archive const &persist = App::persistentData();
Reader(persist.entryBlock(d->persistentPath)).withHeader() >> names();
d->refuge.read();

LOG_DEBUG("Found serialized Config:\n") << names();

Expand All @@ -117,15 +105,15 @@ void Config::read()
else
{
// Versions match.
LOG_MSG("") << d->persistentPath << " matches version " << version->asText();
LOG_MSG("") << d->refuge.path() << " matches version " << version->asText();
}

// Also check the timestamp of written config vs. the config script.
// If script is newer, it should be rerun.
if(scriptFile.status().modifiedAt > persist.entryStatus(d->persistentPath).modifiedAt)
if(scriptFile.status().modifiedAt > d->refuge.lastWrittenAt())
{
LOG_MSG("%s is newer than %s, rerunning the script.")
<< d->configPath << d->persistentPath;
<< d->configPath << d->refuge.path();
shouldRunScript = true;
}
}
Expand Down Expand Up @@ -157,7 +145,7 @@ void Config::read()

void Config::write() const
{
Writer(App::persistentData().entryBlock(d->persistentPath)).withHeader() << names();
d->refuge.write();
}

Record &Config::names()
Expand Down

0 comments on commit dfed485

Please sign in to comment.