/// <summary>
        /// Gets a list of operations supported by specific type.
        /// </summary>
        /// <param name="property">Property to get operations for.</param>
        /// <returns>Array of operations.</returns>
        public static UltimaPacketFilterTypeOperation[] GetTypeOperations(UltimaPacketPropertyDefinition property)
        {
            List <UltimaPacketFilterTypeOperation> operations = new List <UltimaPacketFilterTypeOperation>();
            TypeCode code = Type.GetTypeCode(property.Info.PropertyType);

            switch (code)
            {
            case TypeCode.Boolean: return(_BooleanOperations);

            case TypeCode.Byte:
            case TypeCode.Char:
            case TypeCode.Decimal:
            case TypeCode.Double:
            case TypeCode.Int16:
            case TypeCode.Int32:
            case TypeCode.Int64:
            case TypeCode.SByte:
            case TypeCode.Single:
            case TypeCode.UInt16:
            case TypeCode.UInt32:
            case TypeCode.UInt64: return(_NumberOperations);

            case TypeCode.String: return(_StringOperations);
            }

            return(null);
        }
Esempio n. 2
0
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            UltimaPacketPropertyDefinition definition = parameter as UltimaPacketPropertyDefinition;

            if (definition != null)
            {
                return(UltimaPacketFilterParser.GetTypeOperations(definition));
            }

            return(null);
        }
        /// <summary>
        /// Gets short composed filter.
        /// </summary>
        /// <param name="property">Filter property.</param>
        /// <param name="operation">Filter operation.</param>
        /// <param name="value">Filter value.</param>
        /// <returns>Composed filter.</returns>
        public static string GetComposedFilter(UltimaPacketPropertyDefinition property, UltimaPacketFilterTypeOperation operation, string value)
        {
            string format = GetShortDescription(operation);

            if (format != null)
            {
                return(String.Format(format, property, value));
            }

            return(null);
        }
        private void UpdateSample()
        {
            UltimaPacketFilterProperty      filter    = Filters.SelectedValue as UltimaPacketFilterProperty;
            UltimaPacketPropertyDefinition  property  = Definition.SelectedValue as UltimaPacketPropertyDefinition;
            UltimaPacketFilterTypeOperation?operation = Operation.SelectedValue as UltimaPacketFilterTypeOperation?;

            if (filter != null && property != null && operation != null)
            {
                SampleValue.Text = UltimaPacketFilterParser.GetSample(property, (UltimaPacketFilterTypeOperation)operation, false);
                Sample.Text      = UltimaPacketFilterParser.GetComposedSample(property, (UltimaPacketFilterTypeOperation)operation, false, false);
            }
        }
        /// <summary>
        /// Gets sample for property.
        /// </summary>
        /// <param name="property">Property definition.</param>
        /// <param name="operation">Property operation.</param>
        /// <param name="alternate">Determines whether to compose alternate sample.</param>
        /// <returns>Composed sample.</returns>
        public static string GetSample(UltimaPacketPropertyDefinition property, UltimaPacketFilterTypeOperation operation, bool alternate)
        {
            TypeCode code = Type.GetTypeCode(property.Info.PropertyType);

            if (operation == UltimaPacketFilterTypeOperation.Contains || operation == UltimaPacketFilterTypeOperation.In)
            {
                return(GetSampleList(code));
            }
            else if (alternate)
            {
                return(GetSampleOne(code));
            }
            else
            {
                return(GetSampleTwo(code));
            }
        }
        private void Definition_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            UltimaPacketFilterProperty     filter   = Filters.SelectedValue as UltimaPacketFilterProperty;
            UltimaPacketPropertyDefinition property = Definition.SelectedValue as UltimaPacketPropertyDefinition;

            if (property != null)
            {
                Operation.ItemsSource = UltimaPacketFilterParser.GetTypeOperations(property);
            }

            if (filter != null)
            {
                Operation.SelectedValue = null;
                Operation.SelectedValue = filter.Operation;
            }

            UpdateSample();
        }
        /// <summary>
        /// Determines if operation is valid for sepecific property.
        /// </summary>
        /// <param name="property">Property to check for.</param>
        /// <param name="operation">Operation to validate.</param>
        /// <param name="first">First valid operation if operation is not valid.</param>
        /// <returns>True if valid, false otherwise.</returns>
        public static bool IsValidOperation(UltimaPacketPropertyDefinition property, UltimaPacketFilterTypeOperation operation, ref UltimaPacketFilterTypeOperation first)
        {
            UltimaPacketFilterTypeOperation[] validOperations = GetTypeOperations(property);

            if (validOperations != null)
            {
                foreach (UltimaPacketFilterTypeOperation typeOperation in validOperations)
                {
                    if (operation == typeOperation)
                    {
                        return(true);
                    }
                }

                first = validOperations[0];
                return(false);
            }

            return(false);
        }
        /// <summary>
        /// Gets sample description for property.
        /// </summary>
        /// <param name="property">Property definition.</param>
        /// <param name="operation">Property operation.</param>
        /// <param name="shorty">Determines whether to compose short sample.</param>
        /// <param name="alternate">Determines whether to compose alternate sample.</param>
        /// <returns>Composed sample.</returns>
        public static string GetComposedSample(UltimaPacketPropertyDefinition property, UltimaPacketFilterTypeOperation operation, bool shorty, bool alternate)
        {
            string format = null;

            if (shorty)
            {
                format = GetShortDescription(operation);
            }
            else
            {
                format = GetLongDescription(operation);
            }

            if (format != null)
            {
                TypeCode code   = Type.GetTypeCode(property.Info.PropertyType);
                string   sample = null;

                if (operation == UltimaPacketFilterTypeOperation.Contains || operation == UltimaPacketFilterTypeOperation.In)
                {
                    sample = GetSampleList(code);
                }
                else if (alternate)
                {
                    sample = GetSampleOne(code);
                }
                else
                {
                    sample = GetSampleTwo(code);
                }

                if (sample != null)
                {
                    return(String.Format(format, property, sample));
                }
            }

            return(null);
        }
        /// <summary>
        /// Tries to parse string into specific type based on operation.
        /// </summary>
        /// <param name="value">Value to parse.</param>
        /// <param name="property">Property to parse to.</param>
        /// <param name="operation">Operation to parse for.</param>
        /// <param name="result">Result.</param>
        /// <returns>True if successful, false otherwise.</returns>
        public static bool TryParse(string value, UltimaPacketPropertyDefinition property, UltimaPacketFilterTypeOperation operation, out object result)
        {
            result = null;

            if (String.IsNullOrEmpty(value))
            {
                return(false);
            }

            TypeCode code = Type.GetTypeCode(property.Info.PropertyType);

            switch (operation)
            {
            case UltimaPacketFilterTypeOperation.Greater:
            case UltimaPacketFilterTypeOperation.Lesser:
            {
                return(TryParseSingle(value, code, ref result));
            }

            case UltimaPacketFilterTypeOperation.Contains:
            case UltimaPacketFilterTypeOperation.In:
            {
                string[] split = value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                if (split.Length > 0)
                {
                    switch (code)
                    {
                    case TypeCode.Boolean: result = TryParseList <Boolean>(split); break;

                    case TypeCode.Char: result = TryParseList <Char>(split); break;

                    case TypeCode.Byte: result = TryParseList <Byte>(split); break;

                    case TypeCode.Decimal: result = TryParseList <Decimal>(split); break;

                    case TypeCode.Double: result = TryParseList <Double>(split); break;

                    case TypeCode.Int16: result = TryParseList <Int16>(split); break;

                    case TypeCode.Int32: result = TryParseList <Int32>(split); break;

                    case TypeCode.Int64: result = TryParseList <Int64>(split); break;

                    case TypeCode.SByte: result = TryParseList <SByte>(split); break;

                    case TypeCode.Single: result = TryParseList <Single>(split); break;

                    case TypeCode.String: result = TryParseList <String>(split); break;

                    case TypeCode.UInt16: result = TryParseList <UInt16>(split); break;

                    case TypeCode.UInt32: result = TryParseList <UInt32>(split); break;

                    case TypeCode.UInt64: result = TryParseList <UInt64>(split); break;
                    }

                    if (result != null)
                    {
                        return(true);
                    }
                }

                return(false);
            }
            }

            return(false);
        }