From 1925579508e2982a82802b3b96a3d162a6ef1ff8 Mon Sep 17 00:00:00 2001 From: HaeinLEE Date: Fri, 14 Mar 2025 11:06:54 +0900 Subject: [PATCH] =?UTF-8?q?=ED=8C=8C=EC=9D=BC=20=EC=9E=85=EC=B6=9C?= =?UTF-8?q?=EB=A0=A5=EA=B3=BC=20JSON=EB=B3=80=ED=99=98=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Script/Replay/ReplayManager.cs | 74 +++++++++++++++++++-------- 1 file changed, 52 insertions(+), 22 deletions(-) diff --git a/Assets/Script/Replay/ReplayManager.cs b/Assets/Script/Replay/ReplayManager.cs index 9b208b3..9a4daa7 100644 --- a/Assets/Script/Replay/ReplayManager.cs +++ b/Assets/Script/Replay/ReplayManager.cs @@ -65,19 +65,26 @@ public class ReplayManager : Singleton /// public void SaveReplayData(PlayerType winnerPlayerType) { - string time = DateTime.Now.ToString(("yyyy-MM-dd HH_mm_ss")); - _recordingReplayData.gameDate = time; - string winner = winnerPlayerType == PlayerType.PlayerA ? "PlayerA" : "PlayerB"; - - - string json = JsonUtility.ToJson(_recordingReplayData, true); - - - string path = Path.Combine(Application.persistentDataPath, $"{time}.json"); - File.WriteAllText(path, json); - - //최신 데이터 10개만 유지되도록 저장 - RecordCountChecker(); + try + { + string time = DateTime.Now.ToString(("yyyy-MM-dd HH_mm_ss")); + _recordingReplayData.gameDate = time; + string winner = winnerPlayerType == PlayerType.PlayerA ? "PlayerA" : "PlayerB"; + + + string json = JsonUtility.ToJson(_recordingReplayData, true); + + + string path = Path.Combine(Application.persistentDataPath, $"{time}.json"); + File.WriteAllText(path, json); + + //최신 데이터 10개만 유지되도록 저장 + RecordCountChecker(); + } + catch(Exception e) + { + Debug.LogError($"An error occurred while saving replay data:{e.Message}"); + } } @@ -86,10 +93,26 @@ public class ReplayManager : Singleton { List records = new List(); string path = Application.persistentDataPath; - var files = Directory.GetFiles(path, "*.json"); - foreach (var file in files) + + try { - records.Add(JsonUtility.FromJson(File.ReadAllText(file))); + var files = Directory.GetFiles(path, "*.json"); + foreach (var file in files) + { + try + { + ReplayRecord record = JsonUtility.FromJson(File.ReadAllText(file)); + records.Add(record); + } + catch (Exception e) + { + Debug.LogError($"Replaydata cannot be converted to JSON: {e.Message}"); + } + } + } + catch (Exception e) + { + Debug.LogError($"Replay Directory Error: {e.Message}"); } return records; @@ -97,12 +120,19 @@ public class ReplayManager : Singleton private void RecordCountChecker() { - string path = Application.persistentDataPath; - var files = Directory.GetFiles(path, "*.json"); - if (files.Length <= 10) - return; - File.Delete(files[0]); - RecordCountChecker(); + try + { + string path = Application.persistentDataPath; + var files = Directory.GetFiles(path, "*.json"); + if (files.Length <= 10) + return; + File.Delete(files[0]); + RecordCountChecker(); + } + catch (Exception e) + { + Debug.LogError($"Replay Directory Error: {e.Message}"); + } }