Skip to content

Commit 3f74dfa

Browse files
committed
feat: json save
1 parent 00dab2b commit 3f74dfa

File tree

2 files changed

+41
-15
lines changed

2 files changed

+41
-15
lines changed

Assets/Code/Infrastructure/Services/GameStater/GameStarter.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
1-
using System.Collections.Generic;
21
using Code.Infrastructure.Factory;
32
using Code.Infrastructure.Services.PersistenceProgress;
43
using Code.Infrastructure.Services.PersistenceProgress.Player;
54
using Code.Infrastructure.Services.SaveLoad;
6-
using UnityEditor;
75
using UnityEngine;
86

97
namespace Code.Infrastructure.Services.GameStater
108
{
119
public class GameStarter : IGameStarter
1210
{
1311
private readonly IPersistenceProgressService _progressService;
14-
private readonly ISaveLoadService _saveLoadService;
12+
private readonly ISaveLoadFacade _saveLoadFacade;
1513
private readonly IUIFactory _uiFactory;
1614

1715
public GameStarter(
1816
IPersistenceProgressService progressService,
19-
ISaveLoadService saveLoadService,
17+
ISaveLoadFacade saveLoadFacade,
2018
IUIFactory uiFactory)
2119
{
2220
_progressService = progressService;
23-
_saveLoadService = saveLoadService;
21+
_saveLoadFacade = saveLoadFacade;
2422
_uiFactory = uiFactory;
2523
}
2624

@@ -32,6 +30,8 @@ public void Initialize()
3230
InitUI();
3331

3432
SetUpRandomData();
33+
_saveLoadFacade.SaveProgress(SaveMethod.PlayerPrefs);
34+
_saveLoadFacade.SaveProgress(SaveMethod.Json);
3535
}
3636

3737
private void InitProgress()
@@ -48,8 +48,8 @@ private void InitUI()
4848
private PlayerData LoadProgress()
4949
{
5050
Debug.Log("LoadProgress");
51-
52-
return _saveLoadService.Load();
51+
52+
return _saveLoadFacade.Load(SaveMethod.PlayerPrefs);
5353
}
5454

5555
private PlayerData SetUpBaseProgress()
@@ -109,10 +109,8 @@ private void SetUpRandomData()
109109
};
110110

111111
_progressService.PlayerData = progress;
112-
_saveLoadService.SaveProgress();
113112

114113
Debug.Log("✅ Random player data saved.");
115114
}
116-
117115
}
118116
}
Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,59 @@
11
using System;
2+
using System.IO;
23
using Code.Infrastructure.Services.PersistenceProgress;
34
using Code.Infrastructure.Services.PersistenceProgress.Player;
5+
using Sirenix.Serialization;
6+
using UnityEngine;
47

58
namespace Code.Infrastructure.Services.SaveLoad
69
{
710
public class JsonSaveLoadService : ISaveLoadService
811
{
12+
private const string FileName = "player_data.json";
13+
914
private readonly IPersistenceProgressService _progressService;
15+
private string FilePath => Path.Combine(Application.persistentDataPath, FileName);
1016

1117
public JsonSaveLoadService(IPersistenceProgressService progressService)
1218
{
1319
_progressService = progressService;
1420
}
1521

16-
public void SaveProgress()
17-
{
18-
throw new NotImplementedException();
19-
}
22+
public void SaveProgress() => Save(_progressService.PlayerData);
2023

2124
public void Save(PlayerData playerData)
2225
{
23-
throw new NotImplementedException();
26+
try
27+
{
28+
byte[] data = SerializationUtility.SerializeValue(playerData, DataFormat.JSON);
29+
File.WriteAllBytes(FilePath, data);
30+
31+
Debug.Log($"💾 JSON Save complete at: {FilePath}");
32+
}
33+
catch (Exception e)
34+
{
35+
Debug.LogError($"❌ Failed to save JSON: {e.Message}");
36+
}
2437
}
2538

2639
public PlayerData Load()
2740
{
28-
throw new NotImplementedException();
41+
try
42+
{
43+
if (!File.Exists(FilePath))
44+
{
45+
Debug.LogWarning($"📂 JSON file not found at: {FilePath}");
46+
return null;
47+
}
48+
49+
byte[] data = File.ReadAllBytes(FilePath);
50+
return SerializationUtility.DeserializeValue<PlayerData>(data, DataFormat.JSON);
51+
}
52+
catch (Exception e)
53+
{
54+
Debug.LogError($"❌ Failed to load JSON: {e.Message}");
55+
return null;
56+
}
2957
}
3058
}
3159
}

0 commit comments

Comments
 (0)