コード例 #1
0
        /// <summary>
        /// 保存存档
        /// </summary>
        /// <param name="datas"></param>
        /// <param name="stream"></param>
        public void Save(T datas, string path)
        {
            using (Stream stream = new FileStream(path, FileMode.Create))
            {
                if (datas == null)
                {
                    throw new System.ArgumentNullException("datas");
                }
                GameDatas = datas;
                using (MemoryStream ms = BaseFileUtils.SerializeToMemoryStream(GameDatas))
                {
                    byte[] uncompressed = ms.ToArray();
                    Content              = BaseFileUtils.GZCompressToBytes(uncompressed);
                    Header.PlayTime      = GameDatas.PlayTime;
                    Header.Version       = _curVersion;
                    Header.IsHide        = _isHide;
                    Header.ContentLength = Content.Length;
                    Header.SaveTimeTicks = DateTime.Now.Ticks;
                    Header.ContentHash   = BaseFileUtils.Hash(Content);
                    Header.IsCompressed  = true;
                    string headerStr = JsonUtility.ToJson(Header);

                    using (BinaryWriter writer = new BinaryWriter(stream))
                    {
                        writer.Write(headerStr);
                        writer.Write(Content);
                        writer.Close();
                    }
                }
                stream.Close();
            }
        }
コード例 #2
0
        /// <summary>
        /// 载入存档
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="name"></param>
        /// <param name="curVersion"></param>
        /// <param name="isReadContent"></param>
        /// <param name="fileTime"></param>
        /// <returns></returns>
        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 != BaseFileUtils.Hash(archive.Content))
                                {
                                    archive.IsBroken = true;
                                    return;
                                }
                            }
                            if (isReadContent && archive.IsCompatible && contentSize > 0)
                            {
                                byte[] toBeDeserialized = null;
                                if (archive.Header.IsCompressed)
                                {
                                    toBeDeserialized = BaseFileUtils.GZDecompressToBytes(archive.Content);
                                }
                                else
                                {
                                    toBeDeserialized = archive.Content;
                                }
                                using (MemoryStream ms = new MemoryStream(toBeDeserialized))
                                {
                                    archive.GameDatas = BaseFileUtils.DeSerializeStream <T>(ms);
                                }
                            }
                        }
                    }
                    reader.Close();
                }
                catch (Exception e)
                {
                    archive.IsBroken = true;
                    CLog.Error("读取存档{0}时出现异常:{1}, 因此认为是损坏的存档。", archive.Name, e.Message);
                }
            }
            return;
        }