-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataManager.cs
More file actions
371 lines (314 loc) · 11.7 KB
/
DataManager.cs
File metadata and controls
371 lines (314 loc) · 11.7 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
using Microsoft.Xna.Framework;
using Newtonsoft.Json;
using Terraria;
using TShockAPI;
using static FishMach.Plugin;
namespace FishMach;
internal class DataManager
{
// 缓存目录
private static string CacheDir => Path.Combine(MainPath, "钓鱼机缓存");
// 查找钓鱼机方法
public static List<MachData> Machines = new();
public static Dictionary<Point, MachData> MachByPos = new();
public static Dictionary<int, MachData> MachByChest = new();
public static Dictionary<string, MachData> MachByRegName = new();
// 查找传输箱方法
public static Dictionary<int, HashSet<MachData>> OutChestMap = new();
// 区域名 -> 该区域内所有箱子索引的 HashSet
public static Dictionary<string, HashSet<int>> RegionChests = new();
public static MachData? FindTile(Point pos)
=> MachByPos.TryGetValue(pos, out var data) ? data : null;
public static MachData? FindChest(int idx)
=> MachByChest.TryGetValue(idx, out var data) ? data : null;
public static MachData? FindRegion(string name)
=> MachByRegName.TryGetValue(name, out var data) ? data : null;
public static bool IsAfmRegion(string name) => name.StartsWith("afm_");
// 获取机器文件路径
private static string GetPath(MachData data) =>
Path.Combine(CacheDir, $"{data.ChestIndex}-{data.Owner}-{data.Pos.X}-{data.Pos.Y}.json");
#region 保存指定文件
public static void Save(MachData data)
{
if (!Directory.Exists(CacheDir))
Directory.CreateDirectory(CacheDir);
File.WriteAllText(GetPath(data), JsonConvert.SerializeObject(data, Formatting.Indented));
}
#endregion
#region 删除指定文件
public static void Del(MachData data)
{
string path = GetPath(data);
if (File.Exists(path))
File.Delete(path);
}
#endregion
#region 添加或更新机器
public static void AddOrSave(MachData data)
{
// 移除旧映射(先查找旧位置)
if (MachByPos.ContainsKey(data.Pos))
MachByPos.Remove(data.Pos);
// 移除旧箱子映射
var oldChest = MachByChest.FirstOrDefault(x => x.Value == data).Key;
if (oldChest != 0)
MachByChest.Remove(oldChest);
// 移除旧区域映射
var oldRegion = MachByRegName.FirstOrDefault(x => x.Value == data).Key;
if (oldRegion != null)
MachByRegName.Remove(oldRegion);
// 添加新映射
if (!Machines.Contains(data))
Machines.Add(data);
MachByPos[data.Pos] = data;
if (data.ChestIndex != -1)
MachByChest[data.ChestIndex] = data;
if (!string.IsNullOrEmpty(data.RegName))
MachByRegName[data.RegName] = data;
// 处理传输箱映射(一对多)
foreach (var outIdx in data.OutChests)
{
if (!OutChestMap.TryGetValue(outIdx, out var set))
{
set = new HashSet<MachData>();
OutChestMap[outIdx] = set;
}
set.Add(data);
}
Save(data);
}
#endregion
#region 设置输出机映射
/// <summary>添加一个传输箱,自动维护映射并保存</summary>
public static void AddOutChest(MachData data, int newOut)
{
if (data == null || newOut == -1) return;
if (data.OutChests.Contains(newOut)) return;
data.OutChests.Add(newOut);
if (!OutChestMap.TryGetValue(newOut, out var set))
{
set = new HashSet<MachData>();
OutChestMap[newOut] = set;
}
set.Add(data);
Save(data);
}
/// <summary>移除一个传输箱,自动维护映射并保存</summary>
public static void RemoveOutChest(MachData data, int outIdx)
{
if (data == null || !data.OutChests.Remove(outIdx)) return;
if (OutChestMap.TryGetValue(outIdx, out var set))
{
set.Remove(data);
if (set.Count == 0)
OutChestMap.Remove(outIdx);
}
Save(data);
}
/// <summary>清空所有传输箱</summary>
public static void ClearOutChests(MachData data)
{
if (data == null) return;
foreach (var outIdx in data.OutChests.ToList())
RemoveOutChest(data, outIdx);
}
#endregion
#region 读取所有文件
public static void LoadAll()
{
if (!Directory.Exists(CacheDir))
{
// 目录不存在:清空内存数据(无需删除物理目录)
Machines.Clear();
MachByPos.Clear();
MachByChest.Clear();
MachByRegName.Clear();
OutChestMap.Clear();
return;
}
var files = Directory.GetFiles(CacheDir, "*.json");
var newMach = new List<MachData>();
foreach (var file in files)
{
try
{
var data = JsonConvert.DeserializeObject<MachData>(File.ReadAllText(file));
if (data == null) continue;
// 如果文件中存在 WorldId 且与当前世界 ID 不一致,则跳过(空或不存在则视为兼容)
if (!string.IsNullOrEmpty(data.WorldId) && data.WorldId != Main.worldID.ToString())
continue;
newMach.Add(data);
}
catch (Exception ex)
{
TShock.Log.ConsoleError($"[自动钓鱼机] 加载文件 {file} 失败: {ex.Message}");
}
}
// 清空现有字典
MachByPos.Clear();
MachByChest.Clear();
MachByRegName.Clear();
OutChestMap.Clear();
// 赋值新列表
Machines = newMach;
// 重建字典映射
foreach (var data in Machines)
{
MachByPos[data.Pos] = data;
if (data.ChestIndex != -1)
MachByChest[data.ChestIndex] = data;
if (!string.IsNullOrEmpty(data.RegName))
MachByRegName[data.RegName] = data;
if (data.HasOut)
foreach (var outIdx in data.OutChests)
{
if (!OutChestMap.TryGetValue(outIdx, out var set))
{
set = new HashSet<MachData>();
OutChestMap[outIdx] = set;
}
set.Add(data);
}
foreach (var kv in data.Custom)
{
if (kv.Value.Expiry.Kind != DateTimeKind.Utc)
kv.Value.Expiry = DateTime.SpecifyKind(kv.Value.Expiry, DateTimeKind.Utc);
}
foreach (var kv in data.ActiveZoneBuffs)
{
if (kv.Value.Kind != DateTimeKind.Utc)
data.ActiveZoneBuffs[kv.Key] = DateTime.SpecifyKind(kv.Value, DateTimeKind.Utc);
}
}
}
#endregion
#region 移除指定缓存
public static void Remove(Point pos)
{
var data = FindTile(pos);
if (data == null) return;
data.ClearAnim();
// 从动画活跃集合中移除
ActiveAnim.Remove(data);
MachByPos.Remove(pos);
if (!string.IsNullOrEmpty(data.RegName))
{
try { TShock.Regions.DeleteRegion(data.RegName); }
catch (Exception ex)
{
TShock.Log.ConsoleError($"[自动钓鱼机] 删除区域 {data.RegName} 失败: {ex.Message}");
}
}
if (data.ChestIndex != -1)
MachByChest.Remove(data.ChestIndex);
if (!string.IsNullOrEmpty(data.RegName))
MachByRegName.Remove(data.RegName);
// 从传输箱映射中移除
foreach (var outIdx in data.OutChests)
{
if (OutChestMap.TryGetValue(outIdx, out var set))
{
set.Remove(data);
if (set.Count == 0)
OutChestMap.Remove(outIdx);
}
}
Machines.Remove(data);
// 标记调度器中的机器为失效
FishSched.Invalidate(data);
Del(data);
}
#endregion
#region 更新区域附近箱子方法
public static void UpdateRegionChests(MachData data)
{
var region = TShock.Regions.GetRegionByName(data.RegName);
if (region == null) return;
// 1. 重建区域箱子缓存
var chestsInRegion = new HashSet<int>();
for (int i = 0; i < Main.chest.Length; i++)
{
var chest = Main.chest[i];
if (chest != null && region.Area.Contains(chest.x, chest.y))
chestsInRegion.Add(i);
}
RegionChests[data.RegName] = chestsInRegion;
// 2. 清理该钓鱼机中不存在的输出箱
bool outChanged = false;
foreach (int outIdx in data.OutChests.ToList())
{
if (Main.chest[outIdx] == null)
{
RemoveOutChest(data, outIdx);
outChanged = true;
}
}
if (outChanged) data.ClearAnim();
}
public static void UpdateAllRegionChests()
{
// 收集所有区域名
var regionNames = Machines.Select(m => m.RegName).Distinct().ToList();
if (regionNames.Count == 0) return;
// 临时字典用于重建缓存
var temp = new Dictionary<string, HashSet<int>>();
foreach (var name in regionNames)
temp[name] = new HashSet<int>();
// 一次遍历所有箱子,构建每个区域的箱子索引集合
for (int i = 0; i < Main.chest.Length; i++)
{
var chest = Main.chest[i];
if (chest == null) continue;
foreach (var name in regionNames)
{
var region = TShock.Regions.GetRegionByName(name);
if (region != null && region.Area.Contains(chest.x, chest.y))
temp[name].Add(i);
}
}
// 更新全局缓存
foreach (var kv in temp)
RegionChests[kv.Key] = kv.Value;
// 清理所有钓鱼机中不存在的输出箱
foreach (var data in Machines)
{
bool outChanged = false;
foreach (int outIdx in data.OutChests.ToList())
{
if (Main.chest[outIdx] == null)
{
RemoveOutChest(data, outIdx);
outChanged = true;
}
}
if (outChanged) data.ClearAnim();
}
}
#endregion
#region 清空所有
public static void Clear()
{
// 清除所有区域
var Regions = TShock.Regions.Regions.Where(r => IsAfmRegion(r.Name)).ToList();
foreach (var r in Regions)
{
try { TShock.Regions.DeleteRegion(r.Name); }
catch (Exception ex)
{
TShock.Log.ConsoleError($"[自动钓鱼机] 删除区域 {r.Name} 失败: {ex.Message}");
}
}
ActiveAnim.Clear(); // 清空动画活跃集合
Machines.Clear();
MachByPos.Clear();
MachByChest.Clear();
MachByRegName.Clear();
OutChestMap.Clear();
FishSched.Clear();
RegionChests.Clear();
AfmPlrMag.PlyDatas.Clear(); // 清理玩家数据
if (Directory.Exists(CacheDir))
Directory.Delete(CacheDir, true);
}
#endregion
}