/// <summary> /// Tries to process specified logic. Will return result of conditional logic, or false for common states. /// </summary> /// <param name="logic">Logic block to process.</param> public bool ProcessLogic(QuestLogic logic) { switch (logic.operation) { case "+=": this.SetVariable(logic.lhs, this.GetVariable(logic.lhs) + logic.rhs); return(false); case "=": this.SetVariable(logic.lhs, logic.rhs); return(false); case "==": return(this.GetVariable(logic.lhs) == logic.rhs); case "!=": return(this.GetVariable(logic.lhs) != logic.rhs); case "<": return(this.GetVariable(logic.lhs) < logic.rhs); case ">": return(this.GetVariable(logic.lhs) > logic.rhs); default: return(false); } }
static QuestLogic ParseLogic(QuestLogicFilter filter, string code, int lineId) { var matchingFull = Regex.Match(code, "(\\w+)\\s*(?:([<>=+!]+)\\s*(-?\\w+)?)?"); if (!matchingFull.Success || (!string.IsNullOrEmpty(matchingFull.Groups[2].Value) && string.IsNullOrEmpty(matchingFull.Groups[3].Value))) { throw new Exception(string.Format("Invalid logic syntax at line: {0}", lineId)); } var logic = new QuestLogic(); logic.lhs = matchingFull.Groups[1].Value.ToLowerInvariant(); if (!string.IsNullOrEmpty(matchingFull.Groups[3].Value)) { logic.operation = matchingFull.Groups[2].Value; if (!int.TryParse(matchingFull.Groups[3].Value, out logic.rhs)) { throw new Exception(string.Format("Invalid expression at line: {0} , only numbers supported on right side", lineId)); } } else { logic.operation = ">"; logic.rhs = 0; } switch (logic.operation) { case "+=": case "=": if (filter != QuestLogicFilter.State) { throw new Exception(string.Format("Should be conditional expression at line: {0}", lineId)); } break; case "!=": case "==": case "<": case ">": if (filter != QuestLogicFilter.Expression) { throw new Exception(string.Format("Should be non conditional expression at line: {0}", lineId)); } break; default: throw new Exception(string.Format("Invalid logic operation at line: {0}", lineId)); } return(logic); }