-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
257 lines (231 loc) · 8.93 KB
/
App.xaml.cs
File metadata and controls
257 lines (231 loc) · 8.93 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
using System;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Reflection;
using System.Security.Principal;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
namespace PoeTradeSearch
{
/// <summary>
/// App.xaml에 대한 상호 작용 논리
/// </summary>
public partial class App : Application, IDisposable
{
private string mLogFilePath;
public System.Windows.Forms.NotifyIcon mTrayIcon { get; set; }
private void AppDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
RunException(e.Exception);
e.Handled = true;
}
private void RunException(Exception ex)
{
try
{
File.AppendAllText(
mLogFilePath,
String.Format("{0} Error: {1}\r\n\r\n{2}\r\n\r\n", ex.Source, ex.Message, ex.StackTrace)
);
}
catch { }
Application.Current.Shutdown(ex.HResult);
}
private bool IsAdministrator()
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
if (null != identity)
{
WindowsPrincipal principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
return false;
}
private void PoeExeUpdates(string path)
{
// 마우스 훜시 프로그램에 딜레이가 생겨 쓰레드 처리
Thread thread = new Thread(() =>
{
File.Delete(path + "poe_exe.zip");
File.Delete(path + "update.cmd");
File.Delete(path + "update.dat");
using (var client = new WebClient())
{
try
{
client.DownloadFile(
"https://raw.githubusercontent.com/phiDelPark/PoeTradeSearch/master/_POE_Data/_POE_EXE.zip",
path + "poe_exe.zip"
);
}
catch
{
MessageBox.Show("서버 접속이 원할하지 않을 수 있습니다." + '\n' + "다음에 다시 시도해 주세요.", "업데이트 실패");
throw;
}
}
if (File.Exists(path + "poe_exe.zip"))
{
ZipFile.ExtractToDirectory(path + "poe_exe.zip", path);
File.Delete(path + "poe_exe.zip");
}
});
thread.Start();
thread.Join();
while (!File.Exists(path + "update.cmd"))
{
Thread.Sleep(100);
}
Process.Start(path + "update.cmd");
}
private void TrayMenuClick(object sender, EventArgs e)
{
string path = (string)Current.Properties["DataPath"];
int idx = (int)(sender as System.Windows.Forms.MenuItem).Tag;
switch (idx)
{
case 0:
Current.Shutdown();
break;
case 1:
WinSetting winSetting = new WinSetting();
winSetting.Show();
break;
case 2:
case 4:
if (idx == 4)
{
File.Delete(path + "FiltersKO.txt");
File.Delete(path + "Parser.txt");
}
_ = Process.Start(new ProcessStartInfo(Assembly.GetExecutingAssembly().Location)
{
Arguments = "/wait_shutdown"
});
Current.Shutdown();
break;
case 3:
PoeExeUpdates(path);
Current.Shutdown();
break;
}
}
private Mutex mMutex = null;
private bool CheckMutex()
{
if (mMutex != null)
{
mMutex.Close();
mMutex = null;
}
bool createdNew;
Assembly assembly = Assembly.GetExecutingAssembly();
mMutex = new Mutex(true, String.Format(
CultureInfo.InvariantCulture, "Local\\{{{0}}}{{{1}}}", assembly.GetType().GUID, assembly.GetName().Name
), out createdNew);
return !createdNew;
}
protected virtual void Dispose(bool disposing)
{
if (disposing && (mMutex != null))
{
mMutex.ReleaseMutex();
mMutex.Close();
mMutex = null;
}
}
public void Dispose()
{
mTrayIcon.Visible = false;
mTrayIcon.Dispose();
Dispose(true);
GC.SuppressFinalize(this);
}
[STAThread]
protected override void OnStartup(StartupEventArgs e)
{
foreach (string item in e.Args)
{
if (item == "/wait_shutdown")
{
for (int i = 0; i < 10; i++)
{
if (!CheckMutex()) break;
Thread.Sleep(1000);
if (i == 9)
{
MessageBox.Show("이 프로그램이 종료되지 않았습니다.", "실행 오류", MessageBoxButton.OK, MessageBoxImage.Error);
Environment.Exit(-1);
return;
}
}
}
}
if (CheckMutex())
{
MessageBox.Show("이 프로그램은 이미 시작되었습니다.", "중복 실행", MessageBoxButton.OK, MessageBoxImage.Information);
Environment.Exit(-1);
return;
}
#if DEBUG
string path = System.IO.Path.GetFullPath(@"..\..\") + "_POE_Data\\";
#else
string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
path = path.Remove(path.Length - 4) + "\\";
#endif
Application.Current.Properties["DataPath"] = path;
Application.Current.Properties["IsAdministrator"] = IsAdministrator();
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
Application.Current.Properties["FileVersion"] = fvi.FileVersion;
mLogFilePath = Assembly.GetExecutingAssembly().Location;
mLogFilePath = mLogFilePath.Remove(mLogFilePath.Length - 4) + ".log";
if (File.Exists(mLogFilePath)) File.Delete(mLogFilePath);
Application.Current.DispatcherUnhandledException += AppDispatcherUnhandledException;
Uri uri = new Uri("pack://application:,,,/PoeTradeSearch;component/Icon1.ico");
using (Stream iconStream = Application.GetResourceStream(uri).Stream)
{
System.Windows.Forms.ContextMenu TrayCM = new System.Windows.Forms.ContextMenu();
TrayCM.MenuItems.Add(new System.Windows.Forms.MenuItem() { Text = "설정", Tag = 1 });
TrayCM.MenuItems.Add(new System.Windows.Forms.MenuItem() { Text = "-" });
TrayCM.MenuItems.Add(new System.Windows.Forms.MenuItem() { Text = "재시작", Tag = 2 });
TrayCM.MenuItems.Add(new System.Windows.Forms.MenuItem() { Text = "업데이트", Name = "this_update", Tag = 3 });
TrayCM.MenuItems.Add(new System.Windows.Forms.MenuItem() { Text = "-" });
TrayCM.MenuItems.Add(new System.Windows.Forms.MenuItem() { Text = "종료", Tag = 0 });
foreach (System.Windows.Forms.MenuItem item in TrayCM.MenuItems)
{
item.Click += TrayMenuClick;
}
mTrayIcon = new System.Windows.Forms.NotifyIcon
{
Icon = new Icon(iconStream),
ContextMenu = TrayCM,
Visible = true
};
/*
TrayIcon.MouseClick += (sender, args) =>
{
switch (args.Button)
{
case System.Windows.Forms.MouseButtons.Left:
break;
case System.Windows.Forms.MouseButtons.Right:
break;
}
};
*/
}
base.OnStartup(e);
}
protected override void OnExit(ExitEventArgs e)
{
Dispose();
base.OnExit(e);
}
}
}