internal int InitializeStruct(IList <ITcAdsSubItem> subItems, Type type, AdsBinaryReader reader, int readerOffset, out object value) { int num = 0; object obj2 = type.InvokeMember(null, BindingFlags.CreateInstance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly, null, null, null); FieldInfo[] fields = type.GetFields(); if (fields.Length != subItems.Count) { throw new ArgumentException($"Cannot convert data type of symbol to this type.Expected number of fields {subItems.Count}, actual number is {fields.Length}", "type"); } for (int i = 0; i < fields.Length; i++) { TcAdsSubItem item = (TcAdsSubItem)subItems[i]; if ((item.BaseType.Category == DataTypeCategory.Primitive) || (item.BaseType.Category == DataTypeCategory.String)) { if (fields[i].FieldType != ((TcAdsDataType)item.BaseType).ManagedType) { throw new ArgumentException("Cannot convert data type of symbol to this type.", "type"); } object obj3 = null; num += this.InitializePrimitiveType(item.BaseType.Name, fields[i].FieldType, item.DataTypeId, item.Size, reader, readerOffset + item.Offset, out obj3); fields[i].SetValue(obj2, obj3); } else if (item.BaseType.Category == DataTypeCategory.Enum) { if (fields[i].FieldType != ((TcAdsDataType)item.BaseType).ManagedType) { throw new ArgumentException("Cannot convert data type of symbol to this type.", "type"); } object obj4 = null; num += this.InitializeEnum(item.BaseType.Name, fields[i].FieldType, item.DataTypeId, item.Size, reader, readerOffset + item.Offset, out obj4); fields[i].SetValue(obj2, obj4); } else if (item.BaseType.Category == DataTypeCategory.Struct) { object obj5 = null; num += this.InitializeStruct(item.BaseType.SubItems, fields[i].FieldType, reader, readerOffset + item.Offset, out obj5); fields[i].SetValue(obj2, obj5); } else { if (item.BaseType.Category != DataTypeCategory.Array) { throw new ArgumentException($"Cannot convert data type of symbol to this type. ADS Data type '{item.BaseType.Name}' of subitem '{item.SubItemName}' not supported.", "type"); } object obj6 = null; num += this.InitializeArray(fields[i].FieldType, (TcAdsDataType)item.BaseType, reader, readerOffset + item.Offset, -1, out obj6); fields[i].SetValue(obj2, obj6); } } value = obj2; return(num); }
internal TcAdsSymbolInfo CreateSubSymbol(TcAdsSymbolInfo parent, int subIndex) { if (parent == null) { throw new ArgumentNullException("parent"); } ITcAdsDataType dataType = parent.DataType; if (dataType == null) { throw new ArgumentException("DataType not specified", "parent"); } if (((subIndex < 0) || (dataType.SubItems == null)) || (subIndex >= dataType.SubItems.Count)) { throw new ArgumentOutOfRangeException("subIndex"); } TcAdsSymbolInfo subSymbol = null; if (parent.IsPointer && (subIndex > 0)) { throw new ArgumentOutOfRangeException("subIndex"); } TcAdsSubItem typeSubEntry = (TcAdsSubItem)dataType.SubItems[subIndex]; TcAdsDataType typeByName = (TcAdsDataType)this._parser.GetTypeByName(typeSubEntry.Name); if (typeSubEntry != null) { if (typeByName != null) { typeSubEntry.AlignSubItemToType(typeByName); } subSymbol = new TcAdsSymbolInfo(this._parser, parent, subIndex) { size = (uint)typeSubEntry.Size, dataTypeId = typeSubEntry.DataTypeId, typeEntryFlags = this.createTypeEntryFlags(parent, typeSubEntry), flags = this.createSubSymbolFlags(parent, typeSubEntry), shortName = typeSubEntry.SubItemName }; subSymbol.instancePath = $"{parent.instancePath}.{subSymbol.shortName}"; subSymbol.typeName = typeSubEntry.Name; subSymbol.comment = typeSubEntry.Comment; subSymbol.arrayInfo = typeSubEntry.ArrayInfo; subSymbol.attributes = typeSubEntry.Attributes; this.calcFieldAddress(parent, subSymbol, typeSubEntry, out subSymbol.indexGroup, out subSymbol.indexOffset); } return(subSymbol); }
private void calcFieldAddress(TcAdsSymbolInfo parent, TcAdsSymbolInfo subSymbol, TcAdsSubItem subEntry, out uint indexGroup, out uint indexOffset) { if (parent == null) { throw new ArgumentNullException("parent"); } if (subSymbol == null) { throw new ArgumentNullException("subSymbol"); } int subEntryOffset = 0; if (subEntry != null) { subEntryOffset = subEntry.Offset; } if (subSymbol.IsStatic) { indexGroup = 0xf019; indexOffset = 0; } else if (subSymbol.IsBitType && !parent.IsBitType) { this.calcBitAccessAddress(parent, subEntryOffset, out indexGroup, out indexOffset); } else if ((subEntry != null) && ((subEntry.Flags & (AdsDataTypeFlags.None | AdsDataTypeFlags.PropItem)) > AdsDataTypeFlags.None)) { indexGroup = 0xf017; indexOffset = subEntry.TypeHashValue; } else if (subSymbol.IsDereferencedPointer) { if (subSymbol.IsBitType) { indexGroup = 0xf01a; indexOffset = 0; } else { indexGroup = 0xf014; indexOffset = 0; } } else if ((subSymbol.Category != DataTypeCategory.Reference) && !subSymbol.IsDereferencedReference) { indexGroup = parent.indexGroup; indexOffset = parent.indexOffset + ((uint)subEntryOffset); } else if (subSymbol.IsBitType) { indexGroup = 0xf01b; indexOffset = 0; } else { indexGroup = 0xf016; indexOffset = 0; } }
private AdsDataTypeFlags createTypeEntryFlags(TcAdsSymbolInfo parent, TcAdsSubItem typeSubEntry) => (typeSubEntry.Flags | ((AdsDataTypeFlags.None | AdsDataTypeFlags.PropItem | AdsDataTypeFlags.ReferenceTo) & parent.typeEntryFlags));
private AdsSymbolFlags createSubSymbolFlags(TcAdsSymbolInfo parent, TcAdsSubItem typeSubEntry) => (((DataTypeFlagConverter.Convert(typeSubEntry.Flags) & (AdsSymbolFlags.Attributes | AdsSymbolFlags.BitValue | AdsSymbolFlags.ExtendedFlags | AdsSymbolFlags.InitOnReset | AdsSymbolFlags.ItfMethodAccess | AdsSymbolFlags.MethodDeref | AdsSymbolFlags.Persistent | AdsSymbolFlags.ReadOnly | AdsSymbolFlags.ReferenceTo | AdsSymbolFlags.Static | AdsSymbolFlags.TComInterfacePtr | AdsSymbolFlags.TypeGuid)) | (AdsSymbolFlags.ContextMask & parent.flags)) | ((AdsSymbolFlags.None | AdsSymbolFlags.Persistent) & parent.flags));