예제 #1
0
        public static void ParseTypes(AdsStream dataTypeStream, Encoding encoding, IBinder binder, bool buildInTypesInStream, DataTypeCollection <IDataType> buildInTypes)
        {
            AdsBinaryReader reader         = new AdsBinaryReader(dataTypeStream);
            string          referencedType = null;

            while (dataTypeStream.Position < dataTypeStream.Length)
            {
                AdsDataTypeEntry entry = new AdsDataTypeEntry(true, encoding, reader);
                try
                {
                    IDataType type  = null;
                    bool      flag1 = buildInTypes.TryGetType(entry.entryName, out type);
                    DataType  type2 = (DataType)type;
                    if (buildInTypesInStream && flag1)
                    {
                        continue;
                    }
                    DataType type3  = null;
                    int      length = 0;
                    if (DataTypeStringParser.TryParseReference(entry.entryName, out referencedType))
                    {
                        type3 = new ReferenceType(entry, referencedType);
                    }
                    else if (DataTypeStringParser.TryParsePointer(entry.entryName, out referencedType))
                    {
                        type3 = new PointerType(entry, referencedType);
                    }
                    else
                    {
                        bool flag2;
                        if (DataTypeStringParser.TryParseString(entry.entryName, out length, out flag2))
                        {
                            type3 = !flag2 ? ((DataType) new StringType(length)) : ((DataType) new WStringType(length));
                        }
                        else if (entry.methodCount > 0)
                        {
                            type3 = new RpcStructType(entry);
                        }
                        else if (entry.subItems > 0)
                        {
                            bool flag3 = false;
                            bool flag4 = false;
                            if (entry.subItems > 1)
                            {
                                int             num2       = 0;
                                AdsFieldEntry[] subEntries = entry.subEntries;
                                int             index      = 0;
                                while (true)
                                {
                                    if (index >= subEntries.Length)
                                    {
                                        flag3 = entry.BitSize < num2;
                                        break;
                                    }
                                    AdsFieldEntry entry2 = subEntries[index];
                                    if (!entry2.IsStatic && !entry2.IsProperty)
                                    {
                                        int num4 = 0;
                                        num4   = !entry2.IsBitType ? (entry2.Offset * 8) : entry2.Offset;
                                        flag4 |= num4 < num2;
                                        num2  += entry2.BitSize;
                                    }
                                    index++;
                                }
                            }
                            type3 = !flag3 ? ((DataType) new StructType(entry)) : ((DataType) new UnionType(entry));
                        }
                        else if (entry.arrayDim > 0)
                        {
                            type3 = new ArrayType(entry);
                        }
                        else if (entry.enumInfoCount <= 0)
                        {
                            if (DataTypeStringParser.TryParseSubRange(entry.entryName, out referencedType))
                            {
                                type3 = (DataType)SubRangeTypeFactory.Create(entry, binder);
                            }
                            else if (((entry.baseTypeId > AdsDatatypeId.ADST_VOID) && (entry.typeName != null)) && (entry.typeName != string.Empty))
                            {
                                type3 = new AliasType(entry);
                            }
                            else
                            {
                                DataTypeCategory cat = CategoryConverter.FromId(entry.DataTypeId);
                                if (cat != DataTypeCategory.Unknown)
                                {
                                    Type tp = null;
                                    if (PrimitiveTypeConverter.TryGetManagedType(entry.DataTypeId, out tp))
                                    {
                                        if (cat == DataTypeCategory.Primitive)
                                        {
                                            type3 = new PrimitiveType(entry, PrimitiveTypeConverter.GetPrimitiveFlags(entry.DataTypeId), tp);
                                        }
                                        else if (cat == DataTypeCategory.String)
                                        {
                                            type3 = new StringType(entry);
                                        }
                                    }
                                }
                                if (type3 == null)
                                {
                                    type3 = new DataType(cat, entry);
                                }
                            }
                        }
                        else
                        {
                            AdsDatatypeId baseTypeId = entry.baseTypeId;
                            if (baseTypeId == AdsDatatypeId.ADST_INT16)
                            {
                                type3 = new EnumType <short>(entry);
                            }
                            else if (baseTypeId == AdsDatatypeId.ADST_INT32)
                            {
                                type3 = new EnumType <int>(entry);
                            }
                            else
                            {
                                switch (baseTypeId)
                                {
                                case AdsDatatypeId.ADST_INT8:
                                    type3 = new EnumType <sbyte>(entry);
                                    break;

                                case AdsDatatypeId.ADST_UINT8:
                                    type3 = new EnumType <byte>(entry);
                                    break;

                                case AdsDatatypeId.ADST_UINT16:
                                    type3 = new EnumType <ushort>(entry);
                                    break;

                                case AdsDatatypeId.ADST_UINT32:
                                    type3 = new EnumType <uint>(entry);
                                    break;

                                case AdsDatatypeId.ADST_INT64:
                                    type3 = new EnumType <long>(entry);
                                    break;

                                case AdsDatatypeId.ADST_UINT64:
                                    type3 = new EnumType <ulong>(entry);
                                    break;

                                default:
                                    throw new AdsException("Enum base type mismatch!");
                                }
                            }
                        }
                    }
                    binder.RegisterType(type3);
                }
                catch (Exception exception)
                {
                    Module.Trace.TraceWarning($"Cannot parse DataTypeEntry. Skipping dataType '{entry.entryName}'!", exception);
                }
            }
        }
예제 #2
0
        internal static bool TryParseType(string typeName, IBinder resolver, out IDataType type)
        {
            if (typeName == null)
            {
                throw new ArgumentNullException("typeName");
            }
            if (resolver == null)
            {
                throw new ArgumentNullException("resolver");
            }
            if (typeName == string.Empty)
            {
                throw new ArgumentException();
            }
            DataType type2     = null;
            int      length    = -1;
            bool     isUnicode = false;

            if (DataTypeStringParser.TryParseString(typeName, out length, out isUnicode))
            {
                type2 = !isUnicode ? ((DataType) new StringType(length)) : ((DataType) new WStringType(length));
                resolver.RegisterType(type2);
                resolver.OnTypeGenerated(type2);
                type = type2;
                return(true);
            }
            DimensionCollection dims = null;
            string baseType          = null;

            if (DataTypeStringParser.TryParseArray(typeName, out dims, out baseType))
            {
                IDataType type3 = null;
                if (resolver.TryResolveType(baseType, out type3))
                {
                    type2 = new ArrayType(typeName, (DataType)type3, dims, AdsDataTypeFlags.DataType);
                    resolver.RegisterType(type2);
                    resolver.OnTypeGenerated(type2);
                    type = type2;
                    return(true);
                }
                resolver.OnTypeResolveError(typeName);
                object[] args = new object[] { typeName };
                Module.Trace.TraceWarning("Array Type '{0}' could not be resolved. Not in DataType tables. Ignoring Type!", args);
                type = null;
                return(false);
            }
            string referencedType = null;

            if (DataTypeStringParser.TryParsePointer(typeName, out referencedType))
            {
                type2 = new PointerType(typeName, referencedType);
                resolver.RegisterType(type2);
                resolver.OnTypeGenerated(type2);
                type = type2;
                return(true);
            }
            if (DataTypeStringParser.TryParseReference(typeName, out referencedType))
            {
                type2 = new ReferenceType(typeName, referencedType, resolver.PlatformPointerSize);
                resolver.RegisterType(type2);
                resolver.OnTypeGenerated(type2);
                type = type2;
                return(true);
            }
            type2 = (DataType)SubRangeTypeFactory.Create(typeName, resolver);
            if (type2 == null)
            {
                type = null;
                resolver.OnTypeResolveError(typeName);
                return(false);
            }
            resolver.RegisterType(type2);
            resolver.OnTypeGenerated(type2);
            type = type2;
            return(true);
        }