chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Xml.Linq;
using System.Xml.Serialization;
 
namespace OpenTap.Package
{
    internal class PackageFileSerializerPlugin : TapSerializerPlugin
    {
        List<ICustomPackageData> plugins = null;
        public PackageFileSerializerPlugin()
        {
            plugins = CustomPackageActionHelper.GetAllData();
        }
 
        public override bool Deserialize(XElement node, ITypeData t, Action<object> setter)
        {
            if (node.Name.LocalName == "File" && t.IsA(typeof(PackageFile)))
            {
                PackageFile packageFile = new PackageFile();
 
                foreach (XAttribute attr in node.Attributes())
                {
                    switch (attr.Name.LocalName)
                    {
                        case "Path":
                            packageFile.RelativeDestinationPath = attr.Value;
                            break;
                        case "SourcePath":
                            packageFile.SourcePath = attr.Value;
                            break;
                        case "LicenseRequired":
                            packageFile.LicenseRequired = attr.Value;
                            break;
                    }
                }
 
                foreach (XElement elm in node.Elements())
                {
                    if (elm.Name.LocalName == "Plugins")
                    {
                        Serializer.Deserialize(elm, o => packageFile.Plugins = o as List<PluginFile>, typeof(List<PluginFile>));
                        continue;
                    }
 
                    if (elm.Name.LocalName == "IgnoreDependency")
                    {
                        packageFile.IgnoredDependencies.Add(elm.Value);
                        continue;
                    }
 
                    var handlingPlugins = plugins.Where(s => s.GetType().GetDisplayAttribute().Name == elm.Name.LocalName).ToArray();
 
 
                    if (handlingPlugins.Length > 0)
                    {
                        if (handlingPlugins.Length > 1)
                            Log.Warning($"Detected multiple plugins able to handle XMl tag {elm.Name.LocalName}. Unexpected behavior may occur.");
 
                        ICustomPackageData p = handlingPlugins[0];
                        if(elm.HasAttributes || !elm.IsEmpty)
                            Serializer.Deserialize(elm, o => p = (ICustomPackageData)o, p.GetType());
                        packageFile.CustomData.Add(p);
                        continue;
                    }
 
                    packageFile.CustomData.Add(new MissingPackageData(elm));
                }
 
                setter.Invoke(packageFile);
                return true;
            }
 
            return false;
        }
 
        public override bool Serialize(XElement node, object obj, ITypeData expectedType)
        {
            if (expectedType.IsA(typeof(PackageFile)) == false)
            {
                return false;
            }
 
            foreach (IMemberData prop in expectedType.GetMembers().Where(s => !s.HasAttribute<XmlIgnoreAttribute>()))
            {
                object val = prop.GetValue(obj);
                string name = prop.Name;
                var defaultValueAttr = prop.GetAttribute<DefaultValueAttribute>();
                if (defaultValueAttr != null)
                {
                    if (Object.Equals(defaultValueAttr.Value, val))
                        continue;
                    if (defaultValueAttr.Value == null)
                    {
                        if (val is IEnumerable enu && enu.IsEnumerableEmpty()) // the value is an empty IEnumerable
                        {
                            continue; // We take an empty IEnumerable to be the same as null
                        }
                    }
                }
 
                if (name == "RelativeDestinationPath")
                {
                    name = "Path";
                }
 
                if (name == "DoObfuscate")
                {
                    name = "Obfuscate";
                }
 
                if (name == "Plugins")
                {
                    XElement plugins = new XElement("Plugins");
                    Serializer.Serialize(plugins, val, prop.TypeDescriptor);
                    node.Add(plugins);
                    continue;
                }
                if (name == "IgnoredDependencies")
                {
                    if (val is List<string> igDeps)
                    {
                        foreach (string igDep in igDeps)
                        {
                            node.Add(new XElement("IgnoreDependency") { Value = igDep });
                        }
                        continue;
                    }
                }
                if (name == "CustomData")
                {
                    if (val is List<ICustomPackageData> packageActions)
                    {
                        foreach (ICustomPackageData action in packageActions)
                        {
                            if (action is MissingPackageData)
                                continue;
 
                            XElement xAction = new XElement(action.GetType().GetDisplayAttribute().Name);
                            Serializer.Serialize(xAction, action, TypeData.GetTypeData(action));
                            node.Add(xAction);
                        }
                    }
                    continue;
                }
 
                node.SetAttributeValue(name, val);
            }
 
 
            return true;
        }
    }
}