|
- using System.IO;
- using System.IO.Compression;
- using HarmonyLib;
-
- namespace Kompressor
- {
- [HarmonyPatch(typeof(File), "WriteAllBytes")]
- public class FileWriteAllBytesPatch
- {
- public static void Prefix(string path, ref byte[] bytes)
- {
- if (!path.EndsWith("GameSave.GC")) return; // not a save file; do nothing
- GamecraftModdingAPI.Utility.Logging.MetaLog($"Writing compressed save to {path}");
- MemoryStream data = new MemoryStream();
- // compress files
- GZipStream gzip = new GZipStream(data, CompressionLevel.Optimal, true);
- gzip.Write(bytes, 0, bytes.Length);
- //GamecraftModdingAPI.Utility.Logging.MetaLog($"bytes.Length: {bytes.Length}");
- gzip.Flush();
- gzip.Close();
- gzip.Dispose();
- byte[] compressedData = new byte[data.Length + MyPlugin.COMPRESSION_FRAME_START.Length];
- // copy frame into start of compressedData
- for (byte i = 0; i < MyPlugin.COMPRESSION_FRAME_START.Length; i++)
- {
- compressedData[i] = MyPlugin.COMPRESSION_FRAME_START[i];
- }
- //GamecraftModdingAPI.Utility.Logging.MetaLog($"compressedData.Length: {data.Length}");
- data.Seek(0, 0);
- data.Read(compressedData, MyPlugin.COMPRESSION_FRAME_START.Length, (int) data.Length);
- // update stuff to write
- bytes = compressedData;
- }
- }
- }
|