Low spec Gamecraft mod
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

35 lines
1.4KB

  1. using System.IO;
  2. using System.IO.Compression;
  3. using HarmonyLib;
  4. namespace Kompressor
  5. {
  6. [HarmonyPatch(typeof(File), "WriteAllBytes")]
  7. public class FileWriteAllBytesPatch
  8. {
  9. public static void Prefix(string path, ref byte[] bytes)
  10. {
  11. if (!path.EndsWith("GameSave.GC")) return; // not a save file; do nothing
  12. GamecraftModdingAPI.Utility.Logging.MetaLog($"Writing compressed save to {path}");
  13. MemoryStream data = new MemoryStream();
  14. // compress files
  15. GZipStream gzip = new GZipStream(data, CompressionLevel.Optimal, true);
  16. gzip.Write(bytes, 0, bytes.Length);
  17. //GamecraftModdingAPI.Utility.Logging.MetaLog($"bytes.Length: {bytes.Length}");
  18. gzip.Flush();
  19. gzip.Close();
  20. gzip.Dispose();
  21. byte[] compressedData = new byte[data.Length + MyPlugin.COMPRESSION_FRAME_START.Length];
  22. // copy frame into start of compressedData
  23. for (byte i = 0; i < MyPlugin.COMPRESSION_FRAME_START.Length; i++)
  24. {
  25. compressedData[i] = MyPlugin.COMPRESSION_FRAME_START[i];
  26. }
  27. //GamecraftModdingAPI.Utility.Logging.MetaLog($"compressedData.Length: {data.Length}");
  28. data.Seek(0, 0);
  29. data.Read(compressedData, MyPlugin.COMPRESSION_FRAME_START.Length, (int) data.Length);
  30. // update stuff to write
  31. bytes = compressedData;
  32. }
  33. }
  34. }