Exemplo n.º 1
0
        ConfigStatus SetEnumProperty <T>(
            T assignObject, PropertyInfo property, string paramName, string value)
        {
            Type propertyType   = property.PropertyType;
            var  expectedValues = new List <string>();

            foreach (string fieldName in Enum.GetNames(propertyType))
            {
                FieldInfo fieldInfo = propertyType.GetField(fieldName);
                var       attr      = (MapValueAttribute)fieldInfo
                                      .GetCustomAttributes(typeof(MapValueAttribute), false).SingleOrDefault();
                if (attr == null)
                {
                    continue;
                }

                if (value.ToLower() == attr.QueryParameterValue.ToLower())
                {
                    property.SetValue(assignObject, Enum.Parse(propertyType, fieldName));
                    return(ConfigStatus.OkStatus());
                }

                expectedValues.Add(attr.QueryParameterValue);
            }

            return(ConfigStatus.WarningStatus(
                       ErrorStrings.InvalidEnumValue(paramName, value, expectedValues)));
        }
Exemplo n.º 2
0
        ConfigStatus AddBoolEnvironmentVariable(LaunchGameRequest launchRequest,
                                                IDictionary <string, string> queryParams,
                                                string variableName, string queryParamName)
        {
            if (!queryParams.ContainsKey(queryParamName) ||
                launchRequest.EnvironmentVariablePairs.ContainsKey(variableName))
            {
                return(ConfigStatus.OkStatus());
            }

            if (!bool.TryParse(queryParams[queryParamName], out bool isTrue))
            {
                if (!int.TryParse(queryParams[queryParamName], out int intVal) || intVal < 0 ||
                    intVal > 1)
                {
                    return(ConfigStatus.WarningStatus(ErrorStrings.InvalidQueryParameterType(
                                                          queryParamName,
                                                          queryParams[queryParamName],
                                                          typeof(bool))));
                }

                isTrue = intVal == 1;
            }

            if (isTrue)
            {
                launchRequest.EnvironmentVariablePairs.Add(variableName, "1");
            }

            return(ConfigStatus.OkStatus());
        }
Exemplo n.º 3
0
 ConfigStatus SetUlongProperty <T>(
     T assignObject, PropertyInfo property, string paramName, string value)
 {
     if (!ulong.TryParse(value, out ulong ulongValue))
     {
         return(ConfigStatus.WarningStatus(
                    ErrorStrings.InvalidQueryParameterType(paramName, value, typeof(ulong))));
     }
     property.SetValue(assignObject, ulongValue);
     return(ConfigStatus.OkStatus());
 }
Exemplo n.º 4
0
 ConfigStatus SetIntProperty <T>(
     T assignObject, PropertyInfo property, string paramName, string value)
 {
     if (!int.TryParse(value, out int intValue))
     {
         return(ConfigStatus.WarningStatus(
                    ErrorStrings.InvalidQueryParameterType(paramName, value, typeof(int))));
     }
     property.SetValue(assignObject, intValue);
     return(ConfigStatus.OkStatus());
 }
Exemplo n.º 5
0
        ConfigStatus SetBoolProperty <T>(
            T assignObject, PropertyInfo property, string paramName, string value)
        {
            value = value.ToLower();
            if (!bool.TryParse(value, out bool boolValue))
            {
                if (!int.TryParse(value, out int intVal) || intVal < 0 || intVal > 1)
                {
                    return(ConfigStatus.WarningStatus(
                               ErrorStrings.InvalidQueryParameterType(paramName, value, typeof(bool))));
                }

                boolValue = intVal == 1;
            }
            property.SetValue(assignObject, boolValue);
            return(ConfigStatus.OkStatus());
        }
Exemplo n.º 6
0
        /// <summary>
        /// Parses the query string to a dictionary.
        /// Values in the dictionary contain decoded special characters.
        /// For example: the value 'value%3D%C3%BC' in the query string will be 'value=ü'
        /// in the resulting dictionary.
        /// </summary>
        ConfigStatus ToQueryParams(string queryString,
                                   out IDictionary <string, string> queryParams)
        {
            queryParams = new Dictionary <string, string>();
            if (string.IsNullOrEmpty(queryString))
            {
                return(ConfigStatus.OkStatus());
            }

            try
            {
                NameValueCollection nameValueCollection;
                try
                {
                    // May throw an exception when maximum number of keys is exceeded.
                    nameValueCollection = HttpUtility.ParseQueryString(queryString);
                }
                catch (Exception e)
                {
                    throw new ApplicationException(e.Message);
                }

                foreach (string key in nameValueCollection.AllKeys)
                {
                    if (string.IsNullOrWhiteSpace(key))
                    {
                        throw new ApplicationException("Parameter 'Key' can not be empty.");
                    }

                    queryParams.Add(key, nameValueCollection.Get(key));
                }

                LogQueryString(queryString, queryParams);
                return(ConfigStatus.OkStatus());
            }
            catch (ApplicationException e)
            {
                Trace.TraceWarning($"Error happened while parsing query string. {e.Message}");
                queryParams = new Dictionary <string, string>();
                return(ConfigStatus.WarningStatus(
                           ErrorStrings.QueryParametersWrongFormat));
            }
        }