/// <summary> /// Executes this modification. /// </summary> public bool Execute(IVariantResolver inResolver, object inContext = null, IMethodCache inInvoker = null) { Variant operandValue; if (Operand.TryResolve(inResolver, inContext, out operandValue, inInvoker)) { return(inResolver.TryModify(inContext, VariableKey, Operator, operandValue)); } return(false); }
/// <summary> /// Attempts to apply a modification. /// </summary> static public bool TryModify(this IVariantResolver inResolver, object inContext, TableKeyPair inKey, VariantModifyOperator inOperator, Variant inVariant) { inResolver.RemapKey(ref inKey); VariantTable table; bool bRetrieved = inResolver.TryGetTable(inContext, inKey.TableId, out table); if (!bRetrieved || table == null) { UnityEngine.Debug.LogErrorFormat("[IVariantResolver] Unable to retrieve table with id '{0}'", inKey.TableId.ToDebugString()); return(false); } table.Modify(inKey.VariableId, inOperator, inVariant); return(true); }
/// <summary> /// Attempts to resolve the value. /// </summary> public bool TryResolve(IVariantResolver inResolver, object inContext, out Variant outValue, IMethodCache inInvoker = null) { switch (Type) { case Mode.TableKey: { return(inResolver.TryResolve(inContext, TableKey, out outValue)); } case Mode.Variant: { outValue = Value; return(true); } case Mode.Method: { if (inInvoker == null) { throw new ArgumentNullException("inInvoker", "No IMethodCache provided - cannot invoke a method call operand"); } object obj; if (!inInvoker.TryStaticInvoke(MethodCall, inContext, out obj)) { Log.Error("[VariantOperand] Unable to execute {0}", MethodCall); outValue = default(Variant); return(false); } if (!Variant.TryConvertFrom(obj, out outValue)) { Log.Error("[VariantOperand] Unable to convert result of {0} ({1}) to Variant", MethodCall, obj); outValue = default(Variant); return(false); } return(true); } default: throw new InvalidOperationException("Unknown operand type " + Type.ToString()); } }
/// <summary> /// Evaluates this comparison. /// </summary> public bool Evaluate(IVariantResolver inResolver, object inContext = null, IMethodCache inInvoker = null) { Variant leftValue, rightValue; bool bRetrievedLeft = Left.TryResolve(inResolver, inContext, out leftValue, inInvoker); bool bRetrievedRight = Right.TryResolve(inResolver, inContext, out rightValue, inInvoker); switch (Operator) { case VariantCompareOperator.LessThan: return(leftValue < rightValue); case VariantCompareOperator.LessThanOrEqualTo: return(leftValue <= rightValue); case VariantCompareOperator.EqualTo: return(leftValue == rightValue); case VariantCompareOperator.NotEqualTo: return(leftValue != rightValue); case VariantCompareOperator.GreaterThanOrEqualTo: return(leftValue >= rightValue); case VariantCompareOperator.GreaterThan: return(leftValue > rightValue); case VariantCompareOperator.Exists: return(bRetrievedLeft); case VariantCompareOperator.DoesNotExist: return(!bRetrievedLeft); case VariantCompareOperator.True: return(leftValue.AsBool()); case VariantCompareOperator.False: return(!leftValue.AsBool()); default: throw new InvalidOperationException("Unknown operator " + Operator.ToString()); } }
/// <summary> /// Attempts to evaluate if all conditions described by the given string are true. /// </summary> static public bool TryEvaluate(this IVariantResolver inResolver, object inContext, StringSlice inEvalData, IMethodCache inInvoker = null) { if (inEvalData.IsWhitespace) { return(true); } StringSlice.ISplitter splitter = QuoteAwareSplitter ?? (QuoteAwareSplitter = new StringUtils.ArgsList.Splitter(false)); VariantComparison comp; foreach (var group in inEvalData.EnumeratedSplit(splitter, StringSplitOptions.RemoveEmptyEntries)) { if (!VariantComparison.TryParse(group, out comp) || !comp.Evaluate(inResolver, inContext, inInvoker)) { return(false); } } return(true); }
/// <summary> /// Adds a variant resolver to be run when the condition evaluates to false. /// </summary> /// <param name="ifFalse">The resolver run when any of the variants is not there.</param> public VariantCondition Else(IVariantResolver ifFalse) { if (_ifFalse == null) { _ifFalse = ifFalse; } else { if (_ifFalse is VariantGroup group) { group.With(ifFalse); } else { group = new VariantGroup(); group.With(ifFalse); _ifFalse = group; } } return(this); }
/// <summary> /// Attempts to apply one or more modifications, described by the given string. /// </summary> static public bool TryModify(this IVariantResolver inResolver, object inContext, StringSlice inModifyData, IMethodCache inInvoker = null) { if (inModifyData.IsWhitespace) { return(true); } StringSlice.ISplitter splitter = QuoteAwareSplitter ?? (QuoteAwareSplitter = new StringUtils.ArgsList.Splitter(false)); bool bSuccess = true; VariantModification mod; foreach (var group in inModifyData.EnumeratedSplit(splitter, StringSplitOptions.RemoveEmptyEntries)) { if (!VariantModification.TryParse(group, out mod) || !mod.Execute(inResolver, inContext, inInvoker)) { bSuccess = false; } } return(bSuccess); }
/// <summary> /// Attempts to resolve a variable. /// </summary> static public bool TryResolve(this IVariantResolver inResolver, object inContext, TableKeyPair inKey, out Variant outVariant) { inResolver.RemapKey(ref inKey); bool bRetrieved = inResolver.TryGetVariant(inContext, inKey, out outVariant); if (bRetrieved) { return(true); } VariantTable table; bool bFoundTable = inResolver.TryGetTable(inContext, inKey.TableId, out table); if (!bFoundTable || table == null) { UnityEngine.Debug.LogErrorFormat("[IVariantResolver] Unable to retrieve table with id '{0}'", inKey.TableId.ToDebugString()); outVariant = Variant.Null; return(false); } return(table.TryLookup(inKey.VariableId, out outVariant)); }