-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
268 lines (250 loc) · 9.86 KB
/
Program.cs
File metadata and controls
268 lines (250 loc) · 9.86 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
using System;
using System.Diagnostics;
using System.IO;
using System.Xml;
using System.Xml.XPath;
namespace InvisiLauncher
{
public static class Program
{
private const string configName = "InvisiLauncher.config";
private static readonly string ownPath = AppContext.BaseDirectory;
private static readonly string configFilePath = Path.Join(ownPath, configName);
private static int RC;
private static string[]? Args;
[STAThread]
public static int Main(string[] args)
{
RC = 0;
Args = args;
if (!File.Exists(configFilePath))
{
RC = -1;
Debug.WriteLine(
string.Format(
"Config file NOT found at {0}, returncode = {1}",
configFilePath,
RC.ToString()
)
);
}
else
{
Debug.WriteLine(
string.Format(
"Config file found at {0}",
configFilePath
)
);
if (args.Length == 0)
{
Debug.WriteLine(
string.Format(
"args[] is null or empty! We need arguments in commandline, by default I will try to launch whatever is set in {0} file.",
configName
)
);
}
else
{
if (args.Length == 1)
{
Debug.WriteLine(
string.Format(
"args[] contains 1 element. I expect it to be a filepath which will be used to build arguments string from template stored in configuration at {0}.",
configFilePath
)
);
}
if (args.Length > 1) //we expect only path to PS1 file, but accept everything else behind it
{
Debug.WriteLine(
string.Format(
"args[] contains more than 1 element, {0} in total",
args.Length
)
);
}
}
RC = RunProcess(configFilePath, args.Length);
}
return RC;
}
public static int RunProcess(string _configFilePath, int _iArg)
{
try
{
Process process = new Process
{
StartInfo = GetProcessStartInfoFromConfigFile(_configFilePath, _iArg)
};
Debug.WriteLine(
string.Format(
"Launching new {0} process with these args: {1}",
process.StartInfo.FileName,
process.StartInfo.Arguments
)
);
process.Start();
process.WaitForExit();
return process.ExitCode;
}
catch (Exception ex)
{
RC = -3;
Debug.WriteLine(ex.Message);
return RC;
}
}
/// <summary>
/// Path shenaningans, Iwant full path to PS1 file
/// </summary>
/// <param name="arg">it can be relative path (relative to launcher.exe), it will be converted to full path if necessary</param>
/// <returns></returns>
public static string FixArg1()
{
string arg = Args[0];
bool isFullPath = Path.IsPathRooted(arg) && !Path.GetPathRoot(arg).Equals(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal);
if (!isFullPath)
{
Debug.WriteLine(string.Format("Expanding {0} to full path", arg));
string new_arg = Path.GetFullPath(Path.Combine(ownPath, arg));
Debug.WriteLine(string.Format("{0} was converted to {1}", arg, new_arg));
Args[0] = new_arg;
return Args[0];
}
else
{
return Args[0];
}
}
public static ProcessStartInfo GetProcessStartInfoFromConfigFile(string _configfilepath, int _iArg)
{
string default_filename = @"%Windir%\System32\WINDOWSPOWERSHELL\v1.0\powershell.exe";
string default_arguments = @"-ExecutionPolicy Bypass -sta -noprofile -File {0}";
string custom_filename;
string custom_arguments;
string filename;
string arguments;
string final_filename;
string final_arguments;
using (var fileStream = File.Open(_configfilepath, FileMode.Open))
{
try
{
#region XML setup
XPathDocument xPath = new XPathDocument(fileStream);
XPathNavigator navigator = xPath.CreateNavigator();
XmlNamespaceManager nameSpace = new XmlNamespaceManager(navigator.NameTable);
nameSpace.AddNamespace("ns", "https://nonexistenthost.com/namespace");
XPathExpression query;
#endregion
#region default_filename
query = navigator.Compile("ns:configuration/ns:filename");
query.SetContext(nameSpace);
if (navigator.SelectSingleNode(query) != null)
{
custom_filename = navigator.SelectSingleNode(query).Value;
Debug.WriteLine(
string.Format(
"setting custom config value of {0} = {1}",
nameof(custom_filename),
custom_filename
)
);
filename = custom_filename;
}
else
{
Debug.WriteLine(
string.Format(
"returning to default value of {0} = {1}",
nameof(default_filename),
default_filename
)
);
filename = default_filename;
}
#endregion
#region default_arguments
query = navigator.Compile("ns:configuration/ns:args");
query.SetContext(nameSpace);
if (navigator.SelectSingleNode(query) != null)
{
custom_arguments = navigator.SelectSingleNode(query).Value;
Debug.WriteLine(
string.Format(
"setting custom config value of {0} = {1}",
nameof(custom_arguments),
custom_arguments
)
);
arguments = custom_arguments;
}
else
{
Debug.WriteLine(
string.Format(
"returning to default value of {0} = {1}",
nameof(default_arguments),
default_arguments
)
);
arguments = default_arguments;
}
#endregion
}
catch (Exception ex)
{
RC = - 2;
Debug.WriteLine(ex.Message);
throw;
}
}
final_filename = Environment.ExpandEnvironmentVariables(filename);
switch (_iArg)
{
case 0:
final_arguments = Environment.ExpandEnvironmentVariables(arguments);
break;
case 1:
FixArg1();
final_arguments = Environment.ExpandEnvironmentVariables(
string.Format(
arguments,
Args[0]
)
);
break;
default: //count >1
FixArg1();
#pragma warning disable CS8604 // Possible null reference argument.
final_arguments = Environment.ExpandEnvironmentVariables(
string.Format(
arguments,
string.Join(
" ",
string.Join(" ", Args)
)
)
);
#pragma warning restore CS8604 // Possible null reference argument.
break;
}
//string final_arguments = Environment.ExpandEnvironmentVariables(string.Format(default_arguments, string.Join(" ", _args)));
//string final_arguments = Environment.ExpandEnvironmentVariables(default_arguments);
Debug.WriteLine(string.Format("{0} Value = {1}", nameof(final_filename), final_filename));
Debug.WriteLine(string.Format("{0} Value = {1}", nameof(final_arguments), final_arguments));
ProcessStartInfo info = new ProcessStartInfo(final_filename,
final_arguments)
{
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = false,
WindowStyle = ProcessWindowStyle.Hidden
};
return info;
}
}
}