using Newtonsoft.Json; using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Xml; using TMPro; using UnityEngine; using UnityEngine.Networking; public class LoadHistory : MonoBehaviour { static public LoadHistory Instance { get; private set; } [SerializeField] private GameObject _panelSaveHistory; [SerializeField] private Transform _contentSaveHistory; [SerializeField] private GameObject _prefabElement; private List _saveHistory = new(); private bool _isAutoSaveRequestOnCooldown; private void Awake() { if (Instance != null && Instance != this) { Debug.Log($"Cannot have multiple instances of class {nameof(LoadHistory)}."); Destroy(gameObject); return; } Instance = this; } public void OpenPanel() { _panelSaveHistory.SetActive(true); SceneModeManager.Instance.SetSceneMode(SceneModes.Panel); StartCoroutine(RequestSavedHistory(PostLoadSaveAction.all)); } public void ReceiveSaveHistoryFromServer(List loadedFileNames) { int transformCount = loadedFileNames.Count; byte[] bytes = SaveXML.BuildXmlBytes(); string localUDT = Encoding.UTF8.GetString(bytes); bool isLocalSaved = _isAutoSaveRequestOnCooldown || (localUDT == BackgroundSave.Instance.GetLastestServerUDT()); if (!isLocalSaved) { transformCount++; } if (_saveHistory.Count > transformCount) { _saveHistory.RemoveRange(transformCount, _saveHistory.Count - transformCount); int indexTransform = 0; foreach (Transform item in _contentSaveHistory) { if (indexTransform >= transformCount) { Destroy(item.gameObject); } indexTransform++; } } else if (_saveHistory.Count < transformCount) { int historyCount = _saveHistory.Count; for (int i = 0; i < transformCount - historyCount; i++) { _saveHistory.Add(Instantiate(_prefabElement, _contentSaveHistory).GetComponent()); } } loadedFileNames.Sort((a, b) => SaveHistoryData.GetDateTime(b).CompareTo(SaveHistoryData.GetDateTime(a))); int indexSave = 0; if (!isLocalSaved) { SaveHistoryData saveData = _saveHistory[indexSave]; saveData.SetValues(System.DateTime.Now.ToString("yyyyMMdd_HHmm"), "Not saved\nLocal", false); indexSave++; } foreach (var item in loadedFileNames) { SaveHistoryData data = _saveHistory[indexSave]; data.SetValues(item, "Auto-saved\nServer", true); indexSave++; } } public IEnumerator RequestSavedHistory(PostLoadSaveAction action) { Debug.Log("request save history"); WWWForm form = new(); form.AddField("action", action.ToString()); form.AddField("ref_id", _G.ref_id); using (UnityWebRequest www = UnityWebRequest.Post("https://software.ciemetric.com/_autosave/read.php", form)) { Debug.Log("unitywebrequest"); UnityWebRequest.ClearCookieCache(); yield return www.SendWebRequest(); if (www.result != UnityWebRequest.Result.Success) { Debug.Log(www.error); Debug.Log(www.downloadHandler.error); } else { List result = JsonConvert.DeserializeObject>(www.downloadHandler.text); if (result != null) { ReceiveSaveHistoryFromServer(result); } else { Debug.Log("save history is null"); } } } } public IEnumerator RequestLoadFromServer(PostLoadSaveAction action, DateTime dateTime, Action callbackDone = null) { string date = dateTime.ToString("yyyyMMdd_HHmm"); Debug.Log($"request load from server : action = {action} , ref_id = {_G.ref_id} , date = {date}"); WWWForm form = new(); form.AddField("action", action.ToString()); form.AddField("ref_id", _G.ref_id); form.AddField("date", date); using (UnityWebRequest www = UnityWebRequest.Post("https://software.ciemetric.com/_autosave/read.php", form)) { Debug.Log("unitywebrequest"); UnityWebRequest.ClearCookieCache(); yield return www.SendWebRequest(); if (www.result != UnityWebRequest.Result.Success) { Debug.Log(www.error); Debug.Log(www.downloadHandler.error); } else { string downloadedUDT = www.downloadHandler.text; //Debug.Log(downloadedUDT); if (downloadedUDT != null && downloadedUDT.Length > 0) { Restart.restart(); // loading visuals GameObject.Find("HIDER").transform.Find("LOADpnl").GetComponent().alpha = 0; GameObject.Find("HIDER").transform.Find("LoadingCircle").gameObject.SetActive(true); Get.o4("Canvas", "Panel_SCENE", "DASH", "FN").gameObject.GetComponent().text = _G.FileName; // load scene XmlDocument xmlDoc = new(); xmlDoc.LoadXml(downloadedUDT); LoadXML.SetGLOBAL(xmlDoc); BackgroundSave.Instance.InitTimer(); DOIT.CloseALLPNL(); Debug.Log("success loaded from server"); } else { Debug.Log("no file loaded from server"); } } callbackDone?.Invoke(); } } public IEnumerator RequestAutoSaveToServer(string udt, Action callbackSuccess = null) { if (_isAutoSaveRequestOnCooldown) { Debug.Log("can't request auto-save more than once a minute"); callbackSuccess?.Invoke(false); yield break; } else { StartCoroutine(CooldownAutoSaveRequest()); } Debug.Log("request save to server"); WWWForm form = new(); form.AddField("ref_id", _G.ref_id); form.AddField("data", udt); using (UnityWebRequest www = UnityWebRequest.Post("https://software.ciemetric.com/_autosave/save.php", form)) { Debug.Log("unitywebrequest"); UnityWebRequest.ClearCookieCache(); yield return www.SendWebRequest(); if (www.result != UnityWebRequest.Result.Success) { Debug.Log(www.error); Debug.Log(www.downloadHandler.error); callbackSuccess?.Invoke(false); } else { Debug.Log(www.downloadHandler.text); BackgroundSave.Instance.SetLastestServerUDT(udt); Debug.Log("success saved to server"); callbackSuccess?.Invoke(true); } } } private IEnumerator CooldownAutoSaveRequest() { if (_isAutoSaveRequestOnCooldown) { yield break; } _isAutoSaveRequestOnCooldown = true; yield return new WaitForSecondsRealtime(60.0f); _isAutoSaveRequestOnCooldown = false; } } public enum PostLoadSaveAction { all, data }