Skip to content

Commit 3ac247c

Browse files
committed
Separate DeserializeFieldGroupIntoProperty from DeserializePropertyMixin
1 parent 8414283 commit 3ac247c

File tree

1 file changed

+45
-18
lines changed

1 file changed

+45
-18
lines changed

src/MHServerEmu/Games/GameData/Calligraphy/CalligraphySerializer.cs

+45-18
Original file line numberDiff line numberDiff line change
@@ -186,39 +186,66 @@ private static bool DeserializeFieldGroup(Prototype prototype, Blueprint bluepri
186186
/// Deserializes a property mixin field group of a Calligraphy prototype.
187187
/// </summary>
188188
private static bool DeserializePropertyMixin(Prototype prototype, Blueprint blueprint, Blueprint groupBlueprint, byte blueprintCopyNum,
189-
PrototypeId prototypeDataRef, string prototypeName, Type classType, BinaryReader reader)
189+
PrototypeId prototypeDataRef, string prototypeFilePath, Type classType, BinaryReader reader)
190190
{
191-
// TODO: do actual deserialization in DeserializeFieldGroupIntoProperty()
191+
PrototypePropertyCollection collection = null;
192+
193+
// Property mixins are used both for initializing property infos and filling prototype property collections
194+
// If this isn't a default prototype, it means the field group needs to be deserialized into a property collection
195+
if (prototypeDataRef != groupBlueprint.DefaultPrototypeId)
196+
{
197+
// Look for a property collection to deserialize into
198+
// TODO: check mixins for collections
199+
200+
// PropertyId fields are currently also being sent here, so they are not going to have a Properties field
201+
// Also the only PropertyList field (from ModPrototype) is currently being considered just a regular property collection
202+
var collectionFieldInfo = classType.GetProperty("Properties");
203+
if (collectionFieldInfo != null)
204+
{
205+
collection = (PrototypePropertyCollection)collectionFieldInfo.GetValue(prototype);
206+
207+
// Initialize a new collection in this field if there isn't one already
208+
if (collection == null)
209+
{
210+
collection = new();
211+
collectionFieldInfo.SetValue(prototype, collection);
212+
}
213+
}
214+
}
215+
216+
// This handles both cases (initialization and filling property collections)
217+
DeserializeFieldGroupIntoProperty(collection, groupBlueprint, blueprintCopyNum, prototypeFilePath, reader, "Property Fields");
218+
219+
// Property field groups do not have any list fields, so numListFields should always be 0
220+
short numListFields = reader.ReadInt16();
221+
if (numListFields != 0) Logger.Warn($"Property field group numListFields != 0");
222+
return true;
223+
}
224+
225+
private static bool DeserializeFieldGroupIntoProperty(PrototypePropertyCollection collection, Blueprint groupBlueprint, byte blueprintCopyNum,
226+
string prototypeFilePath, BinaryReader reader, string groupTag)
227+
{
228+
// TODO: deserializeFieldGroupIntoPropertyBuilder
192229
short numSimpleFields = reader.ReadInt16();
193230
for (int i = 0; i < numSimpleFields; i++)
194231
{
195232
var fieldId = (StringId)reader.ReadUInt64();
196233
var type = (CalligraphyBaseType)reader.ReadByte();
197234

198-
// Property mixins don't have any RHStructs, so we can always read the value as uint64
235+
// Property mixin field groups don't have any RHStructs, so we can always read the value as uint64
199236
// (also no types or localized string refs)
200237
var value = reader.ReadUInt64();
201238

202239
// hack: write data to a temporary PrototypePropertyCollection implementation
203-
if (classType == typeof(PropertyPrototype)) continue;
204-
var propertyCollectionFieldInfo = classType.GetProperty("Properties");
205-
var propertyCollection = (PrototypePropertyCollection)propertyCollectionFieldInfo.GetValue(prototype);
206-
207-
if (propertyCollection == null)
240+
if (collection != null)
208241
{
209-
propertyCollection = new();
210-
propertyCollectionFieldInfo.SetValue(prototype, propertyCollection);
211-
}
242+
if (groupBlueprint.TryGetBlueprintMemberInfo(fieldId, out var blueprintMemberInfo) == false)
243+
return Logger.ErrorReturn(false, $"Failed to find member id {fieldId} in blueprint {GameDatabase.GetBlueprintName(groupBlueprint.Id)}");
212244

213-
// Get blueprint member info for this field
214-
if (blueprint.TryGetBlueprintMemberInfo(fieldId, out var blueprintMemberInfo) == false)
215-
return Logger.ErrorReturn(false, $"Failed to find member id {fieldId} in blueprint {GameDatabase.GetBlueprintName(blueprint.Id)}");
216-
217-
propertyCollection.AddPropertyFieldValue(groupBlueprint.Id, blueprintCopyNum, blueprintMemberInfo.Member.FieldName, value);
245+
collection.AddPropertyFieldValue(groupBlueprint.Id, blueprintCopyNum, blueprintMemberInfo.Member.FieldName, value);
246+
}
218247
}
219248

220-
// Property field groups do not have any list fields, so numListFields should always be 0
221-
short numListFields = reader.ReadInt16();
222249
return true;
223250
}
224251

0 commit comments

Comments
 (0)