Skip to content

Commit

Permalink
libdoomsday: DoomsdayApp owns persistent game profiles
Browse files Browse the repository at this point in the history
These game profiles are used for storing packages selections and
for the user's custom additional profiles.
  • Loading branch information
skyjake committed Mar 5, 2016
1 parent e7bf429 commit cf189fd
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 12 deletions.
18 changes: 6 additions & 12 deletions doomsday/apps/client/src/configprofiles.cpp
Expand Up @@ -293,7 +293,7 @@ DENG2_PIMPL(ConfigProfiles)

if(!self.persistentName().isEmpty())
{
App::config().set(confName(), name);
App::config().set(configVarName(), name);
}
}

Expand Down Expand Up @@ -353,7 +353,7 @@ DENG2_PIMPL(ConfigProfiles)
* For a persistent register, determines the name of the Config variable
* that stores the name of the currently selected profile.
*/
String confName() const
String configVarName() const
{
if(self.persistentName().isEmpty()) return "";
return self.persistentName().concatenateMember("profile");
Expand Down Expand Up @@ -419,14 +419,8 @@ DENG2_PIMPL(ConfigProfiles)
current = CUSTOM_PROFILE;
}

// Still nothing?
addCustomProfileIfMissing();

if(App::config().objectNamespace().has(confName()))
{
// Update current profile.
current = App::config()[confName()].value().asText();
}
// Update current profile.
current = App::config().gets(configVarName(), current);

if(!tryFind(current))
{
Expand All @@ -444,7 +438,7 @@ DENG2_PIMPL(ConfigProfiles)
// Make sure these are the values now in use.
apply(current);

App::config().set(confName(), current);
App::config().set(configVarName(), current);
}

/**
Expand All @@ -466,7 +460,7 @@ DENG2_PIMPL(ConfigProfiles)
fetch(current);

// Remember which profile is the current one.
App::config().set(confName(), current);
App::config().set(configVarName(), current);

self.serialize();
}
Expand Down
1 change: 1 addition & 0 deletions doomsday/apps/libdoomsday/include/doomsday/GameProfiles
@@ -0,0 +1 @@
#include "gameprofiles.h"
60 changes: 60 additions & 0 deletions doomsday/apps/libdoomsday/include/doomsday/gameprofiles.h
@@ -0,0 +1,60 @@
/** @file gameprofiles.h Game profiles.
*
* @authors Copyright (c) 2016 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program 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; either version 2 of the License, or (at your
* option) any later version. This program 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 this program; if not, see:
* http://www.gnu.org/licenses</small>
*/

#ifndef LIBDOOMSDAY_GAMEPROFILES_H
#define LIBDOOMSDAY_GAMEPROFILES_H

#include "libdoomsday.h"

#include <de/Profiles>

/**
* Game configuration profiles.
*/
class LIBDOOMSDAY_PUBLIC GameProfiles : public de::Profiles
{
public:
/**
* Game profile.
*/
class LIBDOOMSDAY_PUBLIC Profile : public AbstractProfile
{
public:
Profile();

void setGame(de::String const &id);
void setPackages(de::StringList const &packagesInOrder);

de::String game() const;
de::StringList packages() const;

virtual bool resetToDefaults();
virtual de::String toInfoSource() const;

private:
DENG2_PRIVATE(d)
};

public:
GameProfiles();

protected:
AbstractProfile *profileFromInfoBlock(de::Info::BlockElement const &block);
};

#endif // LIBDOOMSDAY_GAMEPROFILES_H
6 changes: 6 additions & 0 deletions doomsday/apps/libdoomsday/src/doomsdayapp.cpp
Expand Up @@ -18,6 +18,7 @@

#include "doomsday/doomsdayapp.h"
#include "doomsday/games.h"
#include "doomsday/gameprofiles.h"
#include "doomsday/console/exec.h"
#include "doomsday/filesys/sys_direc.h"
#include "doomsday/filesys/fs_util.h"
Expand Down Expand Up @@ -70,6 +71,7 @@ DENG2_PIMPL_NOREF(DoomsdayApp)
Plugins plugins;
Games games;
Game *currentGame = nullptr;
GameProfiles gameProfiles;
BusyMode busyMode;
Players players;
res::Bundles dataBundles;
Expand Down Expand Up @@ -119,6 +121,9 @@ DENG2_PIMPL_NOREF(DoomsdayApp)

~Instance()
{
// Save any changes to the game profiles.
gameProfiles.serialize();

theDoomsdayApp = nullptr;
}

Expand Down Expand Up @@ -367,6 +372,7 @@ void DoomsdayApp::initialize()
d->initialized = true;

d->dataBundles.identify();
d->gameProfiles.deserialize();
}

void DoomsdayApp::initWadFolders()
Expand Down
100 changes: 100 additions & 0 deletions doomsday/apps/libdoomsday/src/gameprofiles.cpp
@@ -0,0 +1,100 @@
/** @file gameprofiles.cpp Game profiles.
*
* @authors Copyright (c) 2016 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program 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; either version 2 of the License, or (at your
* option) any later version. This program 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 this program; if not, see:
* http://www.gnu.org/licenses</small>
*/

#include "doomsday/gameprofiles.h"

#include <de/Record>

#include <QTextStream>

using namespace de;

static String const VAR_GAME ("game");
static String const VAR_PACKAGES("packages");

GameProfiles::GameProfiles()
{
setPersistentName("game");
}

Profiles::AbstractProfile *GameProfiles::profileFromInfoBlock(Info::BlockElement const &block)
{
std::unique_ptr<Profile> prof(new Profile);

prof->setGame(block.keyValue(VAR_GAME).text);

if(Info::ListElement const *pkgs = block.findAs<Info::ListElement>(VAR_PACKAGES))
{
StringList ids;
for(auto const &val : pkgs->values()) ids << val.text;
prof->setPackages(ids);
}

return prof.release();
}

//---------------------------------------------------------------------------------------

DENG2_PIMPL_NOREF(GameProfiles::Profile)
{
String gameId;
StringList packages;
};

GameProfiles::Profile::Profile() : d(new Instance)
{}

void GameProfiles::Profile::setGame(String const &id)
{
d->gameId = id;
}

void GameProfiles::Profile::setPackages(StringList const &packagesInOrder)
{
d->packages = packagesInOrder;
}

String GameProfiles::Profile::game() const
{
return d->gameId;
}

StringList GameProfiles::Profile::packages() const
{
return d->packages;
}

bool GameProfiles::Profile::resetToDefaults()
{
if(isReadOnly()) return false;

d->packages.clear();
return true;
}

String GameProfiles::Profile::toInfoSource() const
{
String info;
QTextStream os(&info);
os.setCodec("UTF-8");

os << VAR_GAME << ": " << d->gameId << "\n"
<< VAR_PACKAGES << " <" << String::join(d->packages, ", ") << ">";

return info;
}

0 comments on commit cf189fd

Please sign in to comment.