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 Steps { get; private set; } public static ObservableCollection 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()?.Browsable ?? true) .Where(x => parentType == null ? true : TestStepList.AllowChild(AsTypeData(parentType).Type, AsTypeData(x)?.Type)) .OrderBy(x => x.GetAttribute()?.Order); Steps = types.ToList(); } public static void LoadAdapterStep() { var datas = new List(); try { if (Directory.Exists(PathHelper.AdapterFolder)) { string[] files = Directory.GetFiles(PathHelper.AdapterFolder, "*.data"); foreach (var file in files) { try { datas.AddRange(XmlHelper.DeserializeFromXml>(file)); } catch (Exception ex) { } } } } catch (Exception ex) { } CustomSteps = new ObservableCollection(datas.OrderBy(d => { if (d.GroupBy == AdapterData.DEFAULT_GROUP) { return 0; } return 1; })); } public static void SaveAdapterStep() { Dictionary> dict = CustomSteps .GroupBy(p => p.GroupBy) .ToDictionary(g => g.Key, g => g.ToList()); List allFiles = new List(); 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); } } } } }