-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathFenceFinder.cs
65 lines (55 loc) · 1.93 KB
/
FenceFinder.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
using StardewModdingAPI;
using StardewValley;
using StardewValley.Locations;
using System.Collections.Generic;
using System.Linq;
namespace NoFenceDecay
{
/// <summary>Finds all fences in the game.</summary>
internal class FenceFinder
{
/*********
** Fields
*********/
/// <summary>Avoid infinite recursion for mods that mess up <see cref="StardewValley.Buildings.Building.indoors" />.</summary>
private readonly IList<GameLocation> searchedLocations = new List<GameLocation>();
/*********
** Public methods
*********/
/// <summary>Finds all fences in the game.</summary>
public IEnumerable<Fence> GetFences()
{
this.searchedLocations.Clear();
return !Context.IsWorldReady ? new Fence[0] : this.GetFences(Game1.locations);
}
/*********
** Private methods
*********/
/// <summary>Finds all fences in the given <see cref="GameLocation" />s.</summary>
private IEnumerable<Fence> GetFences(IEnumerable<GameLocation> locations)
{
return locations.SelectMany(this.GetFences);
}
/// <summary>Finds all fences in the given <see cref="GameLocation" />.</summary>
private IEnumerable<Fence> GetFences(GameLocation l)
{
if (l == null || this.searchedLocations.Contains(l))
{
yield break;
}
this.searchedLocations.Add(l);
foreach (Fence f in l.Objects.Values.OfType<Fence>())
{
yield return f;
}
if (!(l is BuildableGameLocation bLoc))
{
yield break;
}
foreach (Fence f in this.GetFences(bLoc.buildings.Where(item => item != null).Select(item => item.indoors.Value).Where(item => item != null)))
{
yield return f;
}
}
}
}