示例#1
0
        public static Flags DecodeFlagsText(string text)
        {
            var properties     = typeof(Flags).GetProperties(BindingFlags.Instance | BindingFlags.Public);
            var flagproperties = properties.Where(p => p.CanWrite).OrderBy(p => p.Name).Reverse().ToList();

            var sum   = StringToBigInteger(text);
            var flags = new Flags();

            foreach (var p in flagproperties)
            {
                if (Nullable.GetUnderlyingType(p.PropertyType) == typeof(bool))
                {
                    p.SetValue(flags, GetTriState(ref sum));
                }
                else if (p.PropertyType == typeof(bool))
                {
                    p.SetValue(flags, GetBoolean(ref sum));
                }
                else if (p.PropertyType.IsEnum)
                {
                    p.SetValue(flags, GetNumeric(ref sum, Enum.GetValues(p.PropertyType).Cast <int>().Max() + 1));
                }
                else if (p.PropertyType == typeof(int))
                {
                    IntegerFlagAttribute ia = p.GetCustomAttribute <IntegerFlagAttribute>();
                    var radix   = (ia.Max - ia.Min) / ia.Step + 1;
                    var raw_val = GetNumeric(ref sum, radix);
                    var val     = raw_val * ia.Step + ia.Min;
                    p.SetValue(flags, val);
                }
                else if (p.PropertyType == typeof(double))
                {
                    DoubleFlagAttribute ia = p.GetCustomAttribute <DoubleFlagAttribute>();
                    var radix   = (int)Math.Ceiling((ia.Max - ia.Min) / ia.Step) + 1;
                    var raw_val = GetNumeric(ref sum, radix);
                    var val     = Math.Min(Math.Max(raw_val * ia.Step + ia.Min, ia.Min), ia.Max);
                    p.SetValue(flags, val);
                }
            }

            string EncodedSha = GetString(ref sum, 7);

            if (((FFRVersion.Sha.Length >= 7) ? FFRVersion.Sha.Substring(0, 7) : FFRVersion.Sha.PadRight(7, 'X')) != EncodedSha)
            {
                throw new Exception("The encoded version does not match the expected version");
            }

            return(flags);
        }
示例#2
0
        public static string EncodeFlagsText(Flags flags)
        {
            var properties     = typeof(Flags).GetProperties(BindingFlags.Instance | BindingFlags.Public);
            var flagproperties = properties.Where(p => p.CanWrite).OrderBy(p => p.Name).ToList();

            BigInteger sum = 0;

            sum = AddString(sum, 7, (FFRVersion.Sha.Length >= 7) ? FFRVersion.Sha.Substring(0, 7) : FFRVersion.Sha.PadRight(7, 'X'));

            foreach (var p in flagproperties)
            {
                if (Nullable.GetUnderlyingType(p.PropertyType) == typeof(bool))
                {
                    sum = AddTriState(sum, (bool?)p.GetValue(flags));
                }
                else if (p.PropertyType == typeof(bool))
                {
                    sum = AddBoolean(sum, (bool)p.GetValue(flags));
                }
                else if (p.PropertyType.IsEnum)
                {
                    sum = AddNumeric(sum, Enum.GetValues(p.PropertyType).Cast <int>().Max() + 1, (int)p.GetValue(flags));
                }
                else if (p.PropertyType == typeof(int))
                {
                    IntegerFlagAttribute ia = p.GetCustomAttribute <IntegerFlagAttribute>();
                    var radix   = (ia.Max - ia.Min) / ia.Step + 1;
                    var val     = (int)p.GetValue(flags);
                    var raw_val = (val - ia.Min) / ia.Step;
                    sum = AddNumeric(sum, radix, raw_val);
                }
                else if (p.PropertyType == typeof(double))
                {
                    DoubleFlagAttribute ia = p.GetCustomAttribute <DoubleFlagAttribute>();
                    var radix   = (int)Math.Ceiling((ia.Max - ia.Min) / ia.Step) + 1;
                    var val     = (double)p.GetValue(flags);
                    var raw_val = (int)Math.Round((val - ia.Min) / ia.Step);
                    sum = AddNumeric(sum, radix, raw_val);
                }
            }

            return(BigIntegerToString(sum));
        }