summaryrefslogtreecommitdiffstats
path: root/logic/minecraft/JarMod.cpp
blob: 18a9411c1663090475534f720173bd52d280647a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "JarMod.h"
#include "logic/MMCJson.h"
using namespace MMCJson;

JarmodPtr Jarmod::fromJson(const QJsonObject &libObj, const QString &filename)
{
	JarmodPtr out(new Jarmod());
	if (!libObj.contains("name"))
	{
		throw JSONValidationError(filename +
								  "contains a jarmod that doesn't have a 'name' field");
	}
	out->name = libObj.value("name").toString();

	auto readString = [libObj, filename](const QString & key, QString & variable)
	{
		if (libObj.contains(key))
		{
			QJsonValue val = libObj.value(key);
			if (!val.isString())
			{
				QLOG_WARN() << key << "is not a string in" << filename << "(skipping)";
			}
			else
			{
				variable = val.toString();
			}
		}
	};

	readString("url", out->baseurl);
	readString("MMC-hint", out->hint);
	readString("MMC-absoluteUrl", out->absoluteUrl);
	if(!out->baseurl.isEmpty() && out->absoluteUrl.isEmpty())
	{
		out->absoluteUrl = out->baseurl + out->name;
	}
	return out;
}

QJsonObject Jarmod::toJson()
{
	QJsonObject out;
	writeString(out, "name", name);
	writeString(out, "url", baseurl);
	writeString(out, "MMC-absoluteUrl", absoluteUrl);
	writeString(out, "MMC-hint", hint);
	return out;
}

QString Jarmod::url()
{
	if(!absoluteUrl.isEmpty())
		return absoluteUrl;
	else return baseurl + name;
}