mirror of
https://github.com/vcmi/vcmi.git
synced 2025-10-08 23:22:25 +02:00
bigfixing & new file: launcher/jsonutils.cpp
- launcher uses json parser from vcmi lib instead of one from Qt #1469 - fixed abilities overrides for some creatures #1476 - fixed hero portraits in seer huts #1402 - ttf fonts will render text in utf-8 mode. Not really useful at this point - new settings entry, available in launcher: encoding. Unused for now.
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
#include "StdInc.h"
|
||||
#include "cmodlist.h"
|
||||
|
||||
#include "../../lib/JsonNode.h"
|
||||
#include "../../lib/filesystem/CFileInputStream.h"
|
||||
|
||||
bool CModEntry::compareVersions(QString lesser, QString greater)
|
||||
{
|
||||
static const int maxSections = 3; // versions consist from up to 3 sections, major.minor.patch
|
||||
@@ -25,7 +28,7 @@ bool CModEntry::compareVersions(QString lesser, QString greater)
|
||||
return false;
|
||||
}
|
||||
|
||||
CModEntry::CModEntry(QJsonObject repository, QJsonObject localData, QJsonValue modSettings, QString modname):
|
||||
CModEntry::CModEntry(QVariantMap repository, QVariantMap localData, QVariant modSettings, QString modname):
|
||||
repository(repository),
|
||||
localData(localData),
|
||||
modSettings(modSettings),
|
||||
@@ -38,7 +41,7 @@ bool CModEntry::isEnabled() const
|
||||
if (!isInstalled())
|
||||
return false;
|
||||
|
||||
return modSettings.toBool(false);
|
||||
return modSettings.toBool();
|
||||
}
|
||||
|
||||
bool CModEntry::isDisabled() const
|
||||
@@ -89,24 +92,24 @@ QString CModEntry::getName() const
|
||||
QVariant CModEntry::getValue(QString value) const
|
||||
{
|
||||
if (repository.contains(value))
|
||||
return repository[value].toVariant();
|
||||
return repository[value];
|
||||
|
||||
if (localData.contains(value))
|
||||
return localData[value].toVariant();
|
||||
return localData[value];
|
||||
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
QJsonObject CModList::copyField(QJsonObject data, QString from, QString to)
|
||||
QVariantMap CModList::copyField(QVariantMap data, QString from, QString to)
|
||||
{
|
||||
QJsonObject renamed;
|
||||
QVariantMap renamed;
|
||||
|
||||
for (auto it = data.begin(); it != data.end(); it++)
|
||||
{
|
||||
QJsonObject object = it.value().toObject();
|
||||
QVariant object = it.value();
|
||||
|
||||
object.insert(to, object.value(from));
|
||||
renamed.insert(it.key(), QJsonValue(object));
|
||||
object.toMap().insert(to, object.toMap().value(from));
|
||||
renamed.insert(it.key(), object.toMap());
|
||||
}
|
||||
return renamed;
|
||||
}
|
||||
@@ -116,40 +119,40 @@ void CModList::resetRepositories()
|
||||
repositories.clear();
|
||||
}
|
||||
|
||||
void CModList::addRepository(QJsonObject data)
|
||||
void CModList::addRepository(QVariantMap data)
|
||||
{
|
||||
repositories.push_back(copyField(data, "version", "latestVersion"));
|
||||
}
|
||||
|
||||
void CModList::setLocalModList(QJsonObject data)
|
||||
void CModList::setLocalModList(QVariantMap data)
|
||||
{
|
||||
localModList = copyField(data, "version", "installedVersion");
|
||||
}
|
||||
|
||||
void CModList::setModSettings(QJsonObject data)
|
||||
void CModList::setModSettings(QVariant data)
|
||||
{
|
||||
modSettings = data;
|
||||
modSettings = data.toMap();
|
||||
}
|
||||
|
||||
CModEntry CModList::getMod(QString modname) const
|
||||
{
|
||||
assert(hasMod(modname));
|
||||
|
||||
QJsonObject repo;
|
||||
QJsonObject local = localModList[modname].toObject();
|
||||
QJsonValue settings = modSettings[modname];
|
||||
QVariantMap repo;
|
||||
QVariantMap local = localModList[modname].toMap();
|
||||
QVariant settings = modSettings[modname];
|
||||
|
||||
for (auto entry : repositories)
|
||||
{
|
||||
if (entry.contains(modname))
|
||||
{
|
||||
if (repo.empty())
|
||||
repo = entry[modname].toObject();
|
||||
repo = entry[modname].toMap();
|
||||
else
|
||||
{
|
||||
if (CModEntry::compareVersions(repo["version"].toString(),
|
||||
entry[modname].toObject()["version"].toString()))
|
||||
repo = entry[modname].toObject();
|
||||
entry[modname].toMap()["version"].toString()))
|
||||
repo = entry[modname].toMap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,9 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QVariantMap>
|
||||
#include <QVariant>
|
||||
|
||||
class JsonNode;
|
||||
|
||||
namespace ModStatus
|
||||
{
|
||||
enum EModStatus
|
||||
@@ -19,13 +20,13 @@ namespace ModStatus
|
||||
class CModEntry
|
||||
{
|
||||
// repository contains newest version only (if multiple are available)
|
||||
QJsonObject repository;
|
||||
QJsonObject localData;
|
||||
QJsonValue modSettings;
|
||||
QVariantMap repository;
|
||||
QVariantMap localData;
|
||||
QVariant modSettings;
|
||||
|
||||
QString modname;
|
||||
public:
|
||||
CModEntry(QJsonObject repository, QJsonObject localData, QJsonValue modSettings, QString modname);
|
||||
CModEntry(QVariantMap repository, QVariantMap localData, QVariant modSettings, QString modname);
|
||||
|
||||
// installed and enabled
|
||||
bool isEnabled() const;
|
||||
@@ -52,16 +53,16 @@ public:
|
||||
|
||||
class CModList
|
||||
{
|
||||
QVector<QJsonObject> repositories;
|
||||
QJsonObject localModList;
|
||||
QJsonObject modSettings;
|
||||
QVector<QVariantMap> repositories;
|
||||
QVariantMap localModList;
|
||||
QVariantMap modSettings;
|
||||
|
||||
QJsonObject copyField(QJsonObject data, QString from, QString to);
|
||||
QVariantMap copyField(QVariantMap data, QString from, QString to);
|
||||
public:
|
||||
virtual void resetRepositories();
|
||||
virtual void addRepository(QJsonObject data);
|
||||
virtual void setLocalModList(QJsonObject data);
|
||||
virtual void setModSettings(QJsonObject data);
|
||||
virtual void addRepository(QVariantMap data);
|
||||
virtual void setLocalModList(QVariantMap data);
|
||||
virtual void setModSettings(QVariant data);
|
||||
|
||||
// returns mod by name. Note: mod MUST exist
|
||||
CModEntry getMod(QString modname) const;
|
||||
|
@@ -117,21 +117,21 @@ void CModListModel::resetRepositories()
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
void CModListModel::addRepository(QJsonObject data)
|
||||
void CModListModel::addRepository(QVariantMap data)
|
||||
{
|
||||
beginResetModel();
|
||||
CModList::addRepository(data);
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
void CModListModel::setLocalModList(QJsonObject data)
|
||||
void CModListModel::setLocalModList(QVariantMap data)
|
||||
{
|
||||
beginResetModel();
|
||||
CModList::setLocalModList(data);
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
void CModListModel::setModSettings(QJsonObject data)
|
||||
void CModListModel::setModSettings(QVariant data)
|
||||
{
|
||||
beginResetModel();
|
||||
CModList::setModSettings(data);
|
||||
|
@@ -30,9 +30,9 @@ class CModListModel : public QAbstractTableModel, public CModList
|
||||
public:
|
||||
/// CModListContainer overrides
|
||||
void resetRepositories();
|
||||
void addRepository(QJsonObject data);
|
||||
void setLocalModList(QJsonObject data);
|
||||
void setModSettings(QJsonObject data);
|
||||
void addRepository(QVariantMap data);
|
||||
void setLocalModList(QVariantMap data);
|
||||
void setModSettings(QVariant data);
|
||||
|
||||
QString modIndexToName(int index) const;
|
||||
|
||||
|
@@ -5,23 +5,9 @@
|
||||
#include "../../lib/filesystem/Filesystem.h"
|
||||
#include "../../lib/filesystem/CZipLoader.h"
|
||||
|
||||
#include "../jsonutils.h"
|
||||
#include "../launcherdirs.h"
|
||||
|
||||
static QJsonObject JsonFromFile(QString filename)
|
||||
{
|
||||
QFile file(filename);
|
||||
file.open(QFile::ReadOnly);
|
||||
|
||||
return QJsonDocument::fromJson(file.readAll()).object();
|
||||
}
|
||||
|
||||
static void JsonToFile(QString filename, QJsonObject object)
|
||||
{
|
||||
QFile file(filename);
|
||||
file.open(QFile::WriteOnly);
|
||||
file.write(QJsonDocument(object).toJson());
|
||||
}
|
||||
|
||||
static QString detectModArchive(QString path, QString modName)
|
||||
{
|
||||
auto files = ZipArchive::listFiles(path.toUtf8().data());
|
||||
@@ -57,8 +43,8 @@ QString CModManager::settingsPath()
|
||||
|
||||
void CModManager::loadModSettings()
|
||||
{
|
||||
modSettings = JsonFromFile(settingsPath());
|
||||
modList->setModSettings(modSettings["activeMods"].toObject());
|
||||
modSettings = JsonUtils::JsonFromFile(settingsPath()).toMap();
|
||||
modList->setModSettings(modSettings["activeMods"]);
|
||||
}
|
||||
|
||||
void CModManager::resetRepositories()
|
||||
@@ -68,7 +54,7 @@ void CModManager::resetRepositories()
|
||||
|
||||
void CModManager::loadRepository(QString file)
|
||||
{
|
||||
modList->addRepository(JsonFromFile(file));
|
||||
modList->addRepository(JsonUtils::JsonFromFile(file).toMap());
|
||||
}
|
||||
|
||||
void CModManager::loadMods()
|
||||
@@ -78,17 +64,9 @@ void CModManager::loadMods()
|
||||
for (auto modname : installedMods)
|
||||
{
|
||||
ResourceID resID("Mods/" + modname + "/mod.json");
|
||||
|
||||
if (CResourceHandler::get()->existsResource(resID))
|
||||
{
|
||||
auto data = CResourceHandler::get()->load(resID)->readAll();
|
||||
auto array = QByteArray(reinterpret_cast<char *>(data.first.get()), data.second);
|
||||
|
||||
auto mod = QJsonDocument::fromJson(array);
|
||||
assert (mod.isObject()); // TODO: use JsonNode from vcmi code here - QJsonNode parser is just too pedantic
|
||||
|
||||
localMods.insert(QString::fromUtf8(modname.c_str()).toLower(), QJsonValue(mod.object()));
|
||||
}
|
||||
std::string name = *CResourceHandler::get()->getResourceName(resID);
|
||||
auto mod = JsonUtils::JsonFromFile(QString::fromUtf8(name.c_str()));
|
||||
localMods.insert(QString::fromUtf8(modname.c_str()).toLower(), mod);
|
||||
}
|
||||
modList->setLocalModList(localMods);
|
||||
}
|
||||
@@ -209,15 +187,15 @@ bool CModManager::canDisableMod(QString modname)
|
||||
|
||||
bool CModManager::doEnableMod(QString mod, bool on)
|
||||
{
|
||||
QJsonValue value(on);
|
||||
QJsonObject list = modSettings["activeMods"].toObject();
|
||||
QVariant value(on);
|
||||
QVariantMap list = modSettings["activeMods"].toMap();
|
||||
|
||||
list.insert(mod, value);
|
||||
modSettings.insert("activeMods", list);
|
||||
|
||||
modList->setModSettings(modSettings["activeMods"].toObject());
|
||||
modList->setModSettings(modSettings["activeMods"]);
|
||||
|
||||
JsonToFile(settingsPath(), modSettings);
|
||||
JsonUtils::JsonToFile(settingsPath(), modSettings);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -245,7 +223,7 @@ bool CModManager::doInstallMod(QString modname, QString archivePath)
|
||||
return addError(modname, "Failed to extract mod data");
|
||||
}
|
||||
|
||||
QJsonObject json = JsonFromFile(destDir + modDirName + "/mod.json");
|
||||
QVariantMap json = JsonUtils::JsonFromFile(destDir + modDirName + "/mod.json").toMap();
|
||||
|
||||
localMods.insert(modname, json);
|
||||
modList->setLocalModList(localMods);
|
||||
|
@@ -13,8 +13,8 @@ class CModManager
|
||||
bool doInstallMod(QString mod, QString archivePath);
|
||||
bool doUninstallMod(QString mod);
|
||||
|
||||
QJsonObject modSettings;
|
||||
QJsonObject localMods;
|
||||
QVariantMap modSettings;
|
||||
QVariantMap localMods;
|
||||
|
||||
QStringList recentErrors;
|
||||
bool addError(QString modname, QString message);
|
||||
|
Reference in New Issue
Block a user