Skip to content

Commit

Permalink
Added Loading of External DLLs for custom nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexDavies8 committed Feb 9, 2022
1 parent a8605c2 commit c44bf09
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 30 deletions.
Binary file modified .vs/Dynamo/DesignTimeBuild/.dtbcache.v2
Binary file not shown.
6 changes: 6 additions & 0 deletions Dynamo/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<loadFromRemoteSources enabled="true"/>
</runtime>
</configuration>
2 changes: 1 addition & 1 deletion Dynamo/Controls/PropertyEditors/SavePathPropertyEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public string Value
DependencyProperty.Register(
"Value",
typeof(string),
typeof(OpenPathPropertyEditor),
typeof(SavePathPropertyEditor),
new PropertyMetadata(
""),
new ValidateValueCallback(IsValid));
Expand Down
92 changes: 63 additions & 29 deletions Dynamo/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public static bool AutoExecute
}

private string _projectPath;
private List<Assembly> _loadedAssemblies = new List<Assembly>();

public MainWindow()
{
Expand Down Expand Up @@ -112,6 +113,8 @@ private void Window_Loaded(object sender, RoutedEventArgs e)
};

startupWindow.Show();

_loadedAssemblies.Add(Assembly.GetExecutingAssembly());
}

private void BuidFileContextMenu()
Expand Down Expand Up @@ -159,6 +162,12 @@ private void BuildEditContextMenu()
saveItem.Click += (sender, e) => NodeGraphManager.DeselectAllNodes(FlowchartViewModel.Model);
root.Items.Add(saveItem);

root.Items.Add(new Separator());

MenuItem loadAssemblyItem = new MenuItem() { Header = "Load External Nodes" };
loadAssemblyItem.Click += (sender, e) => OpenAssembly();
root.Items.Add(loadAssemblyItem);

EditMenu.Items.Add(root);
}

Expand All @@ -176,42 +185,45 @@ private MenuItem GetAddNodeMenuItem(BuildContextMenuArgs args)
Flowchart flowchart = args.Model as Flowchart;

MenuItem addNodeItem = new MenuItem() { Header = "Add Node" };
Assembly assembly = Assembly.GetExecutingAssembly();
PathTreeItem root = new PathTreeItem("Add Node", null);
foreach (Type type in assembly.GetTypes())
foreach (Assembly assembly in _loadedAssemblies)
{
var nodeAttribute = type.GetCustomAttribute<NodeAttribute>();
if (nodeAttribute != null)
foreach (Type type in assembly.GetTypes())
{
PathTreeItem curr = root;
string[] pathArgs = nodeAttribute.Path.Split('/', StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < pathArgs.Length - 1; i++)
var nodeAttribute = type.GetCustomAttribute<NodeAttribute>();
if (nodeAttribute != null)
{
PathTreeItem branch = new PathTreeItem(pathArgs[i], null);
if (!curr.Children.ContainsKey(pathArgs[i]))
PathTreeItem curr = root;
string[] pathArgs = nodeAttribute.Path.Split('/', StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < pathArgs.Length - 1; i++)
{
curr.Children.Add(pathArgs[i], branch);
curr = branch;
PathTreeItem branch = new PathTreeItem(pathArgs[i], null);
if (!curr.Children.ContainsKey(pathArgs[i]))
{
curr.Children.Add(pathArgs[i], branch);
curr = branch;
}
else
curr = curr.Children[pathArgs[i]];
}
else
curr = curr.Children[pathArgs[i]];
}
curr.Children.Add(
pathArgs[^1],
new PathTreeItem(pathArgs[^1],
() => {
Node node = NodeGraphManager.CreateNode(
pathArgs[^1],
Guid.NewGuid(),
flowchart,
type,
args.ModelSpaceMousePosition.X,
args.ModelSpaceMousePosition.Y
);
if (node is ExecutableNode executableNode)
ExecutionManager.MarkDirty(executableNode);
curr.Children.Add(
pathArgs[^1],
new PathTreeItem(pathArgs[^1],
() =>
{
Node node = NodeGraphManager.CreateNode(
pathArgs[^1],
Guid.NewGuid(),
flowchart,
type,
args.ModelSpaceMousePosition.X,
args.ModelSpaceMousePosition.Y
);
if (node is ExecutableNode executableNode)
ExecutionManager.MarkDirty(executableNode);
}, nodeAttribute.Order)
);
);
}
}
}

Expand Down Expand Up @@ -322,6 +334,28 @@ private void OpenFile()
}
}

private void OpenAssembly()
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Class Assembly (*.dll)|*.dll|All files (*.*)|*.*";
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == true)
{
var path = openFileDialog.FileName;
if (System.IO.File.Exists(path))
{
try
{
_loadedAssemblies.Add(Assembly.LoadFrom(path));
}
catch (Exception ex)
{
// TODO: Error handling
}
}
}
}

private void NewFile()
{
bool temp = ExecutionManager.AutoExecute;
Expand Down
Binary file added Examples/ExternalNodeExample.dll
Binary file not shown.

0 comments on commit c44bf09

Please sign in to comment.