[server] Implement few CConsole functions

This commit is contained in:
RD42 2023-10-25 00:01:33 +08:00
parent a83e0d65cc
commit 5a71f0b9fa
5 changed files with 264 additions and 0 deletions

178
server/console.cpp Normal file
View File

@ -0,0 +1,178 @@
#include "main.h"
ConsoleVariable_s* CConsole::FindVariable(char* pVarName)
{
char VarName[256];
strncpy(VarName, pVarName, 255);
strlwr(VarName);
StringConvarMap::iterator itor;
itor = ConsoleVariables.find(VarName);
if (itor != ConsoleVariables.end())
{
return itor->second;
}
return NULL;
}
void CConsole::AddVariable(char* pVarName, CON_VARTYPE VarType, DWORD VarFlags, void* VarPtr,
VARCHANGEFUNC VarChangeFunc)
{
char VarName[256];
strncpy(VarName, pVarName, 255);
strlwr(VarName);
if (FindVariable(VarName) != NULL)
{
logprintf("Unable to add console variable '%s'. Already exists.", VarName);
return;
}
ConsoleVariable_s* ConVar = new ConsoleVariable_s;
ConVar->VarType = VarType;
ConVar->VarFlags = VarFlags;
ConVar->VarPtr = VarPtr;
ConVar->VarChangeFunc = VarChangeFunc;
if (VarChangeFunc)
VarChangeFunc();
ConsoleVariables.insert(StringConvarMap::value_type(VarName, ConVar));
}
char* CConsole::AddStringVariable(char* pVarName, DWORD VarFlags, char* pInitStr,
VARCHANGEFUNC VarChangeFunc)
{
char* str;
if (!pInitStr)
{
// Empty string
str = (char*)malloc(1);
str[0] = 0;
} else {
str = (char*)malloc(strlen(pInitStr)+1);
strcpy(str,pInitStr);
}
AddVariable(pVarName, CON_VARTYPE_STRING, VarFlags, (void*)str, VarChangeFunc);
return str;
}
void CConsole::RemoveVariable(char* pVarName)
{
ConsoleVariable_s* ConVar = FindVariable(pVarName);
if (ConVar != NULL)
{
if (ConVar->VarType == CON_VARTYPE_STRING)
{
if (ConVar->VarPtr != NULL)
free(ConVar->VarPtr);
}
char VarName[255];
strncpy(VarName, pVarName, 255);
strlwr(VarName);
delete ConVar;
ConsoleVariables.erase(VarName);
}
}
void CConsole::ModifyVariableFlags(char* pVarName, DWORD VarFlags)
{
ConsoleVariable_s* ConVar = FindVariable(pVarName);
if (ConVar != NULL)
{
ConVar->VarFlags = VarFlags;
}
}
char* CConsole::GetStringVariable(char* pVarName)
{
ConsoleVariable_s* ConVar = FindVariable(pVarName);
if (ConVar != NULL)
{
if (ConVar->VarType == CON_VARTYPE_STRING)
return (char*)ConVar->VarPtr;
}
return NULL;
}
void CConsole::SetStringVariable(char* pVarName, char* pString)
{
ConsoleVariable_s* ConVar = FindVariable(pVarName);
if (ConVar != NULL)
{
if (ConVar->VarType == CON_VARTYPE_STRING)
{
if (ConVar->VarPtr != NULL)
free(ConVar->VarPtr);
char* str = (char*)malloc(strlen(pString)+1);
strcpy(str, pString);
ConVar->VarPtr = str;
}
}
}
float CConsole::GetFloatVariable(char* pVarName)
{
ConsoleVariable_s* ConVar = FindVariable(pVarName);
if (ConVar != NULL)
{
if (ConVar->VarType == CON_VARTYPE_FLOAT)
return *(float*)ConVar->VarPtr;
}
return 0.0f;
}
void CConsole::SetFloatVariable(char* pVarName, float fFloat)
{
ConsoleVariable_s* ConVar = FindVariable(pVarName);
if (ConVar != NULL)
{
if (ConVar->VarType == CON_VARTYPE_FLOAT)
*(float*)ConVar->VarPtr = fFloat;
}
}
int CConsole::GetIntVariable(char* pVarName)
{
ConsoleVariable_s* ConVar = FindVariable(pVarName);
if (ConVar != NULL)
{
if (ConVar->VarType == CON_VARTYPE_INT)
return *(int*)ConVar->VarPtr;
}
return 0;
}
void CConsole::SetIntVariable(char* pVarName, int iInt)
{
ConsoleVariable_s* ConVar = FindVariable(pVarName);
if (ConVar != NULL)
{
if (ConVar->VarType == CON_VARTYPE_INT)
*(int*)ConVar->VarPtr = iInt;
}
}
bool CConsole::GetBoolVariable(char* pVarName)
{
ConsoleVariable_s* ConVar = FindVariable(pVarName);
if (ConVar != NULL)
{
if (ConVar->VarType == CON_VARTYPE_BOOL)
return *(bool*)ConVar->VarPtr;
}
return false;
}
void CConsole::SetBoolVariable(char* pVarName, bool bBool)
{
ConsoleVariable_s* ConVar = FindVariable(pVarName);
if (ConVar != NULL)
{
if (ConVar->VarType == CON_VARTYPE_BOOL)
*(bool*)ConVar->VarPtr = bBool;
}
}

47
server/console.h Normal file
View File

@ -0,0 +1,47 @@
#ifndef SAMPSRV_CONSOLE_H
#define SAMPSRV_CONSOLE_H
enum CON_VARTYPE {CON_VARTYPE_FLOAT, CON_VARTYPE_INT, CON_VARTYPE_BOOL, CON_VARTYPE_STRING};
typedef void (*VARCHANGEFUNC)();
struct ConsoleVariable_s
{
CON_VARTYPE VarType;
DWORD VarFlags;
void* VarPtr;
VARCHANGEFUNC VarChangeFunc;
};
typedef std::map<std::string, ConsoleVariable_s*> StringConvarMap;
class CConsole
{
public:
StringConvarMap ConsoleVariables;
ConsoleVariable_s* FindVariable(char* pVarName);
void AddVariable(char* pVarName, CON_VARTYPE VarType, DWORD VarFlags, void* VarPtr,
VARCHANGEFUNC VarChangeFunc = NULL);
char* AddStringVariable(char* pVarName, DWORD VarFlags, char* pInitStr,
VARCHANGEFUNC VarChangeFunc = NULL);
void RemoveVariable(char* pVarName);
void ModifyVariableFlags(char* pVarName, DWORD VarFlags);
char* GetStringVariable(char* pVarName);
void SetStringVariable(char* pVarName, char* pString);
float GetFloatVariable(char* pVarName);
void SetFloatVariable(char* pVarName, float fFloat);
int GetIntVariable(char* pVarName);
void SetIntVariable(char* pVarName, int iInt);
bool GetBoolVariable(char* pVarName);
void SetBoolVariable(char* pVarName, bool bBool);
};
#endif

View File

@ -1,6 +1,8 @@
#include "main.h"
CConsole *pConsole = NULL;
FILE *pLogFile;
//----------------------------------------------------
@ -26,6 +28,24 @@ void LoadLogFile()
}
}
#ifdef LINUX
// strlwr is not included with the GNU C lib it seems.
char* strlwr(char* str)
{
size_t maxlen=strlen(str);
for (size_t i=0; i<maxlen; i++)
{
if ((str[i] >= 'A') && (str[i] <= 'Z'))
{
str[i] -= 32;
}
}
return str;
}
#endif // #ifdef LINUX
//----------------------------------------------------
int main (int argc, char** argv)
@ -42,6 +62,9 @@ int main (int argc, char** argv)
srand(time(NULL));
// Create the Console
pConsole = new CConsole();
// TODO: main
fclose(pLogFile);

View File

@ -28,6 +28,16 @@
#include <stdio.h>
#include <stdlib.h>
// Std
#include <map>
#include <string>
#include "console.h"
void logprintf(char* format, ...);
#ifdef LINUX
char* strlwr(char* str);
#endif
#endif // SAMPSRV_MAIN_H

View File

@ -108,6 +108,12 @@
<References>
</References>
<Files>
<File
RelativePath=".\console.cpp">
</File>
<File
RelativePath=".\console.h">
</File>
<File
RelativePath=".\main.cpp">
</File>