Example #1
0
        private AdsDataTypeEntry GetDataTypeEntry(int index, uint[] dataTypeEntryOffsets, AdsBinaryReader dataTypeReader)
        {
            if ((index < 0) | (index >= dataTypeEntryOffsets.Length))
            {
                throw new ArgumentOutOfRangeException("index");
            }
            dataTypeReader.BaseStream.Position = dataTypeEntryOffsets[index];
            AdsDataTypeEntry entry = new AdsDataTypeEntry();

            entry.Read(-1L, this._symbolEncoding, dataTypeReader);
            return(entry);
        }
Example #2
0
        private TcAdsDataTypeCollection collectDataTypes(int count, uint[] dataTypeOffsets, AdsBinaryReader dataTypeReader)
        {
            TcAdsDataTypeCollection types = new TcAdsDataTypeCollection();

            types.AddRange(this._defaultTypes);
            AdsDataTypeEntry[] entryArray = new AdsDataTypeEntry[count];
            for (int i = 0; i < count; i++)
            {
                entryArray[i] = this.GetDataTypeEntry(i, dataTypeOffsets, dataTypeReader);
            }
            for (int j = 0; j < count; j++)
            {
                if (!(this._streamIncludesBuildInTypes && this._defaultTypes.Contains(entryArray[j].entryName)))
                {
                    TcAdsDataType item = new TcAdsDataType(entryArray[j], this);
                    if (item.IsPointer || item.IsReference)
                    {
                        this.SetPlatformPointerSize(item.Size);
                        Type managedType = typeof(ulong);
                        if (item.Size == 4)
                        {
                            managedType = typeof(uint);
                        }
                        for (int k = 0; k < types.Count; k++)
                        {
                            ITcAdsDataType dataType = types[k];
                            if (dataType.Size <= 0)
                            {
                                if (dataType.Category == DataTypeCategory.Alias)
                                {
                                    ITcAdsDataType type4 = types.ResolveType(dataType, DataTypeResolveStrategy.Alias);
                                    if ((type4 != null) && (type4.Category == DataTypeCategory.Pointer))
                                    {
                                        ((TcAdsDataType)dataType).SetSize(item.Size, managedType);
                                    }
                                }
                                if (dataType.Category == DataTypeCategory.Pointer)
                                {
                                    ((TcAdsDataType)dataType).SetSize(item.Size, managedType);
                                }
                            }
                        }
                    }
                    types.Add(item);
                }
            }
            return(types);
        }
Example #3
0
        public AdsErrorCode TryLoadType(string name, bool lookup, out ITcAdsDataType type)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentOutOfRangeException("name");
            }
            if (lookup)
            {
                DataTypeInfoTable table = this;
                lock (table)
                {
                    if (this._dataTypes.TryGetDataType(name, out type))
                    {
                        return(AdsErrorCode.NoError);
                    }
                }
            }
            int  length    = 0;
            bool isUnicode = false;

            if (DataTypeStringParser.TryParseString(name, out length, out isUnicode))
            {
                int           byteCount = 0;
                AdsDatatypeId dataType  = AdsDatatypeId.ADST_VOID;
                if (isUnicode)
                {
                    char[] chars = new char[] { 'a' };
                    byteCount = Encoding.Unicode.GetByteCount(chars);
                    dataType  = AdsDatatypeId.ADST_WSTRING;
                }
                else
                {
                    char[] chars = new char[] { 'a' };
                    byteCount = Encoding.ASCII.GetByteCount(chars);
                    dataType  = AdsDatatypeId.ADST_STRING;
                }
                type = new TcAdsDataType(name, dataType, (uint)((length + 1) * byteCount), DataTypeCategory.String, typeof(string));
                DataTypeInfoTable table2 = this;
                lock (table2)
                {
                    this._dataTypes.Add(type);
                }
                this.OnTypeGenerated(type);
                return(AdsErrorCode.NoError);
            }
            AdsStream stream = new AdsStream(name.Length + 1);

            using (AdsBinaryWriter writer = new AdsBinaryWriter(stream))
            {
                writer.WritePlcString(name, name.Length + 1, this._encoding);
                AdsStream rdDataStream = new AdsStream(0xffff);
                try
                {
                    int num3 = this._adsClient.ReadWrite(0xf011, 0, rdDataStream, stream);
                }
                catch (AdsErrorException exception1)
                {
                    if (exception1.ErrorCode != AdsErrorCode.DeviceSymbolNotFound)
                    {
                        throw;
                    }
                    type = null;
                }
                using (AdsBinaryReader reader = new AdsBinaryReader(rdDataStream))
                {
                    AdsDataTypeEntry entry = new AdsDataTypeEntry(true, this._encoding, reader);
                    type = new TcAdsDataType(entry, this);
                    DataTypeInfoTable table3 = this;
                    lock (table3)
                    {
                        this._dataTypes.Add(type);
                    }
                }
            }
            if (type != null)
            {
                return(AdsErrorCode.NoError);
            }
            this.OnResolveError(name);
            return(AdsErrorCode.DeviceSymbolNotFound);
        }
Example #4
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);
     }
 }