-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComponentSearchXMLFile.cs
138 lines (124 loc) · 4.52 KB
/
ComponentSearchXMLFile.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Pic.Plugin;
using System.IO;
namespace Interface_Nicolas
{
class ComponentSearchXMLFile : IComponentSearchMethod
{
#region Constructor
private ComponentSearchXMLFile(string filePath)
{
// load from xml file
componentList lComponents = componentList.LoadFromFile(filePath);
_listComponent = lComponents.components.OrderBy(o => o.name).ToList();
}
#endregion
#region IComponentSearchMethod
public List<component> ComponentList => _listComponent;
public byte[] GetAssemblyBytesFromGuid(Guid guid)
{
component cp = _listComponent.Find(c => Guid.Parse(c.guid) == guid);
if (null == cp)
throw new Exception($"Failed to retrieve component with guid = {guid}");
string filePath = Path.Combine(LibraryPath, cp.fileName.Replace('-', '_'));
FileInfo file = new FileInfo(filePath);
// Preliminary check
// must exist
if (!file.Exists)
throw new PluginException(string.Format("File {0} does not exist. Cannot load Component.", filePath));
// must be a dll file
if (!file.Extension.Equals(".dll"))
throw new PluginException(string.Format("File {0} is not a dll file. Cannot load Component.", filePath));
return File.ReadAllBytes(filePath);
}
public bool GetBoolParameterDefaultValue(Guid guid, string name)
{
component cp = _listComponent.Find(c => Guid.Parse(c.guid) == guid);
parameter param = cp.paramDefaults.Find(p => p.name == name);
if (param is parameterBool paramBool)
return paramBool.value;
else
return false;
}
public double GetDoubleParameterDefaultValue(Guid guid, string name)
{
component cp = _listComponent.Find(c => Guid.Parse(c.guid) == guid);
parameter param = cp.paramDefaults.Find(p => p.name == name);
if (param is parameterDouble paramDouble)
return paramDouble.value;
else
return 0.0;
}
public int GetIntParameterDefaultValue(Guid guid, string name)
{
component cp = _listComponent.Find(c => Guid.Parse(c.guid) == guid);
parameter param = cp.paramDefaults.Find(p => p.name == name);
if (param is parameterInt paramInt)
return paramInt.value;
else
return 0;
}
public int GetMultiParameterDefaultValue(Guid guid, string name)
{
component cp = _listComponent.Find(c => Guid.Parse(c.guid) == guid);
parameter param = cp.paramDefaults.Find(p => p.name == name);
if (param is parameterInt paramInt)
return paramInt.value;
else
return 0;
}
#endregion
#region Specific methods
public Guid GetGuidFromName(string name)
{
try
{
component cp = _listComponent.Find(c => c.name == name);
return Guid.Parse(cp.guid);
}
catch (Exception /*ex*/)
{
return Guid.Empty;
}
}
public string GetComponentPathFromName(string name)
{
component cp = _listComponent.Find(c => c.name == name);
return Path.Combine(LibraryPath, cp.fileName.Replace('-', '_'));
}
public static ComponentSearchXMLFile Instance
{
get
{
if (null == _instance)
_instance = new ComponentSearchXMLFile(_xmlFilePath);
return _instance;
}
}
public static string XmlFilePath
{
set
{
_xmlFilePath = value;
if (!File.Exists(_xmlFilePath))
throw new FileNotFoundException(_xmlFilePath);
}
}
public static string LibraryPath
{
get { return Path.GetDirectoryName(_xmlFilePath); }
}
#endregion
#region Static data members
private static ComponentSearchXMLFile _instance;
private static string _xmlFilePath;
#endregion
#region Data members
private List<component> _listComponent;
#endregion
}
}