-
Notifications
You must be signed in to change notification settings - Fork 7
/
LocationDescriptor.cs
67 lines (54 loc) · 1.99 KB
/
LocationDescriptor.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
using Shockah.Kokoro;
using Shockah.Kokoro.Stardew;
using StardewValley;
using System;
using System.Linq;
namespace Shockah.MachineStatus;
internal readonly struct LocationDescriptor : IEquatable<LocationDescriptor>
{
public readonly string Name { get; }
public readonly string TypeName { get; }
public readonly string MapPath { get; }
public LocationDescriptor(string name, string typeName, string mapPath)
{
this.Name = name;
this.TypeName = typeName;
this.MapPath = mapPath;
}
private static string GetNameForLocation(GameLocation location)
{
var selfName = location.NameOrUniqueName ?? "";
var rootName = location.Root?.Value?.NameOrUniqueName ?? "";
if (selfName == rootName)
return selfName;
else if (selfName == "" && rootName == "")
return "";
else if (selfName == "" && rootName != "")
return $"<unknown> @ {rootName}";
else if (selfName != "" && rootName == "")
return selfName;
else
return $"{selfName} @ {rootName}";
}
public static LocationDescriptor Create(GameLocation location)
=> new(GetNameForLocation(location), location.GetType().GetBestName(), location.mapPath.Value ?? "");
public bool Matches(GameLocation location)
=> Name == GetNameForLocation(location) && TypeName == location.GetType().GetBestName() && MapPath == (location.mapPath.Value ?? "");
public GameLocation? Retrieve()
{
var self = this;
return GameExt.GetAllLocations().FirstOrDefault(l => self.Matches(l));
}
public override string ToString()
=> Name;
public bool Equals(LocationDescriptor other)
=> Name == other.Name && TypeName == other.TypeName && MapPath == other.MapPath;
public override bool Equals(object? obj)
=> obj is LocationDescriptor descriptor && Equals(descriptor);
public override int GetHashCode()
=> (Name, TypeName, MapPath).GetHashCode();
public static bool operator ==(LocationDescriptor lhs, LocationDescriptor rhs)
=> lhs.Equals(rhs);
public static bool operator !=(LocationDescriptor lhs, LocationDescriptor rhs)
=> !lhs.Equals(rhs);
}