2015-02-09 00:51:14 +00:00
|
|
|
#include "MMCJson.h"
|
2014-05-09 15:16:25 +00:00
|
|
|
using namespace MMCJson;
|
|
|
|
|
|
|
|
#include "RawLibrary.h"
|
2015-05-26 23:30:18 +00:00
|
|
|
#include <pathutils.h>
|
2014-05-09 15:16:25 +00:00
|
|
|
|
|
|
|
RawLibraryPtr RawLibrary::fromJson(const QJsonObject &libObj, const QString &filename)
|
|
|
|
{
|
|
|
|
RawLibraryPtr out(new RawLibrary());
|
|
|
|
if (!libObj.contains("name"))
|
|
|
|
{
|
|
|
|
throw JSONValidationError(filename +
|
|
|
|
"contains a library that doesn't have a 'name' field");
|
|
|
|
}
|
2014-05-11 10:37:21 +00:00
|
|
|
out->m_name = libObj.value("name").toString();
|
2014-05-09 15:16:25 +00:00
|
|
|
|
2014-07-16 00:03:52 +00:00
|
|
|
auto readString = [libObj, filename](const QString & key, QString & variable) -> bool
|
2014-05-09 15:16:25 +00:00
|
|
|
{
|
2014-07-16 00:03:52 +00:00
|
|
|
if (!libObj.contains(key))
|
|
|
|
return false;
|
|
|
|
QJsonValue val = libObj.value(key);
|
|
|
|
|
|
|
|
if (!val.isString())
|
2014-05-09 15:16:25 +00:00
|
|
|
{
|
2015-02-02 01:14:14 +00:00
|
|
|
qWarning() << key << "is not a string in" << filename << "(skipping)";
|
2014-07-16 00:03:52 +00:00
|
|
|
return false;
|
2014-05-09 15:16:25 +00:00
|
|
|
}
|
2014-07-16 00:03:52 +00:00
|
|
|
|
|
|
|
variable = val.toString();
|
|
|
|
return true;
|
2014-05-09 15:16:25 +00:00
|
|
|
};
|
|
|
|
|
2015-05-26 23:30:18 +00:00
|
|
|
QString urlStr;
|
|
|
|
readString("url", urlStr);
|
|
|
|
out->m_base_url = urlStr;
|
2014-05-11 10:37:21 +00:00
|
|
|
readString("MMC-hint", out->m_hint);
|
|
|
|
readString("MMC-absulute_url", out->m_absolute_url);
|
|
|
|
readString("MMC-absoluteUrl", out->m_absolute_url);
|
2014-05-09 15:16:25 +00:00
|
|
|
if (libObj.contains("extract"))
|
|
|
|
{
|
|
|
|
out->applyExcludes = true;
|
|
|
|
auto extractObj = ensureObject(libObj.value("extract"));
|
|
|
|
for (auto excludeVal : ensureArray(extractObj.value("exclude")))
|
|
|
|
{
|
2014-05-11 10:37:21 +00:00
|
|
|
out->extract_excludes.append(ensureString(excludeVal));
|
2014-05-09 15:16:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (libObj.contains("natives"))
|
|
|
|
{
|
|
|
|
QJsonObject nativesObj = ensureObject(libObj.value("natives"));
|
|
|
|
for (auto it = nativesObj.begin(); it != nativesObj.end(); ++it)
|
|
|
|
{
|
|
|
|
if (!it.value().isString())
|
|
|
|
{
|
2015-02-02 01:14:14 +00:00
|
|
|
qWarning() << filename << "contains an invalid native (skipping)";
|
2014-05-09 15:16:25 +00:00
|
|
|
}
|
|
|
|
OpSys opSys = OpSys_fromString(it.key());
|
|
|
|
if (opSys != Os_Other)
|
|
|
|
{
|
2014-07-19 21:16:02 +00:00
|
|
|
out->m_native_classifiers[opSys] = it.value().toString();
|
2014-05-09 15:16:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (libObj.contains("rules"))
|
|
|
|
{
|
|
|
|
out->applyRules = true;
|
2014-05-11 10:37:21 +00:00
|
|
|
out->m_rules = rulesFromJsonV4(libObj);
|
2014-05-09 15:16:25 +00:00
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
2014-05-11 10:37:21 +00:00
|
|
|
|
|
|
|
RawLibraryPtr RawLibrary::fromJsonPlus(const QJsonObject &libObj, const QString &filename)
|
|
|
|
{
|
|
|
|
auto lib = RawLibrary::fromJson(libObj, filename);
|
|
|
|
if (libObj.contains("insert"))
|
|
|
|
{
|
|
|
|
QJsonValue insertVal = ensureExists(libObj.value("insert"), "library insert rule");
|
2014-07-26 21:00:35 +00:00
|
|
|
if (insertVal.isString())
|
2014-05-11 10:37:21 +00:00
|
|
|
{
|
2014-07-26 21:00:35 +00:00
|
|
|
// it's just a simple string rule. OK.
|
|
|
|
QString insertString = insertVal.toString();
|
|
|
|
if (insertString == "apply")
|
2014-05-11 10:37:21 +00:00
|
|
|
{
|
2014-07-26 21:00:35 +00:00
|
|
|
lib->insertType = RawLibrary::Apply;
|
2014-05-11 10:37:21 +00:00
|
|
|
}
|
2014-07-26 21:00:35 +00:00
|
|
|
else if (insertString == "prepend")
|
2014-05-11 10:37:21 +00:00
|
|
|
{
|
2014-07-26 21:00:35 +00:00
|
|
|
lib->insertType = RawLibrary::Prepend;
|
|
|
|
}
|
|
|
|
else if (insertString == "append")
|
|
|
|
{
|
|
|
|
lib->insertType = RawLibrary::Append;
|
|
|
|
}
|
|
|
|
else if (insertString == "replace")
|
|
|
|
{
|
|
|
|
lib->insertType = RawLibrary::Replace;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw JSONValidationError("A '+' library in " + filename +
|
|
|
|
" contains an invalid insert type");
|
2014-05-11 10:37:21 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-26 21:00:35 +00:00
|
|
|
else if (insertVal.isObject())
|
2014-05-11 10:37:21 +00:00
|
|
|
{
|
2014-07-26 21:00:35 +00:00
|
|
|
// it's a more complex rule, specifying what should be:
|
|
|
|
// * replaced (for now only this)
|
|
|
|
// this was never used, AFAIK. tread carefully.
|
|
|
|
QJsonObject insertObj = insertVal.toObject();
|
|
|
|
if (insertObj.isEmpty())
|
|
|
|
{
|
|
|
|
throw JSONValidationError("Empty compound insert rule in " + filename);
|
|
|
|
}
|
|
|
|
QString insertString = insertObj.keys().first();
|
2015-05-26 23:30:18 +00:00
|
|
|
// really, only replace makes sense in combination with
|
2014-07-26 21:00:35 +00:00
|
|
|
if(insertString != "replace")
|
|
|
|
{
|
|
|
|
throw JSONValidationError("Compound insert rule is not 'replace' in " + filename);
|
|
|
|
}
|
|
|
|
lib->insertData = insertObj.value(insertString).toString();
|
2014-05-11 10:37:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw JSONValidationError("A '+' library in " + filename +
|
2014-07-26 21:00:35 +00:00
|
|
|
" contains an unknown/invalid insert rule");
|
2014-05-11 10:37:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (libObj.contains("MMC-depend"))
|
|
|
|
{
|
|
|
|
const QString dependString = ensureString(libObj.value("MMC-depend"));
|
|
|
|
if (dependString == "hard")
|
|
|
|
{
|
|
|
|
lib->dependType = RawLibrary::Hard;
|
|
|
|
}
|
|
|
|
else if (dependString == "soft")
|
|
|
|
{
|
|
|
|
lib->dependType = RawLibrary::Soft;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw JSONValidationError("A '+' library in " + filename +
|
|
|
|
" contains an invalid depend type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return lib;
|
|
|
|
}
|
|
|
|
|
2014-07-26 21:00:35 +00:00
|
|
|
QJsonObject RawLibrary::toJson() const
|
2014-05-11 10:37:21 +00:00
|
|
|
{
|
|
|
|
QJsonObject libRoot;
|
2014-07-19 21:16:02 +00:00
|
|
|
libRoot.insert("name", (QString)m_name);
|
2014-05-11 10:37:21 +00:00
|
|
|
if (m_absolute_url.size())
|
|
|
|
libRoot.insert("MMC-absoluteUrl", m_absolute_url);
|
|
|
|
if (m_hint.size())
|
|
|
|
libRoot.insert("MMC-hint", m_hint);
|
|
|
|
if (m_base_url != "http://" + URLConstants::AWS_DOWNLOAD_LIBRARIES &&
|
|
|
|
m_base_url != "https://" + URLConstants::AWS_DOWNLOAD_LIBRARIES &&
|
|
|
|
m_base_url != "https://" + URLConstants::LIBRARY_BASE && !m_base_url.isEmpty())
|
|
|
|
{
|
2015-05-31 15:51:20 +00:00
|
|
|
libRoot.insert("url", m_base_url);
|
2014-05-11 10:37:21 +00:00
|
|
|
}
|
|
|
|
if (isNative())
|
|
|
|
{
|
|
|
|
QJsonObject nativeList;
|
2014-07-19 21:16:02 +00:00
|
|
|
auto iter = m_native_classifiers.begin();
|
|
|
|
while (iter != m_native_classifiers.end())
|
2014-05-11 10:37:21 +00:00
|
|
|
{
|
|
|
|
nativeList.insert(OpSys_toString(iter.key()), iter.value());
|
|
|
|
iter++;
|
|
|
|
}
|
|
|
|
libRoot.insert("natives", nativeList);
|
|
|
|
if (extract_excludes.size())
|
|
|
|
{
|
|
|
|
QJsonArray excludes;
|
|
|
|
QJsonObject extract;
|
|
|
|
for (auto exclude : extract_excludes)
|
|
|
|
{
|
|
|
|
excludes.append(exclude);
|
|
|
|
}
|
|
|
|
extract.insert("exclude", excludes);
|
|
|
|
libRoot.insert("extract", extract);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (m_rules.size())
|
|
|
|
{
|
|
|
|
QJsonArray allRules;
|
|
|
|
for (auto &rule : m_rules)
|
|
|
|
{
|
|
|
|
QJsonObject ruleObj = rule->toJson();
|
|
|
|
allRules.append(ruleObj);
|
|
|
|
}
|
|
|
|
libRoot.insert("rules", allRules);
|
|
|
|
}
|
|
|
|
return libRoot;
|
|
|
|
}
|
|
|
|
|
2014-07-26 21:00:35 +00:00
|
|
|
QStringList RawLibrary::files() const
|
|
|
|
{
|
|
|
|
QStringList retval;
|
2015-05-26 23:30:18 +00:00
|
|
|
QString storage = storageSuffix();
|
2014-07-26 21:00:35 +00:00
|
|
|
if (storage.contains("${arch}"))
|
|
|
|
{
|
|
|
|
QString cooked_storage = storage;
|
|
|
|
cooked_storage.replace("${arch}", "32");
|
|
|
|
retval.append(cooked_storage);
|
|
|
|
cooked_storage = storage;
|
|
|
|
cooked_storage.replace("${arch}", "64");
|
|
|
|
retval.append(cooked_storage);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
retval.append(storage);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RawLibrary::filesExist(const QDir &base) const
|
|
|
|
{
|
|
|
|
auto libFiles = files();
|
|
|
|
for(auto file: libFiles)
|
|
|
|
{
|
|
|
|
QFileInfo info(base, file);
|
2015-02-02 01:14:14 +00:00
|
|
|
qWarning() << info.absoluteFilePath() << "doesn't exist";
|
2014-07-26 21:00:35 +00:00
|
|
|
if (!info.exists())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2015-05-31 17:24:39 +00:00
|
|
|
|
2015-05-31 15:51:20 +00:00
|
|
|
QString RawLibrary::url() const
|
2014-07-26 21:00:35 +00:00
|
|
|
{
|
2015-05-26 23:30:18 +00:00
|
|
|
if (!m_absolute_url.isEmpty())
|
|
|
|
{
|
2014-07-26 21:00:35 +00:00
|
|
|
return m_absolute_url;
|
2015-05-26 23:30:18 +00:00
|
|
|
}
|
2014-07-26 21:00:35 +00:00
|
|
|
|
|
|
|
if (m_base_url.isEmpty())
|
|
|
|
{
|
2015-05-26 23:30:18 +00:00
|
|
|
return QString("https://" + URLConstants::LIBRARY_BASE) + storageSuffix();
|
2014-07-26 21:00:35 +00:00
|
|
|
}
|
|
|
|
|
2015-05-31 15:51:20 +00:00
|
|
|
if(m_base_url.endsWith('/'))
|
|
|
|
{
|
|
|
|
return m_base_url + storageSuffix();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return m_base_url + QChar('/') + storageSuffix();
|
|
|
|
}
|
2014-07-26 21:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RawLibrary::isActive() const
|
|
|
|
{
|
|
|
|
bool result = true;
|
|
|
|
if (m_rules.empty())
|
|
|
|
{
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RuleAction ruleResult = Disallow;
|
|
|
|
for (auto rule : m_rules)
|
|
|
|
{
|
|
|
|
RuleAction temp = rule->apply(this);
|
|
|
|
if (temp != Defer)
|
|
|
|
ruleResult = temp;
|
|
|
|
}
|
|
|
|
result = result && (ruleResult == Allow);
|
|
|
|
}
|
|
|
|
if (isNative())
|
|
|
|
{
|
|
|
|
result = result && m_native_classifiers.contains(currentSystem);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-05-26 23:30:18 +00:00
|
|
|
void RawLibrary::setStoragePrefix(QString prefix)
|
|
|
|
{
|
|
|
|
m_storagePrefix = prefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString RawLibrary::defaultStoragePrefix()
|
|
|
|
{
|
|
|
|
return "libraries/";
|
|
|
|
}
|
|
|
|
|
|
|
|
QString RawLibrary::storagePrefix() const
|
|
|
|
{
|
|
|
|
if(m_storagePrefix.isEmpty())
|
|
|
|
{
|
|
|
|
return defaultStoragePrefix();
|
|
|
|
}
|
|
|
|
return m_storagePrefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString RawLibrary::storageSuffix() const
|
2014-05-11 10:37:21 +00:00
|
|
|
{
|
2014-07-26 21:00:35 +00:00
|
|
|
// non-native? use only the gradle specifier
|
|
|
|
if (!isNative())
|
|
|
|
{
|
|
|
|
return m_name.toPath();
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise native, override classifiers. Mojang HACK!
|
|
|
|
GradleSpecifier nativeSpec = m_name;
|
2015-05-26 23:30:18 +00:00
|
|
|
if (m_native_classifiers.contains(currentSystem))
|
2014-07-26 21:00:35 +00:00
|
|
|
{
|
|
|
|
nativeSpec.setClassifier(m_native_classifiers[currentSystem]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nativeSpec.setClassifier("INVALID");
|
|
|
|
}
|
|
|
|
return nativeSpec.toPath();
|
2014-05-11 10:37:21 +00:00
|
|
|
}
|
2015-05-26 23:30:18 +00:00
|
|
|
|
|
|
|
QString RawLibrary::storagePath() const
|
|
|
|
{
|
|
|
|
return PathCombine(storagePrefix(), storageSuffix());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RawLibrary::storagePathIsDefault() const
|
|
|
|
{
|
|
|
|
return m_storagePrefix.isEmpty();
|
|
|
|
}
|