-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Program.cs
180 lines (156 loc) · 5.54 KB
/
Program.cs
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
using System;
using System.ComponentModel;
using static System.Console;
using static System.Environment;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using TE.LocalSystem;
using TE;
using static TE.SystemExitCodes;
namespace TE.Plex
{
/// <summary>
/// Class with program entry point.
/// </summary>
internal sealed class Program
{
/// <summary>
/// The parent process.
/// </summary>
private const int ATTACH_PARENT_PROCESS = -1;
/// <summary>
/// Attach to the console window.
/// </summary>
/// <param name="dwProcessId">
/// The ID of the process.
/// </param>
/// <returns>
/// True if successful, false if not successful.
/// </returns>
[DllImport("kernel32.dll")]
static extern bool AttachConsole(int dwProcessId);
/// <summary>
/// Program entry point.
/// </summary>
[STAThread]
private static int Main(string[] args)
{
// redirect console output to parent process;
// must be before any calls to Console.WriteLine()
AttachConsole(ATTACH_PARENT_PROCESS);
Arguments arguments = new Arguments(args);
bool isSilent = (arguments["silent"] != null);
string logPath = arguments["log"];
Log.SetFolder(logPath);
Log.Delete();
try
{
Log.Write("Getting windows user.");
WindowsUser user = new WindowsUser();
Log.Write("Checking if user is an administrator.");
// Check if the user running this application is an administrator
if (!user.IsAdministrator())
{
string message = "This application must be run from an administrative account.";
if (!isSilent)
{
// If the user is not an administrator, then exit
MessageBox.Show(
message,
"Plex Server Updater",
MessageBoxButtons.OK,
MessageBoxIcon.Stop);
}
Log.Write(message);
ExitCode = ERROR_ACCESS_DENIED;
return ERROR_ACCESS_DENIED;
}
}
catch (Exception ex)
{
if (!isSilent)
{
MessageBox.Show(
ex.Message,
"Plex Server Updater",
MessageBoxButtons.OK,
MessageBoxIcon.Stop);
}
Log.Write(ex);
Log.Write(ex.StackTrace);
ExitCode = 1;
return 1;
}
if (isSilent)
{
try
{
bool isForceUpdate = (arguments["force"] != null);
int waitTime = SilentUpdate.DefaultWaitTime;
if (arguments["wait"] != null)
{
if (!Int32.TryParse(arguments["wait"], out waitTime))
{
waitTime = SilentUpdate.DefaultWaitTime;
}
}
// Run the update silently
Log.Write("Initializing the silent update.");
SilentUpdate silentUpdate = new SilentUpdate(Log.Folder);
silentUpdate.ForceUpdate = isForceUpdate;
silentUpdate.WaitTime = waitTime;
silentUpdate.Run();
if (silentUpdate.IsPlexRunning())
{
ExitCode = ERROR_SUCCESS;
return ERROR_SUCCESS;
}
else
{
ExitCode = 1;
return 1;
}
}
catch (Exception ex)
{
Log.Write(ex.Message);
Log.Write(ex.StackTrace);
ExitCode = 1;
return 1;
}
}
else
{
try
{
// Display the main form
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Log.Write("Initializing the update window.");
MainForm mainForm = new MainForm();
// Check to see if the form is disposed becase there was an
// issue with initializing the form
if (!mainForm.IsDisposed)
{
Log.Write("Displaying the update window.");
Application.Run(mainForm);
}
ExitCode = ERROR_SUCCESS;
return ERROR_SUCCESS;
}
catch (Exception ex)
{
MessageBox.Show(
ex.Message,
"Plex Server Updater",
MessageBoxButtons.OK,
MessageBoxIcon.Stop);
Log.Write(ex);
Log.Write(ex.StackTrace);
ExitCode = 1;
return 1;
}
}
}
}
}