forked from calebhearth/steam-swtor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
169 lines (152 loc) · 4.85 KB
/
Copy pathProgram.cs
File metadata and controls
169 lines (152 loc) · 4.85 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
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Pipes;
using System.Management;
using System.Threading;
using System.Windows.Forms;
namespace SteamSWToR
{
class Program
{
/// <summary>
/// Check that we are running at least Windows 6 (Vista)
/// </summary>
/// <returns>True if we are on Windows Vista or higher.</returns>
static bool IsPreVista()
{
return System.Environment.OSVersion.Version.Major < 6;
}
static int Main(string[] args)
{
// If Operating System is before Vista, then exit
if (IsPreVista())
{
MessageBox.Show("Windows Vista or higher is required.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return 1;
}
if (args.Length == 0)
{
string pipeName = "swtorsteam";
// run ourself as admin
try
{
Process admin = new Process();
admin.StartInfo.FileName = System.Reflection.Assembly.GetEntryAssembly().Location;
admin.StartInfo.Arguments = pipeName;
admin.StartInfo.Verb = "runas";
admin.Start();
}
catch(Exception e)
{
string errmsg = e.Message + "\n";
errmsg += "Failed to escalate. Program will now exit.";
MessageBox.Show(errmsg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return 1;
}
// run the swtor launcher
try
{
Process launcher = new Process();
launcher.StartInfo.FileName = Directory.GetCurrentDirectory() + "\\launcher.exe";
launcher.Start();
}
catch(Exception e)
{
string errmsg = e.Message + "\n";
errmsg += "Launcher failed to begin. Is this exe in SWTOR's home directory? Program will now exit.";
MessageBox.Show(errmsg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return 1;
}
// grab data from the commandline arguments
string exe, arguments, workingdirectory;
try
{
NamedPipeServerStream server = new NamedPipeServerStream(pipeName);
server.WaitForConnection();
StreamReader sr = new StreamReader(server);
string cmdline = sr.ReadLine();
// grab data from the commandline arguments
exe = cmdline.Substring(1, cmdline.IndexOf('"', 1) - 1);
arguments = cmdline.Substring(cmdline.IndexOf("\" ") + 2);
workingdirectory = cmdline.Substring(1, cmdline.IndexOf("swtor.exe") - 1);
}
catch(Exception e)
{
string errmsg = e.Message + "\n";
errmsg += "Failed to read command line arguments from other program. Program will now exit.";
MessageBox.Show(errmsg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return 1;
}
try
{
// start swtor's client
Process swtor = new Process();
swtor.StartInfo.FileName = exe;
swtor.StartInfo.Arguments = arguments;
swtor.StartInfo.WorkingDirectory = workingdirectory;
swtor.Start();
}
catch(Exception e)
{
string errmsg = e.Message + "\n";
errmsg += "swtor.exe failed to begin. Is this exe in SWTOR's home directory? Program will now exit.";
MessageBox.Show(errmsg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return 1;
}
// exit the program
return 0;
}
else
{
// Connect to pipe server
string pipeName = args[0];
NamedPipeClientStream client = new NamedPipeClientStream(pipeName);
try
{
client.Connect(10000);
}
catch (Exception e)
{
string errmsg = e.Message + "\n";
errmsg += "Failed to connect to other program. Program will now exit.";
MessageBox.Show(errmsg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return 1;
}
//Create the query
ObjectQuery query = new ObjectQuery("Select * from Win32_Process Where Name =\"swtor.exe\"");
while (true)
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection processList = searcher.Get();
foreach (ManagementObject obj in processList)
{
string cmdline = obj.GetPropertyValue("CommandLine").ToString();
if (cmdline.Contains("username"))
{
// kill the process
obj.InvokeMethod("Terminate", null);
// write command line to the pipe
try
{
StreamWriter sw = new StreamWriter(client);
sw.AutoFlush = true;
sw.WriteLine(cmdline);
}
catch(Exception e)
{
string errmsg = e.Message + "\n";
errmsg += "Failed to write commandline arguments to pipe. Program will now exit.";
MessageBox.Show(errmsg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return 1;
}
// exit the program
return 0;
}
}
Thread.Sleep(1000);
}
}
}
}
}