mirror of
https://github.com/PrismLauncher/PrismLauncher.git
synced 2026-07-08 13:08:29 +02:00
Simplify Rule
Signed-off-by: TheKodeToad <TheKodeToad@proton.me>
This commit is contained in:
parent
79b7e277f1
commit
3ba9483011
5 changed files with 73 additions and 119 deletions
|
|
@ -242,13 +242,13 @@ bool Library::isActive(const RuntimeContext& runtimeContext) const
|
||||||
if (m_rules.empty()) {
|
if (m_rules.empty()) {
|
||||||
result = true;
|
result = true;
|
||||||
} else {
|
} else {
|
||||||
RuleAction ruleResult = Disallow;
|
Rule::Action ruleResult = Rule::Disallow;
|
||||||
for (auto rule : m_rules) {
|
for (auto rule : m_rules) {
|
||||||
RuleAction temp = rule->apply(this, runtimeContext);
|
Rule::Action temp = rule.apply(runtimeContext);
|
||||||
if (temp != Defer)
|
if (temp != Rule::Defer)
|
||||||
ruleResult = temp;
|
ruleResult = temp;
|
||||||
}
|
}
|
||||||
result = result && (ruleResult == Allow);
|
result = result && (ruleResult == Rule::Allow);
|
||||||
}
|
}
|
||||||
if (isNative()) {
|
if (isNative()) {
|
||||||
result = result && !getCompatibleNative(runtimeContext).isNull();
|
result = result && !getCompatibleNative(runtimeContext).isNull();
|
||||||
|
|
|
||||||
|
|
@ -129,7 +129,7 @@ class Library {
|
||||||
void setHint(const QString& hint) { m_hint = hint; }
|
void setHint(const QString& hint) { m_hint = hint; }
|
||||||
|
|
||||||
/// Set the load rules
|
/// Set the load rules
|
||||||
void setRules(QList<std::shared_ptr<Rule>> rules) { m_rules = rules; }
|
void setRules(QList<Rule> rules) { m_rules = rules; }
|
||||||
|
|
||||||
/// Returns true if the library should be loaded (or extracted, in case of natives)
|
/// Returns true if the library should be loaded (or extracted, in case of natives)
|
||||||
bool isActive(const RuntimeContext& runtimeContext) const;
|
bool isActive(const RuntimeContext& runtimeContext) const;
|
||||||
|
|
@ -203,7 +203,7 @@ class Library {
|
||||||
bool applyRules = false;
|
bool applyRules = false;
|
||||||
|
|
||||||
/// rules associated with the library
|
/// rules associated with the library
|
||||||
QList<std::shared_ptr<Rule>> m_rules;
|
QList<Rule> m_rules;
|
||||||
|
|
||||||
/// MOJANG: container with Mojang style download info
|
/// MOJANG: container with Mojang style download info
|
||||||
MojangLibraryDownloadInfo::Ptr m_mojangDownloads;
|
MojangLibraryDownloadInfo::Ptr m_mojangDownloads;
|
||||||
|
|
|
||||||
|
|
@ -319,7 +319,11 @@ LibraryPtr MojangVersionFormat::libraryFromJson(ProblemContainer& problems, cons
|
||||||
}
|
}
|
||||||
if (libObj.contains("rules")) {
|
if (libObj.contains("rules")) {
|
||||||
out->applyRules = true;
|
out->applyRules = true;
|
||||||
out->m_rules = rulesFromJsonV4(libObj);
|
|
||||||
|
QJsonArray rulesArray = requireArray(libObj.value("rules"));
|
||||||
|
for (auto rule : rulesArray) {
|
||||||
|
out->m_rules.append(Rule::fromJson(requireObject(rule)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (libObj.contains("downloads")) {
|
if (libObj.contains("downloads")) {
|
||||||
out->m_mojangDownloads = libDownloadInfoFromJson(libObj);
|
out->m_mojangDownloads = libDownloadInfoFromJson(libObj);
|
||||||
|
|
@ -355,7 +359,7 @@ QJsonObject MojangVersionFormat::libraryToJson(Library* library)
|
||||||
if (!library->m_rules.isEmpty()) {
|
if (!library->m_rules.isEmpty()) {
|
||||||
QJsonArray allRules;
|
QJsonArray allRules;
|
||||||
for (auto& rule : library->m_rules) {
|
for (auto& rule : library->m_rules) {
|
||||||
QJsonObject ruleObj = rule->toJson();
|
QJsonObject ruleObj = rule.toJson();
|
||||||
allRules.append(ruleObj);
|
allRules.append(ruleObj);
|
||||||
}
|
}
|
||||||
libRoot.insert("rules", allRules);
|
libRoot.insert("rules", allRules);
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
/*
|
/*
|
||||||
* Prism Launcher - Minecraft Launcher
|
* Prism Launcher - Minecraft Launcher
|
||||||
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
||||||
|
* Copyright (C) 2025 TheKodeToad <TheKodeToad@proton.me>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
|
@ -38,72 +39,54 @@
|
||||||
|
|
||||||
#include "Rule.h"
|
#include "Rule.h"
|
||||||
|
|
||||||
RuleAction RuleAction_fromString(QString name)
|
Rule Rule::fromJson(const QJsonObject& object)
|
||||||
{
|
{
|
||||||
if (name == "allow")
|
Rule result;
|
||||||
return Allow;
|
|
||||||
if (name == "disallow")
|
|
||||||
return Disallow;
|
|
||||||
return Defer;
|
|
||||||
}
|
|
||||||
|
|
||||||
QList<std::shared_ptr<Rule>> rulesFromJsonV4(const QJsonObject& objectWithRules)
|
if (object["action"] == "allow")
|
||||||
{
|
result.action = Allow;
|
||||||
QList<std::shared_ptr<Rule>> rules;
|
else if (object["action"] == "disallow")
|
||||||
auto rulesVal = objectWithRules.value("rules");
|
result.action = Disallow;
|
||||||
if (!rulesVal.isArray())
|
|
||||||
return rules;
|
|
||||||
|
|
||||||
QJsonArray ruleList = rulesVal.toArray();
|
if (auto os = object["os"]; os.isObject()) {
|
||||||
for (auto ruleVal : ruleList) {
|
if (auto name = os["name"].toString(); !name.isNull()) {
|
||||||
std::shared_ptr<Rule> rule;
|
result.os = OS{
|
||||||
if (!ruleVal.isObject())
|
name,
|
||||||
continue;
|
os["version"].toString(),
|
||||||
auto ruleObj = ruleVal.toObject();
|
};
|
||||||
auto actionVal = ruleObj.value("action");
|
|
||||||
if (!actionVal.isString())
|
|
||||||
continue;
|
|
||||||
auto action = RuleAction_fromString(actionVal.toString());
|
|
||||||
if (action == Defer)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
auto osVal = ruleObj.value("os");
|
|
||||||
if (!osVal.isObject()) {
|
|
||||||
// add a new implicit action rule
|
|
||||||
rules.append(ImplicitRule::create(action));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto osObj = osVal.toObject();
|
|
||||||
auto osNameVal = osObj.value("name");
|
|
||||||
if (!osNameVal.isString())
|
|
||||||
continue;
|
|
||||||
QString osName = osNameVal.toString();
|
|
||||||
QString versionRegex = osObj.value("version").toString();
|
|
||||||
// add a new OS rule
|
|
||||||
rules.append(OsRule::create(action, osName, versionRegex));
|
|
||||||
}
|
|
||||||
return rules;
|
|
||||||
}
|
|
||||||
|
|
||||||
QJsonObject ImplicitRule::toJson()
|
|
||||||
{
|
|
||||||
QJsonObject ruleObj;
|
|
||||||
ruleObj.insert("action", m_result == Allow ? QString("allow") : QString("disallow"));
|
|
||||||
return ruleObj;
|
|
||||||
}
|
|
||||||
|
|
||||||
QJsonObject OsRule::toJson()
|
|
||||||
{
|
|
||||||
QJsonObject ruleObj;
|
|
||||||
ruleObj.insert("action", m_result == Allow ? QString("allow") : QString("disallow"));
|
|
||||||
QJsonObject osObj;
|
|
||||||
{
|
|
||||||
osObj.insert("name", m_system);
|
|
||||||
if (!m_version_regexp.isEmpty()) {
|
|
||||||
osObj.insert("version", m_version_regexp);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ruleObj.insert("os", osObj);
|
|
||||||
return ruleObj;
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
QJsonObject Rule::toJson()
|
||||||
|
{
|
||||||
|
QJsonObject result;
|
||||||
|
|
||||||
|
if (action == Allow)
|
||||||
|
result["action"] = "allow";
|
||||||
|
else if (action == Disallow)
|
||||||
|
result["action"] = "disallow";
|
||||||
|
|
||||||
|
if (os.has_value()) {
|
||||||
|
QJsonObject osResult;
|
||||||
|
|
||||||
|
osResult["name"] = os->name;
|
||||||
|
|
||||||
|
if (!os->version.isEmpty())
|
||||||
|
osResult["version"] = os->version;
|
||||||
|
|
||||||
|
result["os"] = osResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
Rule::Action Rule::apply(const RuntimeContext& runtimeContext)
|
||||||
|
{
|
||||||
|
if (!runtimeContext.classifierMatches(os->name))
|
||||||
|
return Defer;
|
||||||
|
|
||||||
|
return action;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
/*
|
/*
|
||||||
* Prism Launcher - Minecraft Launcher
|
* Prism Launcher - Minecraft Launcher
|
||||||
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
||||||
|
* Copyright (C) 2025 TheKodeToad <TheKodeToad@proton.me>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
|
@ -38,59 +39,25 @@
|
||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
#include <QList>
|
#include <QList>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <memory>
|
|
||||||
#include "RuntimeContext.h"
|
#include "RuntimeContext.h"
|
||||||
|
|
||||||
class Library;
|
class Library;
|
||||||
class Rule;
|
|
||||||
|
|
||||||
enum RuleAction { Allow, Disallow, Defer };
|
struct Rule {
|
||||||
|
enum Action { Allow, Disallow, Defer };
|
||||||
|
|
||||||
QList<std::shared_ptr<Rule>> rulesFromJsonV4(const QJsonObject& objectWithRules);
|
struct OS {
|
||||||
|
QString name;
|
||||||
|
// FIXME: unsupported
|
||||||
|
// retained to avoid information being lost from files
|
||||||
|
QString version;
|
||||||
|
};
|
||||||
|
|
||||||
class Rule {
|
Action action = Defer;
|
||||||
protected:
|
std::optional<OS> os;
|
||||||
RuleAction m_result;
|
|
||||||
virtual bool applies(const Library* parent, const RuntimeContext& runtimeContext) = 0;
|
|
||||||
|
|
||||||
public:
|
static Rule fromJson(const QJsonObject& json);
|
||||||
Rule(RuleAction result) : m_result(result) {}
|
QJsonObject toJson();
|
||||||
virtual ~Rule() {}
|
|
||||||
virtual QJsonObject toJson() = 0;
|
Action apply(const RuntimeContext& runtimeContext);
|
||||||
RuleAction apply(const Library* parent, const RuntimeContext& runtimeContext)
|
|
||||||
{
|
|
||||||
if (applies(parent, runtimeContext))
|
|
||||||
return m_result;
|
|
||||||
else
|
|
||||||
return Defer;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class OsRule : public Rule {
|
|
||||||
private:
|
|
||||||
// the OS
|
|
||||||
QString m_system;
|
|
||||||
// the OS version regexp
|
|
||||||
QString m_version_regexp;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
virtual bool applies(const Library*, const RuntimeContext& runtimeContext) { return runtimeContext.classifierMatches(m_system); }
|
|
||||||
OsRule(RuleAction result, QString system, QString version_regexp) : Rule(result), m_system(system), m_version_regexp(version_regexp) {}
|
|
||||||
|
|
||||||
public:
|
|
||||||
virtual QJsonObject toJson();
|
|
||||||
static std::shared_ptr<OsRule> create(RuleAction result, QString system, QString version_regexp)
|
|
||||||
{
|
|
||||||
return std::shared_ptr<OsRule>(new OsRule(result, system, version_regexp));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class ImplicitRule : public Rule {
|
|
||||||
protected:
|
|
||||||
virtual bool applies(const Library*, [[maybe_unused]] const RuntimeContext& runtimeContext) { return true; }
|
|
||||||
ImplicitRule(RuleAction result) : Rule(result) {}
|
|
||||||
|
|
||||||
public:
|
|
||||||
virtual QJsonObject toJson();
|
|
||||||
static std::shared_ptr<ImplicitRule> create(RuleAction result) { return std::shared_ptr<ImplicitRule>(new ImplicitRule(result)); }
|
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue