-
Notifications
You must be signed in to change notification settings - Fork 675
Expand file tree
/
Copy pathProgram.cs
More file actions
175 lines (156 loc) · 5.36 KB
/
Program.cs
File metadata and controls
175 lines (156 loc) · 5.36 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System;
using System.Linq;
#if NET6_0_OR_GREATER
using System.Runtime.Versioning;
#endif
using System.Threading;
using Dynamo.Applications;
using Dynamo.Models;
using Dynamo.Utilities;
using Dynamo.ViewModels;
using Dynamo.Wpf.ViewModels.Watch3D;
using static System.Windows.Threading.Dispatcher;
namespace DynamoWPFCLI
{
internal class Program
{
private static EventWaitHandle suspendEvent = new AutoResetEvent(false);
#if NET6_0_OR_GREATER
[SupportedOSPlatform("Windows")]
#endif
[STAThread]
internal static void Main(string[] args)
{
bool useConsole = true;
try
{
var cmdLineArgs = StartupUtils.CommandLineArguments.Parse(args);
useConsole = !cmdLineArgs.NoConsole;
var locale = StartupUtils.SetLocale(cmdLineArgs);
cmdLineArgs.SetDisableAnalytics();
if (cmdLineArgs.ServiceMode)
{
Console.WriteLine("Starting DynamoWPFCLI in service mode");
}
if (cmdLineArgs.KeepAlive)
{
var thread = new Thread(() => RunKeepAlive(cmdLineArgs))
{
Name = "DynamoModelKeepAlive"
};
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
if (!useConsole)
{
suspendEvent.WaitOne();
}
else
{
Console.WriteLine("Starting DynamoWPFCLI in keepalive mode");
Console.ReadLine();
}
ShutDown();
}
else
{
var viewModel = StartupDynamo(cmdLineArgs);
var runner = new CommandLineRunnerWPF(viewModel);
runner.Run(cmdLineArgs);
}
}
catch (Exception e)
{
try
{
DynamoModel.IsCrashing = true;
Dynamo.Logging.Analytics.TrackException(e, true);
}
catch
{
}
if (useConsole)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}
}
}
/// <summary>
/// Start Dynamo Model and ViewModel per cmdLineArgs parameters.
/// </summary>
/// <param name="cmdLineArgs"></param>
/// <returns></returns>
private static DynamoViewModel StartupDynamo(StartupUtils.CommandLineArguments cmdLineArgs)
{
var model = StartupUtils.MakeCLIModel(cmdLineArgs);
if (!string.IsNullOrEmpty(cmdLineArgs.CERLocation))
{
model.CERLocation = cmdLineArgs.CERLocation;
}
model.ShutdownCompleted += (m) => { ShutDown(); };
var viewModel = DynamoViewModel.Start(
new DynamoViewModel.StartConfiguration()
{
DynamoModel = model,
Watch3DViewModel = new DefaultWatch3DViewModel(null, new Watch3DViewModelStartupParams(model))
{
Active = false,
CanBeActivated = false
}
});
cmdLineArgs.ImportedPaths.ToList().ForEach(path =>
{
ImportAssembly(model, path);
});
return viewModel;
}
private static void RunKeepAlive(StartupUtils.CommandLineArguments cmdLineArgs)
{
try
{
StartupDynamo(cmdLineArgs);
if (!cmdLineArgs.NoConsole)
{
Console.WriteLine("-----------------------------------------");
Console.WriteLine("DynamoWPFCLI is running in keepalive mode");
Console.WriteLine("Press Enter to shutdown...");
}
Run();
}
catch
{
Console.WriteLine("Server is shutting down due to an error");
}
}
/// <summary>
/// Attempts to import an assembly as a node library from a given file path.
/// </summary>
/// <param name="model"></param>
/// <param name="path"></param>
private static void ImportAssembly(DynamoModel model, string path)
{
try
{
var filePath = new System.IO.FileInfo(path);
if (!filePath.Exists)
{
Console.WriteLine($"could not find requested import library at path{path}");
}
else
{
Console.WriteLine($"attempting to import assembly {path}");
var assembly = AssemblyHelper.LoadInALCFrom(path);
model.LoadNodeLibrary(assembly, true);
}
}
catch (Exception e)
{
Console.WriteLine($"exception while trying to load assembly {path}: {e}");
}
}
private static void ShutDown()
{
Environment.Exit(0);
}
}
}