PrismLauncher/logic/minecraft/VersionFile.cpp

145 lines
3.0 KiB
C++
Raw Normal View History

2014-03-01 22:06:47 +00:00
#include <QJsonArray>
#include <QJsonDocument>
#include <QDebug>
2014-05-08 20:20:10 +01:00
2015-02-09 00:51:14 +00:00
#include "minecraft/VersionFile.h"
2016-02-26 01:04:21 +00:00
#include "minecraft/RawLibrary.h"
2015-02-09 00:51:14 +00:00
#include "minecraft/MinecraftProfile.h"
#include "minecraft/JarMod.h"
2014-05-10 00:53:32 +01:00
#include "ParseUtils.h"
2014-03-01 22:06:47 +00:00
2014-05-10 00:53:32 +01:00
#include "VersionBuildError.h"
2015-10-05 00:47:27 +01:00
#include <Version.h>
2014-05-10 00:53:32 +01:00
2016-02-26 01:04:21 +00:00
int findLibraryByName(QList<RawLibraryPtr> haystack, const GradleSpecifier &needle)
2014-03-01 22:06:47 +00:00
{
int retval = -1;
for (int i = 0; i < haystack.size(); ++i)
2014-03-01 22:06:47 +00:00
{
2014-09-06 17:16:56 +01:00
if (haystack.at(i)->rawName().matchName(needle))
2014-03-01 22:06:47 +00:00
{
// only one is allowed.
2014-05-10 00:53:32 +01:00
if (retval != -1)
return -1;
retval = i;
2014-03-01 22:06:47 +00:00
}
}
return retval;
2014-03-01 22:06:47 +00:00
}
bool VersionFile::isMinecraftVersion()
{
2015-01-27 21:31:07 +00:00
return fileId == "net.minecraft";
}
bool VersionFile::hasJarMods()
{
return !jarMods.isEmpty();
}
2015-01-27 21:31:07 +00:00
void VersionFile::applyTo(MinecraftProfile *version)
2014-03-01 22:06:47 +00:00
{
if (!version->id.isNull() && !mcVersion.isNull())
{
if (QRegExp(mcVersion, Qt::CaseInsensitive, QRegExp::Wildcard).indexIn(version->id) ==
-1)
{
throw MinecraftVersionMismatch(fileId, mcVersion, version->id);
2014-03-01 22:06:47 +00:00
}
}
if (!id.isNull())
{
version->id = id;
}
if (!mainClass.isNull())
{
version->mainClass = mainClass;
}
if (!appletClass.isNull())
{
version->appletClass = appletClass;
}
2014-03-01 22:06:47 +00:00
if (!processArguments.isNull())
{
if (isMinecraftVersion())
{
version->vanillaProcessArguments = processArguments;
}
2014-03-01 22:06:47 +00:00
version->processArguments = processArguments;
}
if (isMinecraftVersion())
2014-03-01 22:06:47 +00:00
{
2014-05-10 00:53:32 +01:00
if (!type.isNull())
{
version->type = type;
}
2016-03-02 02:03:44 +00:00
if (!m_releaseTime.isNull())
2014-05-10 00:53:32 +01:00
{
version->m_releaseTime = m_releaseTime;
}
2016-03-02 02:03:44 +00:00
if (!m_updateTime.isNull())
2014-05-10 00:53:32 +01:00
{
version->m_updateTime = m_updateTime;
}
2014-03-01 22:06:47 +00:00
}
if (!assets.isNull())
{
version->assets = assets;
}
if (!overwriteMinecraftArguments.isNull())
{
if (isMinecraftVersion())
{
version->vanillaMinecraftArguments = overwriteMinecraftArguments;
}
2014-03-01 22:06:47 +00:00
version->minecraftArguments = overwriteMinecraftArguments;
}
if (!addMinecraftArguments.isNull())
{
version->minecraftArguments += addMinecraftArguments;
}
if (shouldOverwriteTweakers)
{
version->tweakers = overwriteTweakers;
}
for (auto tweaker : addTweakers)
{
version->tweakers += tweaker;
}
version->jarMods.append(jarMods);
2014-05-04 23:10:59 +01:00
version->traits.unite(traits);
2014-03-01 22:06:47 +00:00
if (shouldOverwriteLibs)
{
2016-02-26 01:04:21 +00:00
QList<RawLibraryPtr> libs;
2014-03-01 22:06:47 +00:00
for (auto lib : overwriteLibs)
{
2016-02-26 01:04:21 +00:00
libs.append(RawLibrary::limitedCopy(lib));
2014-03-01 22:06:47 +00:00
}
if (isMinecraftVersion())
{
version->vanillaLibraries = libs;
}
version->libraries = libs;
2014-03-01 22:06:47 +00:00
}
for (auto addedLibrary : addLibs)
2014-03-01 22:06:47 +00:00
{
// find the library by name.
const int index = findLibraryByName(version->libraries, addedLibrary->rawName());
// library not found? just add it.
if (index < 0)
2014-03-01 22:06:47 +00:00
{
2016-02-26 01:04:21 +00:00
version->libraries.append(RawLibrary::limitedCopy(addedLibrary));
continue;
2014-03-01 22:06:47 +00:00
}
auto existingLibrary = version->libraries.at(index);
// if we are higher it means we should update
if (Version(addedLibrary->version()) > Version(existingLibrary->version()))
2014-03-01 22:06:47 +00:00
{
2016-02-26 01:04:21 +00:00
auto library = RawLibrary::limitedCopy(addedLibrary);
version->libraries.replace(index, library);
2014-03-01 22:06:47 +00:00
}
}
}