//////////////////////////////////////////////////////////////////////
//
// WorldLog.cpp: implementation of the WorldLog class.
//
//////////////////////////////////////////////////////////////////////
#include "worldlog.h"
#include <stdlib.h>
#include <stdio.h>
#include <direct.h>
extern void PutLogList(char * cMsg);
extern void PutEventLog(char * cMsg);
extern void PutGameLogData(char *cMsg);
extern class XSocket * G_pListenSock;
extern class XSocket * G_pXWorldLogSock;
extern char G_cTxt[500];
extern char G_cData50000[50000];
extern BOOL G_bIsMainLogActive;
char G_cClientCheckCount = 0;
bool G_bDBMode = FALSE;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CWorldLog::CWorldLog(HWND hWnd)
{
int i;
m_hWnd = hWnd;
for (i = 0; i < DEF_MAXCLIENTS; i++) m_pClientList[i] = NULL;
for (i = 0; i < DEF_MSGQUEUESIZE; i++) m_pMsgQueue[i] = NULL;
for (i = 0; i < DEF_MAXGAMESERVERS; i++) m_pGameList[i] = NULL;
for (i = 0; i < DEF_MAXGAMESERVERSMAPS; i++) m_cMapList[i] = NULL;
for (i = 0; i < DEF_MAXMAINLOGSOCK; i++) m_pMainLogSock[i] = NULL;
for (i = 0; i < DEF_MAXMAINLOGSOCK; i++) m_bisMainRegistered[i] = FALSE;
for (i = 0; i < DEF_MAXPLAYERACCOUNTS; i++) m_pAccountList[i] = NULL;
for (i = 0; i < DEF_MAXITEMS; i++) m_pItemList[i] = NULL;
ZeroMemory(m_cWorldLogName, sizeof(m_cWorldLogName));
ZeroMemory(m_pMsgBuffer, sizeof(m_pMsgBuffer+1));
ZeroMemory(m_cWorldServerAddress, sizeof(m_cWorldServerAddress));
ZeroMemory(m_cMainLogAddress, sizeof(m_cMainLogAddress));
m_iWorldServerPort = 0;
m_iWorldServerInternalPort = 0;
m_iTotalPlayers = 0;
m_iMainLogPort = 0;
m_iQueueHead = 0;
m_iQueueTail = 0;
m_iBlockCount = 0;
m_cBackupDrive = 0;
m_iActiveMainLogSock = 0;
m_iCurMainLogSockIndex = 0;
m_bIsWorldRegistered = FALSE;
m_iTotalMainLogSock = 0;
m_cGMLogCount = 0;
ZeroMemory(m_cGMLogBuffer, sizeof(m_cGMLogBuffer));
m_cItemLogCount = 0;
ZeroMemory(m_cItemLogBuffer, sizeof(m_cItemLogBuffer));
m_cCrusadeLogCount = 0;
ZeroMemory(m_cCrusadeLogBuffer, sizeof(m_cCrusadeLogBuffer));
ZeroMemory(m_sGameServerList, sizeof(m_sGameServerList));
// customization
m_bGameServerList = FALSE;
}
CWorldLog::~CWorldLog(void)
{
int i;
//for (i = 0; i < DEF_MAXCLIENTS; i++) {
//if (m_pClientList[i] != NULL) delete m_pClientList[i];
//}
for (i = 0; i < DEF_MSGQUEUESIZE; i++) {
if (m_pMsgQueue[i] != NULL) delete m_pMsgQueue[i];
}
for (i = 0; i < DEF_MAXGAMESERVERS; i++) {
if (m_pGameList[i] != NULL) delete m_pGameList[i];
}
for (i = 0; i < DEF_MAXGAMESERVERSMAPS; i++) {
if (m_cMapList[i] != NULL) delete m_cMapList[i];
}
for (i = 0; i < DEF_MAXMAINLOGSOCK; i++) {
if (m_pMainLogSock[i] != NULL) delete m_pMainLogSock[i];
}
for (i = 0; i < DEF_MAXPLAYERACCOUNTS; i++) {
if (m_pAccountList[i] != NULL) delete m_pAccountList[i];
}
for (i = 0; i < DEF_MAXITEMS; i++) {
if (m_pItemList[i] != NULL) delete m_pItemList[i];
}
}
bool CWorldLog::bInit(void)
{
int i;
if (bReadServerConfigFile("WLserver.cfg") == FALSE) return FALSE;
//if (bReadServerConfigFile("GateServer.cfg") == 0) return FALSE;
for (i = 0; i < DEF_MAXMAINLOGSOCK; i++) {
Sleep(200);
m_pMainLogSock[i] = new class XSocket(m_hWnd, DEF_SERVERSOCKETBLOCKLIMIT);
m_pMainLogSock[i]->bConnect(m_cMainLogAddress, m_iMainLogPort, WM_ONMAINLOGSOCKETEVENT+i);
m_pMainLogSock[i]->bInitBufferSize(DEF_MSGBUFFERSIZE);
wsprintf(G_cTxt, "(!) Try to connect Main-log-socket(%d)... Addr:%s Port:%d", i, m_cMainLogAddress, m_iMainLogPort);
PutLogList(G_cTxt);
}
if (bInitSQLServer() == NULL) return FALSE;
m_iTotalMainLogSock = 0;
if (bReadItemConfigFile("item.cfg") == FALSE) return FALSE;
if (bReadItemConfigFile("item2.cfg") == FALSE) return FALSE;
if (bReadItemConfigFile("item3.cfg") == FALSE) return FALSE;
if (bReadItemConfigFile("item4.cfg") == FALSE) return FALSE;
return TRUE;
}
bool CWorldLog::bInitSQLServer(void)
{
// hypnotoad: uncoded sql initialization
/*
CoInitialize(NULL);
"<Data Source=character;User ID=sa;pwd=SQL_axszdc;Provider=MSDASQL.1;>"
wsprintf(G_cTxt, "select * from CHARACTER_T where CharID = (select MAX(CharID) from CHARACTER_T)", ??????);
PutLogList("(!) Success Connect Database!");
wsprintf(G_cTxt, "Can't Connect Database (%x)", ??????);
MessageBox(m_hWnd, G_cTxt, "DB connect Error", MB_OK+MB_ICONERROR);
return 0;
*/
return true;
}
void CWorldLog::CleanupLogFiles()
{
PutGMLogData(NULL, NULL, TRUE);
PutItemLogData(NULL, NULL, TRUE);
PutCrusadeLogData(NULL, NULL, TRUE);
}
bool CWorldLog::bReadServerConfigFile(char *cFn)
{
FILE * pFile;
HANDLE hFile;
DWORD dwFileSize;
char * cp, * token, cReadMode, cTotalList;
char seps[] = "= \t\n";
class CStrTok * pStrTok;
pFile = NULL;
cTotalList = 0;
cReadMode = 0;
hFile = CreateFileA(cFn, GENERIC_READ, NULL, NULL, OPEN_EXISTING, NULL, NULL);
dwFileSize = GetFileSize(hFile, NULL);
if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
pFile = fopen(cFn, "rt");
if (pFile == NULL) {
PutLogList("(!) Cannot open configuration file.");
return FALSE;
}
else {
PutLogList("(!) Reading configuration file...");
cp = new char[dwFileSize+1];
ZeroMemory(cp, dwFileSize+1);
fread(cp, dwFileSize, 1, pFile);
pStrTok = new class CStrTok(cp, seps);
token = pStrTok->pGet();
while (token != NULL) {
if (cReadMode != 0) {
switch (cReadMode) {
case 1: // gate-server-port
ZeroMemory(m_cWorldServerAddress, sizeof(m_cWorldServerAddress));
char ServerAddr[50];
::gethostname(ServerAddr, 50);
struct hostent *pHostEnt;
pHostEnt = ::gethostbyname(ServerAddr);
if (pHostEnt != NULL) {
wsprintf(ServerAddr, "%d.%d.%d.%d",
(pHostEnt->h_addr_list[0][0] & 0x00ff),
(pHostEnt->h_addr_list[0][1] & 0x00ff),
(pHostEnt->h_addr_list[0][2] & 0x00ff),
(pHostEnt->h_addr_list[0][3] & 0x00ff));
}
strcpy(m_cWorldServerAddress, ServerAddr);
wsprintf(G_cTxt, "(*) World-Server address : %s", m_cWorldServerAddress);
PutLogList(G_cTxt);
m_iWorldServerPort = atoi(token);
wsprintf(G_cTxt, "(*) World-Server port : %d", m_iWorldServerPort);
PutLogList(G_cTxt);
cReadMode = 0;
break;
case 2: // backup-drive-letter
m_cBackupDrive = token[0];
wsprintf(G_cTxt, "(*) Backup Drive Letter : %c", m_cBackupDrive);
PutLogList(G_cTxt);
cReadMode = 0;
break;
case 3: // internal-world-server-port
m_iWorldServerInternalPort = atoi(token);
wsprintf(G_cTxt, "(*) World-Server Internal port : %d", m_iWorldServerInternalPort);
PutLogList(G_cTxt);
cReadMode = 0;
break;
case 4: // world-server-name
strncpy(m_cWorldLogName, token, 10);
wsprintf(G_cTxt, "(*) World-Server-Name : %s", m_cWorldLogName);
PutLogList(G_cTxt);
cReadMode = 0;
break;
case 6: // main-log-server-address
strncpy(m_cMainLogAddress, token, 15);
wsprintf(G_cTxt, "(*) Main-Log-Server Address : %s", m_cMainLogAddress);
PutLogList(G_cTxt);
cReadMode = 0;
break;
case 7: // main-log-server-port
m_iMainLogPort = atoi(token);
wsprintf(G_cTxt, "(*) Main-Log-Server port : %d", m_iMainLogPort);
PutLogList(G_cTxt);
cReadMode = 0;
break;
case 8: // game-server-list
strcpy(m_sGameServerList[cTotalList << 4], token);
wsprintf(G_cTxt, "Permitted Game Server Address: %s", m_sGameServerList[cTotalList << 4]);
PutLogList(G_cTxt);
cTotalList++;
m_bGameServerList = TRUE;
cReadMode = 0;
break;
default:
break;
}
}
else {
if (memcmp(token, "world-server-port", 17) ==