summaryrefslogtreecommitdiffstats
path: root/libmultimc/src/fullversionfactory.cpp
blob: 8873ee2d3bebe7dbf156eba4b8adff0c2e6eebb9 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "fullversionfactory.h"
#include "fullversion.h"

QSharedPointer<FullVersion> FullVersionFactory::parse4(QJsonObject root, QSharedPointer<FullVersion> product)
{
	product->id = root.value("id").toString();
	
	// if it's on our legacy list, it's legacy
	if(legacyWhitelist.contains(product->id))
		product->isLegacy = true;
	
	product->mainClass = root.value("mainClass").toString();
	auto procArgsValue = root.value("processArguments");
	if(procArgsValue.isString())
	{
		product->processArguments = procArgsValue.toString();
		QString toCompare = product->processArguments.toLower();
		if(toCompare == "legacy")
		{
			product->minecraftArguments = " ${auth_player_name} ${auth_session}";
			product->isLegacy = true;
		}
		else if(toCompare == "username_session")
		{
			product->minecraftArguments = "--username ${auth_player_name} --session ${auth_session}";
		}
		else if(toCompare == "username_session_version")
		{
			product->minecraftArguments = "--username ${auth_player_name} --session ${auth_session} --version ${profile_name}";
		}
	}
	
	auto minecraftArgsValue = root.value("minecraftArguments");
	if(minecraftArgsValue.isString())
	{
		product->minecraftArguments = minecraftArgsValue.toString();
	}
	
	product->releaseTime = root.value("releaseTime").toString();
	product->time = root.value("time").toString();
	
	// Iterate through the list.
	auto librariesValue = root.value("libraries");
	if(librariesValue.isArray())
	{
		QJsonArray libList = root.value("libraries").toArray();
		for (auto lib : libList)
		{
			if (!lib.isObject())
			{
				continue;
			}
			
			QJsonObject libObj = lib.toObject();
			
			QString crud = libObj.value("name").toString();
			product->libraries.append(crud);
			
			// TODO: improve!
			/*
			auto parts = crud.split(':');
			int zz = parts.size();
			*/
		}
	}
	return product;
}

QSharedPointer<FullVersion> FullVersionFactory::parse(QByteArray data)
{
	QSharedPointer<FullVersion> readVersion(new FullVersion());
	
	QJsonParseError jsonError;
	QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError);
	
	if (jsonError.error != QJsonParseError::NoError)
	{
		error_string = QString( "Error reading version file :") + " " + jsonError.errorString();
		m_error = FullVersionFactory::ParseError;
		return QSharedPointer<FullVersion>();
	}
	
	if(!jsonDoc.isObject())
	{
		error_string = "Error reading version file.";
		m_error = FullVersionFactory::ParseError;
		return QSharedPointer<FullVersion>();
	}
	QJsonObject root = jsonDoc.object();
	
	readVersion->minimumLauncherVersion = root.value("minimumLauncherVersion").toDouble();
	switch(readVersion->minimumLauncherVersion)
	{
		case 1:
		case 2:
		case 3:
		case 4:
			return parse4(root, readVersion);
		// ADD MORE HERE :D
		default:
			error_string = "Version file was for an unrecognized launcher version. RIP";
			m_error = FullVersionFactory::UnsupportedVersion;
			return QSharedPointer<FullVersion>();
	}
}


FullVersionFactory::FullVersionFactory()
{
	m_error = FullVersionFactory::AllOK;
	legacyWhitelist.append("1.5.1");
	legacyWhitelist.append("1.5.2");
}