From ceca6959d2a7f258d62ac4f589095b65084706c3 Mon Sep 17 00:00:00 2001
From: Petr Mrázek <peterix@gmail.com>
Date: Sun, 22 Sep 2013 04:21:36 +0200
Subject: Working 1.6 modding (currently only forge)

---
 logic/OneSixRule.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 64 insertions(+), 2 deletions(-)

(limited to 'logic/OneSixRule.cpp')

diff --git a/logic/OneSixRule.cpp b/logic/OneSixRule.cpp
index 85f7d434..545cd641 100644
--- a/logic/OneSixRule.cpp
+++ b/logic/OneSixRule.cpp
@@ -1,10 +1,72 @@
 #include "OneSixRule.h"
+#include <QJsonObject>
+#include <QJsonArray>
+
+QList<QSharedPointer<Rule>> rulesFromJsonV4(QJsonObject &objectWithRules)
+{
+	QList<QSharedPointer<Rule>> rules;
+	auto rulesVal = objectWithRules.value("rules");
+	if (!rulesVal.isArray())
+		return rules;
+
+	QJsonArray ruleList = rulesVal.toArray();
+	for (auto ruleVal : ruleList)
+	{
+		QSharedPointer<Rule> rule;
+		if (!ruleVal.isObject())
+			continue;
+		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;
+		OpSys requiredOs = OpSys_fromString(osNameVal.toString());
+		QString versionRegex = osObj.value("version").toString();
+		// add a new OS rule
+		rules.append(OsRule::create(action, requiredOs, versionRegex));
+	}
+}
+
+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", OpSys_toString(m_system));
+		osObj.insert("version", m_version_regexp);
+	}
+	ruleObj.insert("os", osObj);
+	return ruleObj;
+}
 
 RuleAction RuleAction_fromString(QString name)
 {
-	if(name == "allow")
+	if (name == "allow")
 		return Allow;
-	if(name == "disallow")
+	if (name == "disallow")
 		return Disallow;
 	return Defer;
 }
\ No newline at end of file
-- 
cgit