Skip to content

Commit

Permalink
Merge pull request mrkite#185 from cre4ture/master
Browse files Browse the repository at this point in the history
stability improvements
  • Loading branch information
mrkite authored Sep 15, 2019
2 parents 830f0ff + 4a6d38a commit fea8541
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 29 deletions.
24 changes: 10 additions & 14 deletions definitionmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ void DefinitionManager::installJson(QString path, bool overwrite,
QString destdir = QStandardPaths::writableLocation(
QStandardPaths::DataLocation);

JSONData *def;
std::unique_ptr<JSONData> def;
QFile f(path);
f.open(QIODevice::ReadOnly);
try {
Expand All @@ -257,7 +257,7 @@ void DefinitionManager::installJson(QString path, bool overwrite,

QString key = def->at("name")->asString() + def->at("type")->asString();
QString exeversion = def->at("version")->asString();
delete def;

QString dest = destdir + "/" + QString("%1").arg(qHash(key,42)) + ".json";

// check if build in version is newer than version on disk
Expand All @@ -272,7 +272,7 @@ void DefinitionManager::installJson(QString path, bool overwrite,
return;
}
QString fileversion = def->at("version")->asString();
delete def;

if (exeversion.compare(fileversion, Qt::CaseInsensitive) > 0) {
// force overwriting outdated local copy
QFile::remove(dest);
Expand Down Expand Up @@ -313,7 +313,7 @@ void DefinitionManager::installZip(QString path, bool overwrite,
return;
}
// fetch the pack info
JSONData *info;
std::unique_ptr<JSONData> info;
try {
info = JSON::parse(zip.get("pack_info.json"));
} catch (JSONParseException e) {
Expand All @@ -325,23 +325,20 @@ void DefinitionManager::installZip(QString path, bool overwrite,
}
// let's verify all the jsons in the pack
for (int i = 0; i < info->at("data")->length(); i++) {
JSONData *def;
std::unique_ptr<JSONData> def;
try {
def = JSON::parse(zip.get(info->at("data")->at(i)->asString()));
delete def;
} catch (JSONParseException e) {
QMessageBox::warning(this, tr("Couldn't install %1").arg(path),
tr("%1: %2")
.arg(info->at("data")->at(i)->asString(),
e.reason), QMessageBox::Cancel);
delete info;
zip.close();
return;
}
}

QString key = info->at("name")->asString() + info->at("type")->asString();
delete info;
QString dest = destdir + "/" + QString("%1").arg(qHash(key,42)) + ".zip";
if (!QFile::exists(dest) || overwrite) {
if (QFile::exists(dest) && install)
Expand Down Expand Up @@ -409,7 +406,7 @@ QSize DefinitionManager::sizeHint() const {
void DefinitionManager::loadDefinition(QString path) {
// determine if we're loading a single json or a pack
if (path.endsWith(".json", Qt::CaseInsensitive)) {
JSONData *def;
std::unique_ptr<JSONData> def;
QFile f(path);
if (!f.open(QIODevice::ReadOnly)) return;
try {
Expand Down Expand Up @@ -447,14 +444,15 @@ void DefinitionManager::loadDefinition(QString path) {
d.id = entityManager.addDefinitions(
dynamic_cast<JSONArray*>(def->at("data")));
d.type = Definition::Entity;
} else {
return; // unknown type
}
definitions.insert(path, d);
delete def;
} else {
ZipReader zip(path);
if (!zip.open())
return;
JSONData *info;
std::unique_ptr<JSONData> info;
try {
info = JSON::parse(zip.get("pack_info.json"));
} catch (JSONParseException e) {
Expand All @@ -475,7 +473,7 @@ void DefinitionManager::loadDefinition(QString path) {
d.entityid = -1;
QString key = d.name+"pack";
for (int i = 0; i < info->at("data")->length(); i++) {
JSONData *def;
std::unique_ptr<JSONData> def;
try {
def = JSON::parse(zip.get(info->at("data")->at(i)->asString()));
} catch (JSONParseException e) {
Expand All @@ -498,10 +496,8 @@ void DefinitionManager::loadDefinition(QString path) {
d.entityid = entityManager.addDefinitions(
dynamic_cast<JSONArray*>(def->at("data")), d.entityid);
}
delete def;
}
definitions.insert(path, d);
delete info;
zip.close();
}
}
Expand Down
29 changes: 16 additions & 13 deletions json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,18 @@ class JSONHelper {
QString location() {
int line = 1;
int col = 0;
int cpos = pos;
bool doneCol = false;
while (cpos >= 0) {
if (data.at(cpos) == '\n') {
doneCol = true;
line++;
}
if (!doneCol) col++;
cpos--;
if (len > 0)
{
int cpos = pos;
bool doneCol = false;
while (cpos >= 0) {
if (data.at(cpos) == '\n') {
doneCol = true;
line++;
}
if (!doneCol) col++;
cpos--;
}
}
return QString("Line: %1, Offset: %2").arg(line).arg(col);
}
Expand All @@ -185,20 +188,20 @@ class JSONHelper {
QString data;
};

JSONData *JSON::parse(const QString data) {
std::unique_ptr<JSONData> JSON::parse(const QString data) {
JSONHelper reader(data);
Token type = reader.nextToken();
switch (type) {
case TokenObject: // hash
return new JSONObject(reader);
return std::make_unique<JSONObject>(reader);
case TokenArray: // array
return new JSONArray(reader);
return std::make_unique<JSONArray>(reader);
default:
throw JSONParseException("Doesn't start with object or array",
reader.location());
break;
}
return NULL;
return nullptr;
}
static JSONData Null;
JSONData::JSONData() {
Expand Down
3 changes: 2 additions & 1 deletion json.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <QHash>
#include <QList>
#include <memory>

class JSONHelper;

Expand Down Expand Up @@ -72,7 +73,7 @@ class JSONParseException {

class JSON {
public:
static JSONData *parse(const QString data);
static std::unique_ptr<JSONData> parse(const QString data);
};

#endif // JSON_H_
2 changes: 1 addition & 1 deletion minutor.pro
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ TEMPLATE = app
TARGET = minutor
DEPENDPATH += .
INCLUDEPATH += .
CONFIG += c++11
CONFIG += c++14
QT += widgets network
QMAKE_INFO_PLIST = minutor.plist
unix:LIBS += -lz
Expand Down

0 comments on commit fea8541

Please sign in to comment.