public static object Convert(object data, OpenTagCache srcInfo, Stream srcStream, ResourceDataManager srcResources, OpenTagCache destInfo, Stream destStream, ResourceDataManager destResources, TagCacheMap tagMap) { if (data == null) { return(null); } var type = data.GetType(); if (type.IsPrimitive) { return(data); } if (type == typeof(StringID)) { return(ConvertStringID((StringID)data, srcInfo, destInfo)); } if (type == typeof(TagInstance)) { return(ConvertTag((TagInstance)data, srcInfo, srcStream, srcResources, destInfo, destStream, destResources, tagMap)); } if (type == typeof(ResourceReference)) { return(ConvertResource((ResourceReference)data, srcInfo, srcResources, destInfo, destResources)); } if (type == typeof(GeometryReference)) { return(ConvertGeometry((GeometryReference)data, srcInfo, srcResources, destInfo, destResources)); } if (type.IsArray) { return(ConvertArray((Array)data, srcInfo, srcStream, srcResources, destInfo, destStream, destResources, tagMap)); } if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List <>)) { return(ConvertList(data, type, srcInfo, srcStream, srcResources, destInfo, destStream, destResources, tagMap)); } if (type.GetCustomAttributes(typeof(TagStructureAttribute), false).Length > 0) { return(ConvertStructure(data, type, srcInfo, srcStream, srcResources, destInfo, destStream, destResources, tagMap)); } return(data); }
public static TagInstance ConvertTag(TagInstance srcTag, OpenTagCache srcInfo, Stream srcStream, ResourceDataManager srcResources, OpenTagCache destInfo, Stream destStream, ResourceDataManager destResources, TagCacheMap tagMap) { TagPrinter.PrintTagShort(srcTag); // Deserialize the tag from the source cache var structureType = TagStructureTypes.FindByGroupTag(srcTag.Group.Tag); var srcContext = new TagSerializationContext(srcStream, srcInfo.Cache, srcInfo.StringIDs, srcTag); var tagData = srcInfo.Deserializer.Deserialize(srcContext, structureType); // Acquire the destination tag var destTag = destInfo.Cache.AllocateTag(srcTag.Group); tagMap.Add(srcInfo.CacheFile.FullName, srcTag.Index, destInfo.CacheFile.FullName, destTag.Index); if (srcTag.IsInGroup("decs") || srcTag.IsInGroup("rmd ")) { IsDecalShader = true; } // Convert the source tag tagData = Convert(tagData, srcInfo, srcStream, srcResources, destInfo, destStream, destResources, tagMap); if (srcTag.IsInGroup("decs") || srcTag.IsInGroup("rmd ")) { IsDecalShader = false; } // Re-serialize into the destination cache var destContext = new TagSerializationContext(destStream, destInfo.Cache, destInfo.StringIDs, destTag); destInfo.Serializer.Serialize(destContext, tagData); return(destTag); }
public static object ConvertList(object list, Type type, OpenTagCache srcInfo, Stream srcStream, ResourceDataManager srcResources, OpenTagCache destInfo, Stream destStream, ResourceDataManager destResources, TagCacheMap tagMap) { if (type.GenericTypeArguments[0].IsPrimitive) { return(list); } var count = (int)type.GetProperty("Count").GetValue(list); var getItem = type.GetMethod("get_Item"); var setItem = type.GetMethod("set_Item"); for (var i = 0; i < count; i++) { var oldValue = getItem.Invoke(list, new object[] { i }); var newValue = Convert(oldValue, srcInfo, srcStream, srcResources, destInfo, destStream, destResources, tagMap); setItem.Invoke(list, new object[] { i, newValue }); } return(list); }
public static object ConvertStructure(object data, Type type, OpenTagCache srcInfo, Stream srcStream, ResourceDataManager srcResources, OpenTagCache destInfo, Stream destStream, ResourceDataManager destResources, TagCacheMap tagMap) { // Convert each field var enumerator = new TagFieldEnumerator(new TagStructureInfo(type, destInfo.Version)); while (enumerator.Next()) { var oldValue = enumerator.Field.GetValue(data); var newValue = Convert(oldValue, srcInfo, srcStream, srcResources, destInfo, destStream, destResources, tagMap); enumerator.Field.SetValue(data, newValue); } // Perform fixups FixObjectTypes(data, type, srcInfo); FixShaders(data, srcInfo); var scenario = data as Scenario; if (scenario != null) { FixScenario(scenario); } return(data); }
public static Array ConvertArray(Array array, OpenTagCache srcInfo, Stream srcStream, ResourceDataManager srcResources, OpenTagCache destInfo, Stream destStream, ResourceDataManager destResources, TagCacheMap tagMap) { if (array.GetType().GetElementType().IsPrimitive) { return(array); } for (var i = 0; i < array.Length; i++) { var oldValue = array.GetValue(i); var newValue = Convert(oldValue, srcInfo, srcStream, srcResources, destInfo, destStream, destResources, tagMap); array.SetValue(newValue, i); } return(array); }