public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks, UnityNodeBase parentNode, ResourceResponse resourceResponse, List <Action <UnityNodeBase> > postInstallActions, bool optimizedLoad) { if (!this.isDeserialized) { ResourceBlock dataBlock = dataBlocks[resourceType] as ResourceBlock; AssetResource resource = dataBlock.GetResource(this.referenceID); if (resource != null) { byte[] metaDataBuffer = resource.GetMetaData(); SerializedCollectionData collectionData = ProtocolBufferSerializer.DeserializeCollectionData(metaDataBuffer); string assemblyName = ProtocolBufferSerializer.GetAssemblyName(collectionData.assemblyType); string scriptTypeName = collectionData.typeName; string fieldName = collectionData.fieldName; int itemCount = collectionData.count; Type scriptType = null; //Qualify type check with assembly name, GetType only looks in current assembly otherwise. if (!string.IsNullOrEmpty(assemblyName)) { scriptType = Type.GetType(scriptTypeName + ", " + assemblyName); } else { scriptType = Type.GetType(scriptTypeName); } if (scriptType != null) { Array array = Array.CreateInstance(scriptType, itemCount); if (array != null) { FieldDeserializer arrayDeserializer = new FieldDeserializer(array); ResourceResponse response = new ResourceResponse(); response.SetFieldDeserializer(arrayDeserializer); foreach (UnityNodeBase node in this.ChildNodes) { node.Deserialize(dataBlocks, this, response, postInstallActions, optimizedLoad); arrayDeserializer.IncrementIndex(); } } if (resourceResponse != null) { resourceResponse.GetFieldDeserializer.SetField(fieldName, array); } } } this.isDeserialized = true; } }
public static SerializedCollectionData DeserializeCollectionData(byte[] buffer) { SerializedCollectionData deserializedCollectionData = null; if (buffer == null || buffer.Length == 0) { return(deserializedCollectionData); } using (MemoryStream stream = new MemoryStream(buffer)) { deserializedCollectionData = serializer.Deserialize(stream, null, typeof(SerializedCollectionData)) as SerializedCollectionData; } return(deserializedCollectionData); }
public static byte[] SerializeCollectionData(Type type, string fieldName, int itemCount, UInt32[] itemIDs, Assembly assembly) { SerializedCollectionData serializedCollection = new SerializedCollectionData(); serializedCollection.type = MapPrimitiveType(type); serializedCollection.fieldName = fieldName; serializedCollection.count = itemCount; serializedCollection.itemIDs = itemIDs; serializedCollection.assemblyType = MapAssembly(assembly); serializedCollection.typeName = type.ToString(); byte[] serializedBuffer = null; using (var ms = new MemoryStream()) { serializer.Serialize(ms, serializedCollection); serializedBuffer = ms.ToArray(); } return(serializedBuffer); }