-
-
Notifications
You must be signed in to change notification settings - Fork 399
/
Copy pathMachineManager.cs
343 lines (282 loc) · 12.1 KB
/
MachineManager.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using Pathoschild.Stardew.Automate.Framework.Models;
using Pathoschild.Stardew.Common;
using StardewModdingAPI;
using StardewValley;
using StardewValley.Extensions;
namespace Pathoschild.Stardew.Automate.Framework;
/// <summary>Manages machine groups.</summary>
internal class MachineManager
{
/*********
** Fields
*********/
/// <summary>Encapsulates monitoring and logging.</summary>
private readonly IMonitor Monitor;
/// <summary>The mod configuration.</summary>
private readonly Func<ModConfig> Config;
/// <summary>The internal mod data.</summary>
private readonly DataModel Data;
/// <summary>The machine data for each location.</summary>
private readonly Dictionary<string, MachineDataForLocation> MachineData = new();
/// <summary>The cached machines to process.</summary>
private IMachineGroup[] ActiveMachineGroups = [];
/// <summary>The cached disabled machine groups (e.g. machines not connected to a chest).</summary>
private IMachineGroup[] DisabledMachineGroups = [];
/// <summary>The locations that should be removed on the next update tick.</summary>
private readonly HashSet<GameLocation> RemoveQueue = new(new GameLocationNameComparer());
/// <summary>The locations that should be reloaded on the next update tick.</summary>
private readonly HashSet<GameLocation> ReloadQueue = new(new GameLocationNameComparer());
/*********
** Accessors
*********/
/// <summary>Constructs machine groups.</summary>
public MachineGroupFactory Factory { get; }
/// <summary>An aggregate collection of machine groups linked by Junimo chests.</summary>
public List<GlobalMachineGroup> GlobalMachineGroups { get; } = new();
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="config">The mod configuration.</param>
/// <param name="data">The internal mod data.</param>
/// <param name="defaultFactory">The default automation factory to registry.</param>
/// <param name="monitor">Encapsulates monitoring and logging.</param>
public MachineManager(Func<ModConfig> config, DataModel data, IAutomationFactory defaultFactory, IMonitor monitor)
{
this.Config = config;
this.Data = data;
this.Monitor = monitor;
this.Factory = new(this.GetMachineOverride, this.BuildStorage, monitor);
this.Factory.Add(defaultFactory);
//this.GlobalMachineGroups = new(this.Factory.SortMachines, this.BuildStorage, this.Monitor);
}
/****
** Machine search
****/
/// <summary>Get the machine groups in every location.</summary>
public IEnumerable<IMachineGroup> GetActiveMachineGroups()
{
foreach (IMachineGroup group in this.GlobalMachineGroups)
if (group.HasInternalAutomation)
yield return group;
foreach (IMachineGroup group in this.ActiveMachineGroups)
yield return group;
}
/// <summary>Get the active and disabled machine groups in a specific location for the API.</summary>
/// <param name="location">The location whose machine groups to fetch.</param>
public IEnumerable<IMachineGroup> GetForApi(GameLocation location)
{
string locationKey = this.Factory.GetLocationKey(location);
return this
.ActiveMachineGroups
.Concat(this.DisabledMachineGroups)
.Concat(this.GlobalMachineGroups.SelectMany(group => group.GetAll()))
.Where(p => p.LocationKey == locationKey);
}
/// <summary>Get the registered override settings.</summary>
public IDictionary<string, ModConfigMachine> GetMachineOverrides()
{
ModConfig config = this.Config();
Dictionary<string, ModConfigMachine> overrides = new(this.Data.DefaultMachineOverrides, StringComparer.OrdinalIgnoreCase);
foreach ((string id, ModConfigMachine machineConfig) in config.MachineOverrides)
overrides[id] = machineConfig;
return overrides;
}
/// <summary>Get the settings for a machine.</summary>
/// <param name="id">The unique machine ID.</param>
public ModConfigMachine? GetMachineOverride(string id)
{
return this.Config().MachineOverrides.TryGetValue(id, out ModConfigMachine? config) || this.Data.DefaultMachineOverrides.TryGetValue(id, out config)
? config
: null;
}
/****
** Machine state
****/
/// <summary>Get the machine state for a location, if any.</summary>
/// <param name="location">The location to check.</param>
public MachineDataForLocation? GetMachineDataFor(GameLocation location)
{
string locationKey = this.Factory.GetLocationKey(location);
return this.MachineData.TryGetValue(locationKey, out MachineDataForLocation? data)
? data
: null;
}
/****
** State management
****/
/// <summary>Clear all registered machines.</summary>
public void Clear()
{
this.MachineData.Clear();
this.ActiveMachineGroups = [];
this.DisabledMachineGroups = [];
this.GlobalMachineGroups.Clear();
}
/// <summary>Clear all registered machines and add all locations to the reload queue.</summary>
public void Reset()
{
this.Clear();
foreach (GlobalMachineGroup group in this.GlobalMachineGroups)
group.Rebuild();
this.ReloadQueue.AddRange(CommonHelper.GetLocations());
}
/// <summary>Queue locations to remove and whose machines should be reloaded when <see cref="ReloadQueuedLocations"/> is called.</summary>
/// <param name="locations">The locations to remove.</param>
public void QueueRemove(IEnumerable<GameLocation> locations)
{
this.RemoveQueue.AddRange(locations);
}
/// <summary>Queue a location for which to reload machines when <see cref="ReloadQueuedLocations"/> is called.</summary>
/// <param name="location">The location to reload.</param>
public void QueueReload(GameLocation location)
{
this.ReloadQueue.Add(location);
}
/// <summary>Get whether a reload is already queued for a location.</summary>
/// <param name="location">The location to reload.</param>
public bool IsReloadQueued(GameLocation location)
{
return this.ReloadQueue.Contains(location);
}
/// <summary>Queue locations for which to reload machines when <see cref="ReloadQueuedLocations"/> is called.</summary>
/// <param name="locations">The locations to reload.</param>
public void QueueReload(IEnumerable<GameLocation> locations)
{
this.ReloadQueue.AddRange(locations);
}
/// <summary>Reload any locations queued for reload.</summary>
/// <returns>Returns whether any locations were reloaded.</returns>
public bool ReloadQueuedLocations()
{
if (this.ReloadQueue.Any() || this.RemoveQueue.Any())
{
this.ReloadMachinesIn(this.ReloadQueue, this.RemoveQueue);
this.ReloadQueue.Clear();
this.RemoveQueue.Clear();
return true;
}
return false;
}
/*********
** Private methods
*********/
/// <summary>Build a storage manager for the given containers.</summary>
/// <param name="containers">The storage containers.</param>
private StorageManager BuildStorage(IContainer[] containers)
{
return new StorageManager(containers);
}
/// <summary>Reload the machines in a given location.</summary>
/// <param name="locations">The locations whose machines to reload.</param>
/// <param name="removedLocations">The locations which have been removed, and whose machines should be reloaded if they still exist.</param>
private void ReloadMachinesIn(ISet<GameLocation> locations, ISet<GameLocation> removedLocations)
{
List<IMachineGroup> globalAdded = [];
HashSet<IMachineGroup> globalChanged = [];
bool anyChanged = false;
// remove old groups
{
HashSet<string> locationKeys = [.. locations.Concat(removedLocations).Select(this.Factory.GetLocationKey)];
if (this.Monitor.IsVerbose)
this.Monitor.Log($"Reloading machines in {locationKeys.Count} locations: {string.Join(", ", locationKeys)}...");
foreach (string locationKey in locationKeys)
anyChanged |= this.MachineData.Remove(locationKey);
foreach (GlobalMachineGroup globalGroup in this.GlobalMachineGroups)
{
if (globalGroup.RemoveLocations(locationKeys))
{
anyChanged = true;
globalChanged.Add(globalGroup);
}
}
}
// add new groups
foreach (GameLocation location in locations)
{
string locationKey = this.Factory.GetLocationKey(location);
// collect new groups
List<IMachineGroup> active = [];
List<IMachineGroup> disabled = [];
foreach (IMachineGroup group in this.Factory.GetMachineGroups(location, this.Monitor))
{
if (!group.HasInternalAutomation)
disabled.Add(group);
else if (!group.IsGlobalGroup)
active.Add(group);
else
{
globalAdded.Add(group);
globalChanged.Add(group);
}
}
// add groups
this.MachineData[locationKey] = new MachineDataForLocation(locationKey, active, disabled);
// track change
anyChanged |= active.Any();
}
// rebuild caches
if (anyChanged)
{
List<IMachineGroup> active = [];
List<IMachineGroup> disabled = [];
foreach (MachineDataForLocation locationData in this.MachineData.Values)
{
active.AddRange(locationData.ActiveMachineGroups);
disabled.AddRange(locationData.DisabledMachineGroups);
}
this.ActiveMachineGroups = active.ToArray();
this.DisabledMachineGroups = disabled.ToArray();
}
if (!globalChanged.Any())
return;
// determine distinct groups
List<HashSet<string>> distinctGlobalGroups = [];
foreach (HashSet<string> groupKeys in globalChanged.Select(p => p.GlobalContainerKeys))
{
HashSet<string>? existing = distinctGlobalGroups.FirstOrDefault(p => p.Overlaps(groupKeys));
if (existing != null)
existing.UnionWith(groupKeys);
else
distinctGlobalGroups.Add(groupKeys);
}
foreach (HashSet<string> groupKeys in distinctGlobalGroups)
{
GlobalMachineGroup? selectedGroup = null;
int total = this.GlobalMachineGroups.Count;
for (int i = 0; i < total; i++)
{
GlobalMachineGroup globalGroup = this.GlobalMachineGroups[i];
if (!globalGroup.GlobalContainerKeys.Overlaps(groupKeys))
continue;
selectedGroup ??= globalGroup;
if (selectedGroup == globalGroup)
globalChanged.Add(selectedGroup);
else
{
selectedGroup.Add([.. globalGroup.GetAll()]);
this.GlobalMachineGroups.Remove(globalGroup);
total--;
}
}
// create new group
if (selectedGroup == null)
{
selectedGroup = new GlobalMachineGroup(this.Factory.SortMachines, this.BuildStorage, this.Monitor);
this.GlobalMachineGroups.Add(selectedGroup);
}
// add groups to selected
IList<IMachineGroup> groups = [.. globalAdded.Where(p => p.GlobalContainerKeys.Overlaps(groupKeys))];
if (groups.Any())
selectedGroup.Add(groups);
}
// rebuild groups
foreach (GlobalMachineGroup globalGroup in globalChanged.OfType<GlobalMachineGroup>())
{
globalGroup.Rebuild();
}
}
}