Skip to content

Commit a65374b

Browse files
committed
Clean up CalligraphySerializer
1 parent 87a3fe6 commit a65374b

File tree

4 files changed

+14
-24
lines changed

4 files changed

+14
-24
lines changed

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

-6
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,5 @@ private static void CopyPrototypePropertyCollection()
133133
{
134134
// NYI
135135
}
136-
137-
private static bool IsMixin(Type type)
138-
{
139-
return type == typeof(LocomotorPrototype) || type == typeof(PopulationInfoPrototype) || type == typeof(ProductPrototype);
140-
}
141-
142136
}
143137
}

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ private static bool ParseEnum(FieldParserParams @params)
111111
/// </summary>
112112
private static bool ParseDataRef(FieldParserParams @params)
113113
{
114-
// Data refs can be StringId, AssetTypeId, CurveId, PrototypeId, LocaleStringId or ulong.
115-
// Eventually we will assign appropriate data ref types to all ulong fields.
114+
// Data refs can be StringId, AssetTypeId, CurveId, PrototypeId, or LocaleStringId.
116115
// C# enums are not picky when assigning values with reflection, so we can reuse the same code for all of them.
117116
var value = @params.Reader.ReadUInt64();
118117
@params.FieldInfo.SetValue(@params.OwnerPrototype, value);

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

+2-6
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ public override void Deserialize(Prototype prototype, PrototypeId dataRef, Strea
3131

3232
// Begin deserialization
3333
DoDeserialize(prototype, prototypeHeader, dataRef, prototypeName, reader);
34-
35-
//Logger.Debug("Done!");
3634
}
3735
}
3836

@@ -132,7 +130,7 @@ private static bool DeserializeFieldGroup(Prototype prototype, Blueprint bluepri
132130
// We use MixinAttribute and ListMixinAttribute to differentiate them from RHStructs.
133131

134132
// First we look for a non-list mixin field
135-
var mixinFieldInfo = classManager.GetMixinFieldInfo(classType, mixinType, typeof(MixinAttribute));
133+
var mixinFieldInfo = classManager.GetMixinFieldInfo(classType, mixinType, PrototypeFieldType.Mixin);
136134
if (mixinFieldInfo != null)
137135
{
138136
// Set owner prototype to the existing mixin instance or create a new instance if there isn't one
@@ -145,12 +143,11 @@ private static bool DeserializeFieldGroup(Prototype prototype, Blueprint bluepri
145143

146144
// Get the field info from our mixin
147145
fieldInfo = classManager.GetFieldInfo(mixinType, blueprintMemberInfo, false);
148-
//Logger.Debug($"Found field info for mixin {mixinType.Name}, field name {blueprintMemberInfo.Member.FieldName}");
149146
}
150147
else
151148
{
152149
// Look for a list mixin
153-
mixinFieldInfo = classManager.GetMixinFieldInfo(classType, mixinType, typeof(ListMixinAttribute));
150+
mixinFieldInfo = classManager.GetMixinFieldInfo(classType, mixinType, PrototypeFieldType.ListMixin);
154151
if (mixinFieldInfo != null)
155152
{
156153
PrototypeMixinList list = AcquireOwnedMixinList(prototype, mixinFieldInfo, false);
@@ -162,7 +159,6 @@ private static bool DeserializeFieldGroup(Prototype prototype, Blueprint bluepri
162159

163160
fieldOwnerPrototype = element;
164161
fieldInfo = classManager.GetFieldInfo(mixinType, blueprintMemberInfo, false);
165-
//Logger.Debug($"Found field info for list mixin {mixinType.Name}, field name {blueprintMemberInfo.Member.FieldName}");
166162
}
167163
else
168164
{

src/MHServerEmu/Games/GameData/PrototypeClassManager.cs

+11-10
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,9 @@ public void BindAssetTypesToEnums(AssetDirectory assetDirectory)
157157
/// </summary>
158158
public System.Reflection.PropertyInfo GetFieldInfo(Type prototypeClassType, BlueprintMemberInfo blueprintMemberInfo, bool getPropertyCollection)
159159
{
160+
// Return the C# property info the blueprint member is bound to if we are not looking for a property collection
160161
if (getPropertyCollection == false)
161-
{
162-
// Return the C# property info the blueprint member is bound to if we are not looking for a property collection
163162
return blueprintMemberInfo.Member.RuntimeClassFieldInfo;
164-
}
165163

166164
// TODO: look for a property collection field for this prototype
167165
return null;
@@ -170,29 +168,32 @@ public System.Reflection.PropertyInfo GetFieldInfo(Type prototypeClassType, Blue
170168
/// <summary>
171169
/// Returns a <see cref="System.Reflection.PropertyInfo"/> for a mixin field in a Calligraphy prototype.
172170
/// </summary>
173-
public System.Reflection.PropertyInfo GetMixinFieldInfo(Type ownerClassType, Type fieldClassType, Type mixinAttribute)
171+
public System.Reflection.PropertyInfo GetMixinFieldInfo(Type ownerClassType, Type fieldClassType, PrototypeFieldType fieldType)
174172
{
175-
// Make sure we have a valid attribute type
176-
if ((mixinAttribute == typeof(MixinAttribute) || mixinAttribute == typeof(ListMixinAttribute)) == false)
177-
throw new ArgumentException($"{mixinAttribute.Name} is not a mixin attribute.");
173+
// Make sure we have a valid field type enum value
174+
if ((fieldType == PrototypeFieldType.Mixin || fieldType == PrototypeFieldType.ListMixin) == false)
175+
throw new ArgumentException($"{fieldType} is not a mixin field type.");
178176

179177
// Search the entire class hierarchy for a mixin of the matching type
180178
while (ownerClassType != typeof(Prototype))
181179
{
182180
// We do what PrototypeFieldSet::GetMixinFieldInfo() does right here using reflection
183181
foreach (var property in ownerClassType.GetProperties())
184182
{
185-
if (mixinAttribute == typeof(MixinAttribute))
183+
if (fieldType == PrototypeFieldType.Mixin)
186184
{
187185
// For simple mixins we just return the property if it matches our field type and has the correct attribute
188186
if (property.PropertyType != fieldClassType) continue;
189-
if (property.IsDefined(mixinAttribute)) return property;
187+
if (property.IsDefined(typeof(MixinAttribute))) return property;
190188
}
191-
else if (mixinAttribute == typeof(ListMixinAttribute))
189+
else if (fieldType == PrototypeFieldType.ListMixin)
192190
{
193191
// For list mixins we look for a list that is compatible with our requested field type
194192
if (property.PropertyType != typeof(PrototypeMixinList)) continue;
195193

194+
// NOTE: While we check if the field type defined in the attribute matches our field class type argument exactly,
195+
// the client checks if the argument type is derived from the type defined in the field info.
196+
// This doesn't seem to cause any issues in 1.52, but may need to be changed if we run into issues with other versions.
196197
var attribute = property.GetCustomAttribute<ListMixinAttribute>();
197198
if (attribute.FieldType == fieldClassType)
198199
return property;

0 commit comments

Comments
 (0)