예제 #1
0
 // 保存存档
 public void Save(T datas, string path)
 {
     if (IsInSaving)
     {
         return;
     }
     using (Stream stream = new FileStream(path, FileMode.Create))
     {
         IsInSaving = true;
         GameDatas  = datas ?? throw new ArgumentNullException("datas");
         bool IsHash       = false;
         bool IsCompressed = false;
         //保存文件头
         Header.PlayTime      = GameDatas.PlayTime;
         Header.PlayerID      = GameDatas.PlayerID;
         Header.Version       = curVersion;
         Header.IsHide        = isHide;
         Header.SaveTimeTicks = DateTime.Now.Ticks;
         Header.IsCompressed  = IsCompressed;
         if (IsHash)
         {
             Header.ContentHash = FileUtil.Hash(Content);
         }
         else
         {
             Header.ContentHash = null;
         }
         //保存内容
         Content = SerializationUtility.SerializeValue(GameDatas, DataFormat.Binary);
         if (IsCompressed)
         {
             Content = FileUtil.GZCompressToBytes(Content);
         }
         //写入长度
         Header.ContentLength = Content.Length;
         string headerStr = JsonUtility.ToJson(Header);
         using (BinaryWriter writer = new BinaryWriter(stream))
         {
             writer.Write(headerStr);
             writer.Write(Content);
             writer.Close();
         }
         stream.Close();
         IsInSaving = false;
     }
 }
예제 #2
0
        // 载入存档
        public void Load(string path, string name, int curVersion, bool isReadContent, DateTime fileTime)
        {
            ArchiveFile <T> archive = this;

            archive.Name       = name;
            archive.curVersion = curVersion;
            archive.FileTime   = fileTime;
            using (Stream stream = File.OpenRead(path))
            {
                try
                {
                    BinaryReader reader    = new BinaryReader(stream);
                    string       headerStr = null;
                    //使用try防止无效的存档
                    headerStr = reader.ReadString();
                    if (string.IsNullOrEmpty(headerStr))
                    {
                        archive.IsBroken = true;
                    }
                    else
                    {
                        archive.Header = JsonUtility.FromJson <ArchiveHeader>(headerStr);
                        int contentSize = archive.Header.ContentLength;
                        if (contentSize <= 0)
                        {
                            archive.IsBroken = true;
                        }
                        else
                        {
                            archive.Content = reader.ReadBytes(contentSize);
                            if (!string.IsNullOrEmpty(archive.Header.ContentHash))
                            {
                                // 内容损坏
                                if (archive.Header.ContentHash != FileUtil.Hash(archive.Content))
                                {
                                    archive.IsBroken = true;
                                    return;
                                }
                            }
                            if (isReadContent && archive.IsCompatible && contentSize > 0)
                            {
                                byte[] toBeDeserialized = null;
                                if (archive.Header.IsCompressed)
                                {
                                    toBeDeserialized = FileUtil.GZDecompressToBytes(archive.Content);
                                }
                                else
                                {
                                    toBeDeserialized = archive.Content;
                                }
                                archive.GameDatas = SerializationUtility.DeserializeValue <T>(toBeDeserialized, DataFormat.Binary);
                            }
                        }
                    }
                    reader.Close();
                }
                catch (Exception e)
                {
                    archive.IsBroken = true;
                    CLog.Error("读取存档{0}时出现异常:{1}, 因此认为是损坏的存档。", archive.Name, e.Message);
                }
            }
            return;
        }