// Returns true if error, false if success
        private bool UtilAddToNumberedAttributeToArray <T>(string nameFound,
                                                           IConstraintChecker <T> constraints,
                                                           List <T> resultList)
        {
            string baseName = nameFound;

            // Vector3s require special handling because there are three attributes comprising the value...
            if (typeof(T) == typeof(Vector3))
            {
                // Adjust basename to remove X/Y/Z suffix...
                if ((nameFound.EndsWith("X") || nameFound.EndsWith("Y") || nameFound.EndsWith("Z")))
                {
                    baseName = nameFound.Substring(0, nameFound.Length - 1);
                }

                // If this is not the "X" key, and the "X" key exists, then skip processing...
                // If we don't, then the Vector3 will be placed into the list three times.
                if (!nameFound.EndsWith("X") && Attributes.ContainsKey(baseName + "X"))
                {
                    return(false);
                }
            }

            object tmpResult = UtilGetAttributeAs <T>(baseName, false, constraints, null);

            if (tmpResult == null)
            {
                return(true);
            }

            resultList.Add((T)tmpResult);
            return(false);
        }
示例#2
0
 public void SetConstraintChecker(IConstraintChecker constraintChecker)
 {
     _constraintChecker = constraintChecker;
 }
示例#3
0
 public Bowling()
 {
     this._constraintChecker = new ConstraintChecker();
 }
        private object UtilGetAttributeAs <T>(string attributeName, bool isAttributeRequired, IConstraintChecker <T> constraints, string[] attributeNameAliases)
        {
            Type concreteType = typeof(T);

            // Vector3 are a triple of attributes, so requires special handling...
            if (concreteType == typeof(Vector3))
            {
                return(UtilGetXYZAttributesAsVector3(attributeName, isAttributeRequired, attributeNameAliases));
            }


            constraints = constraints ?? new ConstrainTo.Anything <T>();

            string keyName = UtilLocateKey(isAttributeRequired, attributeName, attributeNameAliases);

            if ((keyName == null) || !Attributes.ContainsKey(keyName))
            {
                return(null);
            }

            T      tmpResult;
            string valueAsString = Attributes[keyName];

            try
            { tmpResult = UtilTo <T>(keyName, valueAsString); }
            catch (Exception)
            {
                IsAttributeProblem = true;
                return(null);
            }

            string constraintViolationMessage = constraints.Check(keyName, tmpResult);

            if (constraintViolationMessage != null)
            {
                QBCLog.Error(QBCLog.BuildMessageWithContext(Element, constraintViolationMessage));
                IsAttributeProblem = true;
                return(null);
            }

            return(tmpResult);
        }
        public T[] GetNumberedAttributesAsArray <T>(string baseName, int countRequired, IConstraintChecker <T> constraints, IEnumerable <string> aliasBaseNames)
        {
            bool isError    = false;
            bool isVector3  = (typeof(T) == typeof(Vector3));
            var  resultList = new List <T>();

            // Search for primary names first --
            // We 'continue' even if problems are encountered.  By doing this, the profile writer can see
            // all his mistakes at once, rather than being nickel-and-dimed to death with error messages.
            var primaryAttributeNames = from attributeName in Attributes.Keys
                                        where UtilIsNumberedAttribute(baseName, attributeName, isVector3)
                                        select attributeName;

            foreach (var numberedAttributeName in primaryAttributeNames)
            {
                isError |= UtilAddToNumberedAttributeToArray <T>(numberedAttributeName, constraints, resultList);
            }

            // Search using alias names --
            // We 'continue' even if problems are encountered.  By doing this, the profile writer can see
            // all his mistakes at once, rather than being nickel-and-dimed to death with error messages.
            if (aliasBaseNames != null)
            {
                var aliasAttributeNames = from aliasBaseName in aliasBaseNames
                                          from attributeName in Attributes.Keys
                                          where UtilIsNumberedAttribute(aliasBaseName, attributeName, isVector3)
                                          select attributeName;

                foreach (var numberedAttributeName in aliasAttributeNames)
                {
                    isError |= UtilAddToNumberedAttributeToArray <T>(numberedAttributeName, constraints, resultList);
                }
            }


            if (resultList.Count < countRequired)
            {
                QBCLog.Error(QBCLog.BuildMessageWithContext(Element,
                                                            "The attribute '{1}N' must be provided at least {2} times (saw it '{3}' times).{0}"
                                                            + "(E.g., ButtonText1, ButtonText2, ButtonText3, ...){0}"
                                                            + "Please modify to supply {2} attributes with a base name of '{1}'.",
                                                            Environment.NewLine,
                                                            baseName,
                                                            countRequired,
                                                            resultList.Count));
                isError = true;
            }


            if (isError)
            {
                resultList.Clear();
                IsAttributeProblem = true;
            }

            return(resultList.ToArray());
        }
 public T?GetAttributeAsNullable <T>(string attributeName, bool isAttributeRequired, IConstraintChecker <T> constraints, string[] attributeNameAliases)
     where T : struct
 {
     return((T?)UtilGetAttributeAs <T>(attributeName, isAttributeRequired, constraints, attributeNameAliases));
 }
        public T[] GetAttributeAsArray <T>(string attributeName, bool isAttributeRequired, IConstraintChecker <T> constraints, string[] attributeNameAliases,
                                           char[] separatorCharacters)
        {
            // Vector3 are triples, so requires special handling...
            if (typeof(T) == typeof(Vector3))
            {
                return((T[])UtilGetAttributeAsVector3s(attributeName, isAttributeRequired, attributeNameAliases));
            }


            constraints         = constraints ?? new ConstrainTo.Anything <T>();
            separatorCharacters = separatorCharacters ?? new[] { ' ', ',', ';' };

            bool   isError    = false;
            string keyName    = UtilLocateKey(isAttributeRequired, attributeName, attributeNameAliases);
            var    resultList = new List <T>();

            if ((keyName == null) || !Attributes.ContainsKey(keyName))
            {
                resultList.Clear();
                return(resultList.ToArray());
            }

            // We 'continue' even if problems are encountered...
            // By doing this, the profile writer can see all his mistakes at once, rather than being
            // nickel-and-dimed to death with error messages.
            foreach (string listEntry in Attributes[keyName].Split(separatorCharacters, StringSplitOptions.RemoveEmptyEntries))
            {
                T tmpResult;

                try
                { tmpResult = UtilTo <T>(keyName, listEntry); }

                catch (Exception)
                {
                    isError = true;
                    continue;
                }

                string constraintViolationMessage = constraints.Check(keyName, tmpResult);
                if (constraintViolationMessage != null)
                {
                    QBCLog.Error(QBCLog.BuildMessageWithContext(Element, constraintViolationMessage));
                    isError = true;
                    continue;
                }

                resultList.Add(tmpResult);
            }

            if (isError)
            {
                resultList.Clear();
                IsAttributeProblem = true;
            }

            return(resultList.ToArray());
        }
 public T GetAttributeAs <T>(string attributeName, bool isAttributeRequired, IConstraintChecker <T> constraints, string[] attributeNameAliases)
     where T : class
 {
     return((T)UtilGetAttributeAs <T>(attributeName, isAttributeRequired, constraints, attributeNameAliases));
 }