Browse Source

allow operators in recipie item filter parsing

Kolja Strohm 1 year ago
parent
commit
491caa2803
2 changed files with 85 additions and 17 deletions
  1. 1 1
      FactoryCraft/ItemFilter.cpp
  2. 84 16
      FactoryCraft/RecipieLoader.cpp

+ 1 - 1
FactoryCraft/ItemFilter.cpp

@@ -63,7 +63,7 @@ Framework::Text CombinedItemFilter::getLogicUIML() const
     result += (int)op(1, 0);
     result += "\" result_1_1=\"";
     result += (int)op(1, 1);
-    result += ">";
+    result += "\">";
     result += filterA->getLogicUIML();
     result += filterB->getLogicUIML();
     result += "</operator>";

+ 84 - 16
FactoryCraft/RecipieLoader.cpp

@@ -152,15 +152,52 @@ void RecipieLoader::loadRecipie(JSONObject* zRecipie)
 
 ItemFilter* RecipieLoader::loadFilter(JSONObject* zFilter)
 {
-    Framework::Text type = zFilter->zValue("itemType")->asString()->getString();
-    for (int i = 0; i < StaticRegistry<ItemType>::INSTANCE.getCount(); i++)
+    if (zFilter->hasValue("itemType"))
     {
-        if (StaticRegistry<ItemType>::INSTANCE.zElement(i)
-            && StaticRegistry<ItemType>::INSTANCE.zElement(i)
-                   ->getName()
-                   .istGleich(type))
-            return new TypeItemFilter(
-                StaticRegistry<ItemType>::INSTANCE.zElement(i));
+        Framework::Text type
+            = zFilter->zValue("itemType")->asString()->getString();
+        for (int i = 0; i < StaticRegistry<ItemType>::INSTANCE.getCount(); i++)
+        {
+            if (StaticRegistry<ItemType>::INSTANCE.zElement(i)
+                && StaticRegistry<ItemType>::INSTANCE.zElement(i)
+                       ->getName()
+                       .istGleich(type))
+                return new TypeItemFilter(
+                    StaticRegistry<ItemType>::INSTANCE.zElement(i));
+        }
+    }
+    else if (zFilter->hasValue("operator"))
+    {
+        Framework::Text op = zFilter->zValue("operator")
+            ->asString()
+            ->getString();
+        ItemFilter* left = loadFilter(zFilter->zValue("left")->asObject());
+        ItemFilter* right = loadFilter(zFilter->zValue("right")->asObject());
+        if (op.istGleich("&&"))
+        {
+            return new CombinedItemFilter(
+                left, right, [](bool a, bool b) { return a && b; });
+        }
+        else if (op.istGleich("||"))
+        {
+            return new CombinedItemFilter(
+                left, right, [](bool a, bool b) { return a || b; });
+        }
+        else if (op.istGleich("=="))
+        {
+            return new CombinedItemFilter(
+                left, right, [](bool a, bool b) { return a == b; });
+        }
+        else if (op.istGleich("!="))
+        {
+            return new CombinedItemFilter(
+                left, right, [](bool a, bool b) { return a != b; });
+        }
+        else
+        {
+            left->release();
+            right->release();
+        }
     }
     return 0;
 }
@@ -233,11 +270,33 @@ JSONValidator* RecipieLoader::zRecipieValidator()
                     .getText()));
         }
     }
-    JSONValidator* filterValidator = JSONValidator::buildForObject()
-                                         ->withRequiredString("itemType")
-                                         ->whichIsOneOf(itemTypes)
-                                         ->finishString()
-                                         ->finishObject();
+    JSONValidator* typeFilterValidator
+        = JSONValidator::buildForObject()
+              ->setObjectReferenceId("typeFilter")
+              ->withRequiredString("itemType")
+              ->whichIsOneOf(itemTypes)
+              ->finishString()
+              ->finishObject();
+    Framework::RCArray<Framework::Text> operators;
+    operators.add(new Framework::Text("=="));
+    operators.add(new Framework::Text("!="));
+    operators.add(new Framework::Text("&&"));
+    operators.add(new Framework::Text("||"));
+    JSONValidator* operatorFilterValidator
+        = JSONValidator::buildForObject()
+              ->setObjectReferenceId("operatorFilter")
+              ->withRequiredString("operator")
+              ->whichIsOneOf(operators)
+              ->finishString()
+              ->withRequiredAttribute(
+                  "left", JSONValidator::buildForObjectReference("typeFilter"))
+              ->withRequiredAttribute("left",
+                  JSONValidator::buildForObjectReference("operatorFilter"))
+              ->withRequiredAttribute(
+                  "right", JSONValidator::buildForObjectReference("typeFilter"))
+              ->withRequiredAttribute("right",
+                  JSONValidator::buildForObjectReference("operatorFilter"))
+              ->finishObject();
     JSONValidator* outputValidator = JSONValidator::buildForObject()
                                          ->withRequiredString("itemType")
                                          ->whichIsOneOf(itemTypes)
@@ -273,7 +332,11 @@ JSONValidator* RecipieLoader::zRecipieValidator()
                                       ->finishNumber()
                                       ->withRequiredAttribute("filter",
                                           dynamic_cast<JSONValidator*>(
-                                              filterValidator->getThis()))
+                                              typeFilterValidator->getThis()))
+                                      ->withRequiredAttribute("filter",
+                                          dynamic_cast<JSONValidator*>(
+                                              operatorFilterValidator
+                                                  ->getThis()))
                                       ->withRequiredAttribute("modifiers",
                                           JSONValidator::buildForArray()
                                               ->withDefault(Parser::getValue(
@@ -319,7 +382,11 @@ JSONValidator* RecipieLoader::zRecipieValidator()
                                       ->finishNumber()
                                       ->withRequiredAttribute("filter",
                                           dynamic_cast<JSONValidator*>(
-                                              filterValidator->getThis()))
+                                              typeFilterValidator->getThis()))
+                                      ->withRequiredAttribute("filter",
+                                          dynamic_cast<JSONValidator*>(
+                                              operatorFilterValidator
+                                                  ->getThis()))
                                       ->withRequiredAttribute("modifiers",
                                           JSONValidator::buildForArray()
                                               ->withDefault(Parser::getValue(
@@ -354,7 +421,8 @@ JSONValidator* RecipieLoader::zRecipieValidator()
                               ->finishArray())
                       ->finishObject())
               ->finishArray();
-    filterValidator->release();
+    typeFilterValidator->release();
+    operatorFilterValidator->release();
     outputValidator->release();
     return validator;
 }