Skip to content

Commit

Permalink
Cleanup game data saving code
Browse files Browse the repository at this point in the history
  • Loading branch information
cheese3660 committed Nov 7, 2023
1 parent ea55371 commit 9495532
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 26 deletions.
39 changes: 17 additions & 22 deletions SpaceWarp.Core/API/SaveGameManager/ModSaves.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace SpaceWarp.API.SaveGameManager;
[PublicAPI]
public static class ModSaves
{
private static readonly ILogger _logger = new UnityLogSource("SpaceWarp.ModSaves");
private static readonly ILogger Logger = new UnityLogSource("SpaceWarp.ModSaves");

internal static List<PluginSaveData> InternalPluginSaveData = new();

Expand All @@ -25,36 +25,33 @@ public static class ModSaves
public static T RegisterSaveLoadGameData<T>(string modGuid, Action<T> onSave, Action<T> onLoad, T saveData = default)
{
// Create adapter functions to convert Action<T> to CallbackFunctionDelegate
SaveGameCallbackFunctionDelegate saveCallbackAdapter = (object saveData) =>
void SaveCallbackAdapter(object dataToBeSaved)
{
if (onSave != null && saveData is T data)
if (onSave != null && dataToBeSaved is T data)
{
onSave(data);
}
};
}

SaveGameCallbackFunctionDelegate loadCallbackAdapter = (object saveData) =>
void LoadCallbackAdapter(object dataToBeLoaded)
{
if (onLoad != null && saveData is T data)
if (onLoad != null && dataToBeLoaded is T data)
{
onLoad(data);
}
};
}

// Check if this GUID is already registered
if (InternalPluginSaveData.Find(p => p.ModGuid == modGuid) != null)
{
throw new ArgumentException($"Mod GUID '{modGuid}' is already registered. Skipping.", "modGuid");
throw new ArgumentException($"Mod GUID '{modGuid}' is already registered. Skipping.", nameof(modGuid));
}
else
{
if (saveData == null)
saveData = Activator.CreateInstance<T>();

InternalPluginSaveData.Add(new PluginSaveData { ModGuid = modGuid, SaveEventCallback = saveCallbackAdapter, LoadEventCallback = loadCallbackAdapter, SaveData = saveData });
_logger.LogInfo($"Registered '{modGuid}' for save/load events.");
return saveData;
}
saveData ??= Activator.CreateInstance<T>();

InternalPluginSaveData.Add(new PluginSaveData { ModGuid = modGuid, SaveEventCallback = SaveCallbackAdapter, LoadEventCallback = LoadCallbackAdapter, SaveData = saveData });
Logger.LogInfo($"Registered '{modGuid}' for save/load events.");
return saveData;
}

/// <summary>
Expand All @@ -64,11 +61,9 @@ public static T RegisterSaveLoadGameData<T>(string modGuid, Action<T> onSave, Ac
public static void UnRegisterSaveLoadGameData(string modGuid)
{
var toRemove = InternalPluginSaveData.Find(p => p.ModGuid == modGuid);
if (toRemove != null)
{
InternalPluginSaveData.Remove(toRemove);
_logger.LogInfo($"Unregistered '{modGuid}' for save/load events.");
}
if (toRemove == null) return;
InternalPluginSaveData.Remove(toRemove);
Logger.LogInfo($"Unregistered '{modGuid}' for save/load events.");
}

/// <summary>
Expand All @@ -83,6 +78,6 @@ public static void UnRegisterSaveLoadGameData(string modGuid)
public static T ReregisterSaveLoadGameData<T>(string modGuid, Action<T> onSave, Action<T> onLoad, T saveData = default(T))
{
UnRegisterSaveLoadGameData(modGuid);
return RegisterSaveLoadGameData<T>(modGuid, onSave, onLoad, saveData);
return RegisterSaveLoadGameData(modGuid, onSave, onLoad, saveData);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using UnityEngine.Serialization;

namespace SpaceWarp.Backend.SaveGameManager;

Expand All @@ -9,5 +10,5 @@ namespace SpaceWarp.Backend.SaveGameManager;
[Serializable]
public class SpaceWarpSerializedSavedGame : KSP.Sim.SerializedSavedGame
{
public List<PluginSaveData> SerializedPluginSaveData = new();
public List<PluginSaveData> serializedPluginSaveData = new();
}
6 changes: 3 additions & 3 deletions SpaceWarp.Core/Patching/SaveGameManager/SaveGamePatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private static void InjectPluginSaveGameData(string filename, LoadGameData data,
// Take the game's LoadGameData, extend it with our own class and copy plugin save data to it
SpaceWarpSerializedSavedGame modSaveData = new();
InternalExtensions.CopyFieldAndPropertyDataFromSourceToTargetObject(data.SavedGame, modSaveData);
modSaveData.SerializedPluginSaveData = ModSaves.InternalPluginSaveData;
modSaveData.serializedPluginSaveData = ModSaves.InternalPluginSaveData;
data.SavedGame = modSaveData;

// Initiate save callback for plugins that specified a callback function
Expand Down Expand Up @@ -56,10 +56,10 @@ private static bool DeserializeLoadedPluginData(Action resolve, Action<string> r
__instance._data.DataLength = IOProvider.GetFileSize(__instance._filename);

// Perform plugin load data if plugin data is found in the save file
if (serializedSavedGame.SerializedPluginSaveData.Count > 0)
if (serializedSavedGame.serializedPluginSaveData.Count > 0)
{
// Iterate through each plugin
foreach (var loadedData in serializedSavedGame.SerializedPluginSaveData)
foreach (var loadedData in serializedSavedGame.serializedPluginSaveData)
{
// Match registered plugin GUID with the GUID found in the save file
var existingData = ModSaves.InternalPluginSaveData.Find(p => p.ModGuid == loadedData.ModGuid);
Expand Down

0 comments on commit 9495532

Please sign in to comment.