-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataOperationService.cs
More file actions
161 lines (144 loc) · 3.9 KB
/
DataOperationService.cs
File metadata and controls
161 lines (144 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using UnityEngine;
// Enhanced data operation service
public class DataOperationService : IDataOperationService
{
private readonly IDataService dataService;
private readonly Dictionary<Type, IDataPack> dataPacks;
private readonly List<IDataPack> orderedPacks;
public DataOperationService(IDataService dataService)
{
this.dataService = dataService;
this.dataPacks = new Dictionary<Type, IDataPack>();
// Register all data packs
RegisterDataPack(new PlayerDataPack());
RegisterDataPack(new WorldDataPack());
RegisterDataPack(new SettingsDataPack());
// Order packs by priority for loading
orderedPacks = dataPacks.Values.OrderBy(p => p.Priority).ToList();
}
private void RegisterDataPack<T>(T dataPack) where T : IDataPack
{
dataPacks[typeof(T)] = dataPack;
}
// Get specific data pack
public T GetDataPack<T>() where T : class, IDataPack
{
return dataPacks.TryGetValue(typeof(T), out var pack) ? pack as T : null;
}
// Save operations
public void Save()
{
foreach (var pack in dataPacks.Values)
{
try
{
pack.Save(dataService);
}
catch (Exception ex)
{
Debug.LogError($"Failed to save {pack.FileName}: {ex.Message}");
}
}
}
public void Save<T>() where T : class, IDataPack
{
var pack = GetDataPack<T>();
if (pack != null)
{
try
{
pack.Save(dataService);
}
catch (Exception ex)
{
Debug.LogError($"Failed to save {pack.FileName}: {ex.Message}");
}
}
}
public async Task SaveAsync()
{
var tasks = dataPacks.Values.Select(pack => SavePackAsync(pack));
await Task.WhenAll(tasks);
}
private async Task SavePackAsync(IDataPack pack)
{
try
{
await pack.SaveAsync(dataService);
}
catch (Exception ex)
{
Debug.LogError($"Failed to save {pack.FileName}: {ex.Message}");
}
}
// Load operations
public void Load()
{
foreach (var pack in orderedPacks)
{
try
{
pack.Load(dataService);
}
catch (Exception ex)
{
Debug.LogError($"Failed to load {pack.FileName}: {ex.Message}");
}
}
}
public void Load<T>() where T : class, IDataPack
{
var pack = GetDataPack<T>();
if (pack != null)
{
try
{
pack.Load(dataService);
}
catch (Exception ex)
{
Debug.LogError($"Failed to load {pack.FileName}: {ex.Message}");
}
}
}
public async Task LoadAsync()
{
// Load in priority order (can't parallelize due to dependencies)
foreach (var pack in orderedPacks)
{
try
{
await pack.LoadAsync(dataService);
}
catch (Exception ex)
{
Debug.LogError($"Failed to load {pack.FileName}: {ex.Message}");
}
}
}
// Utility methods
public void ResetAll()
{
foreach (var pack in dataPacks.Values)
{
pack.Reset();
}
}
public void Reset<T>() where T : class, IDataPack
{
var pack = GetDataPack<T>();
pack?.Reset();
}
public bool IsAllLoaded()
{
return dataPacks.Values.All(p => p.IsLoaded);
}
public IEnumerable<string> GetLoadedPackNames()
{
return dataPacks.Values.Where(p => p.IsLoaded).Select(p => p.FileName);
}
}