-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarketSpeedLauncher.cs
More file actions
557 lines (498 loc) · 22.4 KB
/
Copy pathMarketSpeedLauncher.cs
File metadata and controls
557 lines (498 loc) · 22.4 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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Automation;
namespace KabuMSLauncher.Services
{
public class LaunchReport
{
public bool Success { get; set; }
public string Message { get; set; } = string.Empty;
}
public class MarketSpeedLauncher
{
[DllImport("user32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")] private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")] private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")] private static extern bool BringWindowToTop(IntPtr hWnd);
[DllImport("user32.dll")] private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[DllImport("user32.dll")] private static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);
[DllImport("kernel32.dll")] private static extern uint GetCurrentThreadId();
[DllImport("user32.dll")] private static extern bool SetCursorPos(int X, int Y);
[DllImport("user32.dll")] private static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, UIntPtr dwExtraInfo);
private const int SW_RESTORE = 9;
private const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
private const uint MOUSEEVENTF_LEFTUP = 0x0004;
// Probe locations (LocalAppData ClickOnce-style install is the standard MSⅡ deployment)
private static readonly string[] CandidatePaths = new[]
{
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"MarketSpeed2\Bin\MarketSpeed2.exe"),
@"C:\Program Files\MarketSpeed2\MarketSpeed2.exe",
@"C:\Program Files (x86)\MarketSpeed2\MarketSpeed2.exe",
};
public LaunchReport LaunchAndLogin(string loginId, string password, Action<string> progress)
{
var report = new LaunchReport();
var exe = ResolveExePath();
if (exe == null)
{
report.Success = false;
report.Message = "MarketSpeed2.exe が見つかりません。インストール済みかご確認ください。";
return report;
}
// Only start a new process if no login window is already on screen.
var preLogin = FindLoginWindow();
if (preLogin == null)
{
progress?.Invoke("MarketspeedⅡ を起動中...");
var startInfo = new ProcessStartInfo
{
FileName = exe,
WorkingDirectory = Path.GetDirectoryName(exe) ?? string.Empty,
UseShellExecute = true,
};
try { Process.Start(startInfo); }
catch (Exception ex)
{
report.Success = false;
report.Message = "起動に失敗:" + ex.Message;
return report;
}
}
else
{
progress?.Invoke("既存のログインウィンドウを使用します...");
}
progress?.Invoke("ログインウィンドウを待機中...");
var loginWindow = WaitForLoginWindow(timeoutSec: 60);
if (loginWindow == null)
{
report.Success = false;
report.Message = "ログインウィンドウが見つかりませんでした(タイムアウト60秒)。\n既にログイン済みの可能性があります。";
return report;
}
// Give the window a moment to fully render before interacting.
Thread.Sleep(700);
BringWindowForeground(loginWindow);
Thread.Sleep(250);
progress?.Invoke("認証情報を入力中...");
string error;
var filled = FillCredentialsAndSubmit(loginWindow, loginId, password, progress, out error);
if (!filled)
{
report.Success = false;
report.Message = string.IsNullOrEmpty(error) ? "ID/PW自動入力に失敗しました。" : error;
return report;
}
report.Success = true;
report.Message = "自動ログインを実行しました。";
return report;
}
private static void BringWindowForeground(AutomationElement element)
{
try
{
var hwnd = new IntPtr(element.Current.NativeWindowHandle);
if (hwnd == IntPtr.Zero) return;
ShowWindow(hwnd, SW_RESTORE);
// AttachThreadInput trick: focus stealing prevention bypass when this process is foreground.
uint targetProcId;
uint targetThread = GetWindowThreadProcessId(hwnd, out targetProcId);
uint currentThread = GetCurrentThreadId();
bool attached = false;
if (targetThread != currentThread)
{
attached = AttachThreadInput(currentThread, targetThread, true);
}
try
{
BringWindowToTop(hwnd);
SetForegroundWindow(hwnd);
}
finally
{
if (attached) AttachThreadInput(currentThread, targetThread, false);
}
}
catch { /* best-effort */ }
}
private static string ResolveExePath()
{
return CandidatePaths.FirstOrDefault(File.Exists);
}
private static AutomationElement WaitForLoginWindow(int timeoutSec)
{
var deadline = DateTime.UtcNow.AddSeconds(timeoutSec);
while (DateTime.UtcNow < deadline)
{
var window = FindLoginWindow();
if (window != null) return window;
Thread.Sleep(500);
}
return null;
}
private static AutomationElement FindLoginWindow()
{
try
{
var ownPid = (uint)Process.GetCurrentProcess().Id;
var root = AutomationElement.RootElement;
var windows = root.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window));
foreach (AutomationElement w in windows)
{
// Skip windows owned by our own process — our launcher title contains "MarketSpeed" and "LOGIN".
try
{
var hwnd = new IntPtr(w.Current.NativeWindowHandle);
if (hwnd == IntPtr.Zero) continue;
uint pid;
GetWindowThreadProcessId(hwnd, out pid);
if (pid == ownPid) continue;
}
catch { continue; }
var name = SafeName(w);
var className = SafeClassName(w);
if (string.IsNullOrEmpty(name) && string.IsNullOrEmpty(className)) continue;
if (LooksLikeMarketSpeedWindow(name, className))
{
if (HasPasswordField(w)) return w;
}
}
}
catch { /* tree may be transient */ }
return null;
}
private static bool LooksLikeMarketSpeedWindow(string name, string className)
{
// Use upper-case title which matches MSⅡ's actual title "MARKETSPEED II ログイン",
// avoiding accidental matches with the launcher's lower-case "MarketspeedⅡ".
if (name != null)
{
if (name.Contains("MARKETSPEED")) return true;
if (name.Contains("マーケットスピード")) return true;
}
var hay = (name + " " + className).ToLowerInvariant();
return hay.Contains("market speed") || hay.Contains("ms2 ");
}
private static bool HasPasswordField(AutomationElement window)
{
try
{
var edits = window.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
foreach (AutomationElement e in edits)
{
if (e.Current.IsPassword) return true;
}
}
catch { }
return false;
}
private static bool FillCredentialsAndSubmit(AutomationElement window, string loginId, string password, Action<string> progress, out string error)
{
error = string.Empty;
try
{
var allEdits = window.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit))
.Cast<AutomationElement>()
.Where(e => e.Current.IsEnabled && !e.Current.IsOffscreen)
.ToList();
if (allEdits.Count < 2)
{
error = "入力フィールドが2つ未満です。MSⅡのバージョンが変わっている可能性があります。";
return false;
}
var passwordEdit = allEdits.FirstOrDefault(e => e.Current.IsPassword);
if (passwordEdit == null)
{
error = "パスワード入力欄が検出できませんでした。";
return false;
}
var idCandidates = allEdits.Where(e => !e.Current.IsPassword).ToList();
if (idCandidates.Count == 0)
{
error = "ID入力欄が検出できませんでした。";
return false;
}
var idEdit = idCandidates.OrderBy(e => GetTop(e)).ThenBy(e => GetLeft(e)).First();
// Re-foreground in case focus shifted during element discovery.
BringWindowForeground(window);
Thread.Sleep(150);
// --- ID field ---
if (!FillAndVerifyIdField(idEdit, loginId))
{
error = "ID入力の検証に失敗しました(フィールドが空のままです)。手動で入力してください。";
return false;
}
// --- Password field ---
if (!FillPasswordField(passwordEdit, password))
{
error = "パスワード入力に失敗しました。手動で入力してください。";
return false;
}
Thread.Sleep(300);
SubmitLogin(window, passwordEdit, progress);
return true;
}
catch (Exception ex)
{
error = "自動入力で例外:" + ex.Message;
return false;
}
}
private static bool FillAndVerifyIdField(AutomationElement idEdit, string loginId)
{
// Attempt 1: ValuePattern
string current = TryReadValue(idEdit);
object pat;
if (idEdit.TryGetCurrentPattern(ValuePattern.Pattern, out pat))
{
try { ((ValuePattern)pat).SetValue(loginId); } catch { }
Thread.Sleep(120);
if (TryReadValue(idEdit) == loginId) return true;
}
// Attempt 2: focus, clear, SendKeys
FocusAndType(idEdit, loginId);
Thread.Sleep(150);
if (TryReadValue(idEdit) == loginId) return true;
// Attempt 3: one more retry after re-foregrounding
var parent = TreeWalker.ControlViewWalker.GetParent(idEdit);
while (parent != null && parent.Current.ControlType != ControlType.Window)
{
parent = TreeWalker.ControlViewWalker.GetParent(parent);
}
if (parent != null) BringWindowForeground(parent);
Thread.Sleep(150);
FocusAndType(idEdit, loginId);
Thread.Sleep(150);
return TryReadValue(idEdit) == loginId;
}
private static bool FillPasswordField(AutomationElement pwEdit, string password)
{
// PasswordBox typically does not expose ValuePattern. Use focus + SendKeys.
FocusAndType(pwEdit, password);
Thread.Sleep(120);
// We cannot read back the password value (IsPassword obscures it).
// Verification is implicit: if the login attempt succeeds, the field had the right contents.
return true;
}
private static void FocusAndType(AutomationElement edit, string text)
{
try { edit.SetFocus(); } catch { }
Thread.Sleep(80);
SendKeysSafe("^a");
Thread.Sleep(40);
SendKeysSafe("{DEL}");
Thread.Sleep(40);
SendKeysSafe(EscapeForSendKeys(text));
}
private static string TryReadValue(AutomationElement edit)
{
try
{
object pat;
if (edit.TryGetCurrentPattern(ValuePattern.Pattern, out pat))
{
return ((ValuePattern)pat).Current.Value ?? string.Empty;
}
}
catch { }
return string.Empty;
}
private static double GetTop(AutomationElement e)
{
try { return e.Current.BoundingRectangle.Top; } catch { return double.MaxValue; }
}
private static double GetLeft(AutomationElement e)
{
try { return e.Current.BoundingRectangle.Left; } catch { return double.MaxValue; }
}
private static void SubmitLogin(AutomationElement window, AutomationElement passwordEdit, Action<string> progress)
{
var loginBtn = FindLoginSubmitButton(window);
if (loginBtn != null)
{
progress?.Invoke("ログインボタンを押下中...");
BringWindowForeground(window);
Thread.Sleep(250);
if (MouseClickElement(loginBtn))
{
// Give MSⅡ time to process the login before deciding to retry.
Thread.Sleep(1500);
if (!IsLoginStillVisible(window)) return;
}
}
// Fallback: Enter on password field (default-button submit).
progress?.Invoke("Enter キーでログイン試行中...");
BringWindowForeground(window);
Thread.Sleep(200);
try { passwordEdit.SetFocus(); } catch { }
Thread.Sleep(150);
SendKeysSafe("{ENTER}");
}
private static AutomationElement FindLoginSubmitButton(AutomationElement window)
{
try
{
var buttons = window.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button))
.Cast<AutomationElement>()
.Where(b => b.Current.IsEnabled && !b.Current.IsOffscreen)
.ToList();
// Tight exact matches (avoids "ログインID保存" / "ログインにお困りの方" / etc.)
var exactNames = new[] { "ログイン", "ログインする", "Login", "LOGIN", "Log In", "サインイン", "Sign In", "OK", "OK" };
var target = buttons.FirstOrDefault(b => exactNames.Any(n =>
string.Equals((SafeName(b) ?? string.Empty).Trim(), n, StringComparison.OrdinalIgnoreCase)));
if (target != null) return target;
// Loose fallback that explicitly excludes known non-submit labels.
target = buttons.FirstOrDefault(b =>
{
var name = (SafeName(b) ?? string.Empty).Trim();
if (string.IsNullOrEmpty(name)) return false;
if (name.Contains("保存") || name.Contains("お困り") || name.Contains("開設") || name.Contains("設定") || name.Contains("ビジター")) return false;
return name.StartsWith("ログイン", StringComparison.OrdinalIgnoreCase)
|| name.StartsWith("Login", StringComparison.OrdinalIgnoreCase);
});
return target;
}
catch
{
return null;
}
}
private static bool ClickLoginButton(AutomationElement window)
{
try
{
var buttons = window.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button))
.Cast<AutomationElement>()
.Where(b => b.Current.IsEnabled && !b.Current.IsOffscreen)
.ToList();
var matchers = new[] { "ログイン", "login", "スタート", "start", "接続", "OK", "OK" };
AutomationElement target = null;
foreach (var m in matchers)
{
target = buttons.FirstOrDefault(b => (SafeName(b) ?? string.Empty).IndexOf(m, StringComparison.OrdinalIgnoreCase) >= 0);
if (target != null) break;
}
if (target == null)
{
// Heuristic fallback: enabled named button furthest to the right (the primary action)
target = buttons
.Where(b => !string.IsNullOrEmpty(SafeName(b)))
.OrderByDescending(b => b.Current.BoundingRectangle.Right)
.FirstOrDefault();
}
if (target == null) return false;
// Strategy A: InvokePattern (some custom WPF buttons silently no-op on this).
object patternObj;
if (target.TryGetCurrentPattern(InvokePattern.Pattern, out patternObj))
{
try { ((InvokePattern)patternObj).Invoke(); } catch { }
Thread.Sleep(400);
if (!IsLoginStillVisible(window)) return true;
}
// Strategy B: physical mouse click at the button's clickable point (most reliable).
if (MouseClickElement(target))
{
Thread.Sleep(400);
if (!IsLoginStillVisible(window)) return true;
}
// Strategy C: focus + space key
try { target.SetFocus(); } catch { }
Thread.Sleep(80);
SendKeysSafe(" ");
return true;
}
catch
{
return false;
}
}
private static bool MouseClickElement(AutomationElement element)
{
try
{
System.Windows.Point pt;
bool gotPoint = false;
try { gotPoint = element.TryGetClickablePoint(out pt); } catch { pt = new System.Windows.Point(); }
if (!gotPoint || (pt.X == 0 && pt.Y == 0))
{
var rect = element.Current.BoundingRectangle;
if (rect.IsEmpty) return false;
pt = new System.Windows.Point(rect.Left + rect.Width / 2, rect.Top + rect.Height / 2);
}
// Re-foreground the owning window right before the click — focus may have shifted.
var parent = TreeWalker.ControlViewWalker.GetParent(element);
while (parent != null && parent.Current.ControlType != ControlType.Window)
{
parent = TreeWalker.ControlViewWalker.GetParent(parent);
}
if (parent != null) BringWindowForeground(parent);
Thread.Sleep(180);
SetCursorPos((int)pt.X, (int)pt.Y);
Thread.Sleep(120);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, UIntPtr.Zero);
Thread.Sleep(60);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, UIntPtr.Zero);
return true;
}
catch
{
return false;
}
}
private static bool IsLoginStillVisible(AutomationElement window)
{
try
{
// Accessing properties throws when the element is no longer valid.
var _ = window.Current.Name;
var edits = window.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
foreach (AutomationElement e in edits)
{
if (e.Current.IsPassword && !e.Current.IsOffscreen) return true;
}
return false;
}
catch
{
return false;
}
}
private static string SafeName(AutomationElement e)
{
try { return e.Current.Name ?? string.Empty; } catch { return string.Empty; }
}
private static string SafeClassName(AutomationElement e)
{
try { return e.Current.ClassName ?? string.Empty; } catch { return string.Empty; }
}
private static void SendKeysSafe(string keys)
{
try { System.Windows.Forms.SendKeys.SendWait(keys); } catch { }
}
private static string EscapeForSendKeys(string text)
{
// SendKeys treats + ^ % ~ ( ) { } [ ] as control characters; escape them.
var sb = new System.Text.StringBuilder(text.Length);
foreach (var ch in text)
{
switch (ch)
{
case '+': case '^': case '%': case '~':
case '(': case ')': case '{': case '}':
case '[': case ']':
sb.Append('{').Append(ch).Append('}');
break;
default:
sb.Append(ch);
break;
}
}
return sb.ToString();
}
}
}