-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathNodeConverter.cs
63 lines (54 loc) · 2.04 KB
/
NodeConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using ReClassNET.DataExchange.ReClass;
using ReClassNET.Logger;
using ReClassNET.Nodes;
using UnrealEngineClassesPlugin.Nodes;
namespace UnrealEngineClassesPlugin
{
public class NodeConverter : ICustomNodeSerializer
{
/// <summary>Name of the type used in the XML data.</summary>
private const string XmlTypePrefix = "UnrealEngineClasses.";
private static readonly Dictionary<string, Type> stringToTypeMap = new[]
{
typeof(FDateTimeNode),
typeof(FGuidNode),
typeof(FQWordNode),
typeof(TArrayNode),
typeof(TSharedPtrNode)
}.ToDictionary(t => XmlTypePrefix + t.Name, t => t);
private static readonly Dictionary<Type, string> typeToStringMap = stringToTypeMap.ToDictionary(kv => kv.Value, kv => kv.Key);
public bool CanHandleNode(BaseNode node) =>
node switch
{
FDateTimeNode _ => true,
FGuidNode _ => true,
FQWordNode _ => true,
FStringNode _ => true,
TArrayNode _ => true,
TSharedPtrNode _ => true,
_ => false
};
public bool CanHandleElement(XElement element) => element.Attribute(ReClassNetFile.XmlTypeAttribute)?.Value.StartsWith(XmlTypePrefix) == true;
public BaseNode CreateNodeFromElement(XElement element, BaseNode parent, IEnumerable<ClassNode> classes, ILogger logger, CreateNodeFromElementHandler createNodeFromElement)
{
if (!stringToTypeMap.TryGetValue(element.Attribute(ReClassNetFile.XmlTypeAttribute)?.Value ?? string.Empty, out var nodeType))
{
logger.Log(LogLevel.Error, $"Skipping node with unknown type: {element.Attribute(ReClassNetFile.XmlTypeAttribute)?.Value}");
logger.Log(LogLevel.Warning, element.ToString());
return null;
}
return BaseNode.CreateInstanceFromType(nodeType, false);
}
public XElement CreateElementFromNode(BaseNode node, ILogger logger, CreateElementFromNodeHandler defaultHandler)
{
return new XElement(
ReClassNetFile.XmlNodeElement,
new XAttribute(ReClassNetFile.XmlTypeAttribute, typeToStringMap[node.GetType()])
);
}
}
}