Debian Codegraph @

src/win32/winmain.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2001 Erik Latoshek [forester] ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #ifdef WIN32_GUI
20
21 #pragma warning(disable : 4047)
22
23 #include "common/setup_before.h"
24 #include "winmain.h"
25
26 #include <cstdio>
27 #include <cstring>
28 #include <cwchar>
29 #include <string>
30 #include <vector>
31
32 #include <windows.h>
33
34 #include <windowsx.h>
35 #include <winuser.h>
36 #include <process.h>
37 #include <richedit.h>
38 #include <commctrl.h>
39 #include <time.h>
40
41 #include "common/addr.h"
42 #include "common/eventlog.h"
43 #include "common/list.h"
44 #include "common/version.h"
45 #include "common/gui_printf.h"
46
47 #include "bnetd/connection.h"
48 #include "bnetd/account.h"
49 #include "bnetd/account_wrap.h"
50 #include "bnetd/ipban.h"
51 #include "bnetd/message.h"
52 #include "bnetd/server.h"
53 #include "bnetd/cmdline.h"
54 #include "resource.h"
55 #include "console_output.h"
56
57 #include "common/setup_after.h"
58
59 #define WM_SHELLNOTIFY (WM_USER+1)
60
61 extern int app_main(int argc, char **argv); /* bnetd main function in bnetd/main.c */
62
63 namespace pvpgn
64 {
65
66 extern HWND ghwndConsole;
67
68 namespace bnetd
69 {
70
71 extern int server_main(int argc, char *argv[]);
72
73 static void guiThread(void*);
74 static void guiAddText(const char *, COLORREF);
75 static void guiAddText_user(const char *, COLORREF);
76 static void guiDEAD(const std::wstring& msg);
77 static void guiMoveWindow(HWND, RECT*);
78 static void guiClearLogWindow();
79 static void guiKillTrayIcon();
80
81 long PASCAL guiWndProc(HWND, UINT, WPARAM, LPARAM);
82 static void guiOnCommand(HWND, int, HWND, UINT);
83 static void guiOnMenuSelect(HWND, HMENU, int, HMENU, UINT);
84 static int guiOnShellNotify(int, int);
85 static BOOL guiOnCreate(HWND, LPCREATESTRUCT);
86 static void guiOnClose(HWND);
87 static void guiOnSize(HWND, UINT, int, int);
88 static void guiOnPaint(HWND);
89 static void guiOnCaptureChanged(HWND);
90 static BOOL guiOnSetCursor(HWND, HWND, UINT, UINT);
91 static void guiOnMouseMove(HWND, int, int, UINT);
92 static void guiOnLButtonDown(HWND, BOOL, int, int, UINT);
93 static void guiOnLButtonUp(HWND, int, int, UINT);
94 static void guiOnServerConfig();
95 static void guiOnAbout(HWND);
96 static void guiOnUpdates();
97 static void guiOnAnnounce(HWND);
98 static void guiOnUserStatusChange(HWND);
99
100 BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam);
101 BOOL CALLBACK AnnDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam);
102 BOOL CALLBACK KickDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam);
103
104 #define MODE_HDIVIDE 1
105 #define MODE_VDIVIDE 1
106
107 struct gui_struc {
108 HWND hwnd;
109 HMENU hmenuTray;
110 HWND hwndUsers;
111 HWND hwndUserCount;
112 HWND hwndUserEditButton;
113 HWND hwndTree;
114 int y_ratio;
115 int x_ratio;
116 HANDLE event_ready;
117 BOOL main_finished;
118 int mode;
119 char szDefaultStatus[128];
120 RECT rectHDivider,
121 rectVDivider,
122 rectConsole,
123 rectUsers,
124 rectConsoleEdge,
125 rectUsersEdge;
126 WPARAM wParam;
127 LPARAM lParam;
128 };
129
130 static struct gui_struc gui;
131
132 char selected_item[255] = {};
133
134 int fprintf(FILE *stream, const char *format, ...)
135 {
136 va_list args;
137 va_start(args, format);
138 if (stream == stderr || stream == stdout)
139 {
140 char buf[1024] = {};
141 std::vsnprintf(buf, sizeof buf, format, args);
142 gui_lvprintf(eventlog_level_error, "{}", buf);
143 return 1;
144 }
145 else
146 return vfprintf(stream, format, args);
147 }
148
149 static void guiThread(void *param)
150 {
151 HMODULE hRichEd = LoadLibraryW(L"RichEd20.dll");
152 if (hRichEd == nullptr)
153 guiDEAD(L"Could not load RichEd20.dll");
154
155 WNDCLASSEXW wc = {};
156 wc.cbSize = sizeof(WNDCLASSEXW);
157 wc.style = CS_HREDRAW | CS_VREDRAW;
158 wc.lpfnWndProc = static_cast<WNDPROC>(guiWndProc);
159 wc.cbClsExtra = 0;
160 wc.cbWndExtra = 0;
161 wc.hInstance = static_cast<HINSTANCE>(param);
162 wc.hIcon = LoadIconW(wc.hInstance, MAKEINTRESOURCEW(IDI_ICON1));
163 wc.hCursor = LoadCursorW(nullptr, IDC_ARROW);
164 wc.hbrBackground = nullptr;
165 wc.lpszMenuName = MAKEINTRESOURCEW(IDR_MENU);
166 wc.lpszClassName = L"BnetdWndClass";
167 wc.hIconSm = nullptr;
168
169 if (!RegisterClassExW(&wc))
170 {
171 FreeLibrary(hRichEd);
172 guiDEAD(L"cant register WNDCLASS");
173 }
174
175 gui.hwnd = CreateWindowExW(
176 0,
177 wc.lpszClassName,
178 L"Player -vs- Player Gaming Network Server",
179 WS_OVERLAPPEDWINDOW,
180 CW_USEDEFAULT,
181 CW_USEDEFAULT,
182 CW_USEDEFAULT,
183 CW_USEDEFAULT,
184 nullptr,
185 nullptr,
186 static_cast<HINSTANCE>(param),
187 nullptr);
188
189 if (!gui.hwnd)
190 {
191 FreeLibrary(hRichEd);
192 guiDEAD(L"cant create window");
193 }
194
195 ShowWindow(gui.hwnd, SW_SHOW);
196 SetEvent(gui.event_ready);
197
198 MSG msg = {};
199 while (GetMessageW(&msg, nullptr, 0, 0))
200 {
201 TranslateMessage(&msg);
202 DispatchMessageW(&msg);
203 }
204
205 FreeLibrary(hRichEd);
206 }
207
208 long PASCAL guiWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
209 {
210 gui.wParam = wParam;
211 gui.lParam = lParam;
212
213 switch (message)
214 {
215 HANDLE_MSG(hwnd, WM_CREATE, guiOnCreate);
216 HANDLE_MSG(hwnd, WM_COMMAND, guiOnCommand);
217 HANDLE_MSG(hwnd, WM_MENUSELECT, guiOnMenuSelect);
218 HANDLE_MSG(hwnd, WM_SIZE, guiOnSize);
219 HANDLE_MSG(hwnd, WM_CLOSE, guiOnClose);
220 HANDLE_MSG(hwnd, WM_PAINT, guiOnPaint);
221 HANDLE_MSG(hwnd, WM_SETCURSOR, guiOnSetCursor);
222 HANDLE_MSG(hwnd, WM_LBUTTONDOWN, guiOnLButtonDown);
223 HANDLE_MSG(hwnd, WM_LBUTTONUP, guiOnLButtonUp);
224 HANDLE_MSG(hwnd, WM_MOUSEMOVE, guiOnMouseMove);
225
226 case WM_CAPTURECHANGED:
227 guiOnCaptureChanged(reinterpret_cast<HWND>(lParam));
228 return 0;
229 case WM_SHELLNOTIFY:
230 return guiOnShellNotify(wParam, lParam);
231 }
232
233 return DefWindowProcW(hwnd, message, wParam, lParam);
234 }
235
236 static BOOL guiOnCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct)
237 {
238 ghwndConsole = CreateWindowExW(
239 0,
240 RICHEDIT_CLASS,
241 nullptr,
242 WS_CHILD | WS_VISIBLE | ES_READONLY | ES_MULTILINE | WS_VSCROLL | WS_HSCROLL | ES_NOHIDESEL,
243 0, 0,
244 0, 0,
245 hwnd,
246 0,
247 0,
248 nullptr);
249
250 if (!ghwndConsole)
251 return FALSE;
252
253 gui.hwndUsers = CreateWindowExW(
254 WS_EX_CLIENTEDGE,
255 L"LISTBOX",
256 nullptr,
257 WS_CHILD | WS_VISIBLE | LBS_STANDARD | LBS_NOINTEGRALHEIGHT,
258 0, 0,
259 0, 0,
260 hwnd,
261 0,
262 0,
263 nullptr);
264
265 if (!gui.hwndUsers)
266 return FALSE;
267
268 //amadeo: temp. button for useredit until rightcklick is working....
269 gui.hwndUserEditButton = CreateWindowExW(
270 0L,
271 L"button",
272 L"Edit User Status",
273 WS_CHILD | WS_VISIBLE | ES_LEFT,
274 0, 0,
275 0, 0,
276 hwnd,
277 reinterpret_cast<HMENU>(881),
278 0,
279 nullptr);
280
281 if (!gui.hwndUserEditButton)
282 return FALSE;
283
284 gui.hwndUserCount = CreateWindowExW(
285 WS_EX_CLIENTEDGE,
286 L"edit",
287 L" 0 user(s) online:",
288 WS_CHILD | WS_VISIBLE | ES_CENTER | ES_READONLY,
289 0, 0,
290 0, 0,
291 hwnd,
292 0,
293 0,
294 nullptr);
295
296 if (!gui.hwndUserCount)
297 return FALSE;
298
299 SendMessageW(gui.hwndUserCount, WM_SETFONT, reinterpret_cast<WPARAM>(GetStockObject(DEFAULT_GUI_FONT)), 0);
300 SendMessageW(gui.hwndUsers, WM_SETFONT, reinterpret_cast<WPARAM>(GetStockObject(DEFAULT_GUI_FONT)), 0);
301 SendMessageW(gui.hwndUserEditButton, WM_SETFONT, reinterpret_cast<WPARAM>(GetStockObject(DEFAULT_GUI_FONT)), 0);
302 BringWindowToTop(gui.hwndUsers);
303 std::snprintf(gui.szDefaultStatus, sizeof(gui.szDefaultStatus), "%s", "Void");
304
305 gui.y_ratio = (100 << 10) / 100;
306 gui.x_ratio = (0 << 10) / 100;
307
308 return TRUE;
309 }
310
311 static void guiOnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
312 {
313 switch (id)
314 {
315 case IDM_EXIT:
316 guiOnClose(hwnd);
317 break;
318 case IDM_SAVE:
319 server_save_wraper();
320 break;
321 case IDM_RESTART_LUA:
322 server_restart_wraper(restart_mode_lua);
323 break;
324 case IDM_RESTART:
325 server_restart_wraper(restart_mode_all);
326 break;
327 case IDM_SHUTDOWN:
328 server_quit_wraper();
329 break;
330 case IDM_CLEAR:
331 guiClearLogWindow();
332 break;
333 case IDM_RESTORE:
334 guiOnShellNotify(IDI_TRAY, WM_LBUTTONDBLCLK);
335 break;
336 case IDM_USERLIST:
337 guiOnUpdateUserList();
338 break;
339 case IDM_SERVERCONFIG:
340 guiOnServerConfig();
341 break;
342 case IDM_ABOUT:
343 guiOnAbout(hwnd);
344 break;
345 case ID_HELP_CHECKFORUPDATES:
346 guiOnUpdates();
347 break;
348 case IDM_ANN:
349 guiOnAnnounce(hwnd);
350 break;
351 case ID_USERACTIONS_KICKUSER:
352 case 881:
353 guiOnUserStatusChange(hwnd);
354 break;
355 }
356 }
357
358 static void guiOnMenuSelect(HWND hwnd, HMENU hmenu, int item, HMENU hmenuPopup, UINT flags)
359 {
360 // char str[256];
361 //by greini
362 /*if( item == 0 || flags == -1)
363 SetWindowText( gui.hwndStatus, gui.szDefaultStatus );
364 else
365 {
366 LoadString( GetWindowInstance(hwnd), item, str, sizeof(str) );
367 if( str[0] ) SetWindowText( gui.hwndStatus, str );
368 }*/
369 }
370
371 static int guiOnShellNotify(int uID, int uMessage)
372 {
373 if (uID == IDI_TRAY)
374 {
375 if (uMessage == WM_LBUTTONDBLCLK)
376 {
377 if (!IsWindowVisible(gui.hwnd))
378 ShowWindow(gui.hwnd, SW_RESTORE);
379
380 SetForegroundWindow(gui.hwnd);
381 }
382 else if (uMessage == WM_RBUTTONDOWN)
383 {
384 POINT cp;
385 GetCursorPos(&cp);
386 SetForegroundWindow(gui.hwnd);
387 TrackPopupMenu(gui.hmenuTray, TPM_LEFTALIGN | TPM_LEFTBUTTON, cp.x, cp.y, 0, gui.hwnd, NULL);
388 }
389 }
390
391 return 0;
392 }
393
394 static void guiOnPaint(HWND hwnd)
395 {
396 PAINTSTRUCT ps;
397 HDC dc;
398
399 dc = BeginPaint(hwnd, &ps);
400
401 DrawEdge(dc, &gui.rectHDivider, BDR_SUNKEN, BF_MIDDLE);
402 DrawEdge(dc, &gui.rectConsoleEdge, BDR_SUNKEN, BF_RECT);
403 DrawEdge(dc, &gui.rectUsersEdge, BDR_SUNKEN, BF_RECT);
404
405 EndPaint(hwnd, &ps);
406
407 UpdateWindow(ghwndConsole);
408 UpdateWindow(gui.hwndUsers);
409 UpdateWindow(gui.hwndUserCount);
410 UpdateWindow(gui.hwndUserEditButton);
411 }
412
413 static void guiOnClose(HWND hwnd)
414 {
415 guiKillTrayIcon();
416 if (!gui.main_finished)
417 {
418 eventlog(eventlog_level_debug, __FUNCTION__, "GUI wants server dead...");
419 std::exit(0);
420 }
421 else
422 {
423 eventlog(eventlog_level_debug, __FUNCTION__, "GUI wants to exit...");
424 eventlog_close();
425 SetEvent(gui.event_ready);
426 }
427 }
428
429 static void guiOnSize(HWND hwnd, UINT state, int cx, int cy)
430 {
431 if (state == SIZE_MINIMIZED)
432 {
433 NOTIFYICONDATAW dta = {};
434
435 dta.cbSize = sizeof(NOTIFYICONDATAW);
436 dta.hWnd = hwnd;
437 dta.uID = IDI_TRAY;
438 dta.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
439 dta.uCallbackMessage = WM_SHELLNOTIFY;
440 dta.hIcon = LoadIconW(GetWindowInstance(hwnd), MAKEINTRESOURCE(IDI_ICON1));
441 std::swprintf(dta.szTip, sizeof dta.szTip / sizeof *dta.szTip, L"%ls %ls", PVPGN_SOFTWAREW, PVPGN_VERSIONW);
442 Shell_NotifyIconW(NIM_ADD, &dta);
443 ShowWindow(hwnd, SW_HIDE);
444 return;
445 }
446
447 if (state == SIZE_RESTORED)
448 {
449 NOTIFYICONDATAW dta = {};
450
451 dta.hWnd = hwnd;
452 dta.uID = IDI_TRAY;
453 Shell_NotifyIconW(NIM_DELETE, &dta);
454 }
455
456 int cy_status = 0;
457 int cy_edge = GetSystemMetrics(SM_CYEDGE);
458 int cx_edge = GetSystemMetrics(SM_CXEDGE);
459 int cy_frame = (cy_edge << 1) + GetSystemMetrics(SM_CYBORDER) + 1;
460 int cy_console = ((cy - cy_status - cy_frame - cy_edge * 2) * gui.y_ratio) >> 10;
461 gui.rectConsoleEdge.left = 0;
462 gui.rectConsoleEdge.right = cx - 140;
463 gui.rectConsoleEdge.top = 0;
464 gui.rectConsoleEdge.bottom = cy - cy_status;
465 gui.rectConsole.left = cx_edge;
466 gui.rectConsole.right = cx - 140 - cx_edge;
467 gui.rectConsole.top = cy_edge;
468 gui.rectConsole.bottom = cy - cy_status;
469 gui.rectUsersEdge.left = cx - 140;
470 gui.rectUsersEdge.top = 18;
471 gui.rectUsersEdge.right = cx;
472 gui.rectUsersEdge.bottom = cy - cy_status - 10;
473 gui.rectUsers.left = cx - 138;
474 gui.rectUsers.right = cx;
475 gui.rectUsers.top = 18 + cy_edge;
476 gui.rectUsers.bottom = cy - cy_status - 20;
477 guiMoveWindow(ghwndConsole, &gui.rectConsole);
478 guiMoveWindow(gui.hwndUsers, &gui.rectUsers);
479 MoveWindow(gui.hwndUserCount, cx - 140, 0, 140, 18, TRUE);
480 MoveWindow(gui.hwndUserEditButton, cx - 140, cy - cy_status - 20, 140, 20, TRUE);
481 }
482
483 static BOOL guiOnSetCursor(HWND hwnd, HWND hwndCursor, UINT codeHitTest, UINT msg)
484 {
485 POINT p;
486
487 if (hwnd == hwndCursor && codeHitTest == HTCLIENT)
488 {
489 GetCursorPos(&p);
490 ScreenToClient(hwnd, &p);
491 if (PtInRect(&gui.rectHDivider, p))
492 SetCursor(LoadCursorW(0, IDC_SIZENS));
493
494 return TRUE;
495 }
496
497 return FORWARD_WM_SETCURSOR(hwnd, hwndCursor, codeHitTest, msg, DefWindowProcW);
498 }
499
500 static void guiOnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
501 {
502 POINT p;
503
504 p.x = x;
505 p.y = y;
506
507 if (PtInRect(&gui.rectHDivider, p))
508 {
509 SetCapture(hwnd);
510 gui.mode |= MODE_HDIVIDE;
511 }
512 }
513
514 static void guiOnLButtonUp(HWND hwnd, int x, int y, UINT keyFlags)
515 {
516 ReleaseCapture();
517 gui.mode &= ~(MODE_HDIVIDE | MODE_VDIVIDE);
518 }
519
520 static void guiOnCaptureChanged(HWND hwndNewCapture)
521 {
522 gui.mode &= ~(MODE_HDIVIDE | MODE_VDIVIDE);
523 }
524
525 static void guiOnMouseMove(HWND hwnd, int x, int y, UINT keyFlags)
526 {
527 int offset, cy_console, cy_users;
528 RECT r;
529
530 if (gui.mode & MODE_HDIVIDE)
531 {
532 offset = y - gui.rectHDivider.top;
533 if (!offset) return;
534 cy_console = gui.rectConsole.bottom - gui.rectConsole.top;
535 cy_users = gui.rectUsers.bottom - gui.rectUsers.top;
536
537 if (cy_console + offset <= 0)
538 offset = -cy_console;
539 else if (cy_users - offset <= 0)
540 offset = cy_users;
541
542 cy_console += offset;
543 cy_users -= offset;
544 if (cy_console + cy_users == 0) return;
545 gui.y_ratio = (cy_console << 10) / (cy_console + cy_users);
546 GetClientRect(hwnd, &r);
547 guiOnSize(hwnd, 0, r.right, r.bottom);
548 InvalidateRect(hwnd, NULL, FALSE);
549 }
550 }
551
552 static void guiOnUpdates()
553 {
554 ShellExecuteW(nullptr, L"open", L"http://pvpgn.pro/", nullptr, nullptr, SW_SHOW);
555 }
556
557 static void guiOnAnnounce(HWND hwnd)
558 {
559 DialogBoxW(GetModuleHandleW(nullptr), MAKEINTRESOURCEW(IDD_ANN), hwnd, static_cast<DLGPROC>(AnnDlgProc));
560 }
561
562 static void guiOnUserStatusChange(HWND hwnd)
563 {
564 int index;
565 index = SendMessageW(gui.hwndUsers, LB_GETCURSEL, 0, 0);
566 SendMessageA(gui.hwndUsers, LB_GETTEXT, index, reinterpret_cast<LPARAM>(selected_item));
567 DialogBoxW(GetModuleHandleW(nullptr), MAKEINTRESOURCEW(IDD_KICKUSER), hwnd, static_cast<DLGPROC>(KickDlgProc));
568 SendMessageW(gui.hwndUsers, LB_SETCURSEL, -1, 0);
569 }
570
571 static void guiOnAbout(HWND hwnd)
572 {
573 DialogBoxW(GetModuleHandleW(nullptr), MAKEINTRESOURCEW(IDD_ABOUT), hwnd, static_cast<DLGPROC>(AboutDlgProc));
574 }
575
576 static void guiOnServerConfig()
577 {
578 ShellExecuteW(nullptr, nullptr, L"conf\\bnetd.conf", nullptr, nullptr, SW_SHOW);
579 }
580
581 extern void guiOnUpdateUserList()
582 {
583 t_connection * c;
584 t_elem const * curr;
585 t_account * acc;
586
587 SendMessageW(gui.hwndUsers, LB_RESETCONTENT, 0, 0);
588
589 LIST_TRAVERSE_CONST(connlist(), curr)
590 {
591 if (!(c = (t_connection *)elem_get_data(curr)))
592 continue;
593 if (!(acc = conn_get_account(c)))
594 continue;
595
596 SendMessageA(gui.hwndUsers, LB_ADDSTRING, 0, reinterpret_cast<LPARAM>(account_get_name(acc)));
597 }
598
599 std::wstring user_count(std::to_wstring(connlist_login_get_length()) + L" user(s) online:");
600 SendMessageW(gui.hwndUserCount, WM_SETTEXT, 0, reinterpret_cast<LPARAM>(user_count.c_str()));
601 }
602
603 static void guiAddText(const char *str, COLORREF clr)
604 {
605 int text_length = SendMessageW(ghwndConsole, WM_GETTEXTLENGTH, 0, 0);
606
607 if (text_length > 30000)
608 {
609 CHARRANGE ds = {};
610 ds.cpMin = 0;
611 ds.cpMax = text_length - 30000;
612 SendMessageW(ghwndConsole, EM_EXSETSEL, 0, reinterpret_cast<LPARAM>(&ds));
613 SendMessageW(ghwndConsole, EM_REPLACESEL, FALSE, 0);
614 }
615
616 CHARRANGE cr = {};
617 cr.cpMin = text_length;
618 cr.cpMax = text_length;
619 SendMessageW(ghwndConsole, EM_EXSETSEL, 0, reinterpret_cast<LPARAM>(&cr));
620
621 CHARFORMATW fmt = {};
622 fmt.cbSize = sizeof(CHARFORMATW);
623 fmt.dwMask = CFM_COLOR | CFM_FACE | CFM_SIZE | CFM_BOLD | CFM_ITALIC | CFM_STRIKEOUT | CFM_UNDERLINE;
624 fmt.yHeight = 160;
625 fmt.dwEffects = 0;
626 fmt.crTextColor = clr;
627 std::swprintf(fmt.szFaceName, sizeof fmt.szFaceName / sizeof *fmt.szFaceName, L"%ls", L"Courier New");
628
629 SendMessageW(ghwndConsole, EM_SETCHARFORMAT, SCF_SELECTION, reinterpret_cast<LPARAM>(&fmt));
630 SendMessageA(ghwndConsole, EM_REPLACESEL, FALSE, reinterpret_cast<LPARAM>(str));
631 }
632
633 static void guiDEAD(const std::wstring& message)
634 {
635 wchar_t* error_message = nullptr;
636
637 FormatMessageW(
638 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
639 nullptr,
640 GetLastError(),
641 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
642 reinterpret_cast<LPWSTR>(&error_message),
643 0,
644 nullptr);
645
646 wchar_t* nl = std::wcschr(error_message, '\r');
647 if (nl)
648 *nl = '0';
649
650 MessageBoxW(0, std::wstring(message + L"\nGetLastError() = " + error_message).c_str(), L"guiDEAD", MB_ICONSTOP | MB_OK);
651
652 LocalFree(error_message);
653
654 std::exit(1);
655 }
656
657 static void guiMoveWindow(HWND hwnd, RECT* r)
658 {
659 MoveWindow(hwnd, r->left, r->top, r->right - r->left, r->bottom - r->top, TRUE);
660 }
661
662 static void guiClearLogWindow()
663 {
664 SendMessageW(ghwndConsole, WM_SETTEXT, 0, 0);
665 }
666
667 static void guiKillTrayIcon()
668 {
669 NOTIFYICONDATAW dta = {};
670
671 dta.cbSize = sizeof(NOTIFYICONDATAW);
672 dta.hWnd = gui.hwnd;
673 dta.uID = IDI_TRAY;
674 dta.uFlags = 0;
675 Shell_NotifyIconW(NIM_DELETE, &dta);
676 }
677
678 BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
679 {
680 switch (Message)
681 {
682 case WM_INITDIALOG:
683 return TRUE;
684 case WM_COMMAND:
685 switch (LOWORD(wParam))
686 {
687 case IDOK:
688 EndDialog(hwnd, IDOK);
689 break;
690 }
691 break;
692 default:
693 return FALSE;
694 }
695
696 return TRUE;
697 }
698
699 BOOL CALLBACK AnnDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
700 {
701
702 switch (Message)
703 {
704 case WM_INITDIALOG:
705 return TRUE;
706 case WM_COMMAND:
707 switch (LOWORD(wParam))
708 {
709 case IDOK:
710 {
711 int len = GetWindowTextLengthA(GetDlgItem(hwnd, IDC_EDIT1));
712
713 if (len > 0)
714 {
715 std::string buff(len, '\0');
716 GetDlgItemTextA(hwnd, IDC_EDIT1, &buff[0], buff.capacity());
717
718 t_message* const message = message_create(message_type_error, nullptr, buff.c_str());
719 if (message)
720 {
721 message_send_all(message);
722 message_destroy(message);
723 }
724
725 SetDlgItemTextW(hwnd, IDC_EDIT1, L"");
726 }
727 else
728 {
729 MessageBoxW(hwnd, L"You didn't enter anything!", L"Warning", MB_OK);
730 }
731 break;
732 }
733 }
734 break;
735 case WM_CLOSE:
736 EndDialog(hwnd, IDOK);
737 break;
738 default:
739 return FALSE;
740 }
741 return TRUE;
742 }
743
744 BOOL CALLBACK KickDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
745 {
746 switch (Message)
747 {
748 case WM_INITDIALOG:
749 if (selected_item[0] != 0)
750 {
751 SetDlgItemTextA(hwnd, IDC_EDITKICK, selected_item);
752 }
753
754 return TRUE;
755 case WM_COMMAND:
756 switch (LOWORD(wParam))
757 {
758 case IDC_KICK_EXECUTE:
759 {
760 GetDlgItemTextA(hwnd, IDC_EDITKICK, selected_item, sizeof selected_item);
761
762 t_connection* const conngui = connlist_find_connection_by_accountname(selected_item);
763 t_account* const accountgui = accountlist_find_account(selected_item);
764
765 if (conngui == nullptr)
766 {
767 MessageBoxA(hwnd, std::string(std::string(selected_item) + " could not be found in Userlist!").c_str(), "Error", MB_OK);
768 }
769 else
770 {
771 HWND hButton = GetDlgItem(hwnd, IDC_CHECKBAN);
772 HWND hButton1 = GetDlgItem(hwnd, IDC_CHECKKICK);
773 HWND hButton2 = GetDlgItem(hwnd, IDC_CHECKADMIN);
774 HWND hButton3 = GetDlgItem(hwnd, IDC_CHECKMOD);
775 HWND hButton4 = GetDlgItem(hwnd, IDC_CHECKANN);
776
777 BOOL messageq = FALSE;
778 BOOL kickq = FALSE;
779
780 if (SendMessageW(hButton2, BM_GETCHECK, 0, 0) == BST_CHECKED)
781 {
782 account_set_admin(accountgui);
783 account_set_command_groups(accountgui, 255);
784 messageq = TRUE;
785 }
786
787 if (SendMessageW(hButton3, BM_GETCHECK, 0, 0) == BST_CHECKED)
788 {
789 account_set_auth_operator(accountgui, nullptr, 1);
790 messageq = TRUE;
791 }
792
793 if (SendMessageW(hButton4, BM_GETCHECK, 0, 0) == BST_CHECKED)
794 {
795 account_set_strattr(accountgui, "BNET\\auth\\announce", "true");
796 messageq = TRUE;
797 }
798
799 if (SendMessageW(hButton, BM_GETCHECK, 0, 0) == BST_CHECKED)
800 {
801 char temp[64];
802 std::snprintf(temp, sizeof temp, "%s", addr_num_to_addr_str(conn_get_addr(conngui), 0));
803
804 char ipadr[110];
805 unsigned int i_GUI;
806
807 for (i_GUI = 0; temp[i_GUI] != ':'; i_GUI++)
808 ipadr[i_GUI] = temp[i_GUI];
809
810 ipadr[i_GUI] = 0;
811
812 std::strcpy(temp, " a ");
813 std::strcat(temp, ipadr);
814 handle_ipban_command(nullptr, temp);
815
816 temp[0] = 0;
817 std::strcpy(temp, " has been added to IpBanList");
818 std::strcat(ipadr, temp);
819 if (messageq == TRUE)
820 {
821 std::strcat(ipadr, " and UserStatus changed");
822 MessageBoxA(hwnd, ipadr, "ipBan & StatusChange", MB_OK);
823 messageq = FALSE;
824 kickq = FALSE;
825 }
826 else
827 MessageBoxA(hwnd, ipadr, "ipBan", MB_OK);
828 }
829
830 if (SendMessageW(hButton1, BM_GETCHECK, 0, 0) == BST_CHECKED)
831 {
832 conn_set_state(conngui, conn_state_destroy);
833 kickq = TRUE;
834 }
835
836 if ((messageq == TRUE) && (kickq == TRUE))
837 {
838 std::strcat(selected_item, "has been kicked and Status has changed");
839 MessageBoxA(hwnd, selected_item, "UserKick & StatusChange", MB_OK);
840 }
841
842 if ((kickq == TRUE) && (messageq == FALSE))
843 {
844 std::strcat(selected_item, " has been kicked from the server");
845 MessageBoxA(hwnd, selected_item, "UserKick", MB_OK);
846 }
847
848 if ((kickq == FALSE) && (messageq == TRUE))
849 {
850 std::strcat(selected_item, "'s Status has been changed");
851 MessageBoxA(hwnd, selected_item, "StatusChange", MB_OK);
852 }
853
854 selected_item[0] = 0;
855 }
856 break;
857 }
858 }
859 break;
860 case WM_CLOSE:
861 EndDialog(hwnd, IDC_EDITKICK);
862 break;
863 default:
864 return FALSE;
865 }
866 return TRUE;
867 }
868
869 }
870
871 }
872
873
874 using namespace pvpgn;
875 using namespace pvpgn::bnetd;
876
877 int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE reserved, LPSTR lpCmdLine, int nCmdShow)
878 {
879 Console console;
880
881 if (cmdline_load(__argc, __argv) != 1)
882 {
883 return -1;
884 }
885
886 if (cmdline_get_console())
887 {
888 console.RedirectIOToConsole();
889 return app_main(__argc, __argv);
890 }
891
892 pvpgn::bnetd::gui.main_finished = FALSE;
893 pvpgn::bnetd::gui.event_ready = CreateEventW(nullptr, FALSE, FALSE, nullptr);
894 _beginthread(pvpgn::bnetd::guiThread, 0, (void*)hInstance);
895 WaitForSingleObject(pvpgn::bnetd::gui.event_ready, INFINITE);
896
897 auto result = app_main(__argc, __argv);
898
899 pvpgn::bnetd::gui.main_finished = TRUE;
900 eventlog(pvpgn::eventlog_level_debug, __FUNCTION__, "server exited ( return : {} )", result);
901 WaitForSingleObject(pvpgn::bnetd::gui.event_ready, INFINITE);
902
903 return 0;
904 }
905
906 #endif
907