-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
177 lines (166 loc) · 5.92 KB
/
Program.cs
File metadata and controls
177 lines (166 loc) · 5.92 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
using System.Linq.Expressions;
using System;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Point = System.Drawing.Point;
using Rectangle = System.Drawing.Rectangle;
using WindowsInput.Native;
using WindowsInput;
using System.Threading;
using System.Reflection;
using System.Runtime.CompilerServices;
using Newtonsoft.Json;
public static class Program
{
static InputSimulator sim = new InputSimulator();
public static void Main(string[] args)
{
//Console.ReadLine();
TimeSpan GOAL_TICK_TIME = new TimeSpan(0, 0, 0, 0, 50);
const bool ServerRegulatedTick = false;
if (!HttpListener.IsSupported)
{
Console.WriteLine("HttpListner not supported");
return;
}
HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://*:8000/");
Console.WriteLine("Starting on port 8000");
listener.Start();
Stopwatch s = new Stopwatch();
while (true)
{
HttpListenerContext context = listener.GetContext();
Task task1 = Task.Factory.StartNew(() => Handle(context));
}
}
public static void Handle(HttpListenerContext context)
{
HttpListenerResponse response = context.Response;
byte[] buffer = Process(context, ref response);
response.ContentLength64 = buffer.Length;
Stream output = response.OutputStream;
try
{
output.Write(buffer, 0, buffer.Length);
output.Close();
}
catch
{
return;
}
}
public static string ReadBody(HttpListenerRequest request)
{
using (System.IO.Stream body = request.InputStream)
{
using (var reader = new System.IO.StreamReader(body, request.ContentEncoding))
{
return reader.ReadToEnd();
}
}
}
//Change default image size here, if the image is to blury and your willing to take the hit to performance
static Size imageSize = new Size(1920, 1080);
//Try to keep this ratio at 4:3 , so the screen doesn't look funky
public static byte[] Process(HttpListenerContext l, ref HttpListenerResponse r)
{
if (l.Request.HttpMethod == "POST")
{
try
{
int longside = Int32.Parse(l.Request.Headers["res"]);
imageSize = new Size(longside, ((int)(longside * 0.75f)));
}catch { }
{
Rectangle bounds = Screen.GetBounds(Point.Empty);
using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
}
var body = ReadBody(l.Request);
r.AddHeader("height", bounds.Height.ToString());
r.AddHeader("width", bounds.Width.ToString());
Bitmap resized = (Bitmap)resizeImage(bitmap, imageSize);
MemoryStream s = new MemoryStream();
resized.SaveJPG100(s);
HandleInput(body);
//sim.Keyboard.KeyPress((VirtualKeyCode)key);
return s.ToArray();
//return (byte[])converter.ConvertTo(bitmap, typeof(byte[]));
}
}
}
else if (l.Request.HttpMethod == "GET")
{
var path = GetThisFilePath();
var directory = Path.GetDirectoryName(path);
return System.Text.Encoding.UTF8.GetBytes(File.ReadAllText(directory + "\\Response.html"));
}
return new byte[0];
}
private static void HandleInput(string body)
{
var list = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(body);
Console.WriteLine(list);
ProcessEvents(list);
}
private static void ProcessEvents(List<Dictionary<string, string>> events)
{
foreach(Dictionary<string, string> e in events){
string type = e["type"];
switch (type)
{
case "keydown":
int keydown = Int32.Parse(e["keyCode"]);
sim.Keyboard.KeyDown((VirtualKeyCode)keydown);
break;
case "keyup":
int keyup = Int32.Parse(e["keyCode"]);
sim.Keyboard.KeyUp((VirtualKeyCode)keyup);
break;
case "mouseup":
switch (e["button"])
{
case "0":
sim.Mouse.LeftButtonUp();
break;
case "2":
sim.Mouse.RightButtonUp();
break;
}
break;
case "mousedown":
switch (e["button"])
{
case "0":
sim.Mouse.LeftButtonDown();
break;
case "2":
sim.Mouse.RightButtonDown();
break;
}
break;
case "mousemove":
int x = Helper.FracToScreen(Double.Parse(e["xPer"]));
int y = Helper.FracToScreen(Double.Parse(e["yPer"]));
sim.Mouse.MoveMouseTo(x, y);
break;
}
}
}
private static string GetThisFilePath([CallerFilePath] string path = null)
{
return path;
}
public static Image resizeImage(Image imgToResize, Size size)
{
return (Image)(new Bitmap(imgToResize, size));
}
}