pollymc/logic/minecraft/VersionFile.cpp
Petr Mrázek d4eacb56b3 NOISSUE make new Mojang version format pass through MultiMC structures
Not yet used effectively, but it is read and written properly
2016-03-07 02:03:36 +01:00

145 lines
3.0 KiB
C++

#include <QJsonArray>
#include <QJsonDocument>
#include <QDebug>
#include "minecraft/VersionFile.h"
#include "minecraft/Library.h"
#include "minecraft/MinecraftProfile.h"
#include "minecraft/JarMod.h"
#include "ParseUtils.h"
#include "VersionBuildError.h"
#include <Version.h>
int findLibraryByName(QList<LibraryPtr> haystack, const GradleSpecifier &needle)
{
int retval = -1;
for (int i = 0; i < haystack.size(); ++i)
{
if (haystack.at(i)->rawName().matchName(needle))
{
// only one is allowed.
if (retval != -1)
return -1;
retval = i;
}
}
return retval;
}
bool VersionFile::isMinecraftVersion()
{
return fileId == "net.minecraft";
}
bool VersionFile::hasJarMods()
{
return !jarMods.isEmpty();
}
void VersionFile::applyTo(MinecraftProfile *version)
{
if (!version->id.isNull() && !mcVersion.isNull())
{
if (QRegExp(mcVersion, Qt::CaseInsensitive, QRegExp::Wildcard).indexIn(version->id) ==
-1)
{
throw MinecraftVersionMismatch(fileId, mcVersion, version->id);
}
}
if (!id.isNull())
{
version->id = id;
}
if (!mainClass.isNull())
{
version->mainClass = mainClass;
}
if (!appletClass.isNull())
{
version->appletClass = appletClass;
}
if (!processArguments.isNull())
{
if (isMinecraftVersion())
{
version->vanillaProcessArguments = processArguments;
}
version->processArguments = processArguments;
}
if (isMinecraftVersion())
{
if (!type.isNull())
{
version->type = type;
}
if (!m_releaseTime.isNull())
{
version->m_releaseTime = m_releaseTime;
}
if (!m_updateTime.isNull())
{
version->m_updateTime = m_updateTime;
}
}
if (!assets.isNull())
{
version->assets = assets;
}
if (!overwriteMinecraftArguments.isNull())
{
if (isMinecraftVersion())
{
version->vanillaMinecraftArguments = overwriteMinecraftArguments;
}
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);
version->traits.unite(traits);
if (shouldOverwriteLibs)
{
QList<LibraryPtr> libs;
for (auto lib : overwriteLibs)
{
libs.append(Library::limitedCopy(lib));
}
if (isMinecraftVersion())
{
version->vanillaLibraries = libs;
}
version->libraries = libs;
}
for (auto addedLibrary : addLibs)
{
// find the library by name.
const int index = findLibraryByName(version->libraries, addedLibrary->rawName());
// library not found? just add it.
if (index < 0)
{
version->libraries.append(Library::limitedCopy(addedLibrary));
continue;
}
auto existingLibrary = version->libraries.at(index);
// if we are higher it means we should update
if (Version(addedLibrary->version()) > Version(existingLibrary->version()))
{
auto library = Library::limitedCopy(addedLibrary);
version->libraries.replace(index, library);
}
}
}