Пример #1
0
        /// <summary>
        /// Provides high-level validation looking for obvious syntactic errors, throwing
        /// ArgumentException if found. Note that just because a string passes this function,
        /// that doesn't guarantee that it will work when used on a part module. That's
        /// because *real* validation needs the module as input, so it can hunt for fields,
        /// other modules, etc.  This function's validation only covers the *syntax*, not
        /// the content.
        /// </summary>
        /// <param name="sourceID"></param>
        public static void Validate(string sourceID)
        {
            if (string.IsNullOrEmpty(sourceID))
            {
                throw new ArgumentException("Null or empty color source");
            }

            // Maybe it's a color string.
            if (Colors.IsColorString(sourceID))
            {
                return;
            }

            // Is it a parameterized source?
            if (ParsedParameters.TryParse(sourceID) != null)
            {
                return;
            }

            // Is it an identifier?  (e.g. module or field name)
            if (Identifiers.IsSimpleIdentifier(sourceID))
            {
                return;
            }

            // Could it be a numeric value?
            try
            {
                double.Parse(sourceID);
                return;
            }
            catch (FormatException)
            {
                // nope,  not a number
            }

            // If we made it this far, it doesn't appear to be syntactically valid.
            throw new ArgumentException("Invalid color source syntax: " + sourceID);
        }
Пример #2
0
        /// <summary>
        /// Given a color source ID (which might be a literal color, the ID of a controller,
        /// or a parameterized source), try to find the appropriate source. If it can't
        /// be parsed or found, throws a ColorSourceException.
        private static IColorSource FindPrivate(PartModule module, string sourceID)
        {
            if (string.IsNullOrEmpty(sourceID))
            {
                throw new ColorSourceException(module, "Null or empty color source");
            }

            // Maybe it's a color string.
            if (Colors.IsColorString(sourceID))
            {
                return(Constant(Colors.Parse(sourceID, Color.black)));
            }

            // Is it a parameterized source?
            ParsedParameters parsedParams = ParsedParameters.TryParse(sourceID);

            if (parsedParams != null)
            {
                // It has the right syntax for a parameterizeed source. Do we recognize it?
                for (int i = 0; i < PARSEABLE_SOURCES.Length; ++i)
                {
                    IColorSource candidate = PARSEABLE_SOURCES[i](module, parsedParams);
                    if (candidate != null)
                    {
                        return(candidate);
                    }
                }
                throw new ColorSourceException(module, "Unknown function type '" + parsedParams.Identifier + "'");
            }

            // Maybe it's another field on the module?
            for (int i = 0; i < module.Fields.Count; ++i)
            {
                BaseField field = module.Fields[i];
                if (!sourceID.Equals(field.name))
                {
                    continue;
                }
                if (!ColorSourceIDField.Is(field))
                {
                    throw new ColorSourceException(
                              module, sourceID + " field on " + module.ClassName + " of "
                              + module.part.GetTitle() + " is not a color source ID field");
                }
                return(Find(module, field.GetValue <string>(module)));
            }

            // Maybe it's a module on the part?
            for (int i = 0; i < module.part.Modules.Count; ++i)
            {
                IColorSource candidate = module.part.Modules[i] as IColorSource;
                if (candidate == null)
                {
                    continue;
                }
                if (sourceID.Equals(candidate.ColorSourceID))
                {
                    return(candidate);
                }
            }

            // not found
            throw new ColorSourceException(module, "Can't find a color source named '" + sourceID + "'");
        }