📄 hlmodmasterdlg.cpp
字号:
// HLModMasterDlg.cpp : implementation file
//
#include "stdafx.h"
#include <afxpriv.h>
#include "HLModMasterProtocol.h"
#include "HLModMaster.h"
#include "HLModMasterDlg.h"
#include "../../common/proto_oob.h"
#include "Token.h"
#include <time.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// UDP Port
static double pfreq;
static double curtime = 0.0;
static double lastcurtime = 0.0;
static int lowshift;
/*
=============
NET_StringToAdr
localhost
idnewt
idnewt:28000
192.246.40.70
192.246.40.70:28000
=============
*/
#define DO(src,dest) \
copy[0] = s[src]; \
copy[1] = s[src + 1]; \
sscanf (copy, "%x", &val); \
((struct sockaddr_ipx *)sadr)->dest = val
BOOL NET_StringToSockaddr (const char *s, struct sockaddr *sadr)
{
struct hostent *h;
char *colon;
char copy[128];
memset (sadr, 0, sizeof(*sadr));
{
((struct sockaddr_in *)sadr)->sin_family = AF_INET;
((struct sockaddr_in *)sadr)->sin_port = 0;
strcpy (copy, s);
// strip off a trailing :port if present
for (colon = copy ; *colon ; colon++)
if (*colon == ':')
{
*colon = 0;
((struct sockaddr_in *)sadr)->sin_port = htons((short)atoi(colon+1));
}
if (copy[0] >= '0' && copy[0] <= '9')
{
*(int *)&((struct sockaddr_in *)sadr)->sin_addr = inet_addr(copy);
if ( ((struct sockaddr_in *)sadr)->sin_addr.s_addr == INADDR_NONE )
return FALSE;
}
else
{
if (! (h = gethostbyname(copy)) )
return FALSE;
*(int *)&((struct sockaddr_in *)sadr)->sin_addr = *(int *)h->h_addr_list[0];
}
}
return TRUE;
}
#undef DO
netadr_t StringToAdr (char *s)
{
netadr_t a;
int b1, b2, b3, b4, p;
b1 = b2 = b3 = b4 = p = 0;
sscanf (s, "%i.%i.%i.%i:%i", &b1, &b2, &b3, &b4, &p);
a.sin_addr.S_un.S_un_b.s_b1 = b1;
a.sin_addr.S_un.S_un_b.s_b2 = b2;
a.sin_addr.S_un.S_un_b.s_b3 = b3;
a.sin_addr.S_un.S_un_b.s_b4 = b4;
a.sin_port = ntohs(p);
return a;
}
char *idprintf( unsigned char *buf, int nLen )
{
static char szReturn[2048];
unsigned char c;
char szChunk[10];
int i;
memset(szReturn, 0, 2048);
for (i = 0; i < nLen; i++)
{
c = (unsigned char)buf[i];
sprintf(szChunk, "%02x", c);
strcat(szReturn, szChunk);
}
return szReturn;
}
/*
================
Sys_FloatTime
================
*/
double Sys_FloatTime (void)
{
static int sametimecount;
static unsigned int oldtime;
static int first = 1;
LARGE_INTEGER PerformanceCount;
unsigned int temp, t2;
double time;
QueryPerformanceCounter (&PerformanceCount);
temp = ((unsigned int)PerformanceCount.LowPart >> lowshift) |
((unsigned int)PerformanceCount.HighPart << (32 - lowshift));
if (first)
{
oldtime = temp;
first = 0;
}
else
{
// check for turnover or backward time
if ((temp <= oldtime) && ((oldtime - temp) < 0x10000000))
{
oldtime = temp; // so we can't get stuck
}
else
{
t2 = temp - oldtime;
time = (double)t2 * pfreq;
oldtime = temp;
curtime += time;
if (curtime == lastcurtime)
{
sametimecount++;
if (sametimecount > 100000)
{
curtime += 1.0;
sametimecount = 0;
}
}
else
{
sametimecount = 0;
}
lastcurtime = curtime;
}
}
return curtime;
}
int CHLModMasterDlg::DoModal()
{
// can be constructed with a resource template or InitModalIndirect
ASSERT(m_lpszTemplateName != NULL || m_hDialogTemplate != NULL ||
m_lpDialogTemplate != NULL);
// load resource as necessary
LPCDLGTEMPLATE lpDialogTemplate = m_lpDialogTemplate;
HGLOBAL hDialogTemplate = m_hDialogTemplate;
HINSTANCE hInst = AfxGetResourceHandle();
if (m_lpszTemplateName != NULL)
{
hInst = AfxFindResourceHandle(m_lpszTemplateName, RT_DIALOG);
HRSRC hResource = ::FindResource(hInst, m_lpszTemplateName, RT_DIALOG);
hDialogTemplate = LoadResource(hInst, hResource);
}
if (hDialogTemplate != NULL)
lpDialogTemplate = (LPCDLGTEMPLATE)LockResource(hDialogTemplate);
// return -1 in case of failure to load the dialog template resource
if (lpDialogTemplate == NULL)
return -1;
// disable parent (before creating dialog)
HWND hWndParent = PreModal();
AfxUnhookWindowCreate();
CWnd* pParentWnd = CWnd::FromHandle(hWndParent);
BOOL bEnableParent = FALSE;
if (hWndParent != NULL && ::IsWindowEnabled(hWndParent))
{
::EnableWindow(hWndParent, FALSE);
bEnableParent = TRUE;
}
TRY
{
// create modeless dialog
AfxHookWindowCreate(this);
if (CreateDlgIndirect(lpDialogTemplate, CWnd::FromHandle(hWndParent), hInst))
{
if (m_nFlags & WF_CONTINUEMODAL)
{
// enter modal loop
DWORD dwFlags = MLF_SHOWONIDLE;
if (GetStyle() & DS_NOIDLEMSG)
dwFlags |= MLF_NOIDLEMSG;
VERIFY(RunModalLoop(dwFlags) == m_nModalResult);
}
// hide the window before enabling the parent, etc.
if (m_hWnd != NULL)
SetWindowPos(NULL, 0, 0, 0, 0, SWP_HIDEWINDOW|
SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOZORDER);
}
}
CATCH_ALL(e)
{
//DELETE_EXCEPTION(e);
m_nModalResult = -1;
}
END_CATCH_ALL
if (bEnableParent)
::EnableWindow(hWndParent, TRUE);
if (hWndParent != NULL && ::GetActiveWindow() == m_hWnd)
::SetActiveWindow(hWndParent);
// destroy modal window
DestroyWindow();
PostModal();
// unlock/free resources as necessary
if (m_lpszTemplateName != NULL || m_hDialogTemplate != NULL)
UnlockResource(hDialogTemplate);
if (m_lpszTemplateName != NULL)
FreeResource(hDialogTemplate);
return m_nModalResult;
}
int CHLModMasterDlg::RunModalLoop(DWORD dwFlags)
{
ASSERT(::IsWindow(m_hWnd)); // window must be created
ASSERT(!(m_nFlags & WF_MODALLOOP)); // window must not already be in modal state
// for tracking the idle time state
BOOL bIdle = TRUE;
LONG lIdleCount = 0;
BOOL bShowIdle = (dwFlags & MLF_SHOWONIDLE) && !(GetStyle() & WS_VISIBLE);
HWND hWndParent = ::GetParent(m_hWnd);
m_nFlags |= (WF_MODALLOOP|WF_CONTINUEMODAL);
MSG* pMsg = &AfxGetThread()->m_msgCur;
static clock_t tLast = 0;
clock_t tCurrent;
// acquire and dispatch messages until the modal state is done
for (;;)
{
ASSERT(ContinueModal());
// Check on network
ServiceMessages();
tCurrent = clock();
if ( (float)(tCurrent - tLast)/CLOCKS_PER_SEC > 5.0f )
{
tLast = tCurrent;
UpdateCount();
}
if (!ContinueModal())
goto ExitModal;
// phase1: check to see if we can do idle work
while (bIdle &&
!::PeekMessage(pMsg, NULL, NULL, NULL, PM_NOREMOVE))
{
ASSERT(ContinueModal());
// show the dialog when the message queue goes idle
if (bShowIdle)
{
ShowWindow(SW_SHOWNORMAL);
UpdateWindow();
bShowIdle = FALSE;
}
// call OnIdle while in bIdle state
if (!(dwFlags & MLF_NOIDLEMSG) && hWndParent != NULL && lIdleCount == 0)
{
// send WM_ENTERIDLE to the parent
::SendMessage(hWndParent, WM_ENTERIDLE, MSGF_DIALOGBOX, (LPARAM)m_hWnd);
}
if ((dwFlags & MLF_NOKICKIDLE) ||
!SendMessage(WM_KICKIDLE, MSGF_DIALOGBOX, lIdleCount++))
{
// stop idle processing next time
bIdle = FALSE;
}
}
// phase2: pump messages while available
do
{
ASSERT(ContinueModal());
// pump message, but quit on WM_QUIT
if (::PeekMessage(pMsg, NULL, NULL, NULL, PM_NOREMOVE))
{
if (!AfxGetThread()->PumpMessage())
{
AfxPostQuitMessage(0);
return -1;
}
}
// show the window when certain special messages rec'd
if (bShowIdle &&
(pMsg->message == 0x118 || pMsg->message == WM_SYSKEYDOWN))
{
ShowWindow(SW_SHOWNORMAL);
UpdateWindow();
bShowIdle = FALSE;
}
if (!ContinueModal())
goto ExitModal;
// reset "no idle" state after pumping "normal" message
if (AfxGetThread()->IsIdleMessage(pMsg))
{
bIdle = TRUE;
lIdleCount = 0;
}
} while (::PeekMessage(pMsg, NULL, NULL, NULL, PM_NOREMOVE));
}
ExitModal:
m_nFlags &= ~(WF_MODALLOOP|WF_CONTINUEMODAL);
return m_nModalResult;
}
void CHLModMasterDlg::ServiceMessages()
{
static double tLastUpdateTime = (double)0;
float fElapsed, fTimeSinceScreenUpdate;
float fInRate = 0.0f;
float fOutRate = 0.0f;
struct timeval tv;
double tCurTime;
FD_ZERO( &fdset);
FD_SET( net_socket, &fdset );
tv.tv_sec = 0;
tv.tv_usec = 1000; /* seconds */
if ( select (net_socket+1, &fdset, NULL, NULL, &tv) == SOCKET_ERROR )
{
return;
}
if ( FD_ISSET( net_socket, &fdset) )
GetPacketCommand ();
tCurTime = Sys_FloatTime();
fElapsed = (float)(tCurTime - GetStartTime());
if (fElapsed > 0.0f)
{
fInRate = GetBytesProcessed()/fElapsed;
fOutRate = GetOutBytesProcessed()/fElapsed;
}
fTimeSinceScreenUpdate = (float)(tCurTime - tLastUpdateTime);
if ( fTimeSinceScreenUpdate > 0.2f )
{
char szText[64];
sprintf(szText, "%i", (int)(m_fCycleTime - fElapsed));
m_statTime.SetWindowText(szText);
sprintf(szText, "%i", GetInTransactions());
m_statInTransactions.SetWindowText(szText);
sprintf(szText, "%i / %.2f", (int)GetBytesProcessed(), fInRate);
m_statLoad.SetWindowText(szText);
sprintf(szText, "%i / %.2f", (int)GetOutBytesProcessed(), fOutRate);
m_statLoadOut.SetWindowText(szText);
tLastUpdateTime = Sys_FloatTime();
}
// Reset rate when needed
if (fElapsed >= m_fCycleTime)
ResetTimer();
}
char *AdrToString (netadr_t a)
{
static char s[64];
sprintf (s, "%i.%i.%i.%i:%i",
a.sin_addr.S_un.S_un_b.s_b1,
a.sin_addr.S_un.S_un_b.s_b2,
a.sin_addr.S_un.S_un_b.s_b3,
a.sin_addr.S_un.S_un_b.s_b4,
ntohs(a.sin_port));
return s;
}
BOOL CompareAdr (netadr_t a, netadr_t b)
{
if (a.sin_addr.s_addr == b.sin_addr.s_addr
&& a.sin_port == b.sin_port)
return TRUE;
return FALSE;
}
BOOL CompareBaseAdr (netadr_t a, netadr_t b)
{
if (a.sin_addr.s_addr == b.sin_addr.s_addr)
return TRUE;
return FALSE;
}
/////////////////////////////////////////////////////////////////////////////
// CHLModMasterDlg dialog
CHLModMasterDlg::CHLModMasterDlg( int nMasterPort,
BOOL bGenerateLogs,
CString strLocalIPAddress,
CWnd* pParent /*=NULL*/)
: CDialog(CHLModMasterDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CHLModMasterDlg)
m_bShowTraffic = TRUE;
//}}AFX_DATA_INIT
logfile = NULL;
m_bShowPackets = FALSE;
net_hostport = nMasterPort;
m_bGenerateLogs = bGenerateLogs;
m_strLocalIPAddress = strLocalIPAddress;
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_tStartTime = (double)0;
m_fBytesProcessed = 0.0f;
m_fBytesSent = 0.0f;
m_fCycleTime = 60.0f;
m_nInTransactions = 0;
modlist = NULL;
bannedips = NULL;
m_nUniqueID = 1;
// Timer
unsigned int lowpart, highpart;
LARGE_INTEGER PerformanceFreq;
if (!QueryPerformanceFrequency (&PerformanceFreq))
Sys_Error ("No hardware timer available");
// get 32 out of the 64 time bits such that we have around
// 1 microsecond resolution
lowpart = (unsigned int)PerformanceFreq.LowPart;
highpart = (unsigned int)PerformanceFreq.HighPart;
lowshift = 0;
while (highpart || (lowpart > 2000000.0))
{
lowshift++;
lowpart >>= 1;
lowpart |= (highpart & 1) << 31;
highpart >>= 1;
}
pfreq = 1.0 / (double)lowpart;
}
CHLModMasterDlg::~CHLModMasterDlg()
{
FreeMods();
FreeServers();
if (net_socket)
closesocket (net_socket);
if (logfile)
{
fclose(logfile);
logfile = NULL;
}
}
void CHLModMasterDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CHLModMasterDlg)
DDX_Control(pDX, IDC_LIST1, m_modList);
DDX_Control(pDX, IDC_STATIC_LOADOUT, m_statLoadOut);
DDX_Control(pDX, IDC_STATIC_TRANSACTIONS, m_statInTransactions);
DDX_Control(pDX, IDC_STATIC_TIME, m_statTime);
DDX_Control(pDX, IDC_STATIC_LOAD, m_statLoad);
DDX_Control(pDX, IDC_STATIC_ACTIVE, m_statActive);
DDX_Check(pDX, IDC_SHOWTRAFFIC, m_bShowTraffic);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CHLModMasterDlg, CDialog)
//{{AFX_MSG_MAP(CHLModMasterDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_SIZE()
ON_BN_CLICKED(IDC_LIST, OnList)
ON_BN_CLICKED(IDC_RELOAD_MOTD, OnReload)
ON_LBN_DBLCLK(IDC_LIST1, OnDblclkList1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CHLModMasterDlg message handlers
BOOL CHLModMasterDlg::OnInitDialog()
{
CDialog::OnInitDialog();
NET_Init();
Master_Init();
// Read MOTD from MOTD.txt
OnReload();
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
char szText[256];
sprintf(szText, "Half-Life Mod Master: %s / ("__DATE__")", AdrToString(net_local_adr ) );
SetWindowText(szText);
m_tStartTime = Sys_FloatTime();
MoveControls();
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CHLModMasterDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CHLModMasterDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -