Clone of PhatAC @ https://github.com/floaterxk/PhatAC

PhatAC.cpp 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. #include "StdAfx.h"
  2. #include "Server.h"
  3. #include "World.h"
  4. CPhatServer *g_pPhatServer = 0;
  5. HINSTANCE g_hInstance = 0;
  6. HWND g_hWndLauncher = 0;
  7. HWND g_hWndStatus = 0;
  8. HWND g_hWndMain = 0;
  9. DWORD g_dwMagicNumber = 0; // This is used for the admin login.
  10. UINT64 framerecord = 0;
  11. void UpdateFramesHUD(UINT64 frames)
  12. {
  13. if (g_hWndStatus)
  14. {
  15. SetWindowText(GetDlgItem(g_hWndStatus, IDC_FRAMERATE), csprintf("%I64u", frames));
  16. SetWindowText(GetDlgItem(g_hWndStatus, IDC_RP), csprintf("%I64u", g_pGlobals->GetPacketRecvCount()));
  17. SetWindowText(GetDlgItem(g_hWndStatus, IDC_SP), csprintf("%I64u", g_pGlobals->GetPacketSendCount()));
  18. SetWindowText(GetDlgItem(g_hWndStatus, IDC_BR), csprintf("%I64u", g_pGlobals->GetPacketRecvSize()));
  19. SetWindowText(GetDlgItem(g_hWndStatus, IDC_BS), csprintf("%I64u", g_pGlobals->GetPacketSendSize()));
  20. }
  21. if (frames > framerecord)
  22. framerecord = frames;
  23. if (g_hWndMain)
  24. {
  25. HWND hWndCPUText = GetDlgItem(g_hWndMain, IDC_CPULOADTEXT);
  26. HWND hWndCPUBar = GetDlgItem(g_hWndMain, IDC_CPULOADBAR);
  27. double fraction = framerecord ? ((double)frames / framerecord) : 1.0f;
  28. fraction = (1.0 - fraction) * 100.0;
  29. SendMessage(hWndCPUBar, PBM_SETRANGE, 0, MAKELPARAM(0, 100));
  30. SendMessage(hWndCPUBar, PBM_SETPOS, (WORD)fraction, 0);
  31. SetWindowText(hWndCPUText, csprintf("CPU Lag (%.2f%%):", fraction));
  32. }
  33. }
  34. //Costly and ineffecient, but saves coding time.
  35. void UpdateClientsHUD(CClient **clients, WORD slotrange)
  36. {
  37. if (slotrange >= 400)
  38. {
  39. slotrange = 400 - 1;
  40. }
  41. if (!g_hWndMain)
  42. {
  43. return;
  44. }
  45. HWND hWndClients = GetDlgItem(g_hWndMain, IDC_CLIENTS);
  46. if (!hWndClients)
  47. {
  48. return;
  49. }
  50. SendMessage(hWndClients, LB_RESETCONTENT, 0, 0);
  51. WORD wCount = 0;
  52. for (CClient **it = &clients[1]; it <= &clients[slotrange]; it++)
  53. {
  54. CClient *client = *it;
  55. if (!client)
  56. continue;
  57. const char *lbText = client->GetDescription();
  58. DWORD lbIndex = (DWORD)SendMessage(hWndClients, LB_ADDSTRING, 0, (LPARAM)lbText);
  59. wCount++;
  60. }
  61. g_pGlobals->SetClientCount(wCount);
  62. HWND hWndLoadText = GetDlgItem(g_hWndMain, IDC_NETWORKLOADTEXT);
  63. HWND hWndLoad = GetDlgItem(g_hWndMain, IDC_NETWORKLOADBAR);
  64. SetWindowText(hWndLoadText, csprintf("Server Load (%u)", g_pGlobals->GetClientCount()));
  65. SendMessage(hWndLoad, PBM_SETRANGE, 0, MAKELPARAM(0, 400));
  66. SendMessage(hWndLoad, PBM_SETPOS, g_pGlobals->GetClientCount(), 0);
  67. }
  68. LRESULT CALLBACK AboutProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  69. {
  70. switch (message)
  71. {
  72. case WM_INITDIALOG:
  73. return TRUE;
  74. case WM_COMMAND:
  75. switch (LOWORD(wParam))
  76. {
  77. case IDOK:
  78. EndDialog(hDlg, LOWORD(wParam));
  79. return TRUE;
  80. case IDC_WEBSITE:
  81. ShellExecute(0, "open", "https://discord.gg/ve6uAKt", NULL, NULL, SW_SHOW);
  82. break;
  83. default:
  84. break;
  85. }
  86. break;
  87. case WM_CLOSE:
  88. DestroyWindow(hDlg);
  89. break;
  90. }
  91. return FALSE;
  92. }
  93. LRESULT CALLBACK StatusProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  94. {
  95. switch (message)
  96. {
  97. case WM_INITDIALOG:
  98. {
  99. HWND hWndPriority = GetDlgItem(hDlg, IDC_PRIORITY);
  100. SendMessage(hWndPriority, CB_ADDSTRING, 0, (LPARAM)"Lowest");
  101. SendMessage(hWndPriority, CB_ADDSTRING, 0, (LPARAM)"Below Normal");
  102. SendMessage(hWndPriority, CB_ADDSTRING, 0, (LPARAM)"Normal");
  103. SendMessage(hWndPriority, CB_ADDSTRING, 0, (LPARAM)"Above Normal");
  104. SendMessage(hWndPriority, CB_ADDSTRING, 0, (LPARAM)"Highest");
  105. int tp = GetThreadPriority(GetCurrentThread());
  106. int index;
  107. switch (tp)
  108. {
  109. case THREAD_PRIORITY_LOWEST: index = 0; break;
  110. case THREAD_PRIORITY_BELOW_NORMAL: index = 1; break;
  111. case THREAD_PRIORITY_NORMAL: index = 2; break;
  112. case THREAD_PRIORITY_ABOVE_NORMAL: index = 3; break;
  113. case THREAD_PRIORITY_HIGHEST: index = 4; break;
  114. default: index = 2; break;
  115. };
  116. SendMessage(hWndPriority, CB_SETCURSEL, index, 0);
  117. return TRUE;
  118. }
  119. case WM_COMMAND:
  120. {
  121. WORD wEvent = HIWORD(wParam);
  122. WORD wID = LOWORD(wParam);
  123. switch (wID)
  124. {
  125. case IDC_PRIORITY:
  126. if (wEvent == CBN_SELCHANGE)
  127. {
  128. int index = SendMessage(GetDlgItem(hDlg, IDC_PRIORITY), CB_GETCURSEL, 0, 0);
  129. int tp;
  130. switch (index)
  131. {
  132. case 0: tp = THREAD_PRIORITY_LOWEST; break;
  133. case 1: tp = THREAD_PRIORITY_BELOW_NORMAL; break;
  134. case 3: tp = THREAD_PRIORITY_ABOVE_NORMAL; break;
  135. case 4: tp = THREAD_PRIORITY_HIGHEST; break;
  136. case 2:
  137. default: tp = THREAD_PRIORITY_NORMAL; break;
  138. }
  139. SetThreadPriority(GetCurrentThread(), tp);
  140. }
  141. case IDOK:
  142. if (wEvent == BN_CLICKED)
  143. {
  144. g_hWndStatus = 0;
  145. DestroyWindow(hDlg);
  146. }
  147. break;
  148. }
  149. break;
  150. }
  151. case WM_CLOSE:
  152. g_hWndStatus = 0;
  153. DestroyWindow(hDlg);
  154. break;
  155. }
  156. return FALSE;
  157. }
  158. LRESULT CALLBACK LauncherProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  159. {
  160. switch (message)
  161. {
  162. case WM_INITDIALOG:
  163. {
  164. char account[256];
  165. char password[256];
  166. char remoteport[256];
  167. DWORD remoteip;
  168. if (ReadConfigKey("account", account, 256))
  169. SetWindowText(GetDlgItem(hDlg, IDC_ACCOUNT), account);
  170. if (ReadConfigKey("password", password, 256))
  171. SetWindowText(GetDlgItem(hDlg, IDC_PASSWORD), password);
  172. if (ReadConfigKey("remoteport", remoteport, 256))
  173. SetWindowText(GetDlgItem(hDlg, IDC_REMOTEPORT), remoteport);
  174. if (ReadConfigKey("remoteip", &remoteip))
  175. SendMessage(GetDlgItem(hDlg, IDC_REMOTEIP), IPM_SETADDRESS, 0, remoteip);
  176. return TRUE;
  177. }
  178. case WM_COMMAND:
  179. {
  180. WORD wEvent = HIWORD(wParam);
  181. WORD wID = LOWORD(wParam);
  182. switch (wID)
  183. {
  184. case IDC_LAUNCHCLIENT:
  185. if (wEvent == BN_CLICKED)
  186. {
  187. char szRemotePort[10];
  188. char szAccount[64]; *szAccount = 0;
  189. char szPassword[64]; *szPassword = 0;
  190. DWORD dwRemoteIP;
  191. DWORD dwFields = (DWORD)SendMessage(GetDlgItem(hDlg, IDC_REMOTEIP), IPM_GETADDRESS, 0, (LPARAM)&dwRemoteIP);
  192. SendMessage(GetDlgItem(hDlg, IDC_REMOTEPORT), WM_GETTEXT, 10, (LPARAM)&szRemotePort);
  193. SendMessage(GetDlgItem(hDlg, IDC_ACCOUNT), WM_GETTEXT, 64, (LPARAM)&szAccount);
  194. SendMessage(GetDlgItem(hDlg, IDC_PASSWORD), WM_GETTEXT, 64, (LPARAM)&szPassword);
  195. if (!strlen(szAccount) || !strlen(szPassword))
  196. {
  197. MsgBox("Please specify the account name/password to connect with.");
  198. break;
  199. }
  200. if (dwFields == 4)
  201. {
  202. dwRemoteIP = htonl(dwRemoteIP);
  203. char szLaunch[256];
  204. char szLaunchDir[MAX_PATH + 1];
  205. sprintf(szLaunch, "-h %s -p %u -rodat off -a %s:%s", inet_ntoa(*((in_addr *)&dwRemoteIP)), atol(szRemotePort), szAccount, szPassword);
  206. DWORD dwLen = MAX_PATH + 1;
  207. memset(szLaunchDir, 0, dwLen);
  208. dwLen -= 1;
  209. /*
  210. HKEY hKey;
  211. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Microsoft Games\\Asheron's Call\\1.00", NULL, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
  212. {
  213. RegQueryValueEx(hKey, "Portal Dat", NULL, NULL, (BYTE*)szLaunchDir, &dwLen);
  214. RegCloseKey(hKey);
  215. }
  216. else
  217. {
  218. MsgBox("Couldn't find installation directory. Is AC installed?\r\n");
  219. break;
  220. }
  221. */
  222. char dir[MAX_PATH];
  223. GetCurrentDirectory(MAX_PATH, dir);
  224. dir[MAX_PATH - 1] = '\0';
  225. sprintf(szLaunchDir, "%s\\Client", dir);
  226. if (szLaunchDir[strlen(szLaunchDir) - 1] != '\\')
  227. {
  228. char *end = &szLaunchDir[strlen(szLaunchDir)];
  229. end[0] = '\\';
  230. end[1] = '\0';
  231. }
  232. if (!FileExists(((std::string)szLaunchDir + "\\acclient.exe").c_str()))
  233. {
  234. MsgBox("Please copy your Asheron's Call client to the Client folder of PhatAC.\r\n");
  235. }
  236. else
  237. {
  238. //LOG(Temp, Normal, "Launching %s %s\n", szLaunch, szLaunchDir);
  239. ShellExecute(0, "open", "acclient.exe", szLaunch, szLaunchDir, SW_SHOW);
  240. }
  241. }
  242. else
  243. MsgBox("Please specify the remote IP to connect to.");
  244. }
  245. break;
  246. }
  247. break;
  248. }
  249. case WM_CLOSE:
  250. char account[256]; *account = 0;
  251. char password[256]; *password = 0;
  252. char remoteport[256]; *remoteport = 0;
  253. DWORD remoteip;
  254. if (GetWindowText(GetDlgItem(hDlg, IDC_ACCOUNT), account, 256))
  255. SaveConfigKey("account", account);
  256. if (GetWindowText(GetDlgItem(hDlg, IDC_PASSWORD), password, 256))
  257. SaveConfigKey("password", password);
  258. if (GetWindowText(GetDlgItem(hDlg, IDC_REMOTEPORT), remoteport, 256))
  259. SaveConfigKey("remoteport", remoteport);
  260. if (4 == SendMessage(GetDlgItem(hDlg, IDC_REMOTEIP), IPM_GETADDRESS, 0, (LPARAM)&remoteip))
  261. SaveConfigKey("remoteip", remoteip);
  262. g_hWndLauncher = 0;
  263. DestroyWindow(hDlg);
  264. break;
  265. }
  266. return FALSE;
  267. }
  268. void OutputConsole(int category, int level, const char *text)
  269. {
  270. if (level < LOGLEVEL_Normal)
  271. {
  272. return;
  273. }
  274. HWND hWndConsole = g_pGlobals->GetConsoleWindowHandle();
  275. if (!hWndConsole)
  276. return;
  277. int len = (int)SendMessage(hWndConsole, WM_GETTEXTLENGTH, 0, 0);
  278. DWORD start, end;
  279. SendMessage(hWndConsole, EM_GETSEL, (WPARAM)&start, (LPARAM)&end);
  280. SendMessage(hWndConsole, EM_SETSEL, len, len);
  281. SendMessage(hWndConsole, EM_REPLACESEL, FALSE, (LPARAM)text);
  282. SendMessage(hWndConsole, EM_SETSEL, start, end);
  283. }
  284. void SetWindowStringText(HWND hWnd, const char *text)
  285. {
  286. SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM)text);
  287. }
  288. std::string GetWindowStringText(HWND hWnd)
  289. {
  290. char text[300];
  291. text[0] = '\0';
  292. SendMessage(hWnd, WM_GETTEXT, 300, (LPARAM)text);
  293. text[299] = '\0';
  294. return text;
  295. }
  296. int CALLBACK MainProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  297. {
  298. switch (message)
  299. {
  300. case WM_INITDIALOG:
  301. {
  302. g_pGlobals->SetWindowHandle(hDlg);
  303. g_pGlobals->SetConsoleWindowHandle(GetDlgItem(hDlg, IDC_CONSOLE));
  304. g_Logger.AddLogCallback(OutputConsole);
  305. HWND hVersion = GetDlgItem(hDlg, IDC_VERSION);
  306. SetWindowText(hVersion, "PhatAC compiled " __TIMESTAMP__);
  307. HICON hIcon = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_PHATAC));
  308. SendMessage(hDlg, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
  309. SendMessage(hDlg, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
  310. DeleteObject(hIcon);
  311. SetWindowStringText(GetDlgItem(hDlg, IDC_SERVERHOST), GetLocalIPString().c_str());
  312. SetWindowStringText(GetDlgItem(hDlg, IDC_SERVERPORT), "9050");
  313. return TRUE;
  314. }
  315. case WM_COMMAND:
  316. {
  317. WORD wEvent = HIWORD(wParam);
  318. WORD wID = LOWORD(wParam);
  319. switch (wID)
  320. {
  321. case IDM_EXIT:
  322. PostQuitMessage(0);
  323. DestroyWindow(hDlg);
  324. break;
  325. case IDM_ABOUT:
  326. DialogBox(g_hInstance, (LPCTSTR)IDD_ABOUT, hDlg, (DLGPROC)AboutProc);
  327. break;
  328. case IDM_LAUNCHER:
  329. if (!g_hWndLauncher)
  330. {
  331. g_hWndLauncher = CreateDialog(g_hInstance, (LPCTSTR)IDD_LAUNCHER, hDlg, (DLGPROC)LauncherProc);
  332. ShowWindow(g_hWndLauncher, SW_SHOW);
  333. }
  334. break;
  335. case IDM_STATUS:
  336. if (!g_hWndStatus)
  337. {
  338. g_hWndStatus = CreateDialog(g_hInstance, (LPCTSTR)IDD_STATUS, hDlg, (DLGPROC)StatusProc);
  339. ShowWindow(g_hWndStatus, SW_SHOW);
  340. }
  341. break;
  342. case IDC_CLEARLOG:
  343. {
  344. if (wEvent == BN_CLICKED)
  345. {
  346. HWND hWndConsole = GetDlgItem(hDlg, IDC_CONSOLE);
  347. SetWindowText(hWndConsole, "");
  348. LOG(Temp, Normal, "Console cleared.\n");
  349. }
  350. }
  351. break;
  352. case IDC_BROADCAST:
  353. if (wEvent == BN_CLICKED)
  354. {
  355. if (!g_pPhatServer)
  356. {
  357. LOG(Temp, Normal, "You must be running a server to broadcast a system message.\n");
  358. break;
  359. }
  360. char text[400];
  361. memset(text, 0, 400);
  362. SendMessage(GetDlgItem(hDlg, IDC_BROADCASTTEXT), WM_GETTEXT, 399, (LPARAM)text);
  363. g_pPhatServer->SystemBroadcast(text);
  364. }
  365. break;
  366. case IDC_KICK:
  367. if (wEvent == BN_CLICKED)
  368. {
  369. if (!g_pPhatServer)
  370. break;
  371. g_pWorld->Test();
  372. }
  373. break;
  374. case IDC_BAN:
  375. if (wEvent == BN_CLICKED)
  376. {
  377. if (!g_pPhatServer)
  378. break;
  379. HWND hWndClients = GetDlgItem(hDlg, IDC_CLIENTS);
  380. LRESULT lResult = SendMessage(hWndClients, LB_GETCURSEL, 0, 0);
  381. if (lResult == CB_ERR)
  382. break;
  383. //This assumes the description begins with 4 numbers indicating the celit index.
  384. LRESULT lLen = SendMessage(hWndClients, LB_GETTEXTLEN, lResult, 0);
  385. if (lLen >= 4)
  386. {
  387. char *szText = new char[lLen + 1];
  388. SendMessage(hWndClients, LB_GETTEXT, lResult, (LPARAM)szText);
  389. szText[4] = 0;
  390. int clientIndex = atoi(szText);
  391. g_pPhatServer->BanClient(clientIndex);
  392. delete[]szText;
  393. }
  394. }
  395. break;
  396. case IDC_LAUNCH:
  397. if (wEvent == BN_CLICKED)
  398. {
  399. if (!g_pPhatServer)
  400. {
  401. MsgBox("You might want to start the server first. ;)");
  402. break;
  403. }
  404. std::string serverHost = GetLocalIPString();
  405. SetWindowStringText(GetDlgItem(hDlg, IDC_SERVERHOST), serverHost.c_str());
  406. int serverPort = atoi(GetWindowStringText(GetDlgItem(hDlg, IDC_SERVERPORT)).c_str());
  407. if (serverHost.length() != 0 && serverPort != 0)
  408. {
  409. char szLaunch[256];
  410. char szLaunchDir[MAX_PATH + 10];
  411. sprintf(szLaunch, "-h %s -p %u -rodat off -a admin:%06lu", serverHost.c_str(), serverPort, g_dwMagicNumber);
  412. DWORD dwLen = MAX_PATH + 10;
  413. memset(szLaunchDir, 0, dwLen);
  414. dwLen -= 1;
  415. /*
  416. HKEY hKey;
  417. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Microsoft Games\\Asheron's Call\\1.00", NULL, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
  418. {
  419. RegQueryValueEx(hKey, "Portal Dat", NULL, NULL, (BYTE*)szLaunchDir, &dwLen);
  420. RegCloseKey(hKey);
  421. }
  422. else
  423. {
  424. MsgBox("Couldn't find installation directory. Is AC installed?\r\n");
  425. break;
  426. }
  427. */
  428. char dir[MAX_PATH];
  429. GetCurrentDirectory(MAX_PATH, dir);
  430. dir[MAX_PATH - 1] = '\0';
  431. sprintf(szLaunchDir, "%s\\Client", dir);
  432. if (szLaunchDir[strlen(szLaunchDir) - 1] != '\\')
  433. {
  434. char *end = &szLaunchDir[strlen(szLaunchDir)];
  435. end[0] = '\\';
  436. end[1] = '\0';
  437. }
  438. if (!FileExists(((std::string)szLaunchDir + "\\acclient.exe").c_str()))
  439. {
  440. MsgBox("Please copy your Asheron's Call client to the Client folder of PhatAC.\r\n");
  441. }
  442. else
  443. {
  444. //LOG(Temp, Normal, "Launching %s %s\n", szLaunch, szLaunchDir);
  445. ShellExecute(0, "open", "acclient.exe", szLaunch, szLaunchDir, SW_SHOW);
  446. }
  447. }
  448. else
  449. {
  450. MsgBox("Please specify the server host and port.");
  451. }
  452. }
  453. break;
  454. case IDC_TOGGLE:
  455. if (wEvent == BN_CLICKED)
  456. {
  457. if (!g_pPhatServer)
  458. {
  459. std::string serverHost = GetLocalIPString();
  460. SetWindowStringText(GetDlgItem(hDlg, IDC_SERVERHOST), serverHost.c_str());
  461. unsigned long serverIP = GetLocalIP();
  462. int serverPort = atoi(GetWindowStringText(GetDlgItem(hDlg, IDC_SERVERPORT)).c_str());
  463. if (serverIP != 0 && serverPort != 0)
  464. {
  465. serverIP = htonl(serverIP);
  466. g_pPhatServer = new CPhatServer(*((in_addr *)&serverIP), serverPort);
  467. SetWindowText(GetDlgItem(hDlg, IDC_TOGGLE), "Stop");
  468. SetWindowText(GetDlgItem(hDlg, IDC_CONNECTLINK), csprintf("acclient.exe -h %s -p %d -a username:password -rodat off", serverHost.c_str(), serverPort));
  469. }
  470. else
  471. MsgBox("Please specify the server's external IP.");
  472. }
  473. else
  474. {
  475. SafeDelete(g_pPhatServer);
  476. SendMessage(GetDlgItem(hDlg, IDC_CLIENTS), LB_RESETCONTENT, 0, 0);
  477. SetWindowText(GetDlgItem(hDlg, IDC_TOGGLE), "Start");
  478. SetWindowText(GetDlgItem(hDlg, IDC_NETWORKLOADTEXT), "Server Load (0)");
  479. SetWindowText(GetDlgItem(hDlg, IDC_CPULOADTEXT), "CPU Load (0.00%)");
  480. SendMessage(GetDlgItem(hDlg, IDC_NETWORKLOADBAR), PBM_SETRANGE, 0, MAKELPARAM(0, 400));
  481. SendMessage(GetDlgItem(hDlg, IDC_NETWORKLOADBAR), PBM_SETPOS, 0, 0);
  482. SendMessage(GetDlgItem(hDlg, IDC_CPULOADBAR), PBM_SETRANGE, 0, MAKELPARAM(0, 100));
  483. SendMessage(GetDlgItem(hDlg, IDC_CPULOADBAR), PBM_SETPOS, 0, 0);
  484. SetWindowText(GetDlgItem(hDlg, IDC_CONNECTLINK), "");
  485. LOG(Temp, Normal, "Server shutdown.\n");
  486. }
  487. }
  488. break;
  489. }
  490. }
  491. break;
  492. case WM_CLOSE:
  493. if (g_hWndLauncher) SendMessage(g_hWndLauncher, WM_CLOSE, 0, 0);
  494. if (g_hWndStatus) SendMessage(g_hWndStatus, WM_CLOSE, 0, 0);
  495. PostQuitMessage(0);
  496. DestroyWindow(hDlg);
  497. break;
  498. }
  499. return FALSE;
  500. }
  501. int APIENTRY WinMain(HINSTANCE hInstance,
  502. HINSTANCE hPrevInstance,
  503. LPTSTR lpCmdLine,
  504. int nCmdShow)
  505. {
  506. SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
  507. srand((unsigned int)time(NULL));
  508. g_pGlobals = new CGlobals();
  509. g_Logger.Open();
  510. INITCOMMONCONTROLSEX iccex;
  511. iccex.dwSize = sizeof(INITCOMMONCONTROLSEX);
  512. iccex.dwICC = ICC_INTERNET_CLASSES;
  513. InitCommonControlsEx(&iccex);
  514. WSADATA wsaData;
  515. USHORT wVersionRequested = 0x0202;
  516. WSAStartup(wVersionRequested, &wsaData);
  517. g_hInstance = hInstance;
  518. g_hWndMain = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, MainProc);
  519. if (!g_hWndMain)
  520. {
  521. MsgBoxError(GetLastError(), "creating main dialog box");
  522. return 0;
  523. }
  524. g_dwMagicNumber = RandomLong(0, 1234567890);
  525. LOG(UserInterface, Normal, "Welcome to PhatAC..\n");
  526. ShowWindow(g_hWndMain, nCmdShow);
  527. MSG msg;
  528. msg.message = WM_NULL;
  529. while (msg.message != WM_QUIT)
  530. {
  531. Sleep(1);
  532. if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
  533. {
  534. if (IsDialogMessage(g_hWndMain, &msg))
  535. continue;
  536. TranslateMessage(&msg);
  537. DispatchMessage(&msg);
  538. }
  539. else
  540. {
  541. if (g_pPhatServer)
  542. g_pPhatServer->Tick();
  543. }
  544. }
  545. SafeDelete(g_pPhatServer);
  546. SafeDelete(g_pGlobals);
  547. WSACleanup();
  548. #ifdef _DEBUG
  549. if (_CrtDumpMemoryLeaks())
  550. OutputDebugString("Memory leak found!\n");
  551. else
  552. OutputDebugString("No memory leaks found!\n");
  553. #endif
  554. return 0;
  555. }