-
-
Notifications
You must be signed in to change notification settings - Fork 747
Expand file tree
/
Copy pathStartupManager.cs
More file actions
196 lines (162 loc) · 7.48 KB
/
StartupManager.cs
File metadata and controls
196 lines (162 loc) · 7.48 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
namespace ElectronNET.Runtime
{
using System;
using System.Collections.Immutable;
using System.Globalization;
using System.Linq;
using System.Reflection;
using ElectronNET.Runtime.Controllers;
using ElectronNET.Runtime.Data;
using ElectronNET.Runtime.Helpers;
internal class StartupManager
{
public void Initialize()
{
try
{
ElectronNetRuntime.BuildInfo = this.GatherBuildInfo();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
this.CollectProcessData();
this.SetElectronExecutable();
ElectronNetRuntime.StartupMethod = this.DetectAppTypeAndStartup();
Console.WriteLine((string)("Evaluated StartupMethod: " + ElectronNetRuntime.StartupMethod));
if (ElectronNetRuntime.DotnetAppType != DotnetAppType.AspNetCoreApp)
{
ElectronNetRuntime.RuntimeControllerCore = this.CreateRuntimeController();
}
}
private RuntimeControllerBase CreateRuntimeController()
{
switch (ElectronNetRuntime.StartupMethod)
{
case StartupMethod.PackagedDotnetFirst:
case StartupMethod.UnpackedDotnetFirst:
return new RuntimeControllerDotNetFirst();
case StartupMethod.PackagedElectronFirst:
case StartupMethod.UnpackedElectronFirst:
return new RuntimeControllerElectronFirst();
default:
throw new ArgumentOutOfRangeException();
}
}
private StartupMethod DetectAppTypeAndStartup()
{
var isLaunchedByDotNet = LaunchOrderDetector.CheckIsLaunchedByDotNet();
var isUnPackaged = UnpackagedDetector.CheckIsUnpackaged();
if (isLaunchedByDotNet)
{
if (isUnPackaged)
{
return StartupMethod.UnpackedDotnetFirst;
}
return StartupMethod.PackagedDotnetFirst;
}
else
{
if (isUnPackaged)
{
return StartupMethod.UnpackedElectronFirst;
}
return StartupMethod.PackagedElectronFirst;
}
}
private void CollectProcessData()
{
var argsList = Environment.GetCommandLineArgs().ToImmutableList();
ElectronNetRuntime.ProcessArguments = argsList;
var portArg = argsList.FirstOrDefault(e => e.Contains(ElectronNetRuntime.ElectronPortArgumentName, StringComparison.OrdinalIgnoreCase));
if (portArg != null)
{
var parts = portArg.Split('=', StringSplitOptions.TrimEntries);
if (parts.Length > 1 && int.TryParse(parts[1], NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out var result))
{
ElectronNetRuntime.ElectronSocketPort = result;
Console.WriteLine("Use Electron Port: " + result);
}
}
var pidArg = argsList.FirstOrDefault(e => e.Contains(ElectronNetRuntime.ElectronPidArgumentName, StringComparison.OrdinalIgnoreCase));
if (pidArg != null)
{
var parts = pidArg.Split('=', StringSplitOptions.TrimEntries);
if (parts.Length > 1 && int.TryParse(parts[1], NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out var result))
{
ElectronNetRuntime.ElectronProcessId = result;
Console.WriteLine("Electron Process ID: " + result);
}
}
var authTokenArg = argsList.FirstOrDefault(e => e.Contains(ElectronNetRuntime.ElectronAuthTokenArgumentName, StringComparison.OrdinalIgnoreCase));
if (authTokenArg != null)
{
var parts = authTokenArg.Split('=', StringSplitOptions.TrimEntries);
if (parts.Length > 1 && !string.IsNullOrWhiteSpace(parts[1]))
{
var result = parts[1];
ElectronNetRuntime.ElectronAuthToken = result;
Console.WriteLine("Use Auth Token: " + result);
}
}
}
private void SetElectronExecutable()
{
string executable = ElectronNetRuntime.BuildInfo.ElectronExecutable;
if (string.IsNullOrEmpty(executable))
{
throw new Exception("AssemblyMetadataAttribute 'ElectronExecutable' could not be found!");
}
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
executable += ".exe";
}
ElectronNetRuntime.ElectronExecutable = executable;
}
private BuildInfo GatherBuildInfo()
{
var buildInfo = new BuildInfo();
var electronAssembly = Assembly.GetEntryAssembly();
if (electronAssembly == null)
{
Console.WriteLine("GatherBuildInfo: Early exit");
return buildInfo;
}
if (electronAssembly.GetName().Name == "testhost" || electronAssembly.GetName().Name == "ReSharperTestRunner")
{
Console.WriteLine("GatherBuildInfo: Detected testhost");
electronAssembly = AppDomain.CurrentDomain.GetData("ElectronTestAssembly") as Assembly ?? electronAssembly;
}
else
{
Console.WriteLine("GatherBuildInfo: No testhost detected: " + electronAssembly.GetName().Name);
}
var attributes = electronAssembly.GetCustomAttributes<AssemblyMetadataAttribute>().ToList();
if (attributes.Count > 0)
{
buildInfo.ElectronExecutable = attributes.FirstOrDefault(e => e.Key == nameof(buildInfo.ElectronExecutable))?.Value;
buildInfo.ElectronVersion = attributes.FirstOrDefault(e => e.Key == nameof(buildInfo.ElectronVersion))?.Value;
buildInfo.RuntimeIdentifier = attributes.FirstOrDefault(e => e.Key == nameof(buildInfo.RuntimeIdentifier))?.Value;
buildInfo.Title = attributes.FirstOrDefault(e => e.Key == nameof(buildInfo.Title))?.Value;
buildInfo.Version = attributes.FirstOrDefault(e => e.Key == nameof(buildInfo.Version))?.Value;
buildInfo.BuildConfiguration = attributes.FirstOrDefault(e => e.Key == nameof(buildInfo.BuildConfiguration))?.Value;
var isAspNet = attributes.FirstOrDefault(e => e.Key == "IsAspNet")?.Value;
var isSingleInstance = attributes.FirstOrDefault(e => e.Key == nameof(buildInfo.ElectronSingleInstance))?.Value;
var httpPort = attributes.FirstOrDefault(e => e.Key == "AspNetHttpPort")?.Value;
if (isAspNet?.Length > 0 && bool.TryParse(isAspNet, out var isAspNetActive) && isAspNetActive)
{
ElectronNetRuntime.DotnetAppType = DotnetAppType.AspNetCoreApp;
}
if (bool.TryParse(isSingleInstance, out var parsedBool))
{
buildInfo.ElectronSingleInstance = parsedBool;
}
if (httpPort?.Length > 0 && int.TryParse(httpPort, out var port))
{
ElectronNetRuntime.AspNetWebPort = port;
}
}
return buildInfo;
}
}
}