コード例 #1
0
        public VariableValue GetValue(IVariableStore variables)
        {
            var value = IsAssigned ? VariableValue.Create(variables) : VariableValue.Empty;

            foreach (var token in _tokens)
            {
                switch (token.Type)
                {
                case VariableTokenType.Name: value = VariableHandler.Lookup(value, VariableValue.Create(token.Text)); break;

                case VariableTokenType.Number: value = VariableHandler.Lookup(value, VariableValue.Create(int.Parse(token.Text))); break;

                case VariableTokenType.Type: value = VariableHandler.Cast(value, token.Text); break;
                }

                if (value.IsEmpty)
                {
                    break;
                }
            }

            return(value);
        }
コード例 #2
0
        private SetVariableResult SetValue_(ref VariableValue owner, VariableValue value, int index)
        {
            var token = _tokens[index];

            if (index == _tokens.Count - 1)
            {
                switch (token.Type)
                {
                case VariableTokenType.Name:
                {
                    return(VariableHandler.Apply(ref owner, VariableValue.Create(token.Text), value));
                }

                case VariableTokenType.Number:
                {
                    if (int.TryParse(token.Text, out var number))
                    {
                        return(VariableHandler.Apply(ref owner, VariableValue.Create(number), value));
                    }

                    break;
                }

                case VariableTokenType.Type:
                {
                    return(SetVariableResult.ReadOnly);
                }
                }
            }
            else
            {
                var lookup = VariableValue.Empty;
                var child  = VariableValue.Empty;

                switch (token.Type)
                {
                case VariableTokenType.Name:
                {
                    lookup = VariableValue.Create(token.Text);
                    child  = VariableHandler.Lookup(owner, lookup);
                    break;
                }

                case VariableTokenType.Number:
                {
                    if (int.TryParse(token.Text, out var number))
                    {
                        lookup = VariableValue.Create(number);
                        child  = VariableHandler.Lookup(owner, lookup);
                    }

                    break;
                }

                case VariableTokenType.Type:
                {
                    child = VariableHandler.Cast(owner, token.Text);
                    break;
                }
                }

                if (!child.IsEmpty)
                {
                    // if a value is set on a struct (Vector3.x for instance), the variable value needs to be
                    // reassigned to its owner

                    var result = SetValue_(ref child, value, index + 1);

                    if (result == SetVariableResult.Success && !child.HasReference)
                    {
                        result = VariableHandler.Apply(ref owner, lookup, child);
                    }

                    return(result);
                }
            }

            return(SetVariableResult.NotFound);
        }