Skip to content

Commit fbadcaf

Browse files
Add ParseXml method to XBlockParser
Introduce ParseXml(XmlReader, Action<IEnumerable<IMapEntity>>) to deserialize a GameXBlock from an XmlReader and produce IMapEntity instances via the existing lookup/GetInstance logic. The method filters entities by includeEntities/mixin type, deduplicates unknown model error reports (invoking OnError for first occurrence), and invokes the provided callback with the resulting entities or an empty collection if deserialization fails. Also add a missing System.Xml using.
1 parent 1e62e02 commit fbadcaf

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

Maple2.File.Parser/MapXBlock/XBlockParser.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Reflection;
2+
using System.Xml;
23
using System.Xml.Serialization;
34
using Maple2.File.Flat;
45
using Maple2.File.IO;
@@ -171,4 +172,34 @@ private void SetValue(Type type, IMapEntity entity, string name, object value) {
171172

172173
field.SetValue(entity, value);
173174
}
175+
176+
public void ParseXml(XmlReader xmlReader, Action<IEnumerable<IMapEntity>> callback) {
177+
if (serializer.Deserialize(xmlReader) is GameXBlock block) {
178+
var unknownModels = new HashSet<string>();
179+
var entities = block.entitySet.entity
180+
.Where(entity => {
181+
try {
182+
Type mixinType = lookup.GetMixinType(entity.modelName);
183+
return includeEntities.Count == 0 || includeEntities.Any(keep => keep.IsAssignableFrom(mixinType));
184+
} catch {
185+
return false;
186+
}
187+
})
188+
.Select(entity => {
189+
try {
190+
return GetInstance(entity);
191+
} catch (UnknownModelTypeException ex) {
192+
if (unknownModels.Add(entity.modelName)) {
193+
OnError?.Invoke(ex.Message);
194+
}
195+
return null;
196+
}
197+
})
198+
.Where(entity => entity != null);
199+
200+
callback(entities);
201+
} else {
202+
callback(Array.Empty<IMapEntity>());
203+
}
204+
}
174205
}

0 commit comments

Comments
 (0)