game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
GameLibrary Class Reference

Central database for managing all games in the launcher. More...

#include <game_library.h>

Public Member Functions

 GameLibrary ()
 Constructor - initializes library path.
 ~GameLibrary ()
 Destructor - cleanup.
std::string addGame (const Game &game)
 Add a new game to the library.
void addGameToCollection (const std::string &gameId, const std::string &collection)
 Add a game to a collection.
void createCollection (const std::string &name)
 Create a new empty collection.
void deleteCollection (const std::string &name)
 Delete a collection.
bool discoverImagesForGame (const std::string &gameId)
 Discover images for a specific game.
int discoverMissingImages ()
 Discover and update images for games missing cover art.
std::vector< Game * > getAllGames ()
 Get all games in the library.
std::vector< const Game * > getAllGames () const
 Get all games in the library (const version).
std::vector< std::string > getCollectionNames () const
 Get list of all collection names.
std::vector< Game * > getFavorites ()
 Get all favorite games.
std::vector< const Game * > getFavorites () const
 Get all favorite games (const version).
std::optional< Game * > getGame (const std::string &gameId)
 Get a game by ID.
std::optional< const Game * > getGame (const std::string &gameId) const
 Get a game by ID (const version).
std::vector< Game * > getGamesByPlatform (GamePlatform platform)
 Get games filtered by platform.
std::vector< const Game * > getGamesByPlatform (GamePlatform platform) const
 Get games filtered by platform (const version).
std::vector< Game * > getGamesInCollection (const std::string &collectionName)
 Get games in a specific collection.
std::vector< const Game * > getGamesInCollection (const std::string &collectionName) const
 Get games in a specific collection (const version).
int getInstalledGameCount () const
 Get number of installed games.
std::vector< Game * > getInstalledGames ()
 Get all installed games.
std::vector< const Game * > getInstalledGames () const
 Get all installed games (const version).
std::vector< Game * > getRecentlyPlayed (int count=10)
 Get recently played games.
std::vector< const Game * > getRecentlyPlayed (int count=10) const
 Get recently played games (const version).
std::vector< PlaySession > getRecentSessions (const std::string &gameId, int limit=20) const
 Get recent play sessions for a game from the database.
int getTotalGameCount () const
 Get total number of games in library.
double getTotalPlayTime () const
 Get total play time across all games.
void loadFromDisk ()
 Load library from disk.
int refreshStaleImages ()
 Refresh images for games with stale/missing image files.
bool removeGame (const std::string &gameId)
 Remove a game from the library.
void removeGameFromCollection (const std::string &gameId, const std::string &collection)
 Remove a game from a collection.
void saveToDisk ()
 Save library to disk.
std::vector< Game * > searchGames (std::string_view query)
 Search games by name (case-insensitive substring match).
std::vector< const Game * > searchGames (std::string_view query) const
 Search games by name (const version).
bool updateGame (const std::string &gameId, const Game &updatedGame)
 Update an existing game.

Detailed Description

Central database for managing all games in the launcher.

The GameLibrary is responsible for persistent storage and retrieval of all game data. It provides methods for CRUD operations on games, as well as powerful query capabilities for filtering and sorting.

Thread Safety

This class is NOT thread-safe. External synchronization is required if accessed from multiple threads.

Definition at line 57 of file game_library.h.

Constructor & Destructor Documentation

◆ GameLibrary()

GameLibrary::GameLibrary ( )

Constructor - initializes library path.

Determines the platform-specific library storage location:

  • Linux: ~/.local/share/gamelauncher/
  • Windows: APPDATA%/gamelauncher/

Definition at line 27 of file game_library.cpp.

References Logger::error().

◆ ~GameLibrary()

GameLibrary::~GameLibrary ( )
default

Destructor - cleanup.

Note: The destructor does NOT automatically save to disk. Call saveToDisk() explicitly before destruction if needed.

Member Function Documentation

◆ addGame()

std::string GameLibrary::addGame ( const Game & game)

Add a new game to the library.

Parameters
gameGame object to add (ID will be generated if empty)
Returns
The ID assigned to the game

If the game doesn't have an ID, a unique one will be generated. The game is added to the in-memory library but NOT saved to disk until saveToDisk() is called.

Definition at line 120 of file game_library.cpp.

References GameStatistics::dateAdded, Logger::error(), GameRunnerConfig::executablePath, Game::id, Logger::info(), Game::metadata, Game::name, Game::runnerConfig, Game::source, Game::statistics, GameMetadata::steamAppId, and Logger::warn().

◆ addGameToCollection()

void GameLibrary::addGameToCollection ( const std::string & gameId,
const std::string & collection )

Add a game to a collection.

Parameters
gameIdID of game to add
collectionName of collection

If the game is already in the collection, this is a no-op. If the game doesn't exist, prints an error to stderr.

Definition at line 931 of file game_library.cpp.

References Game::collections, Logger::error(), getGame(), and Logger::info().

◆ createCollection()

void GameLibrary::createCollection ( const std::string & name)

Create a new empty collection.

Parameters
nameName of collection to create

Collections are user-defined groups for organizing games. If a collection with this name already exists, this is a no-op.

Definition at line 888 of file game_library.cpp.

References Logger::info().

◆ deleteCollection()

void GameLibrary::deleteCollection ( const std::string & name)

Delete a collection.

Parameters
nameName of collection to delete

Removes the collection from the list AND removes all games from this collection. The games themselves are not deleted, only the collection membership.

Definition at line 903 of file game_library.cpp.

References Logger::info().

◆ discoverImagesForGame()

bool GameLibrary::discoverImagesForGame ( const std::string & gameId)

Discover images for a specific game.

Parameters
gameIdID of game to discover images for
Returns
True if images were found and updated

Definition at line 735 of file game_library.cpp.

References Logger::debug(), SteamCDN::discoverImages(), Logger::error(), SteamPaths::getPrimarySteamRoot(), GameImagePaths::header, GameImagePaths::heroImage, GameMetadata::imagePaths, Game::metadata, Game::name, GameMetadata::steamAppId, and GameImagePaths::verticalCover.

◆ discoverMissingImages()

int GameLibrary::discoverMissingImages ( )

Discover and update images for games missing cover art.

Returns
Number of games that had images discovered

Scans Steam's library cache for images and updates the database. Should be called in a background thread.

Definition at line 641 of file game_library.cpp.

References SteamPaths::getPrimarySteamRoot(), and Logger::info().

◆ getAllGames() [1/2]

std::vector< Game * > GameLibrary::getAllGames ( )

Get all games in the library.

Returns
Vector of pointers to all games

The returned pointers remain valid until games are removed or the library is destroyed. No particular order is guaranteed.

Definition at line 332 of file game_library.cpp.

◆ getAllGames() [2/2]

std::vector< const Game * > GameLibrary::getAllGames ( ) const

Get all games in the library (const version).

Returns
Vector of const pointers to all games

Definition at line 346 of file game_library.cpp.

◆ getCollectionNames()

std::vector< std::string > GameLibrary::getCollectionNames ( ) const

Get list of all collection names.

Returns
Vector of collection names (may be empty)

Definition at line 926 of file game_library.cpp.

◆ getFavorites() [1/2]

std::vector< Game * > GameLibrary::getFavorites ( )

Get all favorite games.

Returns
Vector of pointers to favorite games

Definition at line 386 of file game_library.cpp.

◆ getFavorites() [2/2]

std::vector< const Game * > GameLibrary::getFavorites ( ) const

Get all favorite games (const version).

Returns
Vector of const pointers to favorite games

Definition at line 399 of file game_library.cpp.

◆ getGame() [1/2]

std::optional< Game * > GameLibrary::getGame ( const std::string & gameId)

Get a game by ID.

Parameters
gameIdUnique ID of game to retrieve
Returns
Optional containing pointer to game if found, nullopt otherwise

Returns a pointer to the game in the library. The pointer remains valid until the game is removed or the library is destroyed.

Definition at line 306 of file game_library.cpp.

Referenced by addGameToCollection(), and removeGameFromCollection().

◆ getGame() [2/2]

std::optional< const Game * > GameLibrary::getGame ( const std::string & gameId) const

Get a game by ID (const version).

Parameters
gameIdUnique ID of game to retrieve
Returns
Optional containing const pointer to game if found, nullopt otherwise

Definition at line 317 of file game_library.cpp.

◆ getGamesByPlatform() [1/2]

std::vector< Game * > GameLibrary::getGamesByPlatform ( GamePlatform platform)

Get games filtered by platform.

Parameters
platformPlatform to filter by
Returns
Vector of pointers to games matching the platform

Definition at line 360 of file game_library.cpp.

◆ getGamesByPlatform() [2/2]

std::vector< const Game * > GameLibrary::getGamesByPlatform ( GamePlatform platform) const

Get games filtered by platform (const version).

Parameters
platformPlatform to filter by
Returns
Vector of const pointers to games matching the platform

Definition at line 373 of file game_library.cpp.

◆ getGamesInCollection() [1/2]

std::vector< Game * > GameLibrary::getGamesInCollection ( const std::string & collectionName)

Get games in a specific collection.

Parameters
collectionNameName of collection to query
Returns
Vector of pointers to games in the collection

Definition at line 575 of file game_library.cpp.

◆ getGamesInCollection() [2/2]

std::vector< const Game * > GameLibrary::getGamesInCollection ( const std::string & collectionName) const

Get games in a specific collection (const version).

Parameters
collectionNameName of collection to query
Returns
Vector of const pointers to games in the collection

Definition at line 590 of file game_library.cpp.

◆ getInstalledGameCount()

int GameLibrary::getInstalledGameCount ( ) const

Get number of installed games.

Returns
Count of games with status Installed or Running

Definition at line 614 of file game_library.cpp.

References Installed, and Running.

◆ getInstalledGames() [1/2]

std::vector< Game * > GameLibrary::getInstalledGames ( )

Get all installed games.

Returns
Vector of pointers to games with status Installed or Running

Definition at line 493 of file game_library.cpp.

References Installed, and Running.

◆ getInstalledGames() [2/2]

std::vector< const Game * > GameLibrary::getInstalledGames ( ) const

Get all installed games (const version).

Returns
Vector of const pointers to installed games

Definition at line 506 of file game_library.cpp.

References Installed, and Running.

◆ getRecentlyPlayed() [1/2]

std::vector< Game * > GameLibrary::getRecentlyPlayed ( int count = 10)

Get recently played games.

Parameters
countMaximum number of games to return (default: 10)
Returns
Vector of pointers to recently played games, sorted by last played time (most recent first)

Definition at line 412 of file game_library.cpp.

◆ getRecentlyPlayed() [2/2]

std::vector< const Game * > GameLibrary::getRecentlyPlayed ( int count = 10) const

Get recently played games (const version).

Parameters
countMaximum number of games to return (default: 10)
Returns
Vector of const pointers to recently played games

Definition at line 448 of file game_library.cpp.

◆ getRecentSessions()

std::vector< PlaySession > GameLibrary::getRecentSessions ( const std::string & gameId,
int limit = 20 ) const

Get recent play sessions for a game from the database.

Parameters
gameIdGame ID
limitMaximum sessions to return
Returns
Vector of PlaySession records, most recent first

Definition at line 484 of file game_library.cpp.

◆ getTotalGameCount()

int GameLibrary::getTotalGameCount ( ) const

Get total number of games in library.

Returns
Total game count (includes all statuses)

Definition at line 609 of file game_library.cpp.

◆ getTotalPlayTime()

double GameLibrary::getTotalPlayTime ( ) const

Get total play time across all games.

Returns
Total play time in seconds (sum of all game play times)

Definition at line 627 of file game_library.cpp.

◆ loadFromDisk()

void GameLibrary::loadFromDisk ( )

Load library from disk.

Reads all .game files from the library directory and loads them into memory. Also loads the collections list from collections.txt. Creates the library directory if it doesn't exist.

Note
This clears any games currently in memory before loading.

Definition at line 49 of file game_library.cpp.

References Logger::info().

◆ refreshStaleImages()

int GameLibrary::refreshStaleImages ( )

Refresh images for games with stale/missing image files.

Returns
Number of games that had images refreshed

Checks if stored image paths still exist on disk. For any that don't, attempts to re-discover images from Steam's cache. This handles cases where Steam has updated its cache structure or cleared old images. Should be called in a background thread.

Definition at line 775 of file game_library.cpp.

References SteamCDN::discoverImages(), SteamPaths::getPrimarySteamRoot(), GameImagePaths::header, GameImagePaths::heroImage, Logger::info(), and GameImagePaths::verticalCover.

◆ removeGame()

bool GameLibrary::removeGame ( const std::string & gameId)

Remove a game from the library.

Parameters
gameIdUnique ID of game to remove
Returns
True if game was found and removed, false otherwise

Removes the game from memory AND deletes its .game file from disk.

Definition at line 245 of file game_library.cpp.

References Logger::error(), and Logger::info().

◆ removeGameFromCollection()

void GameLibrary::removeGameFromCollection ( const std::string & gameId,
const std::string & collection )

Remove a game from a collection.

Parameters
gameIdID of game to remove
collectionName of collection

If the game isn't in the collection, this is a no-op. If the game doesn't exist, prints an error to stderr.

Definition at line 955 of file game_library.cpp.

References Game::collections, Logger::error(), getGame(), and Logger::info().

◆ saveToDisk()

void GameLibrary::saveToDisk ( )

Save library to disk.

Writes all games to individual .game files in the library directory. Also saves the collections list to collections.txt. Creates the library directory if it doesn't exist.

Definition at line 72 of file game_library.cpp.

References Logger::error(), and Logger::info().

◆ searchGames() [1/2]

std::vector< Game * > GameLibrary::searchGames ( std::string_view query)

Search games by name (case-insensitive substring match).

Parameters
querySearch query string
Returns
Vector of pointers to games whose name contains the query

Performance: O(n) where n is total number of games. Uses std::string_view for efficient string handling.

Definition at line 519 of file game_library.cpp.

◆ searchGames() [2/2]

std::vector< const Game * > GameLibrary::searchGames ( std::string_view query) const

Search games by name (const version).

Parameters
querySearch query string
Returns
Vector of const pointers to matching games

Definition at line 547 of file game_library.cpp.

◆ updateGame()

bool GameLibrary::updateGame ( const std::string & gameId,
const Game & updatedGame )

Update an existing game.

Parameters
gameIdID of game to update
updatedGameNew game data (ID will be preserved)
Returns
True if game was found and updated, false otherwise

Updates the game in memory but does NOT save to disk until saveToDisk() is called.

Definition at line 277 of file game_library.cpp.

References Logger::error(), Game::id, and Logger::info().


The documentation for this class was generated from the following files: