Exemplo n.º 1
0
        static int GetStringEnum(StringEnum.Lookup lookup, ulong value, byte[] buffer, Type enumType)
        {
            bool isFlag = enumType.GetCustomAttributes(typeof(FlagsAttribute), false).Length > 0;

            int  @sizeof;
            bool signed;

            switch (Type.GetTypeCode(enumType))
            {
            case TypeCode.SByte:
                @sizeof = 1;
                signed  = true;
                break;

            case TypeCode.Int16:
                @sizeof = 2;
                signed  = true;
                break;

            case TypeCode.Int32:
                @sizeof = 4;
                signed  = true;
                break;

            case TypeCode.Int64:
                @sizeof = 8;
                signed  = true;
                break;

            default:
                @sizeof = -1;
                signed  = false;
                break;
            }

            int result;

            fixed(byte *destination = buffer)
            {
                switch (lookup.Type)
                {
                case StringEnum.LookupTypes.Indexed:
                    if (signed)
                    {
                        if (isFlag)
                        {
                            result = StringEnum.WriteIndexedFlag((long)value, lookup.TableStart, lookup.StringStart, destination, buffer.Length, @sizeof);
                        }
                        else
                        {
                            result = StringEnum.WriteIndexed((long)value, lookup.TableStart, lookup.StringStart, destination, buffer.Length);
                        }
                    }
                    else
                    {
                        if (isFlag)
                        {
                            result = StringEnum.WriteIndexedFlag(value, lookup.TableStart, lookup.StringStart, destination, buffer.Length);
                        }
                        else
                        {
                            result = StringEnum.WriteIndexed(value, lookup.TableStart, lookup.StringStart, destination, buffer.Length);
                        }
                    }
                    break;

                case StringEnum.LookupTypes.Sorted:
                    if (signed)
                    {
                        if (isFlag)
                        {
                            result = StringEnum.WriteSortedFlag((long)value, lookup.TableStart, lookup.StringStart, destination, buffer.Length, @sizeof);
                        }
                        else
                        {
                            result = StringEnum.WriteSorted((long)value, lookup.TableStart, lookup.StringStart, destination, buffer.Length);
                        }
                    }
                    else
                    {
                        if (isFlag)
                        {
                            result = StringEnum.WriteSortedFlag(value, lookup.TableStart, lookup.StringStart, destination, buffer.Length);
                        }
                        else
                        {
                            result = StringEnum.WriteSorted(value, lookup.TableStart, lookup.StringStart, destination, buffer.Length);
                        }
                    }
                    break;

                case StringEnum.LookupTypes.Verbose:
                    if (signed)
                    {
                        if (isFlag)
                        {
                            result = StringEnum.WriteVerboseFlag((long)value, lookup.TableStart, lookup.StringStart, destination, buffer.Length, @sizeof);
                        }
                        else
                        {
                            result = StringEnum.WriteVerbose((long)value, lookup.TableStart, lookup.StringStart, destination, buffer.Length);
                        }
                    }
                    else
                    {
                        if (isFlag)
                        {
                            result = StringEnum.WriteVerboseFlag(value, lookup.TableStart, lookup.StringStart, destination, buffer.Length);
                        }
                        else
                        {
                            result = StringEnum.WriteVerbose(value, lookup.TableStart, lookup.StringStart, destination, buffer.Length);
                        }
                    }
                    break;

                case StringEnum.LookupTypes.Numeric:
                    if (signed)
                    {
                        result = Serializer.WriteInt64((long)value, destination, buffer.Length);
                    }
                    else
                    {
                        result = Serializer.WriteUInt64(value, destination, buffer.Length);
                    }
                    break;

                default:
                    throw new NotImplementedException();
                }
            }

            return(result);
        }