📄 hlmodmasterdlg.cpp
字号:
int fromlen;
fromlen = sizeof(packet_from);
packet_length = recvfrom (net_socket, (char *)packet_data, sizeof(packet_data), 0, (struct sockaddr *)&packet_from, &fromlen);
if (packet_length == -1)
{
UTIL_VPrintf ("ERROR: GetPacketCommand: %s", strerror(errno));
return;
}
packet_data[packet_length] = 0; // so it can be printed as a string
m_fBytesProcessed += (float)packet_length;
m_nInTransactions++;
if (packet_length)
PacketCommand ();
}
void CHLModMasterDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
// Resize edit control, too.
MoveControls();
}
double CHLModMasterDlg::GetStartTime()
{
return m_tStartTime;
}
void CHLModMasterDlg::ResetTimer()
{
m_tStartTime = Sys_FloatTime();
m_fBytesProcessed = 0.0f;
m_nInTransactions = 0;
m_fBytesSent = 0.0f;
}
float CHLModMasterDlg::GetBytesProcessed()
{
return m_fBytesProcessed;
}
int CHLModMasterDlg::GetInTransactions()
{
return m_nInTransactions;
}
float CHLModMasterDlg::GetOutBytesProcessed()
{
return m_fBytesSent;
}
void CHLModMasterDlg::UTIL_PrintMod( int idx, mod_t *mod )
{
keypair_t *kp;
kp = mod->keys;
UTIL_Printf( "%5i:\r\n", idx );
while ( kp )
{
UTIL_Printf( "\"%s\"\t\"%s\"\r\n", kp->key, kp->value );
kp = kp->next;
}
}
void CHLModMasterDlg::OnList()
{
mod_t *mod;
// curtime = Sys_FloatTime();
int i = 1;
// TODO: Add your control notification handler code here
UTIL_Printf("------- Mod List -----------\r\n");
//UTIL_Printf("# %21s %5s \r\n", "Address", "Active");
for ( mod = modlist ; mod ; mod = mod->next )
{
UTIL_PrintMod( i++, mod );
}
UTIL_Printf("-------------------------------\r\n");
}
BOOL CHLModMasterDlg::MSG_ReadData(int nLength, void *nBuffer)
{
char *start;
start = (char *)packet_data + msg_readcount;
// Read error
if ((msg_readcount + nLength) > packet_length)
{
nBuffer = NULL;
return FALSE;
}
memcpy(nBuffer, start, nLength);
msg_readcount += nLength;
return TRUE;
}
void CHLModMasterDlg::MoveControls()
{
CEdit *pEdit;
CListBox *pBox;
CRect r;
GetClientRect( &r );
CRect rcItem;
int nListBottom = r.Height() - 10;
rcItem = CRect( 10 , 120, r.Width() - 10 - 100, nListBottom );
pEdit = (CEdit *)GetDlgItem(IDC_EDIT1);
if (pEdit && pEdit->GetSafeHwnd())
pEdit->MoveWindow(&rcItem, TRUE);
rcItem = CRect( r.Width() - 10 - 100, 120, r.Width() - 10, nListBottom );
pBox = (CListBox *)GetDlgItem( IDC_LIST1 );
if ( pBox && pBox->GetSafeHwnd() )
pBox->MoveWindow( &rcItem, TRUE );
}
mod_t *CHLModMasterDlg::AddMod( mod_t **pList, mod_t *pMod )
{
const char *pname, *pdir;
char szID[20];
pname = GetValue( pMod, "game" );
pdir = GetValue( pMod, "gamedir" ); // Guaranteed
// allocate a new mod
if ( pname )
{
UTIL_VPrintf ("Adding: '%s'\r\n", pname );
}
else
{
SetKey( pMod, "game", pdir );
pname = pdir;
UTIL_VPrintf ("Adding: gd = '%s'\r\n", pdir );
}
// Assign the unique id.
sprintf( szID, "%i", m_nUniqueID++);
SetKey( pMod, "*uniqueid", szID );
if ( !GetValue( pMod, "requests" ) )
{
SetKey( pMod, "requests", "0" );
}
pMod->next = *pList;
*pList = pMod;
int idx;
idx = m_modList.AddString( pname );
m_modList.SetItemDataPtr( idx, (void *)pMod );
return pMod;
}
void CHLModMasterDlg::ParseMods()
{
// Count the # of .sav files in the directory.
WIN32_FIND_DATA wfd;
HANDLE hResult;
memset(&wfd, 0, sizeof(WIN32_FIND_DATA));
char szFileName[MAX_PATH];
char szSearchPath[MAX_PATH];
sprintf( szSearchPath, "mods/*.mod" );
hResult = FindFirstFile(szSearchPath, &wfd);
if (hResult != INVALID_HANDLE_VALUE)
{
BOOL bMoreFiles;
while (1)
{
sprintf( szFileName, "mods/%s", wfd.cFileName );
ParseModFile( szFileName );
bMoreFiles = FindNextFile(hResult, &wfd);
if (!bMoreFiles)
break;
}
FindClose(hResult);
}
}
banned_sv_t *CHLModMasterDlg::AddServer( banned_sv_t **pList, netadr_t *adr )
{
banned_sv_t *sv;
for ( sv = *pList ; sv ; sv=sv->next )
{
if ( CompareAdr( *adr, sv->address ) )
{
break;
}
}
if (!sv)
{
// allocate a new server
sv = (banned_sv_t *)malloc(sizeof(*sv));
memset (sv, 0, sizeof(*sv));
sv->next = *pList;
sv->address = *adr;
// update the current data
*pList = sv;
}
return sv;
}
void CHLModMasterDlg::ParseServers()
{
// Read from a file.
char *pszBuffer = NULL;
FILE *fp;
int nSize;
fp = fopen("servers.lst", "rb");
if (!fp)
return;
// Get filesize
fseek ( fp, 0, SEEK_END );
nSize = ftell ( fp );
fseek ( fp, 0, SEEK_SET );
pszBuffer = new char[nSize + 1];
fread ( pszBuffer, nSize, 1, fp );
pszBuffer[nSize] = '\0';
fclose( fp );
// Now parse server lists
netadr_t adr;
CToken token(pszBuffer);
token.SetCommentMode(TRUE); // Skip comments when parsing.
banned_sv_t **pList;
while ( 1 )
{
token.ParseNextToken();
if (strlen(token.token) <= 0)
break;
if (!stricmp( token.token, "Banned" ) )
{
pList = &bannedips;
}
else
{
// Bogus
AfxMessageBox("Bogus list type");
break;
}
// Now parse all addresses between { }
token.ParseNextToken();
if (strlen(token.token) <= 0)
{
AfxMessageBox("Expecting {");
break;
}
if ( stricmp ( token.token, "{" ) )
{
AfxMessageBox("Expecting {");
break;
}
// Parse addresses until we get to "}"
while ( 1 )
{
BOOL bIsOld = FALSE;
banned_sv_t *pServer;
// Now parse all addresses between { }
token.ParseNextToken();
if (strlen(token.token) <= 0)
{
AfxMessageBox("Expecting }");
break;
}
if ( !stricmp( token.token, "old" ) )
{
bIsOld = TRUE;
token.ParseNextToken();
}
if ( !stricmp ( token.token, "}" ) )
break;
// It's an address
memset( &adr, 0, sizeof( netadr_t ) );
//adr = StringToAdr( token.token );
NET_StringToSockaddr ( token.token, (struct sockaddr *)&adr );
pServer = AddServer( pList, &adr );
}
}
delete[] pszBuffer;
}/*
==============
COM_Parse
Parse a token out of a string
==============
*/
char com_token[1024];
BOOL com_ignorecolons;
char *COM_Parse (char *data)
{
int c;
int len;
len = 0;
com_token[0] = 0;
if (!data)
return NULL;
// skip whitespace
skipwhite:
while ( (c = *data) <= ' ')
{
if (c == 0)
return NULL; // end of file;
data++;
}
// skip // comments
if (c=='/' && data[1] == '/')
{
while (*data && *data != '\n')
data++;
goto skipwhite;
}
// handle quoted strings specially
if (c == '\"')
{
data++;
while (1)
{
c = *data++;
if (c=='\"' || !c)
{
com_token[len] = 0;
return data;
}
com_token[len] = c;
len++;
}
}
// parse single characters
if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || (!com_ignorecolons && c==':'))
{
com_token[len] = c;
len++;
com_token[len] = 0;
return data+1;
}
// parse a regular word
do
{
com_token[len] = c;
data++;
len++;
c = *data;
if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || (!com_ignorecolons && c==':'))
break;
} while (c>32);
com_token[len] = 0;
return data;
}
const char *CHLModMasterDlg::GetValue( mod_t *pMod, const char *pszKey )
{
if ( !pMod )
return NULL;
keypair_t *kp;
kp = pMod->keys;
while ( kp )
{
if ( !stricmp( kp->key, pszKey ) )
return kp->value;
kp = kp->next;
}
return NULL;
}
void CHLModMasterDlg::SetKey( mod_t *pMod, const char *pszKey, const char *pszValue )
{
if ( !pMod )
return;
keypair_t *kp = pMod->keys;
while ( kp )
{
if ( !stricmp( kp->key, pszKey ) )
{
free( kp->value );
kp->value = strdup( pszValue );
return;
}
kp = kp->next;
}
kp = (keypair_t *)malloc( sizeof( keypair_t ) );
memset( kp, 0, sizeof( keypair_t ) );
kp->key = strdup( pszKey );
kp->value = strdup( pszValue );
// Link it.
kp->next = pMod->keys;
pMod->keys = kp;
}
void CHLModMasterDlg::ParseModFile( char *pszFileName )
{
// Read from a file.
char *pszBuffer = NULL;
FILE *fp;
int nSize;
char szKey[ 256 ];
fp = fopen( pszFileName, "rb" );
if (!fp)
return;
// Get filesize
fseek ( fp, 0, SEEK_END );
nSize = ftell ( fp );
fseek ( fp, 0, SEEK_SET );
pszBuffer = new char[nSize + 1];
fread ( pszBuffer, nSize, 1, fp );
pszBuffer[nSize] = '\0';
fclose( fp );
// Now parse mod lists
mod_t **pList;
mod_t *n = ( mod_t *)malloc( sizeof( mod_t ) );
memset( n, 0, sizeof( mod_t ) );;
pList = &modlist;
char *token;
token = pszBuffer;
while ( 1 )
{
// First token
token = COM_Parse( token );
if ( strlen( com_token ) <= 0 )
break;
strcpy( szKey, com_token );
token = COM_Parse( token );
SetKey( n, szKey, com_token );
}
delete[] pszBuffer;
const char *gamedir = GetValue( n, "gamedir" );
if ( !gamedir )
{
keypair_t *kp, *nextkp;
kp = n->keys;
while ( kp )
{
nextkp = kp->next;
free( kp->key );
free( kp->value );
free( kp );
kp = nextkp;
}
free( n );
}
else
{
SetKey( n, "*file", pszFileName );
AddMod( pList, n );
}
}
void CHLModMasterDlg::UpdateCount()
{
// Now count mods and display current count:
char szModCount[64];
mod_t *mod;
int nCount = 0;
for (mod=modlist ; mod ; mod=mod->next)
{
nCount++;
}
sprintf(szModCount, "%i HL mods", nCount );
if (m_statActive.GetSafeHwnd())
m_statActive.SetWindowText(szModCount);
}
void CHLModMasterDlg::FreeMods()
{
mod_t *mod, *next;
keypair_t *kp, *nextkp;
FILE *fp;
const char *filename;
mod = modlist;
while (mod)
{
next = mod->next;
fp = NULL;
filename = GetValue( mod, "*file" );
if ( filename )
{
fp = fopen( filename, "wb" );
fprintf( fp, "// Mod Info %s\r\n", filename );
}
kp = mod->keys;
while ( kp )
{
nextkp = kp->next;
if ( fp && kp->key[0] != '*' )
{
fprintf( fp, "\"%s\"\t\"%s\"\r\n", kp->key, kp->value );
}
free ( kp->key );
free ( kp->value );
free ( kp );
kp = nextkp;
}
if ( fp )
{
fclose( fp );
fp = NULL;
}
free (mod);
mod = next;
}
modlist = NULL;
}
void CHLModMasterDlg::FreeServers()
{
banned_sv_t *sv, *next;
sv = bannedips;
while (sv)
{
next = sv->next;
free (sv);
sv = next;
}
}
void CHLModMasterDlg::OnReload()
{
FreeMods();
FreeServers();
m_modList.ResetContent();
ParseMods();
ParseServers();
}
void CHLModMasterDlg::OnDblclkList1()
{
mod_t *pmod;
int idx;
idx = m_modList.GetCurSel();
if ( idx == LB_ERR )
return;
pmod = (mod_t *)m_modList.GetItemDataPtr( idx );
if ( !pmod )
return;
UTIL_PrintMod( 1, pmod );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -