Skip to content

Commit

Permalink
Refactor: C++ API for registering and specifying game modes
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Jan 19, 2016
1 parent a34f865 commit be9e22a
Show file tree
Hide file tree
Showing 25 changed files with 343 additions and 307 deletions.
26 changes: 8 additions & 18 deletions doomsday/apps/api/api_base.h
Expand Up @@ -64,17 +64,7 @@ DENG_API_TYPEDEF(Base) // v2
* @note Game registration order defines the order of the automatic game
* identification/selection logic.
*/
gameid_t (*DefineGame)(GameDef const *definition);

/**
* Retrieves the game identifier for a previously defined game.
* @see DD_DefineGame().
*
* @param identityKey Identity key of the game.
*
* @return Game identifier.
*/
gameid_t (*GameIdForKey)(char const *identityKey);
//gameid_t (*DefineGame)(GameDef const *definition);

/**
* Adds a new resource to the list for the identified @a game.
Expand All @@ -95,8 +85,8 @@ DENG_API_TYPEDEF(Base) // v2
* For package resources this may be C-String containing a
* semicolon delimited list of identity keys.
*/
void (*AddGameResource)(gameid_t game, resourceclassid_t classId, int fFlags,
char const *names, void *params);
//void (*AddGameResource)(gameid_t game, resourceclassid_t classId, int fFlags,
// char const *names, void const *params);

/**
* Retrieve extended info about the current game.
Expand All @@ -105,7 +95,7 @@ DENG_API_TYPEDEF(Base) // v2
*
* @return @c true if successful else @c false (i.e., no game loaded).
*/
dd_bool (*gameInfo)(GameInfo *info);
dd_bool (*GameInfo_)(GameInfo *info);

/**
* Determines whether the current run of the thinkers should be considered a
Expand Down Expand Up @@ -145,10 +135,10 @@ DENG_API_T(Base);
#define DD_SetInteger _api_Base.SetInteger
#define DD_GetVariable _api_Base.GetVariable
#define DD_SetVariable _api_Base.SetVariable
#define DD_DefineGame _api_Base.DefineGame
#define DD_GameIdForKey _api_Base.GameIdForKey
#define DD_AddGameResource _api_Base.AddGameResource
#define DD_GameInfo _api_Base.gameInfo
//#define DD_DefineGame _api_Base.DefineGame
//#define DD_GameIdForKey _api_Base.GameIdForKey
//#define DD_AddGameResource _api_Base.AddGameResource
#define DD_GameInfo _api_Base.GameInfo_
#define DD_IsSharpTick _api_Base.IsSharpTick
#define Net_SendPacket _api_Base.SendPacket
#define R_SetupMap _api_Base.SetupMap
Expand Down
3 changes: 2 additions & 1 deletion doomsday/apps/api/apis.h
Expand Up @@ -70,7 +70,8 @@
enum {
DE_API_BASE_v1 = 0, // 1.10
DE_API_BASE_v2 = 1, // 1.14
DE_API_BASE = DE_API_BASE_v2,
DE_API_BASE_v3 = 2, // 2.0
DE_API_BASE = DE_API_BASE_v3,

DE_API_BINDING_v1 = 100, // 1.10
DE_API_BINDING_v2 = 101, // 1.15
Expand Down
79 changes: 1 addition & 78 deletions doomsday/apps/client/src/dd_main.cpp
Expand Up @@ -1113,7 +1113,7 @@ static dint DD_ActivateGameWorker(void *context)
DENG2_ASSERT(App_CurrentGame().pluginId() != 0);

plugins.setActivePluginId(App_CurrentGame().pluginId());
gx.PreInit(App_Games().id(App_CurrentGame()));
gx.PreInit(App_CurrentGame().id().toUtf8());
plugins.setActivePluginId(0);
}
}
Expand Down Expand Up @@ -1250,80 +1250,6 @@ dd_bool DD_GameInfo(GameInfo *info)
return false;
}

#undef DD_AddGameResource
void DD_AddGameResource(gameid_t gameId, resourceclassid_t classId, dint rflags,
char const *names, void *params)
{
if(!VALID_RESOURCECLASSID(classId))
App_Error("DD_AddGameResource: Unknown resource class %i.", (dint)classId);

if(!names || !names[0])
App_Error("DD_AddGameResource: Invalid name argument.");

// Construct and attach the new resource record.
Game &game = App_Games().byId(gameId);
ResourceManifest *manifest = new ResourceManifest(classId, rflags);
game.addManifest(*manifest);

// Add the name list to the resource record.
QStringList nameList = String(names).split(";", QString::SkipEmptyParts);
foreach(QString const &nameRef, nameList)
{
manifest->addName(nameRef);
}

if(params && classId == RC_PACKAGE)
{
// Add the identityKey list to the resource record.
QStringList idKeys = String((char const *) params).split(";", QString::SkipEmptyParts);
foreach(QString const &idKeyRef, idKeys)
{
manifest->addIdentityKey(idKeyRef);
}
}
}

#undef DD_DefineGame
gameid_t DD_DefineGame(GameDef const *def)
{
LOG_AS("DD_DefineGame");
if(!def) return 0; // Invalid id.

// Game mode identity keys must be unique. Ensure that is the case.
try
{
/*Game &game =*/ App_Games().byIdentityKey(def->identityKey);
LOGDEV_WARNING("Ignored new game \"%s\", identity key '%s' already in use")
<< def->defaultTitle << def->identityKey;
return 0; // Invalid id.
}
catch(Games::NotFoundError const &)
{} // Ignore the error.

Game *game = Game::fromDef(*def);
if(!game) return 0; // Invalid def.

// Add this game to our records.
game->setPluginId(DoomsdayApp::plugins().activePluginId());
App_Games().add(*game);
return App_Games().id(*game);
}

#undef DD_GameIdForKey
gameid_t DD_GameIdForKey(char const *identityKey)
{
try
{
return App_Games().id(App_Games().byIdentityKey(identityKey));
}
catch(Games::NotFoundError const &)
{
LOG_AS("DD_GameIdForKey");
LOGDEV_WARNING("Game \"%s\" is not defined, returning 0.") << identityKey;
}
return 0; // Invalid id.
}

Game &App_CurrentGame()
{
return DoomsdayApp::currentGame();
Expand Down Expand Up @@ -3242,9 +3168,6 @@ DENG_DECLARE_API(Base) =
DD_SetInteger,
DD_GetVariable,
DD_SetVariable,
DD_DefineGame,
DD_GameIdForKey,
DD_AddGameResource,
DD_GameInfo,
DD_IsSharpTick,
Net_SendPacket,
Expand Down
1 change: 1 addition & 0 deletions doomsday/apps/client/src/ui/dialogs/logsettingsdialog.cpp
Expand Up @@ -217,6 +217,7 @@ LogSettingsDialog::LogSettingsDialog(String const &name)
: DialogWidget(name, WithHeading), d(new Instance(this))
{
heading().setText(tr("Log Filter & Alerts"));
heading().setImage(style().images().image("log"));

// Layout.
GridLayout layout(area().contentRule().left(), area().contentRule().top());
Expand Down
4 changes: 3 additions & 1 deletion doomsday/apps/libdoomsday/include/doomsday/doomsdayapp.h
Expand Up @@ -20,7 +20,6 @@
#define LIBDOOMSDAY_DOOMSDAYAPP_H

#include "plugins.h"
#include "games.h"
#include "busymode.h"
#include "gameapi.h"
#include "players.h"
Expand All @@ -31,6 +30,9 @@

namespace res { class Bundles; }

class Games;
class Game;

/**
* Common application-level state and components.
*
Expand Down
38 changes: 37 additions & 1 deletion doomsday/apps/libdoomsday/include/doomsday/game.h
@@ -1,6 +1,6 @@
/** @file game.h Game mode configuration (metadata, resource files, etc...).
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2003-2016 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2005-2013 Daniel Swanson <danij@dengine.net>
*
* @par License
Expand Down Expand Up @@ -103,6 +103,20 @@ class LIBDOOMSDAY_PUBLIC Game : public AbstractGame

virtual ~Game();

/**
* Sets the packages required for loading the game.
*
* All these packages are loaded when the game is loaded.
*
* @param packageIds List of package IDs.
*/
void setRequiredPackages(de::StringList const &packageIds);

/**
* Returns the list of required package IDs for loading the game.
*/
de::StringList requiredPackages() const;

/**
* Determines the status of the game.
*
Expand Down Expand Up @@ -212,6 +226,28 @@ class LIBDOOMSDAY_PUBLIC Game : public AbstractGame
*/
bool isRequiredFile(de::File1 &file);

/**
* Adds a new resource to the list for the identified @a game.
*
* @note Resource order defines the load order of resources (among those of
* the same type). Resources are loaded from most recently added to least
* recent.
*
* @param game Unique identifier/name of the game.
* @param classId Class of resource being defined.
* @param fFlags File flags (see @ref fileFlags).
* @param names One or more known potential names, seperated by semicolon
* (e.g., <pre> "name1;name2" </pre>). Valid names include
* absolute or relative file paths, possibly with encoded
* symbolic tokens, or predefined symbols into the virtual
* file system.
* @param params Additional parameters. Usage depends on resource type.
* For package resources this may be C-String containing a
* semicolon delimited list of identity keys.
*/
void addResource(resourceclassid_t classId, de::dint rflags,
char const *names, void const *params);

public:
/**
* Construct a new Game instance from the specified definition @a def.
Expand Down
4 changes: 3 additions & 1 deletion doomsday/apps/libdoomsday/include/doomsday/gameapi.h
Expand Up @@ -32,6 +32,8 @@ extern "C" {
struct event_s;

/// General constants (not to be used with Get/Set).
/// @note Many of these have become unused as better APIs are introduced for
/// sharing information.
enum {
DD_DISABLE,
DD_ENABLE,
Expand Down Expand Up @@ -135,7 +137,7 @@ typedef struct {
size_t apiSize; ///< sizeof(game_export_t)

// Base-level.
void (*PreInit) (gameid_t gameId);
void (*PreInit) (char const *gameId);
void (*PostInit) (void);
dd_bool (*TryShutdown) (void);
void (*Shutdown) (void);
Expand Down
26 changes: 13 additions & 13 deletions doomsday/apps/libdoomsday/include/doomsday/games.h
Expand Up @@ -22,6 +22,7 @@
#define LIBDOOMSDAY_GAMES_H

#include "game.h"

#include <de/types.h>
#include <de/str.h>
#include <de/Observers>
Expand Down Expand Up @@ -78,26 +79,13 @@ class LIBDOOMSDAY_PUBLIC Games
/// @return Number of games marked as currently playable.
int numPlayable() const;

/**
* @param game Game instance.
* @return Unique identifier associated with @a game.
*/
gameid_t id(Game const &game) const;

/**
* @return Game associated with @a identityKey.
*
* @throws NotFoundError if no game is associated with @a identityKey.
*/
Game &byIdentityKey(de::String identityKey) const;

/**
* @return Game associated with @a gameId.
*
* @throws NotFoundError if no game is associated with @a gameId.
*/
Game &byId(gameid_t gameId) const;

/**
* @return Game associated with unique index @a idx.
*
Expand All @@ -107,6 +95,18 @@ class LIBDOOMSDAY_PUBLIC Games

void clear();

/**
* Register a new game.
*
* @param def GameDef structure defining the new game.
*
* @return Unique identifier/name assigned to resultant game.
*
* @note Game registration order defines the order of the automatic game
* identification/selection logic.
*/
Game &defineGame(GameDef const *def);

/**
* Add a new Game to this collection. If @a game is already present in the
* collection this is no-op.
Expand Down
12 changes: 6 additions & 6 deletions doomsday/apps/libdoomsday/include/doomsday/savedsession.h
Expand Up @@ -3,16 +3,16 @@
* @authors Copyright © 2014 Daniel Swanson <danij@dengine.net>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
* 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 Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at your
* 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 Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* 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>
*/

Expand Down
12 changes: 6 additions & 6 deletions doomsday/apps/libdoomsday/include/doomsday/session.h
Expand Up @@ -3,16 +3,16 @@
* @authors Copyright © 2014 Daniel Swanson <danij@dengine.net>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
* 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 Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at your
* 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 Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* 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>
*/

Expand Down
9 changes: 9 additions & 0 deletions doomsday/apps/libdoomsday/src/doomsdayapp.cpp
Expand Up @@ -17,6 +17,7 @@
*/

#include "doomsday/doomsdayapp.h"
#include "doomsday/games.h"
#include "doomsday/filesys/sys_direc.h"
#include "doomsday/filesys/fs_util.h"
#include "doomsday/resource/bundles.h"
Expand Down Expand Up @@ -172,6 +173,14 @@ DENG2_PIMPL_NOREF(DoomsdayApp)
}
}

#ifdef UNIX
NativePath const systemWads("/usr/share/games/doom");
if(systemWads.exists())
{
attachWadFeed("system", systemWads);
}
#endif

// Add all paths from the DOOMWADPATH environment variable.
if(getenv("DOOMWADPATH"))
{
Expand Down

0 comments on commit be9e22a

Please sign in to comment.