Skip to content

Commit

Permalink
Support versioning on save.
Browse files Browse the repository at this point in the history
  • Loading branch information
isadorasophia committed Nov 25, 2023
1 parent 467ad28 commit f03425a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
11 changes: 9 additions & 2 deletions src/Murder/Assets/Save/SaveDataAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public class SaveData : GameAsset
/// </summary>
public string SaveName { get; init; } = string.Empty;

/// <summary>
/// Game version, used for game save compatibility.
/// </summary>
public readonly float SaveVersion;

/// <summary>
/// This is save path, used by its assets.
/// </summary>
Expand All @@ -77,11 +82,13 @@ public class SaveData : GameAsset
[JsonConstructor]
public SaveData() { }

protected SaveData(string name, BlackboardTracker tracker)
protected SaveData(string name, float version, BlackboardTracker tracker)
{
Guid = Guid.NewGuid();
Name = Guid.ToString();

SaveVersion = version;

SaveName = name;

FilePath = Path.Join(Name, $"{Name}.json");
Expand All @@ -90,7 +97,7 @@ protected SaveData(string name, BlackboardTracker tracker)
BlackboardTracker = tracker;
}

public SaveData(string name) : this(name, new BlackboardTracker()) { }
public SaveData(string name, float version) : this(name, version, new BlackboardTracker()) { }

/// <summary>
/// Get a world asset to instantiate in the game.
Expand Down
1 change: 1 addition & 0 deletions src/Murder/Core/Particles/ParticleSystemTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public ParticleSystemTracker(Emitter emitter, Particle particle, int seed = 0)
/// </summary>
/// <param name="allowSpawn">Whether spawning new entities is allowed, e.g. the entity is not deactivated.</param>
/// <param name="emitterPosition">Emitter position in game where the particles are fired from.</param>
/// <param name="id">Entity id, used when generating the correct seed for the particle.</param>
/// <returns>Returns whether the emitter is still running.</returns>
public bool Step(bool allowSpawn, Vector2 emitterPosition, int id)
{
Expand Down
10 changes: 8 additions & 2 deletions src/Murder/Data/GameDataManager_Save.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public partial class GameDataManager
/// Creates an implementation of SaveData for the game.
/// </summary>
protected virtual SaveData CreateSaveData(string name = "_default") =>
_game is not null ? _game.CreateSaveData(name) : new SaveData(name);
_game is not null ? _game.CreateSaveData(name) : new SaveData(name, _game?.Version ?? 0);

/// <summary>
/// Directory used for saving custom data.
Expand Down Expand Up @@ -367,10 +367,16 @@ public bool LoadAllSaves()

public bool LoadSaveAtPath(string path)
{
foreach (var asset in FetchAssetsAtPath(path, recursive: false, stopOnFailure: true))
foreach (GameAsset asset in FetchAssetsAtPath(path, recursive: false, skipFailures: false, stopOnFailure: true))
{
if (asset is SaveData saveData)
{
if (saveData.SaveVersion < _game?.Version)
{
// Skip loading saves with incompatible version, for now.
continue;
}

_allSavedData.Add(saveData.Guid, saveData);
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/Murder/IMurderGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void OnExit() { }
/// <summary>
/// Creates save data for the game.
/// </summary>
public SaveData CreateSaveData(string name) => new(name);
public SaveData CreateSaveData(string name) => new(name, Version);

/// <summary>
/// Creates the client custom sound player.
Expand Down Expand Up @@ -81,5 +81,10 @@ public RenderContext CreateRenderContext(GraphicsDevice graphicsDevice, Camera2D
/// This is the name of the game, used when creating assets and loading save data.
/// </summary>
public string Name { get; }

/// <summary>
/// This is the version of the game, used when checking for save compatibility.
/// </summary>
public float Version => 0;
}
}

0 comments on commit f03425a

Please sign in to comment.