diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/App.cs b/AxibugEmuOnline.Client/Assets/Script/AppMain/App.cs index 34e04577..47909e9d 100644 --- a/AxibugEmuOnline.Client/Assets/Script/AppMain/App.cs +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/App.cs @@ -9,6 +9,7 @@ using System.Threading.Tasks; using UnityEngine; using static AxibugEmuOnline.Client.HttpAPI; using static AxibugEmuOnline.Client.Manager.LogManager; +using static AxibugEmuOnline.Client.SaveFile; namespace AxibugEmuOnline.Client.ClientCore { diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/AppLogin.cs b/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/AppLogin.cs index 33c1932d..11b34204 100644 --- a/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/AppLogin.cs +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/AppLogin.cs @@ -81,6 +81,8 @@ namespace AxibugEmuOnline.Client.Manager App.roomMgr.SendGetRoomList(); App.log.Info("获取在线玩家列表"); App.user.Send_GetUserList(); + //开始同步存档 + App.SavMgr.StartSyncSlot(); Eventer.Instance.PostEvent(EEvent.OnLoginSucceed); } diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/SaveSlotManager/SaveFile.cs b/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/SaveSlotManager/SaveFile.cs index da83eb70..75733541 100644 --- a/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/SaveSlotManager/SaveFile.cs +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/SaveSlotManager/SaveFile.cs @@ -2,7 +2,11 @@ using AxibugEmuOnline.Client.Tools; using AxibugProtobuf; using System; +using System.Collections.Generic; +using System.Linq; using System.Runtime.InteropServices; +using UnityEngine; +using SyncingFileRecord = System.Collections.Generic.Dictionary>>; namespace AxibugEmuOnline.Client { @@ -86,20 +90,7 @@ namespace AxibugEmuOnline.Client { byte[] saveOrderData = new byte[4]; - //FileStream streaming = System.IO.File.OpenRead(FilePath); - //int res = streaming.Read(saveOrderData, 0, 4); - //if (res != 4) //无效的存档文件 - //{ - // IsEmpty = true; - // File.Delete(FilePath); - //} - //else - //{ - // Sequecen = BitConverter.ToUInt32(saveOrderData, 0); - //} - //streaming.Dispose(); - - int res = AxiIO.File.ReadBytesToArr(FilePath, saveOrderData,0,4); + int res = AxiIO.File.ReadBytesToArr(FilePath, saveOrderData, 0, 4); if (res < 4) //无效的存档文件 { IsEmpty = true; @@ -232,6 +223,129 @@ namespace AxibugEmuOnline.Client } + + private void SetSavingFlag() + { + SyncingFilesUtility.Add(this); + } + + private void ClearSavingFlag() + { + SyncingFilesUtility.Remove(this); + } + + public static class SyncingFilesUtility + { + static SyncingFileRecord m_syncFiles = new SyncingFileRecord(); + + public static void ReadFromPlayprefs() + { + var jsonStr = AxiPlayerPrefs.GetString("SYNCING_SAVE"); + if (string.IsNullOrEmpty(jsonStr)) return; + + var temp = JsonUtility.FromJson(jsonStr); + if (temp.List == null || temp.List.Count == 0) return; + + foreach (var file in temp.List) + { + Add(file); + } + } + + public static void ContinueSync() + { + var allFiles = m_syncFiles.Values.SelectMany(v => v.Values).SelectMany(v => v); + foreach (var file in allFiles) + { + var savFile = App.SavMgr.GetSaveFile(file.romID, file.platform, file.slotIndex); + savFile.TrySync(); + } + } + + public static void Add(SaveFile savFile) + { + SyncingFiles file = new SyncingFiles { romID = savFile.RomID, platform = savFile.EmuPlatform, slotIndex = savFile.SlotIndex }; + Add(file); + } + + private static void Add(SyncingFiles file) + { + if (!m_syncFiles.TryGetValue(file.platform, out var mapper)) + { + mapper = new Dictionary>(); + m_syncFiles.Add(file.platform, mapper); + } + + if (!mapper.TryGetValue(file.romID, out var syncingTables)) + { + syncingTables = new HashSet(); + mapper[file.romID] = syncingTables; + } + + if (syncingTables.Add(file)) + { + WritePlayerprefs(); + } + } + + public static void Remove(SaveFile savFile) + { + SyncingFiles file = new SyncingFiles { romID = savFile.RomID, platform = savFile.EmuPlatform, slotIndex = savFile.SlotIndex }; + if (!m_syncFiles.TryGetValue(file.platform, out var mapper)) + { + return; + } + + if (!mapper.TryGetValue(savFile.RomID, out var syncingTables)) + { + return; + } + + if (syncingTables.Remove(file)) + { + WritePlayerprefs(); + } + } + + private static void WritePlayerprefs() + { + var allFiles = m_syncFiles.Values.SelectMany(v => v.Values).SelectMany(v => v); + var temp = new SyncingFilesList { List = new List(allFiles) }; + + var jsonStr = JsonUtility.ToJson(temp); + AxiPlayerPrefs.SetString("SYNCING_SAVE", jsonStr); + } + + } + + [Serializable] + class SyncingFilesList + { + [SerializeField] + public List List; + } + + [Serializable] + public struct SyncingFiles + { + public int romID; + public RomPlatformType platform; + public int slotIndex; + + public override int GetHashCode() + { + return HashCode.Combine(romID, platform, slotIndex); + } + + public override bool Equals(object obj) + { + if (obj == null) return false; + if (obj.GetHashCode() != GetHashCode()) return false; + + return true; + } + } + [StructLayout(LayoutKind.Explicit, Size = 24)] struct Header { diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/SaveSlotManager/SaveFileSyncStates/DownloadingState.cs b/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/SaveSlotManager/SaveFileSyncStates/DownloadingState.cs index 7884f129..3d8a5cdf 100644 --- a/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/SaveSlotManager/SaveFileSyncStates/DownloadingState.cs +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/SaveSlotManager/SaveFileSyncStates/DownloadingState.cs @@ -1,5 +1,6 @@ using AxibugEmuOnline.Client.ClientCore; using AxibugEmuOnline.Client.Tools; +using System; namespace AxibugEmuOnline.Client { @@ -26,6 +27,13 @@ namespace AxibugEmuOnline.Client m_sequece = (uint)netData.Sequence; m_downloadTask = AxiHttpProxy.GetDownLoad($"{App.httpAPI.WebHost}/{netData.SavUrl}"); m_downloadTaskImg = AxiHttpProxy.GetDownLoad($"{App.httpAPI.WebHost}/{netData.SavImgUrl}"); + + Host.SetSavingFlag(); + } + + public override void OnExit(SimpleFSM.State nextState) + { + Host.ClearSavingFlag(); } public override void OnUpdate() @@ -52,5 +60,6 @@ namespace AxibugEmuOnline.Client FSM.ChangeState(); } } + } } \ No newline at end of file diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/SaveSlotManager/SaveFileSyncStates/SyncedState.cs b/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/SaveSlotManager/SaveFileSyncStates/SyncedState.cs index 46bb251f..2c372a43 100644 --- a/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/SaveSlotManager/SaveFileSyncStates/SyncedState.cs +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/SaveSlotManager/SaveFileSyncStates/SyncedState.cs @@ -6,6 +6,10 @@ namespace AxibugEmuOnline.Client { public class SyncedState : SimpleFSM.State { + public override void OnEnter(SimpleFSM.State preState) + { + Host.ClearSavingFlag(); + } } } } \ No newline at end of file diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/SaveSlotManager/SaveFileSyncStates/UploadingState.cs b/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/SaveSlotManager/SaveFileSyncStates/UploadingState.cs index 73bdae71..224db995 100644 --- a/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/SaveSlotManager/SaveFileSyncStates/UploadingState.cs +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/SaveSlotManager/SaveFileSyncStates/UploadingState.cs @@ -17,10 +17,13 @@ namespace AxibugEmuOnline.Client } Host.GetSavData(out byte[] savData, out byte[] screenData); Host.CloudAPI.SendUpLoadGameSav(Host.RomID, Host.SlotIndex, Host.Sequecen, savData, screenData); + + Host.SetSavingFlag(); } public override void OnExit(SimpleFSM.State nextState) { + Host.ClearSavingFlag(); Host.CloudAPI.OnUploadedSavData -= Api_OnUploadedSavData; } diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/SaveSlotManager/SaveSlotManager.cs b/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/SaveSlotManager/SaveSlotManager.cs index 11328a06..cb78187b 100644 --- a/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/SaveSlotManager/SaveSlotManager.cs +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/Manager/SaveSlotManager/SaveSlotManager.cs @@ -13,6 +13,12 @@ namespace AxibugEmuOnline.Client Dictionary m_saveFileDict = new Dictionary(); + public void StartSyncSlot() + { + SaveFile.SyncingFilesUtility.ReadFromPlayprefs(); + SaveFile.SyncingFilesUtility.ContinueSync(); + } + public void Update() { foreach (var saveFiles in m_saveFileDict.Values) @@ -35,7 +41,7 @@ namespace AxibugEmuOnline.Client return result; } - SaveFile GetSaveFile(int romID, RomPlatformType platform, int slotIndex) + public SaveFile GetSaveFile(int romID, RomPlatformType platform, int slotIndex) { if (!m_saveFileDict.TryGetValue(romID, out SaveFile[] files)) {