파일 입출력과 JSON변환 예외처리

This commit is contained in:
HaeinLEE 2025-03-14 11:06:54 +09:00
parent 70eb7d3268
commit 1925579508

View File

@ -64,6 +64,8 @@ public class ReplayManager : Singleton<ReplayManager>
/// 게임 종료 후 호출하여 리플레이 데이터를 저장합니다.
/// </summary>
public void SaveReplayData(PlayerType winnerPlayerType)
{
try
{
string time = DateTime.Now.ToString(("yyyy-MM-dd HH_mm_ss"));
_recordingReplayData.gameDate = time;
@ -79,6 +81,11 @@ public class ReplayManager : Singleton<ReplayManager>
//최신 데이터 10개만 유지되도록 저장
RecordCountChecker();
}
catch(Exception e)
{
Debug.LogError($"An error occurred while saving replay data:{e.Message}");
}
}
//폴더내 기보 파일을 전부 읽어옵니다.
@ -86,16 +93,34 @@ public class ReplayManager : Singleton<ReplayManager>
{
List<ReplayRecord> records = new List<ReplayRecord>();
string path = Application.persistentDataPath;
try
{
var files = Directory.GetFiles(path, "*.json");
foreach (var file in files)
{
records.Add(JsonUtility.FromJson<ReplayRecord>(File.ReadAllText(file)));
try
{
ReplayRecord record = JsonUtility.FromJson<ReplayRecord>(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;
}
private void RecordCountChecker()
{
try
{
string path = Application.persistentDataPath;
var files = Directory.GetFiles(path, "*.json");
@ -104,6 +129,11 @@ public class ReplayManager : Singleton<ReplayManager>
File.Delete(files[0]);
RecordCountChecker();
}
catch (Exception e)
{
Debug.LogError($"Replay Directory Error: {e.Message}");
}
}
protected override void OnSceneLoaded(Scene scene, LoadSceneMode mode)