Skip to content

Commit cfd34cb

Browse files
committed
Clean up InventoryLocation
1 parent d978f92 commit cfd34cb

File tree

1 file changed

+51
-10
lines changed

1 file changed

+51
-10
lines changed

src/MHServerEmu.Games/Entities/Inventories/InventoryLocation.cs

+51-10
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,53 @@
66

77
namespace MHServerEmu.Games.Entities.Inventories
88
{
9-
// NOTE/TODO: This would not be client-accurate, but we can potentially refactor this as a readonly struct for optimization.
9+
/// <summary>
10+
/// Represents the location of an <see cref="Entity"/> in an <see cref="Inventory"/>.
11+
/// </summary>
1012
public class InventoryLocation : IEquatable<InventoryLocation>
1113
{
14+
// NOTE/TODO: This would not be client-accurate, but maybe we can potentially refactor this as a readonly struct for optimization.
15+
1216
private static readonly Logger Logger = LogManager.CreateLogger();
1317

1418
public static readonly InventoryLocation Invalid = new();
1519

16-
public ulong ContainerId { get; private set; } = 0; // Entity id
20+
public ulong ContainerId { get; private set; } = Entity.InvalidId; // Entity id
1721
public InventoryPrototype InventoryPrototype { get; private set; } = null;
1822
public uint Slot { get; private set; } = Inventory.InvalidSlot;
1923

2024
public PrototypeId InventoryRef { get => InventoryPrototype != null ? InventoryPrototype.DataRef : PrototypeId.Invalid; }
2125
public InventoryCategory InventoryCategory { get => InventoryPrototype != null ? InventoryPrototype.Category : InventoryCategory.None; }
2226
public InventoryConvenienceLabel InventoryConvenienceLabel { get => InventoryPrototype != null ? InventoryPrototype.ConvenienceLabel : InventoryConvenienceLabel.None; }
2327

24-
public bool IsValid { get => ContainerId != 0 && InventoryRef != PrototypeId.Invalid && Slot != Inventory.InvalidSlot; }
28+
public bool IsValid { get => ContainerId != Entity.InvalidId && InventoryRef != PrototypeId.Invalid && Slot != Inventory.InvalidSlot; }
2529

30+
/// <summary>
31+
/// Constructs a default <see cref="InventoryLocation"/>.
32+
/// </summary>
2633
public InventoryLocation() { }
2734

35+
/// <summary>
36+
/// Constructs an <see cref="InventoryLocation"/> with the specified parameters.
37+
/// </summary>
2838
public InventoryLocation(ulong containerId, PrototypeId inventoryRef, uint slot)
2939
{
3040
ContainerId = containerId;
3141
InventoryPrototype = inventoryRef.As<InventoryPrototype>();
3242
Slot = slot;
3343
}
3444

45+
/// <summary>
46+
/// Constructs a copy of the provided <see cref="InventoryLocation"/>.
47+
/// </summary>
3548
public InventoryLocation(InventoryLocation other)
3649
{
37-
ContainerId = other.ContainerId;
38-
InventoryPrototype = other.InventoryPrototype;
39-
Slot = other.Slot;
50+
Set(other);
4051
}
4152

53+
/// <summary>
54+
/// Serializes the provided <see cref="InventoryLocation"/> to an <see cref="Archive"/>.
55+
/// </summary>
4256
public static bool SerializeTo(Archive archive, InventoryLocation invLoc)
4357
{
4458
if (archive.IsPacking == false) return Logger.WarnReturn(false, "SerializeTo(): archive.IsPacking == false");
@@ -56,6 +70,9 @@ public static bool SerializeTo(Archive archive, InventoryLocation invLoc)
5670
return success;
5771
}
5872

73+
/// <summary>
74+
/// Deserializes an <see cref="InventoryLocation"/> from the provided <see cref="Archive"/>.
75+
/// </summary>
5976
public static bool SerializeFrom(Archive archive, InventoryLocation invLoc)
6077
{
6178
if (archive.IsUnpacking == false) return Logger.WarnReturn(false, "SerializeFrom(): archive.IsUnpacking == false");
@@ -75,34 +92,58 @@ public static bool SerializeFrom(Archive archive, InventoryLocation invLoc)
7592
return success;
7693
}
7794

95+
/// <summary>
96+
/// Returns the <see cref="Inventory"/> this <see cref="InventoryLocation"/> refers to.
97+
/// Returns <see langword="null"/> if this location is invalid (i.e. entity is not in an inventory).
98+
/// </summary>
7899
public Inventory GetInventory()
79100
{
80-
// NYI
81-
Logger.Warn("GetInventory(): Not yet implemented!");
82-
return null;
101+
if (IsValid == false) return null;
102+
103+
Entity container = Game.Current.EntityManager.GetEntity<Entity>(ContainerId);
104+
if (container == null) return null;
105+
106+
return container.GetInventoryByRef(InventoryRef);
83107
}
84108

109+
/// <summary>
110+
/// Updates this <see cref="InventoryLocation"/> with the provided parameters.
111+
/// </summary>
85112
public void Set(ulong containerId, PrototypeId inventoryRef, uint slot)
86113
{
87114
ContainerId = containerId;
88115
InventoryPrototype = inventoryRef.As<InventoryPrototype>();
89116
Slot = slot;
90117
}
91118

119+
/// <summary>
120+
/// Copies all parameters from the provided <see cref="InventoryLocation"/>.
121+
/// </summary>
92122
public void Set(InventoryLocation other)
93123
{
94124
ContainerId = other.ContainerId;
95125
InventoryPrototype = other.InventoryPrototype;
96126
Slot = other.Slot;
97127
}
98128

99-
public void Clear() => Set(0, PrototypeId.Invalid, Inventory.InvalidSlot);
129+
/// <summary>
130+
/// Resets this <see cref="InventoryLocation"/> to default parameters.
131+
/// </summary>
132+
public void Clear()
133+
{
134+
Set(Entity.InvalidId, PrototypeId.Invalid, Inventory.InvalidSlot);
135+
}
100136

101137
public override string ToString()
102138
{
103139
return $"{nameof(ContainerId)}={ContainerId}, {nameof(InventoryRef)}={GameDatabase.GetPrototypeName(InventoryRef)}, {nameof(Slot)}={Slot}";
104140
}
105141

142+
public override int GetHashCode()
143+
{
144+
return HashCode.Combine(ContainerId, InventoryPrototype, Slot);
145+
}
146+
106147
public override bool Equals(object obj)
107148
{
108149
if (obj is not InventoryLocation other) return false;

0 commit comments

Comments
 (0)