-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
258 lines (218 loc) · 9.65 KB
/
Program.cs
File metadata and controls
258 lines (218 loc) · 9.65 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
//
// NOTE: To whoever's maintaining this code - Sorry (especially if it's me)
//
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using Ionic.Zip;
using System.Runtime.Remoting;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Loader
{
class FullscreenNotifier : Form
{
// tray icon
NotifyIcon TrayIcon;
System.ComponentModel.IContainer container;
private uint CallBackMessage;
public FullscreenNotifier()
{
FullscreenAppOpen = false;
// make the window not appear in the task bar, set it hopefully out of the screen
// (will clean this up to make it really invisible sometime in the future)
FormBorderStyle = FormBorderStyle.FixedToolWindow;
StartPosition = FormStartPosition.Manual;
Location = new System.Drawing.Point(-4000, -4000);
Size = new System.Drawing.Size(1, 1);
this.Load += new EventHandler(FullscreenNotifier_Loaded);
container = new System.ComponentModel.Container();
TrayIcon = new NotifyIcon(container);
TrayIcon.Icon = new System.Drawing.Icon("Ninja-Toy.ico");
TrayIcon.Text = "ZigFu loader. Having this means you're awesome!";
TrayIcon.ContextMenuStrip = new ContextMenuStrip(container) {
Items = {
new ToolStripLabel("E&xit", null, false, new EventHandler(delegate(object o, EventArgs e) { TrayIcon.Visible = false; this.Close(); }))
},
};
// TODO: left click gives context menu too
}
void FullscreenNotifier_Loaded(object sender, EventArgs e)
{
APPBARDATA appBar = new APPBARDATA();
appBar.cbSize = Marshal.SizeOf(appBar);
appBar.hWnd = this.Handle;
// Define a new window message that is guaranteed to be
// unique throughout the system.
const string Message = "ShitReceiveFullScreenNotification";
CallBackMessage = NativeMethod.RegisterWindowMessage(Message);
if (CallBackMessage == 0) {
throw new Exception(":(");
}
// Register a new appbar and specifies the message identifier
// that the system should use to send it notification
// messages. The call returns FALSE if an error occurs or if
// the full screen notification is already enabled.
appBar.uCallbackMessage = CallBackMessage;
if (NativeMethod.SHAppBarMessage(ABMsg.ABM_NEW, ref appBar) == 0) {
throw new Exception("SHAppBarMessage failed");
}
TrayIcon.Visible = true;
ShowInTaskbar = false;// moved here to avoid losing focus when launching loader.exe from unity
}
public bool FullscreenAppOpen { get; private set; }
protected override void WndProc(ref Message m)
{
if (m.Msg == this.CallBackMessage) {
if (m.WParam.ToInt32() == (int)ABNotify.ABN_FULLSCREENAPP) {
FullscreenAppOpen = (m.LParam != IntPtr.Zero);
Console.WriteLine("A full-screen application is " +
(FullscreenAppOpen ? "opening" : "closing"));
}
}
base.WndProc(ref m);
}
}
class Program
{
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
internal static extern int AttachThreadInput(int idAttach, int idAttachTo, int fAttach);
[STAThread]
static void Main(string[] args)
{
//testAutoUpdate(args);
//ExtractZip("temp.zip");
//testVersionObject();
System.Diagnostics.Process proc = null;
if (args.Length > 0) {
string RootProgram = args[0];
if (RootProgram == "client") {
Console.WriteLine("Client mode!");
bool launched = false;
if (!LoaderLib.LoaderAPI.ServerExists()) {
Console.WriteLine("No server found, launching server!");
LoaderLib.LoaderAPI.LaunchServer();
Console.Write("waiting for server to come online");
while (!LoaderLib.LoaderAPI.ServerExists()) {
System.Threading.Thread.Sleep(500);
Console.Write(".");
}
Console.WriteLine();
Console.WriteLine("Server up and running!");
launched = true;
}
else {
Console.WriteLine("Existing server detected, using it");
}
LoaderLib.LoaderAPI.ConnectToServer().LaunchProcess(@"c:\windows\system32\notepad.exe", "shit", System.Diagnostics.Process.GetCurrentProcess().Id);
if (launched) {
Console.WriteLine("We launched the server, so we're going to kill it!");
LoaderLib.LoaderAPI.ShutdownClient();
}
return;
}
Console.WriteLine("Server! Use Ctrl-C to exit!");
Console.WriteLine("Running Launcher: {0}", RootProgram);
proc = System.Diagnostics.Process.Start(RootProgram);
}
else {
Console.WriteLine("Server! Use Ctrl-C to exit!");
Console.WriteLine("Assuming launcher already running");
}
FullscreenNotifier f = new FullscreenNotifier();
//f.Show();
OpenNIListener gestureShit = new OpenNIListener();
gestureShit.Start();
var SharedObject = LoaderLib.LoaderAPI.StartServer(f.Handle, delegate(object sender, LoaderLib.CreateProcessEventArgs e) {
Console.WriteLine("CreateProcess: {0}, in dir: {1}", e.Command, e.Path);
var launchingProcess = System.Diagnostics.Process.GetProcessById(e.ProcessID);
if (launchingProcess != null) {
ShowWindow(launchingProcess.MainWindowHandle, 11); // magic number = SW_FORCEMINIMIZE
}
//TODO: ugh (change to event instead of polling)
while (f.FullscreenAppOpen) {
System.Threading.Thread.Sleep(100);
}
var newProc = System.Diagnostics.Process.Start(e.Command);
EventHandler<OpenNI.GestureRecognizedEventArgs> callback = delegate(object s2, OpenNI.GestureRecognizedEventArgs ev) {
Console.WriteLine("detected wave");
if ((null != newProc) && (newProc.HasExited == false)) {
newProc.Kill();
}
};
gestureShit.Gesture.GestureRecognized += callback;
try {
newProc.EnableRaisingEvents = true;
newProc.Exited += new EventHandler(delegate(object sender2, EventArgs e2) {
if (launchingProcess != null) {
ShowWindow(launchingProcess.MainWindowHandle, 9); // magic number = SW_SHOW
}
Console.WriteLine("removing gesture callback - happy flow");
gestureShit.Gesture.GestureRecognized -= callback;
});
Console.WriteLine("done with callback");
}
catch (Exception) {
// make sure callback is removed
gestureShit.Gesture.GestureRecognized -= callback;
}
},
// quit delegate
delegate(object s, EventArgs e) {
f.Close();
}
);
f.HandleCreated += delegate(object s, EventArgs e) { SharedObject.ServerWindowHandle = f.Handle; };
Application.Run(f);
}
private static void testVersionObject()
{
VersionString v1 = new VersionString("1.2.3.4");
VersionString v2 = new VersionString(1, 2, 3, 4);
VersionString v3 = new VersionString(1, 3, 3, 4);
VersionString v4 = new VersionString(1, 2, 2, 4);
if (v1.CompareTo(v2) != 0) {
Console.WriteLine("1 bad");
}
else {
Console.WriteLine("1 good");
}
if (v2.CompareTo(v3) >= 0) {
Console.WriteLine("2 bad");
}
else {
Console.WriteLine("2 good");
}
if (v2.CompareTo(v4) <= 0) {
Console.WriteLine("3 bad");
}
else {
Console.WriteLine("3 good");
}
}
private static void testAutoUpdate(string[] args)
{
if (args.Length == 0) {
System.Console.WriteLine("dummy run");
File.WriteAllText("text.txt", "did dummy run");
return;
}
else {
Autoupdate.DoAutoupdate();
}
}
private static void ExtractZip(string path)
{
using (ZipFile z = ZipFile.Read(path)) {
foreach (var e in z.Entries) {
e.Extract(ExtractExistingFileAction.OverwriteSilently);
}
}
}
}
}