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
//            Copyright Keysight Technologies 2012-2019
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.
using System;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Collections;
using System.Linq;
 
namespace OpenTap.Plugins
{
    /// <summary> Serializer implementation for IConstResourceProperty items. </summary>
    internal class ConstResourceSerializer : TapSerializerPlugin
    {
        /// <summary> The order of this serializer. </summary>
        public override double Order { get; } = 3;
 
        /// <summary> Deserialization implementation. </summary>
        public override bool Deserialize( XElement element, ITypeData t, Action<object> setter)
        {
            if (t.DescendsTo(TypeData.FromType(typeof(IConstResourceProperty))))
            {
                if (element.IsEmpty) return true; // Dont do anything with a <port/>
                var name = element.Attribute("Name");
                if (name == null)
                    return false;
                Serializer.Deserialize(element.Element("Device"), obj =>
                 {
                     var resource = obj as IResource;
 
                     if (obj is ComponentSettings)
                     { // for legacy support. type argument was a component settings, not a device. In this case used index to get the resource.
                         obj = ComponentSettings.GetCurrent(obj.GetType());
                         var lst = (IList)obj;
                         var dev = element.Element("Device");
                         if(dev != null && int.TryParse(dev.Value, out int index) && index >= 0 && lst.Count > index)
                         {
                             
                             resource = ((IList)obj)[index] as IResource;
                         }
                     }
                     if(resource == null)
                        return;
                     
                     
                     foreach(var resProp in resource.GetConstProperties())
                     {
                         if(TypeData.GetTypeData(resProp).DescendsTo(t) && resProp.Name == name.Value)
                         {
                             setter(resProp);
                             return;
                         }
                     }
                 }, typeof(IResource));
                return true;
            }
            return false;
        }
 
        /// <summary> For avoiding recursive Serialize calls. </summary>
        readonly HashSet<object> checkRentry = new HashSet<object>();
        
        /// <summary> Serialization implementation. </summary>
        public override bool Serialize( XElement elem, object obj, ITypeData expectedType)
        {
            if (obj is IConstResourceProperty == false) return false;
            if (checkRentry.Add(obj) == false) return false;
            IConstResourceProperty port = (IConstResourceProperty) obj;
            
            try
            {
                {
                    // check if the currently serializing object is the class
                    // that owns the port. In that case, don't continue. 
                    var serializingParentObject = Serializer.SerializerStack
                        .OfType<ObjectSerializer>().FirstOrDefault()?.Object;
                    if (port.Device == serializingParentObject)
                        return false;
                }   
 
                elem.SetAttributeValue("Name", port.Name);
                IList settings = ComponentSettingsList.GetContainer(port.Device.GetType());
                if (port.Device != null && settings != null)
                {
 
                    XElement device = new XElement("Device");
                    device.SetAttributeValue("type", settings.GetType().Name);
                    if (Serializer.Serialize(device, port.Device, TypeData.FromType(port.GetType())))
                    {
                        elem.Add(device);
                    }
                }
                else
                    elem.Add(new XElement("Device"));
                return true;
            }
            finally
            {
                checkRentry.Remove(obj);
            }
        }
    }
}