diff options
Diffstat (limited to 'src/Core/GameInfo')
| -rw-r--r-- | src/Core/GameInfo/gameinfostructure.h | 24 | ||||
| -rw-r--r-- | src/Core/GameInfo/loadsupportedgames.cpp | 97 | ||||
| -rw-r--r-- | src/Core/GameInfo/loadsupportedgames.h | 16 |
3 files changed, 137 insertions, 0 deletions
diff --git a/src/Core/GameInfo/gameinfostructure.h b/src/Core/GameInfo/gameinfostructure.h new file mode 100644 index 0000000..b73e9ef --- /dev/null +++ b/src/Core/GameInfo/gameinfostructure.h @@ -0,0 +1,24 @@ +#ifndef STRUCTGAMEINFO_H +#define STRUCTGAMEINFO_H + +#include <QString> + +struct GameInfo +{ + // gamename is the unique identifier + QString gamename; + + // 6-byte GameSpy identifier + QString cipher; + + // game label + QString label; + + // default port + unsigned short port = 0; + + // known protocol + QString protocol; +}; + +#endif // STRUCTGAMEINFO_H diff --git a/src/Core/GameInfo/loadsupportedgames.cpp b/src/Core/GameInfo/loadsupportedgames.cpp new file mode 100644 index 0000000..9a73947 --- /dev/null +++ b/src/Core/GameInfo/loadsupportedgames.cpp @@ -0,0 +1,97 @@ +#include "loadsupportedgames.h" + +QHash<QString, GameInfo> loadSupportedGames (const QString &applicationPath) +{ + // return hash + QHash<QString, GameInfo> supportedGames; + + // supported games file path (following README structure) + const QString supportPath = applicationPath + "/" + _supportedPath; + + // determine if file exists + if ( ! QFile(supportPath).exists() ) + { + // no game info file exists + logPrimitive() << "No games file found at " << supportPath << endl + << "Please provide the correct file . " << endl; + return supportedGames; + } + + // load config file + QFile supportedFile(supportPath); + if ( ! supportedFile.open(QIODevice::ReadOnly) ) + { + // error occurred. report and quit. + logPrimitive() << "Unable to open the game file. Is the file open or in use?" << endl; + return supportedGames; + } + + // stats + int total = 0; + + // load as json object + QJsonDocument supportedJsonData(QJsonDocument::fromJson(supportedFile.readAll())); + + /* Optimisation for web interface: add games to the database. + * + * If database/json data do not match, replace database table + * with json data. This should only occur during database creation + * or when manual changes were made to the json file (which is rare). + */ + bool dbInsert = supportedJsonData.array().count() != getNumGames(0).value("numTotal", 0); + QSqlQuery q; + if (dbInsert) + { + // no game info file exists + logPrimitive() << "Game info mismatch in database. Reloading json data. (" + << supportedJsonData.array().count() << "/" + << getNumGames(0).value("numTotal", 0) << ")" << endl; + + // void existing data + q.prepare("DELETE FROM gameinfo"); + if ( ! q.exec() ) + reportQuery(q); + } + + for (int i = 0; i < supportedJsonData.array().count(); i++) + { + // get the game object + QJsonObject thisGame = supportedJsonData.array().at(i).toObject(); + + GameInfo gameInfo; + gameInfo.gamename = thisGame.value("gamename").toString(""); + gameInfo.cipher = thisGame.value("cipher").toString(""); + gameInfo.port = static_cast<unsigned short>(thisGame.value("port").toDouble(0)); + gameInfo.label = thisGame.value("label").toString(""); + + // insert in db if needed + if (dbInsert) + { + QString insertString = "INSERT INTO gameinfo (gamename, label) " + "VALUES (:gamename, :label)"; + + q.prepare(insertString); + q.bindValue(":gamename", gameInfo.gamename ); + q.bindValue(":label", gameInfo.label); + + if ( ! q.exec() ) + reportQuery(q); + } + + // add to list + supportedGames.insert(gameInfo.gamename, gameInfo); + total++; + } + + // no games found? report this! + if (total <= 0) + { + // no game info exists + logPrimitive() << "No game info found in file at " << supportPath << endl + << "Please provide the correct file . " << endl; + return supportedGames; + } + + // all games parsed. done. + return supportedGames; +} diff --git a/src/Core/GameInfo/loadsupportedgames.h b/src/Core/GameInfo/loadsupportedgames.h new file mode 100644 index 0000000..333dd0b --- /dev/null +++ b/src/Core/GameInfo/loadsupportedgames.h @@ -0,0 +1,16 @@ +#ifndef LOADSUPPORTEDGAMES_H +#define LOADSUPPORTEDGAMES_H + +#include <QFile> +#include <QJsonObject> +#include <QJsonArray> +#include <QJsonDocument> +#include "Database/Common/commonactions.h" +#include "gameinfostructure.h" + +// load supported games from json file + +const QString _supportedPath = "../data/SupportedGames.json"; +QHash<QString, GameInfo> loadSupportedGames (const QString &applicationPath); + +#endif // LOADSUPPORTEDGAMES_H |
