public override object GetValue(string variableName, ExpressionEvaluator evaluator, Creature player)
        {
            if (fieldNames.IndexOf(variableName) >= 0)
            {
                FieldInfo field = typeof(Character).GetField(variableName);

                object value = field?.GetValue(player);
                CheckValue(player, field, ref value);
                return(value);
            }

            if (propertyNames.IndexOf(variableName) >= 0)
            {
                PropertyInfo property = typeof(Character).GetProperty(variableName);

                object value = property?.GetValue(player);
                CheckValue(player, property, ref value);
                return(value);
            }

            return(player.GetState(variableName));
        }
Пример #2
0
        public override object Evaluate(List <string> args, ExpressionEvaluator evaluator, Creature player, Target target, CastedSpell spell, RollResults dice = null)
        {
            ExpectingArguments(args, 1);

            string propertyName = args[0];

            FieldInfo field = typeof(Character).GetField(propertyName);

            if (field != null)
            {
                return(field.GetValue(player));
            }

            PropertyInfo property = typeof(Character).GetProperty(propertyName);

            if (property != null)
            {
                return(property.GetValue(player));
            }

            if (KnownQualifiers.IsSpellQualifier(propertyName))
            {
                if (evaluator.Variables.ContainsKey(Expressions.STR_CastedSpell))
                {
                    object castedSpell  = evaluator.Variables[Expressions.STR_CastedSpell];
                    Type   instanceType = typeof(CastedSpell);
                    propertyName = propertyName.EverythingAfter(KnownQualifiers.Spell);
                    property     = instanceType.GetProperty(propertyName);
                    return(property.GetValue(castedSpell));
                }
            }

            if (player != null)
            {
                return(player.GetState(propertyName));
            }

            return(null);
        }
Пример #3
0
        public override object Evaluate(List <string> args, ExpressionEvaluator evaluator, Creature player, Target target, CastedSpell spell, RollResults dice = null)
        {
            if (player == null)
            {
                return(null);
            }

            ExpectingArguments(args, 2);

            string variableName = args[0];
            object rawValue     = Expressions.Get <object>(args[1], player, target, spell);
            double valueDouble;

            if (rawValue == null)
            {
                valueDouble = 0;
            }
            else
            {
                valueDouble = MathUtils.GetDouble(rawValue.ToString());
            }
            int valueInt = (int)Math.Round(valueDouble);

            // TODO: Wil says Convert.ConvertTo() can simplify this.

            FieldInfo field = typeof(Character).GetField(variableName);

            if (field != null)
            {
                if (field.FieldType.FullName == "System.Int32")
                {
                    field.SetValue(player, MathUtils.GetInt(field.GetValue(player).ToString()) + valueInt);
                }
                else
                {
                    field.SetValue(player, MathUtils.GetDouble(field.GetValue(player).ToString()) + valueDouble);
                }
                return(null);
            }

            PropertyInfo property = typeof(Character).GetProperty(variableName);

            if (property != null)
            {
                if (property.PropertyType.FullName == "System.Int32")
                {
                    property.SetValue(player, MathUtils.GetInt(property.GetValue(player).ToString()) + valueInt);
                }
                else
                {
                    property.SetValue(player, MathUtils.GetDouble(property.GetValue(player).ToString()) + valueDouble);
                }
                return(null);
            }

            object existingValueRaw = player.GetState(variableName);

            if (existingValueRaw != null)
            {
                if (existingValueRaw is int)
                {
                    player.SetState(variableName, MathUtils.GetInt(existingValueRaw.ToString()) + valueInt);
                }
                else
                {
                    player.SetState(variableName, MathUtils.GetDouble(existingValueRaw.ToString()) + valueDouble);
                }
                return(null);
            }

            throw new Exception($"Variable \"{variableName}\" not found.");
        }