| 1 |
sysadm |
1.1 |
/*
|
| 2 |
|
|
* Copyright (C) 2004 CreepLord (creeplord@pvpgn.org)
|
| 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 |
|
|
#include "common/setup_before.h"
|
| 22 |
|
|
#include <windows.h>
|
| 23 |
|
|
#include <windowsx.h>
|
| 24 |
|
|
#include <richedit.h>
|
| 25 |
|
|
#include "d2cs_resource.h"
|
| 26 |
|
|
#include "common/eventlog.h"
|
| 27 |
|
|
#include "d2cs/version.h"
|
| 28 |
|
|
#include "common/setup_after.h"
|
| 29 |
|
|
|
| 30 |
|
|
#define WM_SHELLNOTIFY (WM_USER+1)
|
| 31 |
|
|
|
| 32 |
|
|
extern int server_main(int argc, char *argv[]); /* d2cs main function in d2cs/main.c */
|
| 33 |
|
|
|
| 34 |
|
|
static void d2cs(void * dummy); /* thread function for d2cs */
|
| 35 |
|
|
|
| 36 |
|
|
static void guiAddText(const char *str, COLORREF clr);
|
| 37 |
|
|
static void KillTrayIcon(HWND hwnd);
|
| 38 |
|
|
static int OnShellNotify(HWND hwnd, int uID, int uMessage);
|
| 39 |
|
|
static void OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify);
|
| 40 |
|
|
static BOOL OnCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct);
|
| 41 |
|
|
static void OnClose(HWND hwnd);
|
| 42 |
|
|
static void OnSize(HWND hwnd, UINT state, int cx, int cy);
|
| 43 |
|
|
|
| 44 |
|
|
LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
|
| 45 |
|
|
BOOL CALLBACK DlgProcAbout(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
|
| 46 |
|
|
|
| 47 |
|
|
HWND ghwndConsole; /* hwnd for eventlog output */
|
| 48 |
|
|
|
| 49 |
|
|
static int gui_run = TRUE; /* default state: run gui */
|
| 50 |
|
|
static int d2cs_run = TRUE; /* default state: run d2cs */
|
| 51 |
|
|
static int d2cs_running = FALSE; /* currect state: not running */
|
| 52 |
|
|
|
| 53 |
|
|
static void KillTrayIcon(HWND hwnd)
|
| 54 |
|
|
{
|
| 55 |
|
|
NOTIFYICONDATA dta;
|
| 56 |
|
|
|
| 57 |
|
|
dta.cbSize = sizeof(NOTIFYICONDATA);
|
| 58 |
|
|
dta.hWnd = hwnd;
|
| 59 |
|
|
dta.uID = ID_TRAY;
|
| 60 |
|
|
dta.uFlags = 0;
|
| 61 |
|
|
Shell_NotifyIcon(NIM_DELETE, &dta);
|
| 62 |
|
|
}
|
| 63 |
|
|
|
| 64 |
|
|
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
|
| 65 |
|
|
LPSTR lpszCmdLine, int nCmdShow)
|
| 66 |
|
|
{
|
| 67 |
|
|
WNDCLASSEX wc;
|
| 68 |
|
|
HWND hwnd;
|
| 69 |
|
|
MSG msg;
|
| 70 |
|
|
|
| 71 |
|
|
/* if running as a service skip starting the GUI and go straight to starting d2cs */
|
| 72 |
|
|
if (__argc==2 && strcmp(__argv[1],"--service")==0)
|
| 73 |
|
|
{
|
| 74 |
|
|
Win32_ServiceRun();
|
| 75 |
|
|
return 1;
|
| 76 |
|
|
}
|
| 77 |
|
|
|
| 78 |
|
|
LoadLibrary("RichEd20.dll");
|
| 79 |
|
|
|
| 80 |
|
|
wc.cbSize = sizeof(WNDCLASSEX);
|
| 81 |
|
|
wc.style = 0;
|
| 82 |
|
|
wc.lpfnWndProc = WndProc;
|
| 83 |
|
|
wc.cbClsExtra = 0;
|
| 84 |
|
|
wc.cbWndExtra = sizeof(LPVOID);
|
| 85 |
|
|
wc.hInstance = hInstance;
|
| 86 |
|
|
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(ID_ICON1));
|
| 87 |
|
|
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
|
| 88 |
|
|
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
|
| 89 |
|
|
wc.lpszMenuName = MAKEINTRESOURCE(ID_MENU);
|
| 90 |
|
|
wc.lpszClassName = "BnetWndClass";
|
| 91 |
|
|
|
| 92 |
|
|
if(!RegisterClassEx( &wc ))
|
| 93 |
|
|
RegisterClass((LPWNDCLASS)&wc.style);
|
| 94 |
|
|
|
| 95 |
|
|
hwnd = CreateWindow(TEXT("BnetWndClass"),"Diablo II Character Server",
|
| 96 |
|
|
WS_OVERLAPPEDWINDOW,
|
| 97 |
|
|
CW_USEDEFAULT, CW_USEDEFAULT,
|
| 98 |
|
|
CW_USEDEFAULT, CW_USEDEFAULT,
|
| 99 |
|
|
NULL,
|
| 100 |
|
|
LoadMenu(hInstance, MAKEINTRESOURCE(ID_MENU)),
|
| 101 |
|
|
hInstance,NULL);
|
| 102 |
|
|
|
| 103 |
|
|
if(hwnd) {
|
| 104 |
|
|
ShowWindow(hwnd, nCmdShow);
|
| 105 |
|
|
UpdateWindow(hwnd);
|
| 106 |
|
|
}
|
| 107 |
|
|
|
| 108 |
|
|
while(GetMessage(&msg, NULL, 0, 0)) {
|
| 109 |
|
|
TranslateMessage( &msg );
|
| 110 |
|
|
DispatchMessage( &msg );
|
| 111 |
|
|
|
| 112 |
|
|
if(!d2cs_running && d2cs_run && gui_run) {
|
| 113 |
|
|
d2cs_running = TRUE;
|
| 114 |
|
|
_beginthread(d2cs, 0, NULL);
|
| 115 |
|
|
}
|
| 116 |
|
|
|
| 117 |
|
|
if(!gui_run && !d2cs_running) {
|
| 118 |
|
|
KillTrayIcon(hwnd);
|
| 119 |
|
|
exit(0);
|
| 120 |
|
|
}
|
| 121 |
|
|
}
|
| 122 |
|
|
return ((int) msg.wParam);
|
| 123 |
|
|
}
|
| 124 |
|
|
|
| 125 |
|
|
LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
| 126 |
|
|
{
|
| 127 |
|
|
switch(uMsg) {
|
| 128 |
|
|
HANDLE_MSG(hwnd, WM_CREATE, OnCreate);
|
| 129 |
|
|
HANDLE_MSG(hwnd, WM_SIZE, OnSize);
|
| 130 |
|
|
HANDLE_MSG(hwnd, WM_COMMAND, OnCommand);
|
| 131 |
|
|
HANDLE_MSG(hwnd, WM_CLOSE, OnClose);
|
| 132 |
|
|
case WM_SHELLNOTIFY:
|
| 133 |
|
|
return OnShellNotify(hwnd, wParam, lParam);
|
| 134 |
|
|
}
|
| 135 |
|
|
return DefWindowProc(hwnd, uMsg, wParam, lParam);
|
| 136 |
|
|
}
|
| 137 |
|
|
|
| 138 |
|
|
static BOOL OnCreate(HWND hwnd, LPCREATESTRUCT lpCreateStruct)
|
| 139 |
|
|
{
|
| 140 |
|
|
ghwndConsole = CreateWindowEx(WS_EX_CLIENTEDGE, RICHEDIT_CLASS, NULL,
|
| 141 |
|
|
WS_CHILD|WS_VISIBLE|ES_READONLY|ES_MULTILINE|
|
| 142 |
|
|
WS_VSCROLL|WS_HSCROLL|ES_NOHIDESEL,
|
| 143 |
|
|
0, 0,
|
| 144 |
|
|
0, 0, hwnd, 0,
|
| 145 |
|
|
0, NULL);
|
| 146 |
|
|
|
| 147 |
|
|
if(!ghwndConsole)
|
| 148 |
|
|
return FALSE;
|
| 149 |
|
|
|
| 150 |
|
|
return TRUE;
|
| 151 |
|
|
}
|
| 152 |
|
|
|
| 153 |
|
|
static void OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
|
| 154 |
|
|
{
|
| 155 |
|
|
switch (id) {
|
| 156 |
|
|
case ID_RESTORE:
|
| 157 |
|
|
OnShellNotify(hwnd, ID_TRAY, WM_LBUTTONDBLCLK);
|
| 158 |
|
|
break;
|
| 159 |
|
|
case ID_START_D2CS:
|
| 160 |
|
|
eventlog(eventlog_level_warn,__FUNCTION__,"Sending Start Signal to d2cs");
|
| 161 |
|
|
d2cs_run = TRUE;
|
| 162 |
|
|
break;
|
| 163 |
|
|
case ID_SHUTDOWN_D2CS:
|
| 164 |
|
|
eventlog(eventlog_level_warn,__FUNCTION__,"Sending Shutdown Signal to d2cs");
|
| 165 |
|
|
d2cs_run = FALSE;
|
| 166 |
|
|
signal_quit_wrapper();
|
| 167 |
|
|
break;
|
| 168 |
|
|
case ID_RESTART_D2CS:
|
| 169 |
|
|
eventlog(eventlog_level_warn,__FUNCTION__,"Sending Restart Signal To d2cs");
|
| 170 |
|
|
signal_quit_wrapper();
|
| 171 |
|
|
break;
|
| 172 |
|
|
case ID_EDITCONFIG_D2CS:
|
| 173 |
|
|
ShellExecute(NULL, "open", "notepad.exe", "conf\\d2cs.conf", NULL, SW_SHOW );
|
| 174 |
|
|
break;
|
| 175 |
|
|
case ID_LOADCONFIG_D2CS:
|
| 176 |
|
|
eventlog(eventlog_level_warn,__FUNCTION__,"Sending Reload Config Signal To d2cs");
|
| 177 |
|
|
signal_reload_config_wrapper();
|
| 178 |
|
|
break;
|
| 179 |
|
|
case ID_LADDER_LOAD:
|
| 180 |
|
|
eventlog(eventlog_level_warn,__FUNCTION__,"Sending Reload Ladder Signal To d2cs");
|
| 181 |
|
|
signal_load_ladder_wrapper();
|
| 182 |
|
|
break;
|
| 183 |
|
|
case ID_RESTART_D2GS:
|
| 184 |
|
|
eventlog(eventlog_level_warn,__FUNCTION__,"Sending Restart d2gs Signal To d2cs");
|
| 185 |
|
|
signal_restart_d2gs_wrapper();
|
| 186 |
|
|
break;
|
| 187 |
|
|
case ID_EXIT:
|
| 188 |
|
|
OnClose(hwnd);
|
| 189 |
|
|
break;
|
| 190 |
|
|
case ID_CLEAR:
|
| 191 |
|
|
SendMessage(ghwndConsole, WM_SETTEXT, 0, 0);
|
| 192 |
|
|
break;
|
| 193 |
|
|
case ID_ABOUT:
|
| 194 |
|
|
DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(ID_ABOUT_BOX), hwnd, (DLGPROC)DlgProcAbout);
|
| 195 |
|
|
break;
|
| 196 |
|
|
}
|
| 197 |
|
|
}
|
| 198 |
|
|
|
| 199 |
|
|
static int OnShellNotify(HWND hwnd, int uID, int uMessage)
|
| 200 |
|
|
{
|
| 201 |
|
|
if(uID == ID_TRAY) {
|
| 202 |
|
|
if(uMessage == WM_LBUTTONDBLCLK) {
|
| 203 |
|
|
if(!IsWindowVisible(hwnd))
|
| 204 |
|
|
ShowWindow(hwnd, SW_RESTORE);
|
| 205 |
|
|
|
| 206 |
|
|
SetForegroundWindow(hwnd);
|
| 207 |
|
|
}
|
| 208 |
|
|
}
|
| 209 |
|
|
return 0;
|
| 210 |
|
|
}
|
| 211 |
|
|
|
| 212 |
|
|
static void OnClose(HWND hwnd)
|
| 213 |
|
|
{
|
| 214 |
|
|
eventlog(eventlog_level_warn,__FUNCTION__,"Sending Exit Signal");
|
| 215 |
|
|
gui_run = FALSE;
|
| 216 |
|
|
d2cs_run = FALSE;
|
| 217 |
|
|
signal_exit_wrapper();
|
| 218 |
|
|
}
|
| 219 |
|
|
|
| 220 |
|
|
static void OnSize(HWND hwnd, UINT state, int cx, int cy)
|
| 221 |
|
|
{
|
| 222 |
|
|
NOTIFYICONDATA dta;
|
| 223 |
|
|
|
| 224 |
|
|
if( state == SIZE_MINIMIZED ) {
|
| 225 |
|
|
dta.cbSize = sizeof(NOTIFYICONDATA);
|
| 226 |
|
|
dta.hWnd = hwnd;
|
| 227 |
|
|
dta.uID = ID_TRAY;
|
| 228 |
|
|
dta.uFlags = NIF_ICON|NIF_MESSAGE|NIF_TIP;
|
| 229 |
|
|
dta.uCallbackMessage = WM_SHELLNOTIFY;
|
| 230 |
|
|
dta.hIcon = LoadIcon(GetWindowInstance(hwnd), MAKEINTRESOURCE(ID_ICON1));
|
| 231 |
|
|
strcpy(dta.szTip, "D2CS Version");
|
| 232 |
|
|
strcat(dta.szTip, " ");
|
| 233 |
|
|
strcat(dta.szTip, D2CS_VERSION_STRING);
|
| 234 |
|
|
|
| 235 |
|
|
Shell_NotifyIcon(NIM_ADD, &dta);
|
| 236 |
|
|
ShowWindow(hwnd, SW_HIDE);
|
| 237 |
|
|
return;
|
| 238 |
|
|
}
|
| 239 |
|
|
|
| 240 |
|
|
MoveWindow(ghwndConsole, 0, 0, cx, cy, TRUE);
|
| 241 |
|
|
}
|
| 242 |
|
|
|
| 243 |
|
|
BOOL CALLBACK DlgProcAbout(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
| 244 |
|
|
{
|
| 245 |
|
|
switch (uMsg) {
|
| 246 |
|
|
case WM_CLOSE:
|
| 247 |
|
|
EndDialog(hwnd, TRUE);
|
| 248 |
|
|
return TRUE;
|
| 249 |
|
|
case WM_INITDIALOG:
|
| 250 |
|
|
return TRUE;
|
| 251 |
|
|
case WM_COMMAND:
|
| 252 |
|
|
switch ((int)wParam) {
|
| 253 |
|
|
case IDOK:
|
| 254 |
|
|
EndDialog(hwnd, TRUE);
|
| 255 |
|
|
return TRUE;
|
| 256 |
|
|
}
|
| 257 |
|
|
}
|
| 258 |
|
|
return FALSE;
|
| 259 |
|
|
}
|
| 260 |
|
|
|
| 261 |
|
|
extern int gui_lprintf(t_eventlog_level l, const char *format, ...)
|
| 262 |
|
|
{
|
| 263 |
|
|
va_list arglist;
|
| 264 |
|
|
va_start(arglist, format);
|
| 265 |
|
|
|
| 266 |
|
|
return gui_lvprintf(l, format, arglist);
|
| 267 |
|
|
}
|
| 268 |
|
|
|
| 269 |
|
|
extern int gui_lvprintf(t_eventlog_level l, const char *format, va_list arglist)
|
| 270 |
|
|
{
|
| 271 |
|
|
char buff[4096];
|
| 272 |
|
|
int result;
|
| 273 |
|
|
COLORREF clr;
|
| 274 |
|
|
|
| 275 |
|
|
result = vsprintf(buff, format, arglist);
|
| 276 |
|
|
|
| 277 |
|
|
switch(l)
|
| 278 |
|
|
{
|
| 279 |
|
|
case eventlog_level_none:
|
| 280 |
|
|
clr = RGB(0, 0, 0);
|
| 281 |
|
|
break;
|
| 282 |
|
|
case eventlog_level_trace:
|
| 283 |
|
|
clr = RGB(255, 0, 255);
|
| 284 |
|
|
break;
|
| 285 |
|
|
case eventlog_level_debug:
|
| 286 |
|
|
clr = RGB(0, 0, 255);
|
| 287 |
|
|
break;
|
| 288 |
|
|
case eventlog_level_info:
|
| 289 |
|
|
clr = RGB(0, 0, 0);
|
| 290 |
|
|
break;
|
| 291 |
|
|
case eventlog_level_warn:
|
| 292 |
|
|
clr = RGB(255, 128, 64);
|
| 293 |
|
|
break;
|
| 294 |
|
|
case eventlog_level_error:
|
| 295 |
|
|
clr = RGB(255, 0, 0);
|
| 296 |
|
|
break;
|
| 297 |
|
|
case eventlog_level_fatal:
|
| 298 |
|
|
clr = RGB(255, 0, 0);
|
| 299 |
|
|
break;
|
| 300 |
|
|
default:
|
| 301 |
|
|
clr = RGB(0, 0, 0);
|
| 302 |
|
|
}
|
| 303 |
|
|
guiAddText(buff, clr);
|
| 304 |
|
|
return result;
|
| 305 |
|
|
}
|
| 306 |
|
|
|
| 307 |
|
|
static void guiAddText(const char *str, COLORREF clr)
|
| 308 |
|
|
{
|
| 309 |
|
|
int start_lines, text_length, end_lines;
|
| 310 |
|
|
CHARRANGE cr;
|
| 311 |
|
|
CHARRANGE ds;
|
| 312 |
|
|
CHARFORMAT fmt;
|
| 313 |
|
|
|
| 314 |
|
|
text_length = SendMessage(ghwndConsole, WM_GETTEXTLENGTH, 0, 0);
|
| 315 |
|
|
|
| 316 |
|
|
if (text_length > 30000)
|
| 317 |
|
|
{
|
| 318 |
|
|
ds.cpMin = 0;
|
| 319 |
|
|
ds.cpMax = text_length - 30000;
|
| 320 |
|
|
SendMessage(ghwndConsole, EM_EXSETSEL, 0, (LPARAM)&ds);
|
| 321 |
|
|
SendMessage(ghwndConsole, EM_REPLACESEL, FALSE, 0);
|
| 322 |
|
|
}
|
| 323 |
|
|
|
| 324 |
|
|
cr.cpMin = text_length;
|
| 325 |
|
|
cr.cpMax = text_length;
|
| 326 |
|
|
SendMessage(ghwndConsole, EM_EXSETSEL, 0, (LPARAM)&cr);
|
| 327 |
|
|
|
| 328 |
|
|
fmt.cbSize = sizeof(CHARFORMAT);
|
| 329 |
|
|
fmt.dwMask = CFM_COLOR|CFM_FACE|CFM_SIZE|CFM_BOLD|CFM_ITALIC|CFM_STRIKEOUT|CFM_UNDERLINE;
|
| 330 |
|
|
fmt.yHeight = 160;
|
| 331 |
|
|
fmt.dwEffects = 0;
|
| 332 |
|
|
fmt.crTextColor = clr;
|
| 333 |
|
|
strcpy(fmt.szFaceName,"Courier New");
|
| 334 |
|
|
|
| 335 |
|
|
SendMessage(ghwndConsole, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&fmt);
|
| 336 |
|
|
SendMessage(ghwndConsole, EM_REPLACESEL, FALSE, (LPARAM)str);
|
| 337 |
|
|
}
|
| 338 |
|
|
|
| 339 |
|
|
#define EXIT_ERROR -1
|
| 340 |
|
|
#define EXIT_OK 0
|
| 341 |
|
|
#define EXIT_SERVICE 1
|
| 342 |
|
|
|
| 343 |
|
|
static void d2cs(void * dummy)
|
| 344 |
|
|
{
|
| 345 |
|
|
switch (server_main(__argc, __argv))
|
| 346 |
|
|
{
|
| 347 |
|
|
case EXIT_SERVICE:
|
| 348 |
|
|
gui_run = FALSE; /* close gui */
|
| 349 |
|
|
case EXIT_ERROR:
|
| 350 |
|
|
d2cs_run = FALSE; /* don't restart */
|
| 351 |
|
|
case EXIT_OK:
|
| 352 |
|
|
; /* do nothing */
|
| 353 |
|
|
}
|
| 354 |
|
|
|
| 355 |
|
|
eventlog(eventlog_level_warn,__FUNCTION__,"Server Stopped");
|
| 356 |
|
|
d2cs_running = FALSE;
|
| 357 |
|
|
}
|
| 358 |
|
|
|
| 359 |
|
|
#endif
|