6
6
7
7
namespace MHServerEmu . Games . Entities . Inventories
8
8
{
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>
10
12
public class InventoryLocation : IEquatable < InventoryLocation >
11
13
{
14
+ // NOTE/TODO: This would not be client-accurate, but maybe we can potentially refactor this as a readonly struct for optimization.
15
+
12
16
private static readonly Logger Logger = LogManager . CreateLogger ( ) ;
13
17
14
18
public static readonly InventoryLocation Invalid = new ( ) ;
15
19
16
- public ulong ContainerId { get ; private set ; } = 0 ; // Entity id
20
+ public ulong ContainerId { get ; private set ; } = Entity . InvalidId ; // Entity id
17
21
public InventoryPrototype InventoryPrototype { get ; private set ; } = null ;
18
22
public uint Slot { get ; private set ; } = Inventory . InvalidSlot ;
19
23
20
24
public PrototypeId InventoryRef { get => InventoryPrototype != null ? InventoryPrototype . DataRef : PrototypeId . Invalid ; }
21
25
public InventoryCategory InventoryCategory { get => InventoryPrototype != null ? InventoryPrototype . Category : InventoryCategory . None ; }
22
26
public InventoryConvenienceLabel InventoryConvenienceLabel { get => InventoryPrototype != null ? InventoryPrototype . ConvenienceLabel : InventoryConvenienceLabel . None ; }
23
27
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 ; }
25
29
30
+ /// <summary>
31
+ /// Constructs a default <see cref="InventoryLocation"/>.
32
+ /// </summary>
26
33
public InventoryLocation ( ) { }
27
34
35
+ /// <summary>
36
+ /// Constructs an <see cref="InventoryLocation"/> with the specified parameters.
37
+ /// </summary>
28
38
public InventoryLocation ( ulong containerId , PrototypeId inventoryRef , uint slot )
29
39
{
30
40
ContainerId = containerId ;
31
41
InventoryPrototype = inventoryRef . As < InventoryPrototype > ( ) ;
32
42
Slot = slot ;
33
43
}
34
44
45
+ /// <summary>
46
+ /// Constructs a copy of the provided <see cref="InventoryLocation"/>.
47
+ /// </summary>
35
48
public InventoryLocation ( InventoryLocation other )
36
49
{
37
- ContainerId = other . ContainerId ;
38
- InventoryPrototype = other . InventoryPrototype ;
39
- Slot = other . Slot ;
50
+ Set ( other ) ;
40
51
}
41
52
53
+ /// <summary>
54
+ /// Serializes the provided <see cref="InventoryLocation"/> to an <see cref="Archive"/>.
55
+ /// </summary>
42
56
public static bool SerializeTo ( Archive archive , InventoryLocation invLoc )
43
57
{
44
58
if ( archive . IsPacking == false ) return Logger . WarnReturn ( false , "SerializeTo(): archive.IsPacking == false" ) ;
@@ -56,6 +70,9 @@ public static bool SerializeTo(Archive archive, InventoryLocation invLoc)
56
70
return success ;
57
71
}
58
72
73
+ /// <summary>
74
+ /// Deserializes an <see cref="InventoryLocation"/> from the provided <see cref="Archive"/>.
75
+ /// </summary>
59
76
public static bool SerializeFrom ( Archive archive , InventoryLocation invLoc )
60
77
{
61
78
if ( archive . IsUnpacking == false ) return Logger . WarnReturn ( false , "SerializeFrom(): archive.IsUnpacking == false" ) ;
@@ -75,34 +92,58 @@ public static bool SerializeFrom(Archive archive, InventoryLocation invLoc)
75
92
return success ;
76
93
}
77
94
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>
78
99
public Inventory GetInventory ( )
79
100
{
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 ) ;
83
107
}
84
108
109
+ /// <summary>
110
+ /// Updates this <see cref="InventoryLocation"/> with the provided parameters.
111
+ /// </summary>
85
112
public void Set ( ulong containerId , PrototypeId inventoryRef , uint slot )
86
113
{
87
114
ContainerId = containerId ;
88
115
InventoryPrototype = inventoryRef . As < InventoryPrototype > ( ) ;
89
116
Slot = slot ;
90
117
}
91
118
119
+ /// <summary>
120
+ /// Copies all parameters from the provided <see cref="InventoryLocation"/>.
121
+ /// </summary>
92
122
public void Set ( InventoryLocation other )
93
123
{
94
124
ContainerId = other . ContainerId ;
95
125
InventoryPrototype = other . InventoryPrototype ;
96
126
Slot = other . Slot ;
97
127
}
98
128
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
+ }
100
136
101
137
public override string ToString ( )
102
138
{
103
139
return $ "{ nameof ( ContainerId ) } ={ ContainerId } , { nameof ( InventoryRef ) } ={ GameDatabase . GetPrototypeName ( InventoryRef ) } , { nameof ( Slot ) } ={ Slot } ";
104
140
}
105
141
142
+ public override int GetHashCode ( )
143
+ {
144
+ return HashCode . Combine ( ContainerId , InventoryPrototype , Slot ) ;
145
+ }
146
+
106
147
public override bool Equals ( object obj )
107
148
{
108
149
if ( obj is not InventoryLocation other ) return false ;
0 commit comments