forked from oleg-shilo/cs-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
css.cs
144 lines (125 loc) · 4.21 KB
/
css.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
using System;
using System.Threading;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Reflection;
//todo: start as shortcut scenario:
//create event here and set it in the cscs.exe as mutex climed in the cscs.exe may be released if cscs.exe does not live long enough
//so css would beleive console should not be visible
class Script
{
static bool done = false;
const int SW_HIDE = 0;
const int SW_SHOW = 5;
[DllImport("User32")]
static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
[DllImport("kernel32")]
static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
private static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, out IntPtr ProcessId);
//[DllImport("kernel32", SetLastError = true)]
//static extern int SetConsoleMode(int hConsoleHandle, int dwMode);
static Process myProcess = new Process();
static Thread inputThread = null;
static bool consoleVisibilityForced = false;
static bool cmdConsole = false;
static bool nologo = false;
static int lineCount = 0;
static public void Main(string[] args)
{
try
{
IntPtr hwnd = GetConsoleWindow();
if (hwnd != IntPtr.Zero)
{
IntPtr processId = IntPtr.Zero;
GetWindowThreadProcessId(hwnd, out processId);
if (Process.GetCurrentProcess().Id == (int)processId)
ShowWindow(hwnd, SW_HIDE);
else
cmdConsole = true;
}
myProcess.StartInfo.FileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "cscs.exe");
string arguments = "";
foreach (string arg in args)
{
if (arg == "/nl")
nologo = true;
arguments += "\"" + arg + "\" ";
}
myProcess.StartInfo.Arguments = arguments;
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
Thread workingThread = new Thread(new ThreadStart(HandleOutput));
workingThread.IsBackground = true;
workingThread.Start();
inputThread = new Thread(new ThreadStart(HandleInput));
inputThread.IsBackground = false;
inputThread.Start();
myProcess.WaitForExit();
Environment.ExitCode = myProcess.ExitCode;
while (!done)
Thread.Sleep(100);
if (!cmdConsole && consoleVisibilityForced)
{
Console.WriteLine("\n\nPress any key to continue . . .");
}
else
{
inputThread.IsBackground = true;
inputThread.Abort();
}
}
catch
{
}
}
static void HandleOutput()
{
try
{
int key = 0;
while (-1 != (key = myProcess.StandardOutput.Read()))
{
if (!cmdConsole && !consoleVisibilityForced)
{
if (key == 10)
lineCount++;
if (nologo || (!nologo && lineCount > 3)) //cscs.exe has finished printing logo
{
ShowWindow(GetConsoleWindow(), SW_SHOW);
consoleVisibilityForced = true;
}
}
Console.Write(Convert.ToChar(key));
}
done = true;
}
catch
{
}
}
static void HandleInput()
{
try
{
ConsoleKeyInfo cki;
while (!myProcess.HasExited)
{
cki = Console.ReadKey(false);
if (cki.Key == ConsoleKey.Enter)
myProcess.StandardInput.WriteLine("");
else
myProcess.StandardInput.Write(cki.KeyChar);
}
}
catch
{
}
}
}