diff --git a/Assets/KJM/KJM_Test/Save.cs b/Assets/KJM/KJM_Test/Save.cs index 4e94380a..5687fb38 100644 --- a/Assets/KJM/KJM_Test/Save.cs +++ b/Assets/KJM/KJM_Test/Save.cs @@ -22,13 +22,16 @@ public class HomeSave { // 일상 시간 public float time; + public int day; // 체력 및 평판 수치 public float health; public float reputation; - // 돌발 이벤트 발생 여부 + //이벤트 public bool isEvent; + public int mealCount; + public int houseworkCount; } // 게임 전체 저장 구조 diff --git a/Assets/KJM/KJM_Test/SaveManager.cs b/Assets/KJM/KJM_Test/SaveManager.cs index be80f79f..0b9c8df1 100644 --- a/Assets/KJM/KJM_Test/SaveManager.cs +++ b/Assets/KJM/KJM_Test/SaveManager.cs @@ -1,3 +1,4 @@ +using System; using System.Collections; using System.Collections.Generic; using System.IO; @@ -9,8 +10,9 @@ using UnityEngine.SceneManagement; public class SaveManager : Singleton { private const string SaveFolder = "QuickSave"; - private const string SaveFileName = "Save_Main.json"; - private string SaveFilePath => Path.Combine(Application.persistentDataPath, SaveFolder, SaveFileName); + private string MainSaveFilePath => GetSavePath("Save_Main"); + private string BackupSaveFilePath => GetSavePath("Save_Backup"); + private Save mainSave; private Save backupSave; @@ -18,17 +20,6 @@ public class SaveManager : Singleton void Start() { - - if (!QuickSaveRaw.Exists(SaveFilePath)) // Save_Main 파일이 없을때 - { - UpdateSaveInfo(); - SaveMain(); //Save_Main 파일 생성 - backupSave = LoadMain(); - SaveBackup(); //Save_Backup 파일 생성 - - Debug.Log("세이브가 존재하지 않아 새로운 세이브 생성."); - } - Load(); //저장된 메인,백업 세이브를 로드 } @@ -37,23 +28,27 @@ public class SaveManager : Singleton if(JsonUtility.ToJson(mainSave) == JsonUtility.ToJson(StatManager.instance.ToSaveData())) //같은 상태는 저장되지 않음. 백업 덮어쓰기 방지. return; + EnsureSaveExists(); + backupSave = LoadMain(); //메인 세이브를 백업 세이브에 로드 SaveBackup(); //백업 세이브 저장 UpdateSaveInfo(); //세이브를 현재 정보로 업데이트 SaveMain(); //메인 세이브 저장 - Debug.Log("세이브"); + Debug.Log("세이브 되었습니다."); } public void Load() { + EnsureSaveExists(); + mainSave = LoadMain(); backupSave = LoadBackup(); StatManager.instance.loadSaveData2StataManager(mainSave); - Debug.Log("메인 로드" + mainSave.homeSave.reputation); - Debug.Log("백업 로드" + backupSave.homeSave.reputation); + Debug.Log("메인 로드" + mainSave.homeSave.reputation); //임시 코드 + Debug.Log("백업 로드" + backupSave.homeSave.reputation); //임시 코드 } private void UpdateSaveInfo() @@ -77,26 +72,117 @@ public class SaveManager : Singleton private Save LoadMain() { - return QuickSaveReader.Create("Save_Main") - .Read("Main"); - } + try + { + return QuickSaveReader.Create("Save_Main").Read("Main"); + } + catch (System.Exception e) + { + Debug.LogWarning("Main 세이브 로드 실패: " + e.Message); + + // 백업 시도 + if (QuickSaveRaw.Exists(BackupSaveFilePath)) + { + Debug.LogWarning("백업 세이브로 복구 시도"); + return LoadBackup(); + } + // 백업도 없을 경우 새 세이브 생성 + Debug.LogError("세이브 전체 손상 → 새 세이브 생성"); + return CreateNewSave(); + } + } + private Save LoadBackup() { - return QuickSaveReader.Create("Save_Backup") - .Read("Backup"); + try + { + return QuickSaveReader.Create("Save_Backup").Read("Backup"); + } + catch (System.Exception e) + { + Debug.LogWarning("Backup 세이브 로드 실패: " + e.Message); + + // 백업 시도 + if (QuickSaveRaw.Exists(MainSaveFilePath)) + { + Debug.LogWarning("메인 세이브로 복구 시도"); + return LoadMain(); + } + + // 메인도 없을 경우 새 세이브 생성 + Debug.LogError("세이브 전체 손상 → 새 세이브 생성"); + return CreateNewSave(); + } } + //더미 세이브 파일 생성 + private Save CreateNewSave() + { + var fresh = StatManager.instance.ToSaveData(); + SaveMain(); + SaveBackup(); + return fresh; + } + + //세이브 파일의 존재 여부 확인 + private void EnsureSaveExists() + { + if (!QuickSaveRaw.Exists(MainSaveFilePath)) // Save_Main 파일이 없을때 + { + if (!QuickSaveRaw.Exists(BackupSaveFilePath)) //Save_Backup 파일도 존재하지 않을때 + { + UpdateSaveInfo(); + SaveMain(); //Save_Main 파일 생성 + backupSave = LoadMain(); + SaveBackup(); //Save_Backup 파일 생성 + + Debug.Log("세이브가 존재하지 않아 새로운 세이브를 생성했습니다."); + } + else + { + mainSave = LoadBackup(); //백업을 메인으로 로드. + SaveMain(); + Debug.Log("메인을 백업 세이브로 로드했습니다."); + } + } + else + { + if (!QuickSaveRaw.Exists(BackupSaveFilePath)) //Save_Backup 파일이 없을떄 + { + backupSave = LoadMain(); + SaveBackup(); + Debug.Log("백업을 메인 세이브로 로드했습니다."); + } + + } + } + + //세이브 파일 디렉토리 확인 + private string GetSavePath(string fileNameWithoutExt) + { + string directory = Path.Combine(Application.persistentDataPath, SaveFolder); + + // 폴더가 없다면 생성 + if (!Directory.Exists(directory)) + Directory.CreateDirectory(directory); + + return Path.Combine(directory, fileNameWithoutExt + ".json"); + } + + // 씬이 바뀔 때 마다 자동저장 protected override void OnSceneLoaded(Scene scene, LoadSceneMode mode) { - StartCoroutine(SaveAfterOneFrame()); //Awake와 충돌 방지를 위해 1프레임 대기 후 자동 저장 + StartCoroutine(SaveAfterOneFrame()); } - - private IEnumerator SaveAfterOneFrame() + + //Start함수 이후에 호출되도록 1프레임 지연 + IEnumerator SaveAfterOneFrame() { - yield return null; // 1 프레임 대기 + yield return null; Save(); - Debug.Log("자동저장"); + Debug.Log("자동저장 되었습니다."); } + } diff --git a/Assets/KJM/KJM_Test/StatManager.cs b/Assets/KJM/KJM_Test/StatManager.cs index d249b02f..b0129abc 100644 --- a/Assets/KJM/KJM_Test/StatManager.cs +++ b/Assets/KJM/KJM_Test/StatManager.cs @@ -20,9 +20,13 @@ public class StatManager : MonoBehaviour public int stageLevel; public float time; + public int day; public float health; public float reputation; + public bool isEvent; //Todo 이벤트 여부 및 관련 조건들 추가 + public int mealCount; + public int houseworkCount; private void Awake() @@ -49,9 +53,13 @@ public class StatManager : MonoBehaviour time = floatValue; + day = intValue; health = floatValue; reputation = floatValue; + isEvent = false; + mealCount = intValue; + houseworkCount = intValue; Debug.Log("ChangeValue"); @@ -77,9 +85,12 @@ public class StatManager : MonoBehaviour homeSave = new HomeSave { time = this.time, + day = this.day, health = this.health, reputation = this.reputation, - isEvent = false + isEvent = false, + mealCount = this.mealCount, + houseworkCount = this.houseworkCount, } }; } @@ -94,9 +105,12 @@ public class StatManager : MonoBehaviour stageLevel = saveData.dungeonSave.stageLevel; time = saveData.homeSave.time; + day = saveData.homeSave.day; health = saveData.homeSave.health; reputation = saveData.homeSave.reputation; isEvent = saveData.homeSave.isEvent; + mealCount = saveData.homeSave.mealCount; + houseworkCount = saveData.homeSave.houseworkCount; } public void SceneChange()