コード例 #1
0
        public bool TryGetUInt(string key, PropertyConstraints constraint, out uint result, out string message,
                               IReloadable target = null)
        {
            Track(key, PropertyTypes.UnsignedInteger, target);

            if (!uint.TryParse(map[key], out result))
            {
                message = Format(key, PropertyTypes.UnsignedInteger);

                return(true);
            }

            // The signed integer validator can be reused here. Using a constraint of NonNegative doesn't really make
            // sense for an unsigned integer (since that's always true), but it's also not an error.
            if (Validate((int)result, constraint))
            {
                message = null;

                return(true);
            }

            message = Format(key, constraint);

            return(false);
        }
コード例 #2
0
        private string Format(string key, PropertyConstraints constraint)
        {
            var tokens = key.Split('.');

            tokens[0] = tokens[0].Capitalize();

            return($"{string.Join(" ", tokens)} must be {validationStrings[(int)constraint]}.");
        }
コード例 #3
0
        public bool TryGetFloat(string key, PropertyConstraints constraint, out string message, ref float result,
                                IReloadable target = null)
        {
            if (!TryGetFloat(key, constraint, out var value, out message, target))
            {
                return(false);
            }

            result = value;

            return(true);
        }
コード例 #4
0
        private bool Validate(int i, PropertyConstraints constraint)
        {
            switch (constraint)
            {
            case PropertyConstraints.None: return(true);

            case PropertyConstraints.NonNegative: return(i >= 0);

            case PropertyConstraints.Positive: return(i > 0);
            }

            Debug.Fail($"Invalid integer constraint ({constraint}).");

            return(false);
        }
コード例 #5
0
        public int GetInt(string key, PropertyConstraints constraint = PropertyConstraints.Positive)
        {
            Track(key, PropertyTypes.Integer, null);

            if (!int.TryParse(map[key], out var result))
            {
                Debug.Fail(Format(key, PropertyTypes.Integer));
            }

            if (!Validate(result, constraint))
            {
                Debug.Fail(Format(key, constraint));
            }

            return(result);
        }
コード例 #6
0
        private bool Validate(float f, PropertyConstraints constraint)
        {
            switch (constraint)
            {
            case PropertyConstraints.None: return(true);

            case PropertyConstraints.NonNegative: return(f >= 0);

            case PropertyConstraints.Positive: return(f > 0);

            case PropertyConstraints.ZeroToOne: return(f >= 0 && f <= 1);
            }

            Debug.Fail($"Invalid float constraint ({constraint}).");

            return(false);
        }
コード例 #7
0
        public bool TryGetFloats(string[] keys, PropertyConstraints constraint, out Dictionary <string, float> results,
                                 out string message, IReloadable target = null)
        {
            results = new Dictionary <string, float>();

            foreach (var key in keys)
            {
                if (!TryGetFloat(key, constraint, out var result, out message, target))
                {
                    return(false);
                }

                results.Add(key, result);
            }

            message = null;

            return(true);
        }
コード例 #8
0
        public bool TryGetInt(string key, PropertyConstraints constraint, out int result, out string message,
                              IReloadable target = null)
        {
            Track(key, PropertyTypes.Integer, target);

            if (!int.TryParse(map[key], out result))
            {
                message = Format(key, PropertyTypes.Integer);

                return(true);
            }

            if (Validate(result, constraint))
            {
                message = null;

                return(true);
            }

            message = Format(key, constraint);

            return(false);
        }