Skip to content

Commit

Permalink
libdeng2|App|Config: Remember the old version when detecting an upgrade
Browse files Browse the repository at this point in the history
The old version is available as Version.OLD_VERSION.

Also, de::Value now has the as<> template for easy casting.
  • Loading branch information
skyjake committed Jul 7, 2013
1 parent 7e4e18e commit ff59d70
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
7 changes: 7 additions & 0 deletions doomsday/libdeng2/include/de/core/config.h
Expand Up @@ -23,6 +23,7 @@
#include "../Process"
#include "../String"
#include "../Path"
#include "../Version"

namespace de {

Expand Down Expand Up @@ -164,6 +165,12 @@ class DENG2_PUBLIC Config
*/
Variable const &operator [] (String const &name) const;

/**
* Returns the old version, when a new installed version has been detected.
* If no upgrade has occurred, returns the current version.
*/
Version upgradedFromVersion() const;

private:
DENG2_PRIVATE(d)
};
Expand Down
4 changes: 4 additions & 0 deletions doomsday/libdeng2/include/de/core/version.h
Expand Up @@ -87,6 +87,10 @@ class Version

bool operator == (Version const &other) const;

bool operator != (Version const &other) const {
return !(*this == other);
}

bool operator > (Version const &other) const;

/**
Expand Down
14 changes: 14 additions & 0 deletions doomsday/libdeng2/include/de/data/value.h
Expand Up @@ -85,6 +85,20 @@ class DENG2_PUBLIC Value : public String::IPatternArg, public ISerializable
*/
virtual Text asText() const = 0;

template <typename ValueType>
ValueType &as() {
ValueType *t = dynamic_cast<ValueType *>(this);
if(!t) throw ConversionError("Value::as<>", "Illegal type conversion");
return *t;
}

template <typename ValueType>
ValueType const &as() const {
ValueType const *t = dynamic_cast<ValueType const *>(this);
if(!t) throw ConversionError("Value::as<>", "Illegal const type conversion");
return *t;
}

/**
* Determine the size of the value. The meaning of this
* depends on the type of the value.
Expand Down
11 changes: 11 additions & 0 deletions doomsday/libdeng2/src/core/app.cpp
Expand Up @@ -350,6 +350,17 @@ void App::initSubsystems(SubsystemInitFlags flags)

d->config->read();

// Immediately after upgrading, OLD_VERSION is also present in the Version module.
Version oldVer = d->config->upgradedFromVersion();
if(oldVer != Version())
{
ArrayValue *old = new ArrayValue;
*old << NumberValue(oldVer.major) << NumberValue(oldVer.minor)
<< NumberValue(oldVer.patch) << NumberValue(oldVer.build);
d->scriptSys.nativeModule("Version").addArray("OLD_VERSION", old).setReadOnly();
}

// Set up the log buffer.
LogBuffer &logBuf = LogBuffer::appBuffer();

// Update the log buffer max entry count: number of items to hold in memory.
Expand Down
23 changes: 23 additions & 0 deletions doomsday/libdeng2/src/core/config.cpp
Expand Up @@ -42,10 +42,27 @@ DENG2_PIMPL_NOREF(Config)
/// The configuration namespace.
Process config;

/// Previous installed version (__version__ in the read persistent Config).
Version oldVersion;

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

void setOldVersion(Value const &old)
{
try
{
ArrayValue const &vers = old.as<ArrayValue>();
oldVersion.major = int(vers.at(0).asNumber());
oldVersion.minor = int(vers.at(1).asNumber());
oldVersion.patch = int(vers.at(2).asNumber());
oldVersion.build = int(vers.at(3).asNumber());
}
catch(...)
{}
}
};

Config::Config(Path const &path) : d(new Instance(path))
Expand Down Expand Up @@ -89,6 +106,7 @@ void Config::read()

// If the saved config is from a different version, rerun the script.
Value const &oldVersion = names()["__version__"].value();
d->setOldVersion(oldVersion);
if(oldVersion.compare(*version))
{
// Version mismatch: store the old version in a separate variable.
Expand Down Expand Up @@ -162,6 +180,11 @@ Variable const &Config::operator [] (String const &name) const
return names()[name];
}

Version Config::upgradedFromVersion() const
{
return d->oldVersion;
}

Value &Config::get(String const &name) const
{
return d->config.globals()[name].value();
Expand Down

0 comments on commit ff59d70

Please sign in to comment.