Esempio n. 1
0
 public static void Write(this BinaryWriter writer, CustomSerializationHeader header)
 {
     writer.Write(header.Magic);
     writer.Write(header.Version);
     writer.Write(header.Reserved);
 }
Esempio n. 2
0
        public void Load(string projectPath, AssetItem[] assetItems, Type[] types, StorageEventHandler <PersistentObject <TID>[]> callback)
        {
            string[] assetPaths        = assetItems.Select(item => item.ToString()).ToArray();
            long[]   customDataOffsets = assetItems.Select(item => item.CustomDataOffset).ToArray();
            QueueUserWorkItem(() =>
            {
                PersistentObject <TID>[] result = new PersistentObject <TID> [assetPaths.Length];
                for (int i = 0; i < assetPaths.Length; ++i)
                {
                    string assetPath       = assetPaths[i];
                    assetPath              = FullPath(projectPath) + assetPath;
                    ISerializer serializer = IOC.Resolve <ISerializer>();
                    try
                    {
                        if (File.Exists(assetPath))
                        {
                            if (types[i] == typeof(PersistentRuntimeTextAsset <TID>))
                            {
                                PersistentRuntimeTextAsset <TID> textAsset = new PersistentRuntimeTextAsset <TID>();
                                textAsset.name = Path.GetFileName(assetPath);
                                textAsset.Text = File.ReadAllText(assetPath);
                                textAsset.Ext  = Path.GetExtension(assetPath);
                                result[i]      = textAsset;
                            }
                            else if (types[i] == typeof(PersistentRuntimeBinaryAsset <TID>))
                            {
                                PersistentRuntimeBinaryAsset <TID> binAsset = new PersistentRuntimeBinaryAsset <TID>();
                                binAsset.name = Path.GetFileName(assetPath);
                                binAsset.Data = File.ReadAllBytes(assetPath);
                                binAsset.Ext  = Path.GetExtension(assetPath);
                                result[i]     = binAsset;
                            }
                            else
                            {
                                using (FileStream fs = File.OpenRead(assetPath))
                                {
                                    long customDataOffset = customDataOffsets[i];
                                    if (customDataOffset == -1)
                                    {
                                        result[i] = (PersistentObject <TID>)serializer.Deserialize(fs, types[i]);
                                    }
                                    else
                                    {
                                        if (customDataOffset > 0)
                                        {
                                            result[i] = (PersistentObject <TID>)serializer.Deserialize(fs, types[i], customDataOffset);
                                        }
                                        else
                                        {
                                            result[i] = (PersistentObject <TID>)Activator.CreateInstance(types[i]);
                                        }

                                        if (fs.Position < fs.Length)
                                        {
                                            using (BinaryReader reader = new BinaryReader(fs))
                                            {
                                                CustomSerializationHeader header = reader.ReadHeader();
                                                if (header.IsValid)
                                                {
                                                    ICustomSerialization customSerialization = (ICustomSerialization)result[i];
                                                    customSerialization.Deserialize(fs, reader);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            Callback(() => callback(new Error(Error.E_NotFound), new PersistentObject <TID> [0]));
                            return;
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.LogErrorFormat("Unable to load asset: {0} -> got exception: {1} ", assetPath, e.ToString());
                        Callback(() => callback(new Error(Error.E_Exception)
                        {
                            ErrorText = e.ToString()
                        }, new PersistentObject <TID> [0]));
                        return;
                    }
                }

                Callback(() => callback(new Error(Error.OK), result));
            });
        }