-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathFunctions.cs
297 lines (249 loc) · 10.8 KB
/
Functions.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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Json;
using System.Security.Principal;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
namespace PoeTradeSearch
{
public partial class MainWindow : Window
{
internal static bool IsAdministrator()
{
var identity = WindowsIdentity.GetCurrent();
var principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
private string GetFileVersion()
{
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
return fvi.FileVersion;
}
private double StrToDouble(string s, double def = 0)
{
if ((s ?? "") != "")
{
try
{
def = double.Parse(s);
}
catch { }
}
return def;
}
private double DamageToDPS(string damage)
{
double dps = 0;
try
{
string[] stmps = Regex.Replace(damage, @"\([a-zA-Z]+\)", "").Split(',');
for (int t = 0; t < stmps.Length; t++)
{
string[] maidps = (stmps[t] ?? "").Trim().Split('-');
if (maidps.Length == 2)
dps += double.Parse(maidps[0].Trim()) + double.Parse(maidps[1].Trim());
}
}
catch { }
return dps;
}
private string SendHTTP(string entity, string urlString, int timeout = 5)
{
string result = "";
try
{
// WebClient 코드는 테스트할게 있어 만들어둔 코드...
if (timeout == 0)
{
using (WebClient webClient = new WebClient())
{
webClient.Encoding = UTF8Encoding.UTF8;
if (entity == null)
{
result = webClient.DownloadString(urlString);
}
else
{
webClient.Headers[HttpRequestHeader.ContentType] = "application/json";
result = webClient.UploadString(urlString, entity);
}
}
}
else
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(urlString));
request.Timeout = timeout * 1000;
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2"; // SGS Galaxy
if (entity == null)
{
request.Method = WebRequestMethods.Http.Get;
}
else
{
request.Accept = "application/json";
request.ContentType = "application/json";
request.Headers.Add("Content-Encoding", "utf-8");
request.Method = WebRequestMethods.Http.Post;
byte[] data = Encoding.UTF8.GetBytes(entity);
request.ContentLength = data.Length;
request.GetRequestStream().Write(data, 0, data.Length);
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (StreamReader streamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
result = streamReader.ReadToEnd();
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
return result;
}
}
internal static class Native
{
[DllImport("user32.dll")] internal static extern IntPtr SetClipboardViewer(IntPtr hWnd);
[DllImport("user32.dll")] internal static extern bool ChangeClipboardChain(IntPtr hWnd, IntPtr hWndNext);
internal const int WM_DRAWCLIPBOARD = 0x0308;
internal const int WM_CHANGECBCHAIN = 0x030D;
[DllImport("user32.dll", CharSet = CharSet.Unicode)] internal static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", CharSet = CharSet.Unicode)] internal static extern IntPtr FindWindowEx(IntPtr parenthWnd, IntPtr childAfter, string lpClassName, string lpWindowName);
[DllImport("user32.dll")] internal static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")] internal static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")] internal static extern bool SetForegroundWindow(IntPtr hWnd);
internal const int GWL_EXSTYLE = -20;
internal const int WS_EX_NOACTIVATE = 0x08000000;
[DllImport("user32.dll")] internal static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll")] internal static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")] internal static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
[DllImport("user32.dll")] internal static extern bool UnregisterHotKey(IntPtr hWnd, int id);
/*
[DllImport("user32.dll")] internal static extern uint GetWindowThreadProcessId(IntPtr hwnd, IntPtr proccess);
[DllImport("user32.dll")] internal static extern IntPtr GetKeyboardLayout(uint thread);
*/
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)] internal static extern IntPtr GetModuleHandle(string lpModuleName);
internal const int WH_MOUSE_LL = 14;
internal delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")] internal static extern IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll")] internal static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll")] internal static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")] internal static extern short GetKeyState(int nVirtKey);
}
internal static class MouseHook
{
internal static event EventHandler MouseAction = delegate { };
internal static void Start()
{
if (_hookID != IntPtr.Zero)
Stop();
_hookID = SetHook(_proc);
}
internal static void Stop()
{
try
{
Native.UnhookWindowsHookEx(_hookID);
_hookID = IntPtr.Zero;
}
catch (Exception)
{
}
}
private static Native.LowLevelMouseProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;
private static IntPtr SetHook(Native.LowLevelMouseProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
return Native.SetWindowsHookEx(Native.WH_MOUSE_LL, proc, Native.GetModuleHandle(curModule.ModuleName), 0);
}
}
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0)
{
if (MouseMessages.WM_MOUSEWHEEL == (MouseMessages)wParam && (Native.GetKeyState(VK_CONTROL) & 0x100) != 0)
{
if (Native.GetForegroundWindow().Equals(Native.FindWindow(Restr.PoeClass, Restr.PoeCaption)))
{
try
{
MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
int GET_WHEEL_DELTA_WPARAM = (short)(hookStruct.mouseData >> 0x10); // HIWORD
MouseEventArgs mouseEventArgs = new MouseEventArgs();
mouseEventArgs.zDelta = GET_WHEEL_DELTA_WPARAM;
mouseEventArgs.x = hookStruct.pt.x;
mouseEventArgs.y = hookStruct.pt.y;
MouseAction(null, mouseEventArgs);
}
catch { }
return new IntPtr(1);
}
}
MainWindow.MouseHookCallbackTime = Convert.ToDateTime(DateTime.Now);
}
return Native.CallNextHookEx(_hookID, nCode, wParam, lParam);
}
private const int VK_CONTROL = 0x11;
public class MouseEventArgs : EventArgs
{
public int zDelta { get; set; }
public int x { get; set; }
public int y { get; set; }
}
private enum MouseMessages
{
WM_LBUTTONDOWN = 0x0201,
WM_LBUTTONUP = 0x0202,
WM_MOUSEMOVE = 0x0200,
WM_MOUSEWHEEL = 0x020A,
WM_RBUTTONDOWN = 0x0204,
WM_RBUTTONUP = 0x0205
}
[StructLayout(LayoutKind.Sequential)]
private struct POINT
{
public int x;
public int y;
}
[StructLayout(LayoutKind.Sequential)]
private struct MSLLHOOKSTRUCT
{
public POINT pt;
public uint mouseData;
public uint flags;
public uint time;
public IntPtr dwExtraInfo;
}
}
internal static class Json
{
internal static string Serialize<T>(object obj) where T : class
{
DataContractJsonSerializer dcsJson = new DataContractJsonSerializer(typeof(T));
MemoryStream mS = new MemoryStream();
dcsJson.WriteObject(mS, obj);
var json = mS.ToArray();
mS.Close();
return Encoding.UTF8.GetString(json, 0, json.Length);
}
internal static T Deserialize<T>(string strData) where T : class
{
DataContractJsonSerializer dcsJson = new DataContractJsonSerializer(typeof(T));
byte[] byteArray = Encoding.UTF8.GetBytes(strData);
MemoryStream mS = new MemoryStream(byteArray);
T tRet = dcsJson.ReadObject(mS) as T;
mS.Dispose();
return (tRet);
}
}
}