using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using HeathenEngineering.Scriptable; using HeathenEngineering.SteamApi.PlayerServices; using Steamworks; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; public sealed class SteamStorage : Singleton { private string fileCloudeName = "PlayerSaveDataFinal"; private string ES3SavefileInCloudName = "PlayerSaveDataES3Final"; [HideInInspector] public bool isLoadedProfiles; private bool isDoneLoadProfiles; [SerializeField] private SteamDataLibrary playerProfilesLiblary; [SerializeField] private SteamDataLibrary EasySaveDataLibrary; [SerializeField] private GameObject loadingProfilesMask; private LoadingProfilesMask currentLoadingMask; private string progressLoadingDots = ""; [HideInInspector] public List availableFiles = new List(); private int tryInitializedCount; private bool waitingForDownloadFile; private float _autSaveTimer; [SerializeField] private GameObject SaveDataConflictPopup; private const bool _showSteamCloudConflictPopup = false; public static DateTime lastTime; private string test1; private string test2; private void Start() { UnityEngine.Object.DontDestroyOnLoad(this); StartCoroutine(LoadPlayerProfilesFromSteam()); } private void Update() { if (waitingForDownloadFile && playerProfilesLiblary.activeFile.result == EResult.k_EResultOK) { Debug.Log("Loaded cloud files size: " + playerProfilesLiblary.activeFile.address.fileSize); for (int i = 0; i < playerProfilesLiblary.Library.Count; i++) { string key = playerProfilesLiblary.Library[i].Key; string playerLiblaryData = GetPlayerLiblaryData(key); if (!string.IsNullOrEmpty(playerLiblaryData)) { SPrefs.SetString(key, playerLiblaryData); } } GameManager.Instance._playerData.LoadPlayerProfiles(); isLoadedProfiles = true; waitingForDownloadFile = false; } AutoSaveToCloud(); } private void AutoSaveToCloud() { if (GameManager.Instance._playerData.currentPlayerProfileIndex == -1) { _autSaveTimer = 0f; return; } _autSaveTimer += Time.unscaledDeltaTime; if (_autSaveTimer > 60f) { EasySaveSystemManager.WriteSaveLog("AutoSave started."); SaveAllPlayerDataToCloude(); _autSaveTimer = 0f; } } public void CheckIsCloudeSaves() { ProfileCreation profileCreation = UnityEngine.Object.FindObjectOfType(); profileCreation.getCloudeButton.interactable = false; if (UnityEngine.Object.FindObjectOfType() != null && SteamManager.Initialized) { if (SteamworksRemoteStorageManager.Initalized) { DisplayCloudButtonWhenFilesAvailable(fileCloudeName, profileCreation); DisplayCloudButtonWhenFilesAvailable(ES3SavefileInCloudName, profileCreation); } } else { EasySaveSystemManager.WriteSaveLog("No Initalized SteamworksRemoteStorageManager"); } } private void DisplayCloudButtonWhenFilesAvailable(string fileName, ProfileCreation profileCreation) { if (!SteamworksRemoteStorageManager.Instance.FileExists(fileName)) { return; } SteamworksRemoteStorageManager.RefreshFileList(); UnityEngine.Object.FindObjectOfType().getCloudeButton.GetComponentInChildren().text = ""; for (int i = 0; i < SteamworksRemoteStorageManager.files.Count; i++) { if (!(SteamworksRemoteStorageManager.files[i].fileName == fileName)) { continue; } profileCreation.getCloudeButton.interactable = true; profileCreation.getCloudeButton.GetComponentInChildren().text = LanguageManager.Instance.GetText("GET_SAVES_FROM_CLOUDE") + " " + SteamworksRemoteStorageManager.files[i].LocalTimestamp; EasySaveSystemManager.WriteSaveLog("Available files in the Steam Cloud: "); lastTime = SteamworksRemoteStorageManager.files[i].LocalTimestamp; foreach (SteamworksRemoteStorageManager.FileAddress file in SteamworksRemoteStorageManager.files) { EasySaveSystemManager.WriteSaveLog("File: " + file.fileName + " " + file.LocalTimestamp); } } } public void GetCloudeSteamSavesButton() { GameManager.Instance.CreateMessageBox("GET_SAVES_FROM_CLOUDE_QUESTION", UnityEngine.Object.FindObjectOfType().transform, 11); } public void GetCloudeSteamSaves() { isDoneLoadProfiles = false; isLoadedProfiles = false; LoadPlayerProfilesWhenAvailable(); SceneManager.LoadScene("Startowa"); } public IEnumerator LoadPlayerProfilesFromSteam() { EasySaveSystemManager.WriteSaveLog("LoadPlayerProfilesFromSteam"); yield return null; ShowAllFiles(); CheckIsCloudeSaves(); DateTime lastWriteTime = File.GetLastWriteTime(Singleton.Instance.GetPlayerDataSaveDestination()); EasySaveDataLibrary.Load(ES3SavefileInCloudName); string text = EasySaveDataLibrary.Library[1].Variable.ObjectValue?.ToString(); string text2 = ""; if (!File.Exists(Singleton.Instance.GetPlayerDataSaveDestination())) { EasySaveSystemManager.WriteSaveLog("File not found: " + Singleton.Instance.GetPlayerDataSaveDestination()); } else { try { ES3Settings settings = new ES3Settings(ES3.EncryptionType.AES, "IvVGe1Hzejd36ne"); text2 = ES3.LoadRawString(Singleton.Instance.GetPlayerDataSaveDestination(), settings); } catch (Exception) { text2 = ES3.LoadRawString(Singleton.Instance.GetPlayerDataSaveDestination()); } } _ = LanguageManager.Instance.GetText("CLOUDCONFLICT_LASTEST_CLOUD") + ": " + lastTime.ToString() + " KB: " + Encoding.Unicode.GetByteCount(text) / 1024 + ".\n" + LanguageManager.Instance.GetText("CLOUDCONFLICT_LASTEST_LOCAL") + ": " + lastWriteTime.ToString() + " KB: " + Encoding.Unicode.GetByteCount(text2) / 1024; EasySaveSystemManager.WriteSaveLog("File from steamCloud: " + text); EasySaveSystemManager.WriteSaveLog("File from local folder: " + text2); if (string.IsNullOrEmpty(text) && string.IsNullOrEmpty(text2)) { LoadFromSprefs(); yield break; } if (string.IsNullOrEmpty(text) && text.Equals(text2)) { EasySaveSystemManager.WriteSaveLog("File from steamCloud: " + text); EasySaveSystemManager.WriteSaveLog("File from local folder: " + text2); } else if (!string.IsNullOrEmpty(text)) { EasySaveSystemManager.WriteSaveLog("File from steamCloud: " + text); EasySaveSystemManager.WriteSaveLog("File from local folder: " + text2); _ = lastTime.Subtract(lastWriteTime).TotalHours; _ = 24.0; } if (isLoadedProfiles) { EasySaveSystemManager.WriteSaveLog("isLoadedProfiles true"); } else if (ES3.FileExists(Singleton.Instance.GetPlayerDataSaveDestination())) { EasySaveSystemManager.WriteSaveLog("Should be loaded from local files"); } else if (SPrefs.HasKey("PLAYER_PROFILES")) { LoadFromSprefs(); } else if (SteamworksRemoteStorageManager.Initalized) { if (!isDoneLoadProfiles) { if (!LoadPlayerProfilesWhenAvailable()) { EasySaveSystemManager.WriteSaveLog("Nie wykryto zapisu gracza w chmurze, za³aduj pliki lokalne"); GameManager.Instance._playerData.LoadPlayerProfiles(); LoadDataIfExists(); isLoadedProfiles = true; DestroyLoadingMask(); } isDoneLoadProfiles = true; } } else { EasySaveSystemManager.WriteSaveLog("No Initalized SteamworksRemoteStorageManager"); StartCoroutine(RepeatLoadProfiles()); ShowLoadingMask(); if (tryInitializedCount == 10) { EasySaveSystemManager.WriteSaveLog("Brak po³¹czenia ze steam cloude, za³adowane lokalne zapisy"); GameManager.Instance._playerData.LoadPlayerProfiles(); isLoadedProfiles = true; DestroyLoadingMask(); tryInitializedCount = 0; } } } private void LoadFromSprefs() { EasySaveSystemManager.WriteSaveLog("SPrefs.HasKey(PLAYER_PROFILES)"); GameManager.Instance._playerData.LoadPlayerProfiles(); LoadDataIfExists(); UnityEngine.Object.FindObjectOfType().ShowProfileSlots(); isLoadedProfiles = true; foreach (GameManager.PlayerData.CPlayer item in GameManager.Instance._playerData.Player) { EasySaveSystemManager.WriteSaveLog("Loaded profile from prefs: " + item.playerName); } } private void ShowSteamCloudConflictPopup(string output) { SaveDataConflictPopup.SetActive(value: true); SaveDataConflictPopup.GetComponent().SetPopupDescription(output); SavesConflictPopupManager.OnLoadCloudButtonClick += LoadCloudButtonClick; SavesConflictPopupManager.OnUsePrefsButtonClick += UsePrefsButtonClick; SavesConflictPopupManager.OnDoNothingButtonClick += DoNothingButtonClick; } private void LoadCloudButtonClick() { SaveDataConflictPopup.SetActive(value: false); Singleton.Instance.LoadEasySaveData(); SceneManager.LoadScene("Startowa"); EasySaveSystemManager.WriteSaveLog("LoadCloudButtonClick"); DisconnectConflictPopupEvents(); } private void UsePrefsButtonClick() { SaveDataConflictPopup.SetActive(value: false); EasySaveSystemManager.WriteSaveLog("UsePrefsButtonClick"); LoadFromSprefs(); SceneManager.LoadScene("Startowa"); DisconnectConflictPopupEvents(); } private void DoNothingButtonClick() { SaveDataConflictPopup.SetActive(value: false); EasySaveSystemManager.WriteSaveLog("DoNothingButtonClick"); DisconnectConflictPopupEvents(); } private void DisconnectConflictPopupEvents() { SavesConflictPopupManager.OnLoadCloudButtonClick -= LoadCloudButtonClick; SavesConflictPopupManager.OnUsePrefsButtonClick -= UsePrefsButtonClick; SavesConflictPopupManager.OnDoNothingButtonClick -= DoNothingButtonClick; } public void LoadDataIfExists() { if (!ES3.FileExists(Singleton.Instance.GetPlayerDataSaveDestination())) { Singleton.Instance.LoadPlayerDataFromSPrefs(); EasySaveSystemManager.WriteSaveLog("LoadDataIfExists: "); int num = 0; foreach (SaveDataManager.SavePlayerDataClass playerDatum in Singleton.Instance.PlayerData) { EasySaveSystemManager.WriteSaveLog(num + " : " + playerDatum.PlayerName); num++; } } if (!ES3.FileExists(Singleton.Instance.GetSettingsDataSaveDestination())) { Singleton.Instance.LoadSettingsDataFromGameManager(); } } private IEnumerator RepeatLoadProfiles() { EasySaveSystemManager.WriteSaveLog("RepeatLoadProfiles"); if (!isLoadedProfiles) { yield return new WaitForSecondsRealtime(0.5f); tryInitializedCount++; EasySaveSystemManager.WriteSaveLog("Trying get profiles..." + tryInitializedCount); StartCoroutine(LoadPlayerProfilesFromSteam()); } } private bool LoadPlayerProfilesWhenAvailable() { if (SteamworksRemoteStorageManager.Instance.FileExists(ES3SavefileInCloudName) && EasySaveDataLibrary.Library[0].Variable.ObjectValue != null) { EasySaveDataLibrary.Load(ES3SavefileInCloudName); Invoke("LoadEasySaveData", 0.5f); } else { if (!SteamworksRemoteStorageManager.Instance.FileExists(fileCloudeName) || EasySaveDataLibrary.Library[0].Variable.ObjectValue == null) { return false; } playerProfilesLiblary.Load(fileCloudeName); } waitingForDownloadFile = true; DestroyLoadingMask(); return true; } public void SaveAllPlayerDataToCloude() { if (SteamManager.Initialized && SteamworksRemoteStorageManager.Initalized) { SetLiblaryFromPrefsData(); SaveEasySaveData(); ShowAllFiles(); } } private void SaveEasySaveData() { if (Singleton.Instance.AreAllProfilesEmpty()) { EasySaveSystemManager.WriteSaveLog("All profiles are empty, so won't save to steam cloud."); return; } EasySaveSystemManager.WriteSaveLog("Saving save data to steam cloud"); Singleton.Instance.SavePlayerSettings(); Singleton.Instance.SavePlayerData(); string text = ES3.LoadRawString(Singleton.Instance.GetSettingsDataSaveDestination()); string text2 = ""; try { ES3Settings settings = new ES3Settings(ES3.EncryptionType.AES, "IvVGe1Hzejd36ne"); text2 = ES3.LoadRawString(Singleton.Instance.GetPlayerDataSaveDestination(), settings); } catch (Exception ex) { EasySaveSystemManager.WriteSaveLog(ex.ToString()); text2 = ES3.LoadRawString(Singleton.Instance.GetPlayerDataSaveDestination()); } EasySaveSystemManager.WriteSaveLog("[EasySave 3] settingsBytes: " + text); EasySaveSystemManager.WriteSaveLog("[EasySave 3] playerBytes: " + text2); if (EasySaveDataLibrary != null) { EasySaveDataLibrary.Library[0].Variable.ObjectValue = text; EasySaveDataLibrary.Library[1].Variable.ObjectValue = text2; EasySaveDataLibrary.SaveAs(ES3SavefileInCloudName); } } private void LoadEasySaveData() { EasySaveSystemManager.WriteSaveLog("[EasySave 3] Loading data from steam cloud"); EasySaveDataLibrary.Load(ES3SavefileInCloudName); string text = EasySaveDataLibrary.Library[0].Variable.ObjectValue?.ToString(); EasySaveSystemManager.WriteSaveLog("EasySaveDataLibrary.Library[0]: " + text); string text2 = EasySaveDataLibrary.Library[1].Variable.ObjectValue?.ToString(); EasySaveSystemManager.WriteSaveLog("EasySaveDataLibrary.Library[1]: " + text2); ES3.SaveRaw(text, Singleton.Instance.GetSettingsDataSaveDestination()); ES3.LoadInto("SettingsData", Singleton.Instance.GetSettingsDataSaveDestination(), Singleton.Instance.SettingsData); ES3Settings settings = new ES3Settings(ES3.EncryptionType.AES, "IvVGe1Hzejd36ne"); ES3.SaveRaw(text2, Singleton.Instance.GetPlayerDataSaveDestination()); try { ES3.LoadInto("PlayerData", Singleton.Instance.GetPlayerDataSaveDestination(), Singleton.Instance.PlayerData, settings); } catch (Exception ex) { EasySaveSystemManager.WriteSaveLog(ex.ToString()); ES3.LoadInto("PlayerData", Singleton.Instance.GetPlayerDataSaveDestination(), Singleton.Instance.PlayerData); } EasySaveSystemManager.WriteSaveLog("Saved data applied."); UnityEngine.Object.FindObjectOfType().ShowProfileSlots(); } public void SaveEvent() { Debug.Log("Zapisano do pliku " + playerProfilesLiblary.activeFile.address.fileSize); } public void LoadEvent() { Debug.Log("Odczytano z pliku " + playerProfilesLiblary.activeFile.address.fileSize); } private void SetLiblaryFromPrefsData() { } private void ClearLiblaryData() { } private void ShowAllFiles() { if (SteamManager.Initialized) { SteamworksRemoteStorageManager.RefreshFileList(); } } private void SetPlayerLiblaryData(string key, string value) { foreach (KeyedVariable item in playerProfilesLiblary.Library) { if (item.Key == key) { item.Variable.ObjectValue = value; return; } } Debug.LogError("SetPlayerLiblaryData nie odnalazl pozycji"); } private string GetPlayerLiblaryData(string key) { foreach (KeyedVariable item in playerProfilesLiblary.Library) { if (item.Key == key) { return item.Variable.ObjectValue.ToString(); } } Debug.LogError("GetPlayerLiblaryData nie odnalazl pozycji"); return ""; } private void ShowLoadingMask() { if ((bool)currentLoadingMask) { progressLoadingDots += "."; currentLoadingMask.loadingText.text = LanguageManager.Instance.GetText("LOADING_PROFILES").ToUpper() + progressLoadingDots; } else { MainGameScene mainGameScene = UnityEngine.Object.FindObjectOfType(); currentLoadingMask = UnityEngine.Object.Instantiate(loadingProfilesMask, mainGameScene.transform).GetComponent(); progressLoadingDots = ""; } } private void DestroyLoadingMask() { if ((bool)currentLoadingMask) { UnityEngine.Object.Destroy(currentLoadingMask.gameObject); currentLoadingMask = null; } } private void OnApplicationQuit() { SaveEasySaveData(); ClearLiblaryData(); } }