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.

53 lines
2.4KB

  1. using System.IO;
  2. using System.IO.Compression;
  3. using HarmonyLib;
  4. namespace Kompressor
  5. {
  6. [HarmonyPatch(typeof(File), "ReadAllBytes")]
  7. public class FileReadAllBytesPatch
  8. {
  9. public static void Postfix(string path, ref byte[] __result)
  10. {
  11. if (!path.EndsWith("GameSave.GC")) return; // not a save file; do nothing
  12. for (byte i = 0; i < MyPlugin.COMPRESSION_FRAME_START.Length; i++)
  13. {
  14. if (__result[i] != MyPlugin.COMPRESSION_FRAME_START[i])
  15. {
  16. // Don't do anything; not compressed
  17. return;
  18. }
  19. }
  20. GamecraftModdingAPI.Utility.Logging.MetaLog($"Reading compressed save from {path}");
  21. // remove frame at start of file
  22. byte[] compressedResult = new byte[__result.Length - MyPlugin.COMPRESSION_FRAME_START.Length];
  23. for (long i = MyPlugin.COMPRESSION_FRAME_START.Length; i < __result.Length; i++)
  24. {
  25. compressedResult[i - MyPlugin.COMPRESSION_FRAME_START.Length] = __result[i];
  26. }
  27. MemoryStream compressedData = new MemoryStream(compressedResult);
  28. // decompress
  29. GZipStream gzip = new GZipStream(compressedData, CompressionMode.Decompress, true);
  30. // update result
  31. MemoryStream decompressedData = new MemoryStream();
  32. byte[] buffer = new byte[1];
  33. int readCount = gzip.Read(buffer, 0, 1);
  34. while (readCount != 0)
  35. {
  36. decompressedData.Write(buffer, 0, 1);
  37. readCount = gzip.Read(buffer, 0, 1);
  38. }
  39. gzip.Close();
  40. byte[] newResult = new byte[decompressedData.Length];
  41. decompressedData.Seek(0, 0);
  42. decompressedData.Read(newResult, 0, (int)decompressedData.Length);
  43. GamecraftModdingAPI.Utility.Logging.MetaLog($"compressedData.Length: {compressedData.Length}");
  44. GamecraftModdingAPI.Utility.Logging.MetaLog($"decompressedData.Length: {decompressedData.Length}");
  45. GamecraftModdingAPI.Utility.Logging.MetaLog($"newResult: \"{System.Text.Encoding.UTF8.GetString(newResult)}\"");
  46. GamecraftModdingAPI.Utility.Logging.MetaLog($"Completed decompression of save");
  47. __result = newResult;
  48. compressedData.Close();
  49. decompressedData.Close();
  50. }
  51. }
  52. }