Beispiel #1
0
 public void Serialize(Stream stream, BinaryWriter writer)
 {
     writer.Write(m_customSerializationAssets.Count);
     for (int i = 0; i < m_customSerializationAssets.Count; ++i)
     {
         ICustomSerialization asset = m_customSerializationAssets[i];
         writer.Write(asset.AllowStandardSerialization);
         writer.Write(m_customSerializationAssetIndices[i]);
         writer.Write(m_typeMap.ToGuid(asset.GetType()).ToByteArray());
         asset.Serialize(stream, writer);
     }
     m_customSerializationAssets       = null;
     m_customSerializationAssetIndices = null;
 }
Beispiel #2
0
        public void Save(string projectPath, string[] folderPaths, AssetItem[] assetItems, PersistentObject <TID>[] persistentObjects, ProjectInfo projectInfo, bool previewOnly, StorageEventHandler callback)
        {
            QueueUserWorkItem(() =>
            {
                if (!previewOnly)
                {
                    if (assetItems.Length != persistentObjects.Length)
                    {
                        throw new ArgumentException("assetItems");
                    }
                }

                if (assetItems.Length > folderPaths.Length)
                {
                    int l = folderPaths.Length;
                    Array.Resize(ref folderPaths, assetItems.Length);
                    for (int i = l; i < folderPaths.Length; ++i)
                    {
                        folderPaths[i] = folderPaths[l - 1];
                    }
                }

                projectPath = FullPath(projectPath);
                if (!Directory.Exists(projectPath))
                {
                    Directory.CreateDirectory(projectPath);
                }

                string projectInfoPath = projectPath + "/Project.rtmeta";
                ISerializer serializer = IOC.Resolve <ISerializer>();
                Error error            = new Error(Error.OK);
                for (int i = 0; i < assetItems.Length; ++i)
                {
                    string folderPath   = folderPaths[i];
                    AssetItem assetItem = assetItems[i];

                    try
                    {
                        string path = projectPath + folderPath;
                        if (!Directory.Exists(path))
                        {
                            Directory.CreateDirectory(path);
                        }

                        string previewPath = path + "/" + assetItem.NameExt + PreviewExt;
                        if (assetItem.Preview == null)
                        {
                            File.Delete(previewPath);
                        }
                        else
                        {
                            File.Delete(previewPath);
                            using (FileStream fs = File.Create(previewPath))
                            {
                                serializer.Serialize(assetItem.Preview, fs);
                            }
                        }

                        if (!previewOnly)
                        {
                            PersistentObject <TID> persistentObject = persistentObjects[i];

                            File.Delete(path + "/" + assetItem.NameExt);

                            if (persistentObject is PersistentRuntimeTextAsset <TID> )
                            {
                                PersistentRuntimeTextAsset <TID> textAsset = (PersistentRuntimeTextAsset <TID>)persistentObject;
                                File.WriteAllText(path + "/" + assetItem.NameExt, textAsset.Text);
                            }
                            else if (persistentObject is PersistentRuntimeBinaryAsset <TID> )
                            {
                                PersistentRuntimeBinaryAsset <TID> binAsset = (PersistentRuntimeBinaryAsset <TID>)persistentObject;
                                File.WriteAllBytes(path + "/" + assetItem.NameExt, binAsset.Data);
                            }
                            else
                            {
                                using (FileStream fs = File.Create(path + "/" + assetItem.NameExt))
                                {
                                    if (RTSLSettings.IsCustomSerializationEnabled && persistentObject is ICustomSerialization)
                                    {
                                        ICustomSerialization customSerialization = (ICustomSerialization)persistentObject;
                                        if (customSerialization.AllowStandardSerialization)
                                        {
                                            serializer.Serialize(persistentObject, fs);
                                        }
                                        assetItem.CustomDataOffset = fs.Position;
                                        using (BinaryWriter writer = new BinaryWriter(fs))
                                        {
                                            writer.Write(CustomSerializationHeader.Default);
                                            customSerialization.Serialize(fs, writer);
                                        }
                                    }
                                    else
                                    {
                                        serializer.Serialize(persistentObject, fs);
                                        assetItem.CustomDataOffset = fs.Position;
                                    }
                                }
                            }

                            File.Delete(path + "/" + assetItem.NameExt + MetaExt);
                            using (FileStream fs = File.Create(path + "/" + assetItem.NameExt + MetaExt))
                            {
                                serializer.Serialize(assetItem, fs);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.LogErrorFormat("Unable to create asset: {0} -> got exception: {1} ", assetItem.NameExt, e.ToString());
                        error.ErrorCode = Error.E_Exception;
                        error.ErrorText = e.ToString();
                        break;
                    }
                }

                File.Delete(projectInfoPath);
                using (FileStream fs = File.Create(projectInfoPath))
                {
                    serializer.Serialize(projectInfo, fs);
                }

                Callback(() => callback(error));
            });
        }