MainWndProc.cpp
上传用户:tt_chan
上传日期:2009-12-03
资源大小:4523k
文件大小:7k
源码类别:

模拟服务器

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. LPARAM OnClientSockMsg(WPARAM wParam, LPARAM lParam);
  3. LPARAM OnLogSvrSockMsg(WPARAM wParam, LPARAM lParam);
  4. BOOL jRegSetKey(LPCTSTR pSubKeyName, LPCTSTR pValueName, DWORD dwFlags, LPBYTE pValue, DWORD nValueSize);
  5. BOOL jRegGetKey(LPCTSTR pSubKeyName, LPCTSTR pValueName, LPBYTE pValue);
  6. BOOL CALLBACK ConfigDlgFunc(HWND hWndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
  7. void InitMonsterGenInfo();
  8. void InitMagicInfo();
  9. void InitMonRaceInfo();
  10. void InitStdItemSpecial();
  11. void InitStdItemEtcInfo();
  12. void InitMerchantInfo();
  13. void InitMoveMapEventInfo();
  14. CMapInfo* InitMapInfo(int nServerIndex);
  15. void InitAdminCommandList();
  16. void UnInitAdminCommandList();
  17. void LoadMap(CMapInfo* pMapInfo);
  18. void SwitchMenuItem(BOOL fFlag)
  19. {
  20. HMENU hMainMenu = GetMenu(g_hMainWnd);
  21. HMENU hMenu = GetSubMenu(hMainMenu, 0);
  22. if (fFlag)
  23. {
  24. EnableMenuItem(hMenu, IDM_STARTSERVICE, MF_GRAYED|MF_BYCOMMAND);
  25. EnableMenuItem(hMenu, IDM_STOPSERVICE, MF_ENABLED|MF_BYCOMMAND);
  26. SendMessage(g_hToolBar, TB_SETSTATE, (WPARAM)IDM_STARTSERVICE, (LPARAM)MAKELONG(TBSTATE_INDETERMINATE, 0));
  27. SendMessage(g_hToolBar, TB_SETSTATE, (WPARAM)IDM_STOPSERVICE, (LPARAM)MAKELONG(TBSTATE_ENABLED, 0));
  28. }
  29. else
  30. {
  31. EnableMenuItem(hMenu, IDM_STARTSERVICE, MF_ENABLED|MF_BYCOMMAND);
  32. EnableMenuItem(hMenu, IDM_STOPSERVICE, MF_GRAYED|MF_BYCOMMAND);
  33. SendMessage(g_hToolBar, TB_SETSTATE, (WPARAM)IDM_STARTSERVICE, (LPARAM)MAKELONG(TBSTATE_ENABLED, 0));
  34. SendMessage(g_hToolBar, TB_SETSTATE, (WPARAM)IDM_STOPSERVICE, (LPARAM)MAKELONG(TBSTATE_INDETERMINATE, 0));
  35. InsertLogMsg(IDS_STOPSERVICE);
  36. }
  37. }
  38. CMapInfo* InitDataInDatabase()
  39. {
  40. int nServerIndex;
  41. InitMagicInfo();
  42. InitMonsterGenInfo();
  43. InitMonRaceInfo();
  44. InitStdItemSpecial();
  45. InitStdItemEtcInfo();
  46. InitMerchantInfo();
  47. InitMoveMapEventInfo();
  48. if (!jRegGetKey(_GAME_SERVER_REGISTRY, _TEXT("ServerNumber"), (LPBYTE)&nServerIndex))
  49. return FALSE;
  50. return InitMapInfo(nServerIndex);
  51. }
  52. UINT WINAPI InitializingServer(LPVOID lpParameter)
  53. {
  54. TCHAR wszPath[128];
  55. TCHAR wszFullPath[256];
  56. DWORD dwReadLen;
  57. ZeroMemory(g_szGoldName, sizeof(g_szGoldName));
  58. LoadString(g_hInst, IDS_GOLD, wszPath, sizeof(wszPath)/sizeof(TCHAR));
  59. WideCharToMultiByte(CP_ACP, 0, wszPath, -1, g_szGoldName, sizeof(g_szGoldName), NULL, NULL);
  60. jRegGetKey(_GAME_SERVER_REGISTRY, _TEXT("MapFileLoc"), (LPBYTE)wszPath);
  61. lstrcpy(wszFullPath, wszPath);
  62. lstrcat(wszFullPath, _TEXT("\"));
  63. lstrcat(wszFullPath, _TEXT("searchTable.tbl"));
  64. HANDLE hFile = CreateFile(wszFullPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  65. if ( hFile != INVALID_HANDLE_VALUE )
  66. {
  67. ReadFile(hFile, &g_SearchTable, sizeof(g_SearchTable), &dwReadLen, NULL);
  68.   CloseHandle(hFile);
  69. }
  70. CMapInfo* pMapInfo = (CMapInfo*)lpParameter;
  71. for (int i = 0; i < g_nNumOfMapInfo; i++)
  72. LoadMap(&pMapInfo[i]);
  73. delete [] pMapInfo;
  74. pMapInfo = NULL;
  75. InitAdminCommandList();
  76. BYTE btInstalled;
  77. if (!jRegGetKey(_GAME_SERVER_REGISTRY, _TEXT("Installed"), (LPBYTE)&btInstalled))
  78. DialogBox(g_hInst, MAKEINTRESOURCE(IDD_CONFIGDLG), NULL, (DLGPROC)ConfigDlgFunc);
  79. DWORD dwIP = 0;
  80. TCHAR szPort[24];
  81. int nPort = 0;
  82. jRegGetKey(_GAME_SERVER_REGISTRY, _TEXT("DBServerIP"), (LPBYTE)&dwIP);
  83. jRegGetKey(_GAME_SERVER_REGISTRY, _TEXT("DBServerPort"), (LPBYTE)&nPort);
  84. _itow(nPort, szPort, 10);
  85. ConnectToServer(g_csock, &g_caddr, _IDM_CLIENTSOCK_MSG, NULL, dwIP, nPort, FD_CONNECT|FD_READ|FD_CLOSE);
  86. // ConnectToServer(g_clsock, &g_claddr, _IDM_LOGSVRSOCK_MSG, NULL, dwIP, 5500, FD_CONNECT|FD_READ|FD_CLOSE);
  87. return 0L;
  88. }
  89. void UnInitializingServer()
  90. {
  91. if (g_pMagicInfo) 
  92. {
  93. delete [] g_pMagicInfo;
  94. g_pMagicInfo = NULL;
  95. }
  96. if (g_pMonGenInfo) 
  97. {
  98. delete [] g_pMonGenInfo;
  99. g_pMonGenInfo = NULL;
  100. }
  101. if (g_pMonRaceInfo) 
  102. {
  103. delete [] g_pMonRaceInfo;
  104. g_pMonRaceInfo = NULL;
  105. }
  106. if (g_pStdItemSpecial) 
  107. {
  108. delete [] g_pStdItemSpecial;
  109. g_pStdItemSpecial = NULL;
  110. }
  111. if (g_pStdItemEtc) 
  112. {
  113. delete [] g_pStdItemEtc;
  114. g_pStdItemEtc = NULL;
  115. }
  116. if (g_pMerchantInfo) 
  117. {
  118. delete [] g_pMerchantInfo;
  119. g_pMerchantInfo = NULL;
  120. }
  121. if (g_pMoveMapEventInfo) 
  122. {
  123. delete [] g_pMoveMapEventInfo;
  124. g_pMoveMapEventInfo = NULL;
  125. }
  126. }
  127. void OnCommand(WPARAM wParam, LPARAM lParam)
  128. {
  129. switch (LOWORD(wParam))
  130. {
  131. case IDM_STARTSERVICE:
  132. {
  133. g_fTerminated = FALSE;
  134. CMapInfo* pMapInfo = InitDataInDatabase();
  135. UINT dwThreadIDForMsg = 0;
  136. unsigned long hThreadForMsg = 0;
  137. hThreadForMsg = _beginthreadex(NULL, 0, InitializingServer, pMapInfo, 0, &dwThreadIDForMsg);
  138. SwitchMenuItem(TRUE);
  139. return;
  140. }
  141. case IDM_STOPSERVICE:
  142. {
  143. g_fTerminated = TRUE;
  144. SwitchMenuItem(FALSE);
  145. UnInitializingServer();
  146. UnInitAdminCommandList();
  147. return;
  148. }
  149. case IDM_CONFIG:
  150. {
  151. DialogBox(g_hInst, MAKEINTRESOURCE(IDD_CONFIGDLG), NULL, (DLGPROC)ConfigDlgFunc);
  152. return;
  153. }
  154. }
  155. }
  156. // **************************************************************************************
  157. //
  158. //
  159. //
  160. // **************************************************************************************
  161. LPARAM APIENTRY MainWndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  162. {
  163. switch (nMsg)
  164. {
  165. #ifdef _SOCKET_ASYNC_IO
  166. case _IDM_SERVERSOCK_MSG:
  167. return OnServerSockMsg(wParam, lParam);
  168. #endif
  169. case _IDM_CLIENTSOCK_MSG:
  170. return OnClientSockMsg(wParam, lParam);
  171. case _IDM_LOGSVRSOCK_MSG:
  172. return OnLogSvrSockMsg(wParam, lParam);
  173. case WM_COMMAND:
  174. OnCommand(wParam, lParam);
  175. break;
  176. case WM_SIZE:
  177. {
  178. if (g_hToolBar && g_hMainWnd && g_hStatusBar) 
  179. {
  180. RECT rcToolBar, rcMain, rcStatusBar;
  181. GetWindowRect(g_hToolBar, &rcToolBar);
  182. GetClientRect(g_hMainWnd, &rcMain);
  183. GetWindowRect(g_hStatusBar, &rcStatusBar);
  184. MoveWindow(g_hToolBar, 0, 0, LOWORD(lParam), (rcToolBar.bottom - rcToolBar.top), TRUE);
  185. MoveWindow(g_hStatusBar, 0, rcMain.bottom - (rcStatusBar.bottom - rcStatusBar.top), 
  186. LOWORD(lParam), (rcStatusBar.bottom - rcStatusBar.top), TRUE);
  187. MoveWindow(g_hLogMsgWnd, 0, (rcToolBar.bottom - rcToolBar.top), (rcMain.right - rcMain.left), 
  188. (rcMain.bottom - rcMain.top) - (rcToolBar.bottom - rcToolBar.top) - (rcStatusBar.bottom - rcStatusBar.top), 
  189. TRUE);
  190. int nStatusPartsWidths[_NUMOFMAX_STATUS_PARTS];
  191. int nCnt = 0;
  192. for (int i = _NUMOFMAX_STATUS_PARTS - 1; i >= 0 ; i--)
  193. nStatusPartsWidths[nCnt++] = (rcStatusBar.right - rcStatusBar.left) - (90 * i);
  194. SendMessage(g_hStatusBar, SB_SETPARTS, _NUMOFMAX_STATUS_PARTS, (LPARAM)nStatusPartsWidths);
  195. }
  196. break;
  197. }
  198. case WM_CLOSE:
  199. {
  200. TCHAR szMsg[128];
  201. TCHAR szTitle[128];
  202. LoadString(g_hInst, IDS_PROGRAM_QUIT, szMsg, sizeof(szMsg));
  203. LoadString(g_hInst, IDS_PROGRAM_TITLE, szTitle, sizeof(szTitle));
  204. if (MessageBox(g_hMainWnd, szMsg, szTitle, MB_ICONSTOP|MB_YESNO) == IDYES)
  205. {
  206. if (SendMessage(g_hToolBar, TB_GETSTATE, (WPARAM)IDM_STARTSERVICE, (LPARAM)0L) == TBSTATE_INDETERMINATE)
  207. OnCommand(IDM_STOPSERVICE, 0L);
  208. ClearSocket(g_ssock);
  209. WSACleanup();
  210. CoUninitialize();
  211. PostQuitMessage(0);
  212. }
  213. return 0L;
  214. }
  215. }
  216. return (DefWindowProc(hWnd, nMsg, wParam, lParam));
  217. }