Exemplo n.º 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);
                }
            }
        }
Exemplo n.º 2
0
        internal void WritePrimitiveValue(string symbolPath, object value, Type managedType, AdsDatatypeId dataType, int byteSize, AdsBinaryWriter writer, int writerOffset)
        {
            if (string.IsNullOrEmpty(symbolPath))
            {
                throw new ArgumentNullException("symbolPath");
            }
            if (managedType == null)
            {
                throw new ArgumentNullException("managedType");
            }
            if (byteSize < 0)
            {
                throw new ArgumentOutOfRangeException("byteSize");
            }
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (writerOffset < 0)
            {
                throw new ArgumentOutOfRangeException("writerOffset");
            }
            Type type  = value.GetType();
            bool flag2 = PrimitiveTypeConverter.IsPlcOpenType(value.GetType());

            if (PrimitiveTypeConverter.IsPlcOpenType(managedType))
            {
                Type tp = null;
                if (PrimitiveTypeConverter.TryGetManagedType(dataType, out tp))
                {
                    managedType = tp;
                }
                else if (managedType == typeof(LTIME))
                {
                    managedType = typeof(ulong);
                    dataType    = AdsDatatypeId.ADST_UINT64;
                }
            }
            if (managedType != type)
            {
                try
                {
                    object obj1 = PrimitiveTypeConverter.Convert(value, managedType);
                    value = obj1;
                }
                catch (MarshalException exception)
                {
                    throw new ArgumentException($"Cannot convert value type '{value.GetType()}' to symbol type '{managedType}'!", "value", exception);
                }
            }
            writer.BaseStream.Position = writerOffset;
            switch (dataType)
            {
            case AdsDatatypeId.ADST_INT16:
                writer.Write((short)value);
                return;

            case AdsDatatypeId.ADST_INT32:
                writer.Write((int)value);
                return;

            case AdsDatatypeId.ADST_REAL32:
                writer.Write((float)value);
                return;

            case AdsDatatypeId.ADST_REAL64:
                writer.Write((double)value);
                return;
            }
            switch (dataType)
            {
            case AdsDatatypeId.ADST_INT8:
                writer.Write((sbyte)value);
                return;

            case AdsDatatypeId.ADST_UINT8:
                writer.Write((byte)value);
                return;

            case AdsDatatypeId.ADST_UINT16:
                writer.Write((ushort)value);
                return;

            case AdsDatatypeId.ADST_UINT32:
                writer.Write((uint)value);
                return;

            case AdsDatatypeId.ADST_INT64:
                writer.Write((long)value);
                return;

            case AdsDatatypeId.ADST_UINT64:
                writer.Write((ulong)value);
                return;
            }
            switch (dataType)
            {
            case AdsDatatypeId.ADST_STRING:
            {
                int  byteCount = Encoding.Default.GetByteCount("a");
                long position  = writer.BaseStream.Position;
                int  length    = ((string)value).Length;
                if (((length + 1) * byteCount) > byteSize)
                {
                    throw AdsErrorException.Create(AdsErrorCode.DeviceInvalidSize);
                }
                writer.WritePlcAnsiString((string)value, length);
                writer.BaseStream.Position = position + byteSize;
                return;
            }

            case AdsDatatypeId.ADST_WSTRING:
            {
                int  byteCount = Encoding.Unicode.GetByteCount("a");
                long position  = writer.BaseStream.Position;
                int  length    = ((string)value).Length;
                if (((length + 1) * byteCount) > byteSize)
                {
                    throw AdsErrorException.Create(AdsErrorCode.DeviceInvalidSize);
                }
                writer.WritePlcUnicodeString((string)value, length);
                writer.BaseStream.Position = position + byteSize;
                return;
            }

            case AdsDatatypeId.ADST_BIT:
                if ((bool)value)
                {
                    writer.Write((byte)1);
                    return;
                }
                writer.Write((byte)0);
                return;
            }
            throw new ArgumentException("Unexpected datatype. Cannot convert datatype of symbol to this type.", "type");
        }
Exemplo n.º 3
0
 internal TcAdsDataType(AdsDataTypeEntry entry, IDataTypeResolver table) : this()
 {
     if (entry == null)
     {
         throw new ArgumentNullException("entry");
     }
     if (table == null)
     {
         throw new ArgumentNullException("table");
     }
     this._table         = table;
     this._size          = entry.size;
     this._typeName      = entry.entryName;
     this._typeHashValue = entry.typeHashValue;
     this._comment       = entry.Comment;
     this._flags         = entry.flags;
     this._offset        = entry.offset;
     this._dataTypeId    = entry.baseTypeId;
     if (entry.nameLength > 0)
     {
         bool   isUnicode            = false;
         int    length               = 0;
         string referencedType       = null;
         AdsDatatypeArrayInfo[] dims = null;
         string baseType             = null;
         if (entry.IsArray)
         {
             if (DataTypeStringParser.TryParseArray(this._typeName, out dims, out baseType))
             {
                 this._category     = DataTypeCategory.Array;
                 this._arrayInfo    = dims;
                 this._baseTypeName = baseType;
             }
             else
             {
                 this._arrayInfo    = entry.arrayInfos;
                 this._category     = DataTypeCategory.Array;
                 this._baseTypeName = entry.Name;
             }
         }
         else if (DataTypeStringParser.TryParseString(this._typeName, out length, out isUnicode))
         {
             this._category = DataTypeCategory.String;
         }
         else if (DataTypeStringParser.TryParseReference(this._typeName, out referencedType))
         {
             this._baseTypeName = referencedType;
             this._category     = DataTypeCategory.Reference;
         }
         else if (DataTypeStringParser.TryParsePointer(this._typeName, out referencedType))
         {
             this._baseTypeName = referencedType;
             this._category     = DataTypeCategory.Pointer;
         }
         else if (DataTypeStringParser.TryParseSubRange(this._typeName, out referencedType))
         {
             this._baseTypeName = entry.Name;
             this._category     = DataTypeCategory.SubRange;
         }
         else if (!string.IsNullOrEmpty(entry.Name))
         {
             this._baseTypeName = entry.Name;
             this._category     = DataTypeCategory.Alias;
         }
     }
     if (entry.subItems <= 0)
     {
         if (entry.IsEnum)
         {
             this._enumInfos = entry.enums;
             this._category  = DataTypeCategory.Enum;
             bool flag3 = PrimitiveTypeConverter.TryGetManagedType(this._dataTypeId, out this._managedType);
         }
     }
     else
     {
         this._subItems = new TcAdsSubItem[entry.subItems];
         bool flag2 = false;
         int  num2  = 0;
         int  index = 0;
         while (true)
         {
             int num1;
             if (index < entry.subItems)
             {
                 if (entry.subEntries[index] != null)
                 {
                     this._subItems[index] = new TcAdsSubItem(entry.subEntries[index], table);
                     if (!this._subItems[index].IsStatic && !this._subItems[index].IsProperty)
                     {
                         int num4 = 0;
                         num4   = !this._subItems[index].IsBitType ? (this._subItems[index].Offset * 8) : this._subItems[index].Offset;
                         flag2 |= num4 < num2;
                         num2  += this._subItems[index].BitSize;
                     }
                     index++;
                     continue;
                 }
                 object[] args = new object[] { index, entry.Name };
                 Module.Trace.TraceError("SubEntry '{0}' missing in TcAdsDataType '{1}'", args);
             }
             if ((entry.subItems <= 1) || (entry.BitSize >= num2))
             {
                 num1 = 0;
             }
             else
             {
                 num1 = (int)!entry.IsBitType;
             }
             this._category = ((num1 & flag2) == 0) ? DataTypeCategory.Struct : DataTypeCategory.Union;
             break;
         }
     }
     if (entry.HasAttributes)
     {
         this._attributes = entry.attributes;
     }
     if (entry.HasRpcMethods)
     {
         this._rpcMethodInfos = entry.methods;
     }
     if (this._category == DataTypeCategory.Unknown)
     {
         this._category = (entry.DataTypeId != AdsDatatypeId.ADST_BIGTYPE) ? DataTypeCategory.Primitive : DataTypeCategory.Interface;
     }
     if ((this._category == DataTypeCategory.Array) && string.IsNullOrEmpty(this._baseTypeName))
     {
         string message = $"Base type of ARRAY '{this._typeName}' not defined!";
         Module.Trace.TraceWarning(message);
     }
 }