-
Notifications
You must be signed in to change notification settings - Fork 670
Expand file tree
/
Copy pathViewExtensionLoader.cs
More file actions
138 lines (120 loc) · 4.58 KB
/
ViewExtensionLoader.cs
File metadata and controls
138 lines (120 loc) · 4.58 KB
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.IO;
using System.Xml;
using Dynamo.Logging;
using DynamoUtilities;
namespace Dynamo.Wpf.Extensions
{
public class ViewExtensionLoader : IViewExtensionLoader, ILogSource
{
internal IViewExtension Load(ViewExtensionDefinition viewExtension)
{
try
{
if (viewExtension.IsEnabled)
{
if (viewExtension.RequiresSignedEntryPoint)
{
CertificateVerification.CheckAssemblyForValidCertificate(viewExtension.AssemblyPath);
}
var assembly = Dynamo.Utilities.AssemblyHelper.LoadInALCFrom(viewExtension.AssemblyPath);
var result = assembly.CreateInstance(viewExtension.TypeName) as IViewExtension;
ExtensionLoading?.Invoke(result);
Analytics.TrackEvent(
Actions.Load,
Categories.ExtensionOperations, viewExtension.TypeName);
return result;
}
return null;
}
catch(Exception ex)
{
var name = viewExtension.TypeName == null ? "null" : viewExtension.TypeName;
Log("Could not create an instance of " + name);
Log(ex.Message);
Log(ex.StackTrace);
return null;
}
}
public IViewExtension Load(string extensionPath)
{
var document = new XmlDocument();
document.Load(extensionPath);
var topNode = document.GetElementsByTagName("ViewExtensionDefinition");
if (topNode.Count == 0)
{
Log("Malformed " + extensionPath + " file");
return null;
}
var definition = new ViewExtensionDefinition();
var path = Path.GetDirectoryName(extensionPath);
foreach (XmlNode item in topNode[0].ChildNodes)
{
if (item.Name == "AssemblyPath")
{
path = Path.Combine(path, item.InnerText);
definition.AssemblyPath = path;
}
else if (item.Name == "TypeName")
{
definition.TypeName = item.InnerText;
}
else if (item.Name == "IsEnabled")
{
// Because the default value for this property is true so we check if user specifically put it false
bool.TryParse(item.InnerText, out bool result);
definition.IsEnabled = result;
}
}
//Check if the view extension definition file was located in a directory which requires certificate validation.
foreach (var pathToVerifyCert in DirectoriesToVerifyCertificates)
{
if (extensionPath.Contains(pathToVerifyCert))
{
definition.RequiresSignedEntryPoint = true;
}
}
var extension = Load(definition);
return extension;
}
public IEnumerable<IViewExtension> LoadDirectory(string extensionsPath)
{
var result = new List<IViewExtension>();
if (Directory.Exists(extensionsPath))
{
var files = Directory.GetFiles(extensionsPath, "*_ViewExtensionDefinition.xml");
foreach (var file in files)
{
var extension = Load(file);
if (extension != null)
{
result.Add(extension);
}
}
}
return result;
}
public event Action<ILogMessage> MessageLogged;
private void Log(ILogMessage obj)
{
if (MessageLogged != null)
{
MessageLogged(obj);
}
}
private void Log(string msg)
{
Log(LogMessage.Info(msg));
}
/// <summary>
/// An event that is raised when an extension starts loading.
/// </summary>
public event Action<IViewExtension> ExtensionLoading;
/// <summary>
/// A list of root directories which require extensions to have a signed entry point
/// File path locations from package definition xml's are validated against this collection
/// </summary>
internal List<string> DirectoriesToVerifyCertificates = new List<string>();
}
}