Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

file transfer optimization #89

Merged
merged 3 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions Source/Client/Managers/SaveManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace GameClient
public static class SaveManager
{
public static string customSaveName = "ServerSave";
private static string tempSaveFilePath;
private static string saveFilePath;

public static void ForceSave()
{
Expand All @@ -31,10 +33,11 @@ public static void ReceiveSavePartFromServer(Packet packet)
Logger.Message($"Receiving save from server");

customSaveName = $"Server - {Network.ip} - {ClientValues.username}";
string filePath = Path.Combine(new string[] { Master.savesFolderPath, customSaveName + ".rws" });
tempSaveFilePath = Path.Combine(new string[] { Master.savesFolderPath, customSaveName + ".rws.temp" });
saveFilePath = Path.Combine(new string[] { Master.savesFolderPath, customSaveName + ".rws" });

Network.listener.downloadManager = new DownloadManager();
Network.listener.downloadManager.PrepareDownload(filePath, fileTransferData.fileParts);
Network.listener.downloadManager.PrepareDownload(tempSaveFilePath, fileTransferData.fileParts);
}

Network.listener.downloadManager.WriteFilePart(fileTransferData.fileBytes);
Expand All @@ -44,6 +47,11 @@ public static void ReceiveSavePartFromServer(Packet packet)
Network.listener.downloadManager.FinishFileWrite();
Network.listener.downloadManager = null;

byte[] compressedSave = File.ReadAllBytes(tempSaveFilePath);
byte[] save = GZip.Decompress(compressedSave);
File.WriteAllBytes(saveFilePath, save);
File.Delete(tempSaveFilePath);

GameDataSaveLoader.LoadGame(customSaveName);
}

Expand All @@ -60,10 +68,15 @@ public static void SendSavePartToServer(string fileName = null)
{
ClientValues.ToggleSendingSaveToServer(true);

string filePath = Path.Combine(new string[] { Master.savesFolderPath, fileName + ".rws" });
saveFilePath = Path.Combine(new string[] { Master.savesFolderPath, fileName + ".rws" });
tempSaveFilePath = $"{saveFilePath}.temp";

byte[] saveBytes = File.ReadAllBytes(saveFilePath); ;
byte[] compressedSave = GZip.Compress(saveBytes);
File.WriteAllBytes(tempSaveFilePath, compressedSave);

Network.listener.uploadManager = new UploadManager();
Network.listener.uploadManager.PrepareUpload(filePath);
Network.listener.uploadManager.PrepareUpload(tempSaveFilePath);
}

FileTransferData fileTransferData = new FileTransferData();
Expand All @@ -86,7 +99,10 @@ public static void SendSavePartToServer(string fileName = null)
if (Network.listener.uploadManager.isLastPart)
{
ClientValues.ToggleSendingSaveToServer(false);
Network.listener.uploadManager = null;
Network.listener.uploadManager = null;

Logger.WriteToConsole(tempSaveFilePath, CommonEnumerators.LogMode.Error);
File.Delete(tempSaveFilePath);
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions Source/Client/Patches/SavePatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using RimWorld;
using UnityEngine;
using Verse;
using Shared;
using System.IO;

namespace GameClient
{
Expand All @@ -22,10 +24,12 @@ public static bool DoPre(ref string fileName, ref int ___lastSaveTick)
ClientValues.ManageDevOptions();
CustomDifficultyManager.EnforceCustomDifficulty();

Logger.Message("Creating local save");
string filePath = GenFilePaths.FilePathForSavedGame(fileName);

Logger.Message($"Creating local save at {filePath}");
try
{
SafeSaver.Save(GenFilePaths.FilePathForSavedGame(fileName), "savegame", delegate
SafeSaver.Save(filePath, "savegame", delegate
{
ScribeMetaHeaderUtility.WriteMetaHeader();
Game target = Current.Game;
Expand Down
28 changes: 9 additions & 19 deletions Source/Server/Managers/SaveManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ public static void ReceiveSavePartFromClient(ServerClient client, Packet packet)
client.listener.downloadManager.FinishFileWrite();
client.listener.downloadManager = null;

byte[] saveBytes = File.ReadAllBytes(tempClientSavePath);
byte[] compressedSave = GZip.Compress(saveBytes);

File.WriteAllBytes(baseClientSavePath, compressedSave);
byte[] completedSave = File.ReadAllBytes(tempClientSavePath);
File.WriteAllBytes(baseClientSavePath, completedSave);
File.Delete(tempClientSavePath);

OnUserSave(client, fileTransferData);
Expand All @@ -50,11 +48,8 @@ public static void SendSavePartToClient(ServerClient client)
{
Logger.WriteToConsole($"[Load save] > {client.username} | {client.SavedIP}");

byte[] decompressedSave = GZip.Decompress(File.ReadAllBytes(baseClientSavePath));
File.WriteAllBytes(tempClientSavePath, decompressedSave);

client.listener.uploadManager = new UploadManager();
client.listener.uploadManager.PrepareUpload(tempClientSavePath);
client.listener.uploadManager.PrepareUpload(baseClientSavePath);
}

FileTransferData fileTransferData = new FileTransferData();
Expand All @@ -68,7 +63,6 @@ public static void SendSavePartToClient(ServerClient client)

if (client.listener.uploadManager.isLastPart)
{
File.Delete(tempClientSavePath);
client.listener.uploadManager = null;
}
}
Expand All @@ -89,10 +83,7 @@ public static bool CheckIfUserHasSave(ServerClient client)
foreach(string save in saves)
{
if (!save.EndsWith(".mpsave")) continue;
if (Path.GetFileNameWithoutExtension(save) == client.username)
{
return true;
}
if (Path.GetFileNameWithoutExtension(save) == client.username) return true;
}

return false;
Expand All @@ -104,10 +95,7 @@ public static byte[] GetUserSaveFromUsername(string username)
foreach (string save in saves)
{
if (!save.EndsWith(".mpsave")) continue;
if (Path.GetFileNameWithoutExtension(save) == username)
{
return File.ReadAllBytes(save);
}
if (Path.GetFileNameWithoutExtension(save) == username) return File.ReadAllBytes(save);
}

return null;
Expand All @@ -117,7 +105,9 @@ public static void ResetClientSave(ServerClient client)
{
if (!CheckIfUserHasSave(client))
{
ResponseShortcutManager.SendIllegalPacket(client, $"Player {client.username}'s save was attempted to be reset while the player doesn't have a save");
ResponseShortcutManager.SendIllegalPacket(client,
$"Player {client.username}'s save was attempted to be reset while the player doesn't have a save");

return;
}

Expand All @@ -128,7 +118,7 @@ public static void ResetClientSave(ServerClient client)
string toDelete = saves.ToList().Find(x => Path.GetFileNameWithoutExtension(x) == client.username);
if (!string.IsNullOrWhiteSpace(toDelete)) File.Delete(toDelete);

Logger.WriteToConsole($"[Delete save] > {client.username}", LogMode.Warning);
Logger.WriteToConsole($"[Delete save] > {client.username}", LogMode.Warning);

MapFileData[] userMaps = MapManager.GetAllMapsFromUsername(client.username);
foreach (MapFileData map in userMaps) MapManager.DeleteMap(map);
Expand Down