Ejemplo n.º 1
0
        public static bool DecompressSave(string saveName, out string newSaveName)
        {
            newSaveName = string.Empty;
            string path = GameConfig.gameSaveFolder + saveName + GameSave.saveExt;

            try
            {
                using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
                {
                    if (!IsCompressedSave(fileStream))
                    {
                        return(false);
                    }
                    using (var lzstream = new LZ4DecompressionStream(fileStream))
                    {
                        newSaveName = UnzipToFile(lzstream, path);
                    }
                }
                return(true);
            }
            catch (Exception e)
            {
                logger.LogError(e);
                return(false);
            }
        }
Ejemplo n.º 2
0
        public static string UnzipToFile(LZ4DecompressionStream lzStream, string fullPath)
        {
            lzStream.ResetStream();
            string dir      = Path.GetDirectoryName(fullPath);
            string filename = "[Recovery]-" + Path.GetFileNameWithoutExtension(fullPath);

            fullPath = Path.Combine(dir, filename + GameSave.saveExt);
            int i = 0;

            while (File.Exists(fullPath))
            {
                fullPath = Path.Combine(dir, $"{filename}[{i++}]{GameSave.saveExt}");
            }
            var buffer = new byte[1024 * 1024];

            using (var fs = new FileStream(fullPath, FileMode.Create))
                using (var br = new BinaryWriter(fs))
                {
                    for (int read = lzStream.Read(buffer, 0, buffer.Length); read > 0; read = lzStream.Read(buffer, 0, buffer.Length))
                    {
                        fs.Write(buffer, 0, read);
                    }
                    fs.Seek(6L, SeekOrigin.Begin);
                    br.Write(fs.Length);
                }
            return(Path.GetFileNameWithoutExtension(fullPath));
        }
Ejemplo n.º 3
0
        static void TestMe()
        {
            var rand    = new Random(98765);
            var ints    = new int[5000].Select(_ => rand.Next()).ToArray();
            var floats  = new float[5000].Select(_ => rand.Next() / 10000f).ToArray();
            var doubles = new double[5000].Select(_ => rand.NextDouble()).ToArray();
            var longs   = new long[5000].Select(_ => rand.Next() * (long)rand.Next()).ToArray();
            var bytes   = new byte[8096]; rand.NextBytes(bytes);

            using (MemoryStream memoryStream = new MemoryStream(1024 * 1024 * 32))
            {
                using (LZ4CompressionStream cp = new LZ4CompressionStream(memoryStream, LZ4CompressionStream.CreateBuffer(1024), true))
                {
                    var w = cp.BufferWriter;
                    for (int i = 0; i < 5000; i++)
                    {
                        w.Write(ints[i]);
                        w.Write(doubles[i]);
                        w.Write(longs[i]);
                        w.Write(bytes[i]);
                        w.Write(floats[i]);
                    }

                    cp.BufferWriter.Flush();
                    cp.Write(bytes, 0, bytes.Length);
                    for (int i = 0; i < 5000; i++)
                    {
                        w.Write((sbyte)bytes[i]);
                        w.Write((uint)ints[i]);
                        w.Write(doubles[i]);
                        w.Write(floats[i]);
                        w.Write((ulong)longs[i]);
                    }
                    w.Write(bytes);
                    for (int i = 0; i < 100; i++)
                    {
                        w.Write(str);
                        w.Write(str2);
                        w.Write(str3);
                        w.Write(chars);
                    }
                }
                memoryStream.Position = 0;
                using (var decs = new LZ4DecompressionStream(memoryStream))
                    using (BinaryReader br = new BinaryReader(decs))
                    {
                        for (int i = 0; i < 5000; i++)
                        {
                            Debug.Assert(
                                (br.ReadInt32() == ints[i] &&
                                 br.ReadDouble() == doubles[i] &&
                                 br.ReadInt64() == longs[i] &&
                                 br.ReadByte() == bytes[i] &&
                                 br.ReadSingle() == floats[i])
                                );
                        }
                        Debug.Assert(br.ReadBytes(bytes.Length).SequenceEqual(bytes));
                        for (int i = 0; i < 5000; i++)
                        {
                            Debug.Assert(
                                (br.ReadSByte() == (sbyte)bytes[i] &&
                                 br.ReadUInt32() == (uint)ints[i] &&
                                 br.ReadDouble() == doubles[i] &&
                                 br.ReadSingle() == (floats[i]) &&
                                 br.ReadUInt64() == (ulong)longs[i])
                                );
                        }
                        Debug.Assert(br.ReadBytes(bytes.Length).SequenceEqual(bytes));
                        for (int i = 0; i < 100; i++)
                        {
                            Debug.Assert(
                                (br.ReadString() == str &&
                                 br.ReadString() == str2 &&
                                 br.ReadString() == str3 &&
                                 br.ReadChars(chars.Length).SequenceEqual(chars)));
                        }
                    }
                Console.WriteLine("Success");
            }
        }