-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPSO2Utility.cs
493 lines (426 loc) · 17.9 KB
/
PSO2Utility.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
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
using System;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
namespace PSO2Utility
{
public class StartUp
{
// http://www.atmarkit.co.jp/fdotnet/dotnettips/145winmutex/winmutex.html
// アプリケーション固定名
private static string mutexName = Application.ProductName;
// 多重起動を防止するミューテックス
private static System.Threading.Mutex mutexObject;
[STAThread]
public static void Main()
{
// Windows 2000(NT 5.0)以降のみグローバル・ミューテックス利用可
OperatingSystem os = Environment.OSVersion;
if ((os.Platform == PlatformID.Win32NT) && (os.Version.Major >= 5))
{
mutexName = @"Global\" + mutexName;
}
try
{
// ミューテックスを生成する
mutexObject = new System.Threading.Mutex(false, mutexName);
}
catch (ApplicationException)
{
return;
}
// ミューテックスを取得する
if (mutexObject.WaitOne(0, false))
{
// アプリケーションを実行
using (PSO2Utility main = new PSO2Utility())
{
Application.Run();
}
// ミューテックスを解放する
mutexObject.ReleaseMutex();
}
// ミューテックスを破棄する
mutexObject.Close();
}
}
public class PSO2Utility : IDisposable
{
// 設定
private PSO2UtilityOptions options = new PSO2UtilityOptions();
// メニュー
private MenuItem menuExecuteGame = new MenuItem();
private MenuItem menuLine1 = new MenuItem();
private MenuItem menuSystemButtons = new MenuItem();
private MenuItem menuAutoExecuteGame = new MenuItem();
private MenuItem menuWindowAutoRestore = new MenuItem();
private MenuItem menuLine2 = new MenuItem();
private MenuItem menuWindowSave = new MenuItem();
private MenuItem menuWindowRestore = new MenuItem();
private MenuItem menuLine3 = new MenuItem();
private MenuItem menuOpenFolderLog = new MenuItem();
private MenuItem menuOpenFolderScreenshot = new MenuItem();
private MenuItem menuLine4 = new MenuItem();
private MenuItem menuOptions = new MenuItem();
private MenuItem menuLine5 = new MenuItem();
private MenuItem menuExit = new MenuItem();
private ContextMenu contextMenu = new ContextMenu();
// タスクトレイのアイコン
private NotifyIcon notifyIcon = new NotifyIcon();
// ウィンドウ関連(システムボタン、位置)用タイマ
private Timer windowTimer = new Timer();
// 設定ウィンドウ
private Form formOptions;
// PSO2のウィンドウ検出が初回
private bool firstTime = true;
public PSO2Utility()
{
// 初期化
InitializeComponent();
// 設定読み込み
options = (PSO2UtilityOptions)options.Load();
options.Changed += new EventHandler(this.options_Changed);
// タスクトレイのアイコンを表示
notifyIcon.Visible = true;
// 起動時にPSO2を自動起動
if (options.AutoExecuteGameEnabled && !String.IsNullOrEmpty(options.GamePath) && File.Exists(options.GamePath) && !Window.Exists(options.WindowClassName))
{
ExecuteGame();
}
}
public void Dispose()
{
// タスクトレイのアイコンを非表示
notifyIcon.Visible = false;
// 設定保存
options.Save();
}
/// <summary>
/// 初期化
/// </summary>
private void InitializeComponent()
{
// コンテキストメニュー作成
CreateMenu();
// タスクトレイのアイコン作成
CreateNotifyIcon();
// タイマー設定
windowTimer.Interval = 1000;
windowTimer.Tick += new EventHandler(windowTimer_Tick);
windowTimer.Enabled = true;
}
/// <summary>
/// コンテキストメニュー作成
/// </summary>
private void CreateMenu()
{
menuExecuteGame.Text = Properties.Resources.ExecuteGame;
menuExecuteGame.DefaultItem = true;
menuExecuteGame.Click += new EventHandler(this.menuExecuteGame_Click);
contextMenu.MenuItems.Add(menuExecuteGame);
menuLine1.Text = "-";
contextMenu.MenuItems.Add(menuLine1);
menuSystemButtons.Text = Properties.Resources.ShowSystemButtons;
menuSystemButtons.Click += new EventHandler(this.menuSystemButtons_Click);
contextMenu.MenuItems.Add(menuSystemButtons);
menuAutoExecuteGame.Text = Properties.Resources.AutoExecuteGame;
menuAutoExecuteGame.Click += new EventHandler(this.menuAutoExecuteGame_Click);
contextMenu.MenuItems.Add(menuAutoExecuteGame);
menuWindowAutoRestore.Text = Properties.Resources.AutoRestoreWindowPosition;
menuWindowAutoRestore.Click += new EventHandler(this.menuWindowAutoRestore_Click);
contextMenu.MenuItems.Add(menuWindowAutoRestore);
menuLine2.Text = "-";
contextMenu.MenuItems.Add(menuLine2);
menuWindowSave.Text = Properties.Resources.SaveWindowPosition;
menuWindowSave.Click += new EventHandler(this.menuWindowSave_Click);
contextMenu.MenuItems.Add(menuWindowSave);
menuWindowRestore.Text = Properties.Resources.RestoreWindowPosition;
menuWindowRestore.Click += new EventHandler(this.menuWindowRestore_Click);
contextMenu.MenuItems.Add(menuWindowRestore);
menuLine3.Text = "-";
contextMenu.MenuItems.Add(menuLine3);
menuOpenFolderLog.Text = Properties.Resources.OpenLogFolder;
menuOpenFolderLog.Click += new EventHandler(this.menuOpenFolderLog_Click);
contextMenu.MenuItems.Add(menuOpenFolderLog);
menuOpenFolderScreenshot.Text = Properties.Resources.OpenScreenshotFolder;
menuOpenFolderScreenshot.Click += new EventHandler(this.menuOpenFolderBmp_Click);
contextMenu.MenuItems.Add(menuOpenFolderScreenshot);
menuLine4.Text = "-";
contextMenu.MenuItems.Add(menuLine4);
menuOptions.Text = Properties.Resources.Options;
menuOptions.Click += new EventHandler(this.menuOptions_Click);
contextMenu.MenuItems.Add(menuOptions);
menuLine5.Text = "-";
contextMenu.MenuItems.Add(menuLine5);
menuExit.Text = Properties.Resources.Exit;
menuExit.Click += new EventHandler(this.menuExit_Click);
contextMenu.MenuItems.Add(menuExit);
contextMenu.Popup += new EventHandler(contextMenu_Popup);
}
/// <summary>
/// 設定の変更
/// </summary>
private void options_Changed(object sender, System.EventArgs e)
{
// 設定保存
options.Save();
}
/// <summary>
/// タスクトレイのアイコン作成
/// </summary>
private void CreateNotifyIcon()
{
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
notifyIcon.Icon = new System.Drawing.Icon(assembly.GetManifestResourceStream("PSO2Utility.tray.ico"), 16, 16);
notifyIcon.Text = Application.ProductName;
notifyIcon.ContextMenu = contextMenu;
notifyIcon.DoubleClick += new EventHandler(notifyIcon_DoubleClick);
}
/// <summary>
/// アイコンのダブルクリック
/// </summary>
private void notifyIcon_DoubleClick(object sender, System.EventArgs e)
{
if (!String.IsNullOrEmpty(options.GamePath) && File.Exists(options.GamePath) && !Window.Exists(options.WindowClassName))
{
ExecuteGame();
}
}
/// <summary>
/// コンテキストメニューのポップアップ
/// </summary>
private void contextMenu_Popup(object sender, System.EventArgs e)
{
menuExecuteGame.Enabled = (!String.IsNullOrEmpty(options.GamePath) && File.Exists(options.GamePath) && !Window.Exists(options.WindowClassName));
menuSystemButtons.Checked = options.SystemButtonsEnabled;
menuAutoExecuteGame.Checked = options.AutoExecuteGameEnabled;
menuWindowAutoRestore.Checked = options.WindowAutoRestoreEnabled;
menuWindowSave.Enabled = menuWindowRestore.Enabled = Window.Exists(options.WindowClassName);
menuOpenFolderLog.Enabled = Directory.Exists(options.LogFolder);
menuOpenFolderScreenshot.Enabled = Directory.Exists(options.PicturesFolder);
}
/// <summary>
/// ウィンドウの位置とサイズを保存
/// </summary>
private void menuWindowSave_Click(object sender, System.EventArgs e)
{
SaveWindow();
}
/// <summary>
/// ウィンドウの位置を復元
/// </summary>
private void menuWindowRestore_Click(object sender, System.EventArgs e)
{
try
{
Window window = new Window(options.WindowClassName);
// ウィンドウの位置を復元
window.Position = options.WindowPosition;
}
catch (WindowNotFoundException)
{
ShowBalloonTip(Properties.Resources.Error, Properties.Resources.WindowNotFound, ToolTipIcon.Error, 3);
}
catch (WindowOperationException)
{
ShowBalloonTip(Properties.Resources.Error, Properties.Resources.WindowOperationFailed, ToolTipIcon.Error, 3);
}
}
/// <summary>
/// ゲームを起動
/// </summary>
private void menuExecuteGame_Click(object sender, System.EventArgs e)
{
ExecuteGame();
}
/// <summary>
/// システムボタンの表示
/// </summary>
private void menuSystemButtons_Click(object sender, System.EventArgs e)
{
options.SystemButtonsEnabled = !options.SystemButtonsEnabled;
}
/// <summary>
/// 起動時にPSO2を自動起動
/// </summary>
private void menuAutoExecuteGame_Click(object sender, System.EventArgs e)
{
options.AutoExecuteGameEnabled = !options.AutoExecuteGameEnabled;
}
/// <summary>
/// 自動的にウィンドウの位置を復元
/// </summary>
private void menuWindowAutoRestore_Click(object sender, System.EventArgs e)
{
options.WindowAutoRestoreEnabled = !options.WindowAutoRestoreEnabled;
firstTime = true;
}
/// <summary>
/// logフォルダを開く
/// </summary>
private void menuOpenFolderLog_Click(object sender, System.EventArgs e)
{
OpenFolder(options.LogFolder);
}
/// <summary>
/// bmpフォルダを開く
/// </summary>
private void menuOpenFolderBmp_Click(object sender, System.EventArgs e)
{
OpenFolder(options.PicturesFolder);
}
/// <summary>
/// 設定
/// </summary>
private void menuOptions_Click(object sender, System.EventArgs e)
{
ShowOptions();
}
/// <summary>
/// 終了
/// </summary>
private void menuExit_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
private void windowTimer_Tick(object sender, EventArgs e)
{
try
{
Window window = new Window(options.WindowClassName);
if (!window.Visible) return;
// システムボタンの表示(初検出時のみだとちゃんと表示されないので毎回やる)
if (options.SystemButtonsEnabled) window.AddSystemButton();
// 初検出の場合
if (firstTime)
{
// 自動的にウィンドウの位置を復元
if (options.WindowAutoRestoreEnabled) window.Position = options.WindowPosition;
firstTime = false;
}
}
catch (WindowNotFoundException)
{
// ウィンドウが無くなっていたら、次回起動時のためにtrueに戻す
firstTime = true;
}
catch (WindowOperationException)
{
ShowBalloonTip(Properties.Resources.Error, Properties.Resources.WindowOperationFailed, ToolTipIcon.Error, 3);
}
}
/// <summary>
/// ウィンドウの位置を保存
/// </summary>
private void SaveWindow()
{
try
{
Point position = Window.GetPosition(options.WindowClassName);
options.WindowPosition = position;
}
catch (WindowNotFoundException)
{
ShowBalloonTip(Properties.Resources.Error, Properties.Resources.WindowNotFound, ToolTipIcon.Error, 3);
}
catch (WindowOperationException)
{
ShowBalloonTip(Properties.Resources.Error, Properties.Resources.WindowOperationFailed, ToolTipIcon.Error, 3);
}
}
/// <summary>
/// 設定ウィンドウ表示
/// </summary>
private void ShowOptions()
{
// すでに表示されているか?
if (formOptions == null || formOptions.IsDisposed)
{
// メニューの無効化
foreach (MenuItem menu in contextMenu.MenuItems)
{
if (!menu.Equals(menuExit)) menu.Enabled = false;
}
// 設定ウィンドウ表示
using (formOptions = new FormOptions(options))
{
formOptions.ShowDialog();
}
// メニューの有効化
foreach (MenuItem menu in contextMenu.MenuItems)
{
if (!menu.Equals(menuExit)) menu.Enabled = true;
}
}
else
{
// アクティブにする
formOptions.Activate();
}
}
/// <summary>
/// フォルダを開く
/// </summary>
private void OpenFolder(string path)
{
if (Directory.Exists(path))
{
if (System.Diagnostics.Process.Start("explorer.exe", "/n," + path) == null)
{
ShowBalloonTip(Properties.Resources.Error, Properties.Resources.ExecutionFailed + "\n" + "explorer.exe", ToolTipIcon.Error, 3);
}
}
else
{
ShowBalloonTip(Properties.Resources.Error, Properties.Resources.DirectoryNotFound + "\n" + path, ToolTipIcon.Error, 3);
}
}
/// <summary>
/// ゲームを実行する
/// </summary>
private void ExecuteGame()
{
if (String.IsNullOrEmpty(options.GamePath))
{
ShowBalloonTip(Properties.Resources.Error, Properties.Resources.RequireOptionGameFolder, ToolTipIcon.Error, 3);
return;
}
if (!File.Exists(options.GamePath))
{
ShowBalloonTip(Properties.Resources.Error, Properties.Resources.FileNotFound, ToolTipIcon.Error, 3);
return;
}
if (Window.Exists(options.WindowClassName))
{
ShowBalloonTip(Properties.Resources.Error, Properties.Resources.AlreadyStarted, ToolTipIcon.Error, 3);
return;
}
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
psi.FileName = options.GamePath;
psi.Verb = "RunAs";
psi.WorkingDirectory = options.GameFolder + @"\pso2_bin";
try
{
System.Diagnostics.Process.Start(psi);
}
catch (System.ComponentModel.Win32Exception)
{
ShowBalloonTip(Properties.Resources.Error, Properties.Resources.ExecutionFailed, ToolTipIcon.Error, 3);
}
catch (FileNotFoundException)
{
ShowBalloonTip(Properties.Resources.Error, Properties.Resources.FileNotFound, ToolTipIcon.Error, 3);
}
}
/// <summary>
/// バルーンチップの表示
/// </summary>
private void ShowBalloonTip(string title, string text, ToolTipIcon icon, int timeout)
{
notifyIcon.BalloonTipTitle = title;
notifyIcon.BalloonTipText = text;
notifyIcon.BalloonTipIcon = icon;
notifyIcon.ShowBalloonTip(timeout * 1000);
}
}
}