Skip to content

Commit e2fdf05

Browse files
committed
feat: xml
1 parent f76250e commit e2fdf05

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ public void Initialize()
3434

3535
SetUpRandomData();
3636
_saveLoadFacade.SaveProgress(SaveMethod.Json);
37+
38+
SetUpRandomData();
39+
_saveLoadFacade.SaveProgress(SaveMethod.Xml);
3740
}
3841

3942
private void InitProgress()
Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,66 @@
1+
using System;
2+
using System.IO;
3+
using System.Xml.Serialization;
14
using Code.Infrastructure.Services.PersistenceProgress;
25
using Code.Infrastructure.Services.PersistenceProgress.Player;
6+
using UnityEngine;
37

48
namespace Code.Infrastructure.Services.SaveLoad
59
{
610
public class XmlSaveLoadService : ISaveLoadService
711
{
12+
private const string FileName = "player_data.xml";
13+
814
private readonly IPersistenceProgressService _progressService;
15+
private readonly string _savePath;
916

1017
public XmlSaveLoadService(IPersistenceProgressService progressService)
1118
{
1219
_progressService = progressService;
20+
_savePath = Path.Combine(Application.persistentDataPath, FileName);
1321
}
1422

1523
public void SaveProgress()
1624
{
17-
25+
Save(_progressService.PlayerData);
1826
}
1927

2028
public void Save(PlayerData playerData)
2129
{
30+
try
31+
{
32+
XmlSerializer serializer = new XmlSerializer(typeof(PlayerData));
33+
using FileStream stream = new FileStream(_savePath, FileMode.Create);
34+
serializer.Serialize(stream, playerData);
35+
Debug.Log($"💾 XML Save complete at: {_savePath}");
36+
}
37+
catch (Exception e)
38+
{
39+
Debug.LogError($"❌ Failed to save XML: {e.Message}");
40+
}
2241
}
2342

2443
public PlayerData Load()
2544
{
26-
return null;
45+
try
46+
{
47+
if (!File.Exists(_savePath))
48+
{
49+
Debug.LogWarning("⚠️ XML save file not found.");
50+
return null;
51+
}
52+
53+
XmlSerializer serializer = new XmlSerializer(typeof(PlayerData));
54+
using FileStream stream = new FileStream(_savePath, FileMode.Open);
55+
var data = (PlayerData)serializer.Deserialize(stream);
56+
Debug.Log("✅ XML Load complete.");
57+
return data;
58+
}
59+
catch (Exception e)
60+
{
61+
Debug.LogError($"❌ Failed to load XML: {e.Message}");
62+
return null;
63+
}
2764
}
2865
}
29-
}
66+
}

0 commit comments

Comments
 (0)