using Jovian.SaveSystem; using Nox.Core; using System; using ZLinq; using UnityEngine; using PlayMode = Nox.Core.PlayMode; namespace Nox.Game { public class NoxSaveData { public static NoxSavedDataSet RestoreSavedData( ISaveSystem saveSystem, GameDataState gameDataState, ref AdventureData adventureData) { var sessions = saveSystem.GetAllSessions().AsValueEnumerable().OrderByDescending(s => s.lastSaveDateUtc).ToList(); if(sessions.Count == 0) { return null; } var latestSession = sessions[0]; var slots = saveSystem.GetSlots(latestSession.sessionId).AsValueEnumerable().OrderByDescending(s => s.timestampUtc).ToList(); if(slots.Count == 0) { return null; } var latestSlot = slots[0]; var saveData = saveSystem.Load(latestSlot); Debug.Log($"Loaded save {latestSlot.DisplayLabel}"); if(saveData == null) { Debug.LogError("Failed to load save data"); return null; } gameDataState.activeSessionId = latestSession.sessionId; gameDataState.savedPartyPosition = saveData.partyPosition.ToVector3(); gameDataState.ActiveParty = saveData.partyDefinition; adventureData = saveData.adventureData; return saveData; } } /// /// The game's save data snapshot. Contains all state needed to restore a game session. /// This is the TData passed to the save system package. /// [Serializable] public sealed class NoxSavedDataSet { // game state public PlayMode activePlayMode; //game mode specific data public AdventureData adventureData; // Party public PartyDefinition partyDefinition; public SerializableVector3 partyPosition; } /// /// JSON-friendly Vector3 representation for save data. /// [Serializable] public struct SerializableVector3 { public float x; public float y; public float z; public static SerializableVector3 Zero => new SerializableVector3 { x = 0, y = 0, z = 0 }; public static SerializableVector3 FromVector3(Vector3 value) { return new SerializableVector3 { x = value.x, y = value.y, z = value.z }; } public Vector3 ToVector3() { return new Vector3(x, y, z); } } }