alielie
2026-09-10 9ec80cafc23f5ee3278cacce3c9f86f4087b435a
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
using AddInPlugin.Entity;
using OpenTap;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using UtilLib;
 
namespace OpenTapEditor.Util
{
    public class StepTypeLoader
    {
        public static List<ITypeData> Steps { get; private set; }
        public static ObservableCollection<AdapterData> CustomSteps { get; private set; }
 
        static StepTypeLoader()
        {
            LoadStep();
        }
 
        public static void LoadStep()
        {
            LoadAllStep(null);
            LoadAdapterStep();
        }
 
        public static void LoadAllStep(TypeData parentType)
        {
            TypeData AsTypeData(ITypeData typedata)
            {
                do
                {
                    if (typedata is TypeData td)
                        return td;
                    typedata = typedata?.BaseType;
                } while (typedata != null);
                return null;
            }
 
            var types = TypeData.GetDerivedTypes(TypeData.FromType(typeof(ITestStep)))
               .Where(x => x.CanCreateInstance)
               .Where(x => x.GetAttribute<BrowsableAttribute>()?.Browsable ?? true)
               .Where(x => parentType == null ? true : TestStepList.AllowChild(AsTypeData(parentType).Type, AsTypeData(x)?.Type))
               .OrderBy(x => x.GetAttribute<DisplayAttribute>()?.Order);
 
            Steps = types.ToList();
        }
 
        public static void LoadAdapterStep()
        {
            var datas = new List<AdapterData>();
            try
            {
                if (Directory.Exists(PathHelper.AdapterFolder))
                {
                    string[] files = Directory.GetFiles(PathHelper.AdapterFolder, "*.data");
                    foreach (var file in files)
                    {
                        try
                        {
                            datas.AddRange(XmlHelper.DeserializeFromXml<List<AdapterData>>(file));
                        }
                        catch (Exception ex)
                        {
 
                        }
                    }
                }
 
            }
            catch (Exception ex)
            {
 
            }
            CustomSteps = new ObservableCollection<AdapterData>(datas.OrderBy(d =>
            {
                if (d.GroupBy == AdapterData.DEFAULT_GROUP)
                {
                    return 0;
                }
                return 1;
            }));
        }
 
        public static void SaveAdapterStep()
        {
            Dictionary<string, List<AdapterData>> dict = CustomSteps
                .GroupBy(p => p.GroupBy)
                .ToDictionary(g => g.Key, g => g.ToList());
            List<string> allFiles = new List<string>();
            foreach (var key in dict.Keys)
            {
                try
                {
                    string filePath = Path.Combine(PathHelper.AdapterFolder, $"{key}.data");
                    XmlHelper.SerializeToXml(filePath, dict[key]);
                    allFiles.Add(filePath);
                }
                catch (Exception ex)
                {
 
                }
            }
 
            foreach (var file in Directory.GetFiles(PathHelper.AdapterFolder, "*.data"))
            {
                if (!allFiles.Contains(file))
                {
                    File.Delete(file);
                }
            }
        }
    }
}