Skip to content

Commit

Permalink
Implemented scribe compression
Browse files Browse the repository at this point in the history
  • Loading branch information
Byte-Nova committed Dec 29, 2024
1 parent 15122b9 commit b1da529
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Source/Client/Core/Configs/ModTweaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private void ShowConvertSaveFloatMenu()
string toConvertPath = str;
string conversionPath = str.Replace(".rws", ".mpsave");

byte[] compressedBytes = GZip.Compress(File.ReadAllBytes(toConvertPath));
byte[] compressedBytes = GZip.CompressBytes(File.ReadAllBytes(toConvertPath));
File.WriteAllBytes(conversionPath, compressedBytes);

RT_Dialog_OK d2 = new RT_Dialog_OK("Save was converted successfully");
Expand Down
4 changes: 2 additions & 2 deletions Source/Client/Managers/SaveManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private static void OnLastPartReceived(SaveData data)
Network.listener.downloadManager = null;

byte[] fileBytes = File.ReadAllBytes(tempSaveFilePath);
fileBytes = GZip.Decompress(fileBytes);
fileBytes = GZip.DecompressBytes(fileBytes);

File.WriteAllBytes(serverSaveFilePath, fileBytes);
File.Delete(tempSaveFilePath);
Expand Down Expand Up @@ -138,7 +138,7 @@ public static void SendSavePartToServer()
ClientValues.ToggleSendingSaveToServer(true);

byte[] saveBytes = File.ReadAllBytes(saveFilePath);
saveBytes = GZip.Compress(saveBytes);
saveBytes = GZip.CompressBytes(saveBytes);

File.WriteAllBytes(tempSaveFilePath, saveBytes);
Network.listener.uploadManager = new UploadManager(tempSaveFilePath);
Expand Down
2 changes: 2 additions & 0 deletions Source/Client/Managers/WorldManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using GameClient.Dialogs;
using GameClient.Misc;
using GameClient.Scribers;
using GameClient.TCP;
using GameClient.Values;
using RimWorld;
Expand Down Expand Up @@ -232,6 +233,7 @@ public static WorldValuesFile PopulateWorldValues()
WorldManager.cachedWorldValues.PollutedTiles = PollutionManagerHelper.GetPlanetPollutedTiles();
WorldManager.cachedWorldValues.NPCSettlements = GetPlanetNPCSettlements();
WorldManager.cachedWorldValues.NPCFactions = GetPlanetNPCFactions();

return WorldManager.cachedWorldValues;
}

Expand Down
2 changes: 2 additions & 0 deletions Source/Client/Patches/GameStatusPatches.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using GameClient.Managers;
using GameClient.Misc;
using GameClient.Scribers;
using GameClient.TCP;
using GameClient.Values;
using HarmonyLib;
Expand Down
52 changes: 48 additions & 4 deletions Source/Client/Scribers/RTScriber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using GameClient.Misc;
using GameClient.Values;
using RimWorld;
using RimWorld.Planet;
using Shared;
using Verse;
using static Shared.CommonEnumerators;
Expand All @@ -16,9 +17,9 @@ public static class RTScriber
{
public static StringWriter stringWriter;

private static readonly string scribeTreeName = "Tree";
public static readonly string scribeTreeName = "Tree";

private static readonly string scribeNodeName = "Node";
public static readonly string scribeNodeName = "Node";

public static string ThingToScribe(Thing toSave, int customCount = -1)
{
Expand All @@ -41,7 +42,7 @@ public static string ThingToScribe(Thing toSave, int customCount = -1)

ClientValues.ToggleUsingScriber(false);

return stringWriter.ToString();
return GZip.CompressString(stringWriter.ToString());
}

public static Thing ScribeToThing(string scribeData, bool hasCustomID)
Expand All @@ -52,7 +53,7 @@ public static Thing ScribeToThing(string scribeData, bool hasCustomID)

try
{
Scribe.loader.InitLoading(scribeData);
Scribe.loader.InitLoading(GZip.DecompressString(scribeData));

Scribe_Deep.Look(ref toLoad, scribeNodeName);

Expand Down Expand Up @@ -125,6 +126,49 @@ public static Thing StringToThing(ThingFile thingData, bool overrideID = false)
}
}

public static class TileScriber
{
public static string TileToScribe(Tile toSave)
{
ClientValues.ToggleUsingScriber(true);

try
{
Scribe.saver.InitSaving("", RTScriber.scribeTreeName);

Scribe_Deep.Look(ref toSave, RTScriber.scribeNodeName);

Scribe.saver.FinalizeSaving();
}
catch (Exception e) { Printer.Error(e.ToString(), LogImportanceMode.Verbose); };

ClientValues.ToggleUsingScriber(false);

return GZip.CompressString(RTScriber.stringWriter.ToString());
}

public static Tile ScribeToTile(string scribeData)
{
ClientValues.ToggleUsingScriber(true);

Tile toLoad = null;

try
{
Scribe.loader.InitLoading(GZip.DecompressString(scribeData));

Scribe_Deep.Look(ref toLoad, RTScriber.scribeNodeName);

Scribe.loader.FinalizeLoading();
}
catch (Exception e) { Printer.Error(e.ToString(), LogImportanceMode.Verbose); };

ClientValues.ToggleUsingScriber(false);

return toLoad;
}
}

public static class MapScriber
{
//Functions
Expand Down
53 changes: 44 additions & 9 deletions Source/Shared/Misc/GZip.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
using System.IO;
using System;
using System.IO;
using System.IO.Compression;
using System.Text;

namespace Shared
{
//Class in charge of managing compression/decompression of bytes

public static class GZip
{
//Compresses a given byte array into a smaller version

public static byte[] Compress(byte[] bytes)
public static byte[] CompressBytes(byte[] bytes)
{
using MemoryStream memoryStream = new MemoryStream();
using (GZipStream gzipStream = new GZipStream(memoryStream, CompressionLevel.Optimal))
Expand All @@ -20,9 +18,7 @@ public static byte[] Compress(byte[] bytes)
return memoryStream.ToArray();
}

//Decompresses a given byte array into the original version

public static byte[] Decompress(byte[] bytes)
public static byte[] DecompressBytes(byte[] bytes)
{
using MemoryStream memoryStream = new MemoryStream(bytes);
using MemoryStream outputStream = new MemoryStream();
Expand All @@ -33,5 +29,44 @@ public static byte[] Decompress(byte[] bytes)

return outputStream.ToArray();
}

public static string CompressString(string text)
{
byte[] buffer = Encoding.UTF8.GetBytes(text);

MemoryStream memoryStream = new MemoryStream();
using (GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, true))
{
gZipStream.Write(buffer, 0, buffer.Length);
}

memoryStream.Position = 0;
byte[] compressedData = new byte[memoryStream.Length];
memoryStream.Read(compressedData, 0, compressedData.Length);

byte[] gZipBuffer = new byte[compressedData.Length + 4];
Buffer.BlockCopy(compressedData, 0, gZipBuffer, 4, compressedData.Length);
Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gZipBuffer, 0, 4);

return Convert.ToBase64String(gZipBuffer);
}

public static string DecompressString(string compressedText)
{
byte[] gZipBuffer = Convert.FromBase64String(compressedText);

using MemoryStream memoryStream = new MemoryStream();
int dataLength = BitConverter.ToInt32(gZipBuffer, 0);
memoryStream.Write(gZipBuffer, 4, gZipBuffer.Length - 4);
byte[] buffer = new byte[dataLength];

memoryStream.Position = 0;
using (GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
{
gZipStream.Read(buffer, 0, buffer.Length);
}

return Encoding.UTF8.GetString(buffer);
}
}
}
4 changes: 2 additions & 2 deletions Source/Shared/Misc/Serializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ public static byte[] ConvertObjectToBytes(object toConvert, bool compression = f
serializer.Serialize(writer, toConvert);
}

if (compression) return GZip.Compress(memoryStream.ToArray());
if (compression) return GZip.CompressBytes(memoryStream.ToArray());
else return memoryStream.ToArray();
}

public static T ConvertBytesToObject<T>(byte[] bytes, bool compression = true)
{
if (compression) bytes = GZip.Decompress(bytes);
if (compression) bytes = GZip.DecompressBytes(bytes);

JsonSerializer serializer = JsonSerializer.Create(DefaultSettings);
MemoryStream memoryStream = new MemoryStream(bytes);
Expand Down

0 comments on commit b1da529

Please sign in to comment.