// END PATCH /// <summary> /// Load descriptors from an Assembly. It uses reflection and HLA attributes in order to explore the code. /// </summary> /// <param name="assembly"> The assembly to explore</param> public virtual void AddDescriptorsFromAssembly(Assembly assembly) { if (assembly != null) { if (log.IsDebugEnabled) log.Debug("Assembly Name :" + assembly.FullName); names.Add(assembly.FullName); //TODO it is not the right name. ?? } else return; HLABasicDataAttribute[] basicDataArray = (HLABasicDataAttribute[])assembly.GetCustomAttributes(typeof(HLABasicDataAttribute), false); foreach (HLABasicDataAttribute basicData in basicDataArray) { //TODO Check that the corresponding native type is OK (size, etc.) basicDataMap.Add(basicData.Name, basicData.BasicDataInfo); } HLASimpleDataAttribute[] simpleDataArray = (HLASimpleDataAttribute[])assembly.GetCustomAttributes(typeof(HLASimpleDataAttribute), false); foreach (HLASimpleDataAttribute simpleData in simpleDataArray) { //TODO Check that the corresponding native type is OK (size, etc.) simpleDataMap.Add(simpleData.Name, simpleData.SimpleDataInfo); } HLAArrayDataAttribute[] arrayDataArray = (HLAArrayDataAttribute[])assembly.GetCustomAttributes(typeof(HLAArrayDataAttribute), false); foreach (HLAArrayDataAttribute arrayData in arrayDataArray) { arrayDataMap.Add(arrayData.Name, arrayData.ArrayDataInfo); } // we process ONLY the visible types. Type[] Types = assembly.GetExportedTypes(); // Display all the types contained in the specified assembly. foreach (Type oType in Types) { HLAObjectClassAttribute objectClass = (HLAObjectClassAttribute)System.Attribute.GetCustomAttribute(oType, typeof(HLAObjectClassAttribute)); if (objectClass != null) { ObjectClassDescriptor ocd = new ObjectClassDescriptor(objectClass.ObjectClassInfo, new XRTIObjectClassHandle(handleCounter++), new List<ObjectClassDescriptor>()); foreach (PropertyInfo propInfo in oType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly)) { HLAAttributeAttribute[] arrayOfCustomAttributes = (HLAAttributeAttribute[])propInfo.GetCustomAttributes(typeof(HLAAttributeAttribute), false); foreach (HLAAttributeAttribute custumAttr in arrayOfCustomAttributes) { ocd.AddAttributeDescriptor(new AttributeDescriptor(custumAttr.AttributeInfo, new XRTIAttributeHandle(handleCounter++), new XRTIDimensionHandleSet())); } } AddObjectClassDescriptor(ocd); } HLASimpleDataAttribute simpleDataAttr = (HLASimpleDataAttribute)System.Attribute.GetCustomAttribute(oType, typeof(HLASimpleDataAttribute)); if (simpleDataAttr != null) { simpleDataAttr.NativeType = oType; simpleDataMap.Add(simpleDataAttr.Name, simpleDataAttr.SimpleDataInfo); } HLAFixedRecordDataAttribute fixedRecordDataAttr = (HLAFixedRecordDataAttribute)System.Attribute.GetCustomAttribute(oType, typeof(HLAFixedRecordDataAttribute)); if (fixedRecordDataAttr != null) { fixedRecordDataAttr.NativeType = oType; foreach (FieldInfo fieldInfo in oType.GetFields()) { HLARecordFieldAttribute[] recordFieldAttrs = (HLARecordFieldAttribute[])fieldInfo.GetCustomAttributes(typeof(HLARecordFieldAttribute), false); foreach (HLARecordFieldAttribute field in recordFieldAttrs) { fixedRecordDataAttr.FixedRecordDataInfo.RecordFields.Add(field.RecordFieldInfo); } } fixedRecordDataMap.Add(fixedRecordDataAttr.Name, fixedRecordDataAttr.FixedRecordDataInfo); } HLAEnumeratedDataAttribute enumerateDataAttr = (HLAEnumeratedDataAttribute)System.Attribute.GetCustomAttribute(oType, typeof(HLAEnumeratedDataAttribute)); if (enumerateDataAttr != null) { enumerateDataAttr.NativeType = oType; string[] names = Enum.GetNames(oType); Array values = Enum.GetValues(oType); for (int i = 0; i < names.Length; i++) { HLAEnumerator enumerator = new HLAEnumerator(); enumerator.Name = names[i]; enumerator.Values = ((int)(values.GetValue(i))).ToString(); enumerateDataAttr.EnumeratedDataInfo.Enumerators.Add(enumerator); } enumeratedDataMap.Add(enumerateDataAttr.Name, enumerateDataAttr.EnumeratedDataInfo); } EventInfo[] events = oType.GetEvents(BindingFlags.Public | BindingFlags.Static); foreach (EventInfo eventInfo in events) { HLAInteractionClassAttribute[] interactionAttrs = (HLAInteractionClassAttribute[])eventInfo.GetCustomAttributes(typeof(HLAInteractionClassAttribute), false); if (interactionAttrs.Length != 0) { foreach (HLAInteractionClassAttribute interacAttr in interactionAttrs) { InteractionClassDescriptor icd = new InteractionClassDescriptor(interacAttr.InteractionClassInfo, new XRTIInteractionClassHandle(handleCounter++)); Type delegateType = eventInfo.EventHandlerType; MethodInfo invoke = delegateType.GetMethod("Invoke"); ParameterInfo[] pars = invoke.GetParameters(); foreach (ParameterInfo p in pars) { HLAInteractionParameterAttribute[] interactionParams = (HLAInteractionParameterAttribute[])p.GetCustomAttributes(typeof(HLAInteractionParameterAttribute), false); if (interactionParams.Length >= 0) icd.AddParameterDescriptor(new ParameterDescriptor(interactionParams[0].ParameterInfo, new XRTIParameterHandle(handleCounter++))); else icd.AddParameterDescriptor(new ParameterDescriptor(p.Name, new XRTIParameterHandle(handleCounter++))); } AddInteractionClassDescriptor(icd); } } } } }
/// <summary> /// Adds a set of descriptors corresponding to the features contained /// in the specified federation description document. Handle /// values will be assigned in the order that the described features are /// encountered in the document, starting at <code>1</code>. /// </summary> /// <param name="fdd">the parsed federation description document to interpret /// </param> public virtual void AddDescriptors(System.Xml.XmlDocument fdd) { System.Xml.XmlElement documentElement = (System.Xml.XmlElement)fdd.DocumentElement; objectModel = new HLAObjectModel(documentElement); names.Add(objectModel.Name); System.String version = objectModel.Version; if (!string.IsNullOrEmpty(version)) { SupportClass.Tokenizer st = new SupportClass.Tokenizer(version, " ."); majorVersion = System.Int32.Parse(st.NextToken()); minorVersion = System.Int32.Parse(st.NextToken()); } // First pass creates descriptors, assigns handles System.Xml.XmlNodeList nl = documentElement.GetElementsByTagName("basicData"); for (int i = 0; i < nl.Count; i++) { System.Xml.XmlElement basicDataElement = (System.Xml.XmlElement)nl.Item(i); HLABasicData basicData = new HLABasicData(basicDataElement); basicDataMap[basicData.Name] = basicData; } nl = documentElement.GetElementsByTagName("simpleData"); for (int i = 0; i < nl.Count; i++) { System.Xml.XmlElement simpleDataElement = (System.Xml.XmlElement)nl.Item(i); HLASimpleData simpleData = new HLASimpleData(simpleDataElement); simpleDataMap[simpleData.Name] = simpleData; } nl = documentElement.GetElementsByTagName("enumeratedData"); for (int i = 0; i < nl.Count; i++) { System.Xml.XmlElement enumeratedDataElement = (System.Xml.XmlElement)nl.Item(i); HLAEnumeratedData enumeratedData = new HLAEnumeratedData(enumeratedDataElement); enumeratedDataMap[enumeratedData.Name] = enumeratedData; } nl = documentElement.GetElementsByTagName("fixedRecordData"); for (int i = 0; i < nl.Count; i++) { System.Xml.XmlElement fixedRecordDataElement = (System.Xml.XmlElement)nl.Item(i); HLAFixedRecordData fixedRecordData = new HLAFixedRecordData(fixedRecordDataElement); fixedRecordDataMap[fixedRecordData.Name] = fixedRecordData; } nl = documentElement.GetElementsByTagName("arrayData"); for (int i = 0; i < nl.Count; i++) { System.Xml.XmlElement arrayDataElement = (System.Xml.XmlElement)nl.Item(i); HLAarrayDataType arrayData = new HLAarrayDataType(arrayDataElement); arrayDataMap[arrayData.Name] = arrayData; } nl = documentElement.GetElementsByTagName(OBJECT_CLASS); for (int i = 0; i < nl.Count; i++) { System.Xml.XmlElement objectClassElement = (System.Xml.XmlElement)nl.Item(i); ObjectClassDescriptor ocd = new ObjectClassDescriptor(objectClassElement, new XRTIObjectClassHandle(handleCounter++), new List<ObjectClassDescriptor>()); System.Xml.XmlNodeList nl2 = objectClassElement.ChildNodes; for (int j = 0; j < nl2.Count; j++) { if (nl2.Item(j) is System.Xml.XmlElement && nl2.Item(j).Name.Equals(ATTRIBUTE)) { System.Xml.XmlElement attributeElement = (System.Xml.XmlElement)nl2.Item(j); AttributeDescriptor ad = new AttributeDescriptor(attributeElement, new XRTIAttributeHandle(handleCounter++), new XRTIDimensionHandleSet(), "HLAreliable".Equals(attributeElement.GetAttribute(TRANSPORTATION)) ? TransportationType.HLA_RELIABLE : TransportationType.HLA_BEST_EFFORT, "Receive".Equals(attributeElement.GetAttribute(ORDER)) ? Hla.Rti1516.OrderType.RECEIVE : OrderType.TIMESTAMP); ocd.AddAttributeDescriptor(ad); // PATCH ANGEL AddAttributeDescriptor(ad); } } AddObjectClassDescriptor(ocd); } nl = documentElement.GetElementsByTagName(INTERACTION_CLASS); for (int i = 0; i < nl.Count; i++) { System.Xml.XmlElement interactionClassElement = (System.Xml.XmlElement)nl.Item(i); InteractionClassDescriptor icd = new InteractionClassDescriptor(interactionClassElement, new XRTIInteractionClassHandle(handleCounter++), new List<InteractionClassDescriptor>(), new XRTIDimensionHandleSet(), "HLAreliable".Equals(interactionClassElement.GetAttribute(TRANSPORTATION)) ? TransportationType.HLA_RELIABLE : TransportationType.HLA_BEST_EFFORT, "Receive".Equals(interactionClassElement.GetAttribute(ORDER)) ? Sxta.Rti1516.Reflection.HLAorderType.Receive : Sxta.Rti1516.Reflection.HLAorderType.TimeStamp); System.Xml.XmlNodeList nl2 = interactionClassElement.ChildNodes; for (int j = 0; j < nl2.Count; j++) { if (nl2.Item(j) is System.Xml.XmlElement && nl2.Item(j).Name.Equals(PARAMETER)) { System.Xml.XmlElement parameterElement = (System.Xml.XmlElement)nl2.Item(j); icd.AddParameterDescriptor(new ParameterDescriptor(parameterElement, new XRTIParameterHandle(handleCounter++))); } } AddInteractionClassDescriptor(icd); } nl = documentElement.GetElementsByTagName(DIMENSION); for (int i = 0; i < nl.Count; i++) { System.Xml.XmlElement dimensionElement = (System.Xml.XmlElement)nl.Item(i); long upperBound = System.Int64.MaxValue; if (dimensionElement.HasAttribute(UPPER_BOUND)) { try { upperBound = System.Int64.Parse(dimensionElement.GetAttribute(UPPER_BOUND)); } catch (System.FormatException) { } } AddDimensionDescriptor(new DimensionDescriptor(dimensionElement.GetAttribute(NAME), new XRTIDimensionHandle(handleCounter++), upperBound)); } // Second pass resolves parents, dimensions nl = documentElement.GetElementsByTagName(OBJECT_CLASS); for (int i = 0; i < nl.Count; i++) { System.Xml.XmlElement objectClassElement = (System.Xml.XmlElement)nl.Item(i); ObjectClassDescriptor ocd = GetObjectClassDescriptor(objectClassElement.GetAttribute(NAME)); if (objectClassElement.ParentNode.Name.Equals(OBJECT_CLASS)) { ocd.AddParentDescriptor(GetObjectClassDescriptor(((System.Xml.XmlElement)objectClassElement.ParentNode).GetAttribute(NAME))); } if (objectClassElement.HasAttribute(PARENTS)) { SupportClass.Tokenizer st = new SupportClass.Tokenizer(objectClassElement.GetAttribute(PARENTS)); // PATCH ANGEL while (st.HasMoreTokens()) //{ ocd.ParentDescriptors.Add(GetObjectClassDescriptor(st.NextToken())); //} } System.Xml.XmlNodeList nl2 = objectClassElement.ChildNodes; for (int j = 0; j < nl2.Count; j++) { if (nl2.Item(j) is System.Xml.XmlElement && nl2.Item(j).Name.Equals(ATTRIBUTE)) { System.Xml.XmlElement attributeElement = (System.Xml.XmlElement)nl2.Item(j); if (attributeElement.HasAttribute(DIMENSIONS)) { AttributeDescriptor ad = ocd.GetAttributeDescriptor(attributeElement.GetAttribute(NAME)); SupportClass.Tokenizer st = new SupportClass.Tokenizer(attributeElement.GetAttribute(DIMENSIONS)); // PATCH ANGEL while (st.HasMoreTokens()) //{ System.String dimension = st.NextToken(); if (!dimension.Equals("NA")) { ad.Dimensions.Add(GetDimensionDescriptor(dimension).Handle); } //} } } } } nl = documentElement.GetElementsByTagName(INTERACTION_CLASS); for (int i = 0; i < nl.Count; i++) { System.Xml.XmlElement interactionClassElement = (System.Xml.XmlElement)nl.Item(i); InteractionClassDescriptor icd = GetInteractionClassDescriptor(interactionClassElement.GetAttribute(NAME)); if (interactionClassElement.HasAttribute(DIMENSIONS)) { SupportClass.Tokenizer st = new SupportClass.Tokenizer(interactionClassElement.GetAttribute(DIMENSIONS)); // PATCH ANGEL while (st.HasMoreTokens()) //{ System.String dimension = st.NextToken(); if (!dimension.Equals("NA")) { icd.Dimensions.Add(GetDimensionDescriptor(dimension).Handle); } //} } if (interactionClassElement.ParentNode.Name.Equals(INTERACTION_CLASS)) { icd.AddParentDescriptor(GetInteractionClassDescriptor(((System.Xml.XmlElement)interactionClassElement.ParentNode).GetAttribute(NAME))); } if (interactionClassElement.HasAttribute(PARENTS)) { SupportClass.Tokenizer st = new SupportClass.Tokenizer(interactionClassElement.GetAttribute(PARENTS)); // PATCH ANGEL while (st.HasMoreTokens()) //{ icd.AddParentDescriptor(GetInteractionClassDescriptor(st.NextToken())); //} } } //PATCH ANGEL CheckAopFdd(); }