1+ using System ;
2+ using System . IO ;
3+ using System . Xml . Serialization ;
14using Code . Infrastructure . Services . PersistenceProgress ;
25using Code . Infrastructure . Services . PersistenceProgress . Player ;
6+ using UnityEngine ;
37
48namespace 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