started working on ini parser

This commit is contained in:
2025-08-20 01:27:25 +03:00
parent 5635cd1d69
commit eff681ca33
47 changed files with 1465 additions and 112 deletions

View File

@@ -7,9 +7,7 @@
CUtlVector<CUtlString> engine_CompiledFiles = {
"engine/engine.cpp",
"engine/cvar.cpp",
"engine/filesystem.cpp",
"engine/filesystem_pak.cpp",
"engine/filesystem_libc.cpp",
"engine/gamewindow_sdl.cpp",
@@ -38,7 +36,9 @@ DECLARE_BUILD_STAGE(engine)
else
{
ldProject.objects.AppendTail((Object_t){tier1_lib});
ldProject.objects.AppendTail((Object_t){tier2_lib});
ldProject.objects.AppendTail((Object_t){rapier_lib});
ldProject.objects.AppendTail((Object_t){material_lib});
if (bSteam)
ldProject.objects.AppendTail((Object_t){steam_lib});
ldProject.linkType = ELINK_DYNAMIC_LIBRARY;

View File

@@ -0,0 +1,338 @@
#include "icvar.h"
#include "tier2/ifilesystem.h"
#include "tier1/utlstring.h"
#include "tier1/utlvector.h"
#include "stdarg.h"
enum EConsoleMessageType
{
CONSOLE_MESSAGE_TYPE_MESSAGE,
CONSOLE_MESSAGE_TYPE_WARNING,
CONSOLE_MESSAGE_TYPE_ERROR,
};
struct ConsoleMessage_t
{
EConsoleMessageType type;
CUtlString szMessage;
};
void Msg( const char* message )
{
printf(message);
}
void Warning( const char* message )
{
}
void Error( const char* message )
{
}
class CConsole: public IConsole
{
public:
virtual void Init() override;
virtual void Shutdown() override;
// Variables
virtual void RegisterVar( ConVar *cvar ) override;
virtual void UnRegisterVar( ConVar *cvar ) override;
virtual ConVar *FindVar( const char *pName ) override;
// Commands
virtual void RegisterCommand( ConCommand *cvar ) override;
virtual void UnRegisterCommand( ConCommand *cvar ) override;
virtual ConCommand *FindCommand( const char *pName ) override;
// Command buffer
virtual void Execute( void ) override;
virtual void ExecuteArguments( CUtlVector<CUtlString> &args ) override;
virtual CUtlVector<CUtlVector<CUtlString>> ParseCommandLine( CUtlString psz ) override;
virtual void AddCommand( const char *psz ) override;
virtual void InsertCommand( const char *psz ) override;
};
IConsole *Console()
{
static CConsole s_console;
return &s_console;
}
void CConsole::Init()
{
}
void CConsole::Shutdown()
{
}
void CConsole::RegisterVar( ConVar *cvar )
{
m_convars.AppendTail(cvar);
}
void CConsole::UnRegisterVar( ConVar *cvar )
{
}
ConVar *CConsole::FindVar( const char *pName )
{
for (auto &var: m_convars)
{
if (!V_strcmp(var->GetName(), pName))
return var;
}
return NULL;
}
void CConsole::RegisterCommand( ConCommand *cvar )
{
m_commands.AppendTail(cvar);
}
void CConsole::UnRegisterCommand( ConCommand *cvar )
{
}
ConCommand *CConsole::FindCommand( const char *pName )
{
for (auto &var: m_commands)
{
if (!V_strcmp(var->GetName(), pName))
return var;
}
return NULL;
}
CUtlString g_commandBuffer;
//-----------------------------------------------------------------------------
// Executes arguments from CConsole::Execute
//-----------------------------------------------------------------------------
void CConsole::ExecuteArguments( CUtlVector<CUtlString> &args )
{
if (args.GetSize()<1)
return;
ConCommand *cmd = CConsole::FindCommand(args[0]);
if (!cmd)
{
V_printf("%s not found\n", args[0].GetString());
return;
}
CUtlBuffer<char*> strbuffer(args.GetSize());
for ( size_t i = 0; i < args.GetSize(); i++)
{
strbuffer[i] = args[i].GetString();
};
(cmd->GetCallback())(args.GetSize(), strbuffer);
}
void CConsole::Execute( void )
{
CUtlVector<CUtlVector<CUtlString>> commands = ParseCommandLine(g_commandBuffer);
g_commandBuffer = 0;
for (auto &command: commands)
{
ExecuteArguments(command);
}
}
//-----------------------------------------------------------------------------
// Parses command buffer.
//-----------------------------------------------------------------------------
CUtlVector<CUtlVector<CUtlString>> CConsole::ParseCommandLine( CUtlString psz )
{
CUtlVector<CUtlString> arguments;
CUtlVector<CUtlVector<CUtlString>> commands;
CUtlString szArgument;
bool bIsQuote = false;
for ( auto &c: (CUtlVector<char>&)psz )
{
if ( c == '\"' )
{
bIsQuote = !bIsQuote;
continue;
}
if ( c == ';' || c == '\n' )
{
if (bIsQuote)
{
continue;
}
if (szArgument != 0)
arguments.AppendTail(szArgument);
if ( arguments.GetSize() > 0 )
commands.AppendTail(arguments);
szArgument = 0;
arguments = {};
continue;
}
if ( c == '\t' || c == ' ' )
{
if (bIsQuote)
{
szArgument.AppendTail(c);
continue;
}
if (szArgument != 0)
arguments.AppendTail(szArgument);
szArgument = 0;
continue;
}
szArgument.AppendTail(c);
};
if (szArgument != 0)
arguments.AppendTail(szArgument);
if ( arguments.GetSize() > 0 )
commands.AppendTail(arguments);
return commands;
}
void CConsole::AddCommand( const char *psz )
{
g_commandBuffer.AppendTail(psz);
}
void CConsole::InsertCommand( const char *psz )
{
g_commandBuffer.AppendHead(psz);
};
ConVar::ConVar( const char *pName, const char *pDefaultValue, int flags )
: ConVar(pName, pDefaultValue, flags, 0)
{
}
ConVar::ConVar( const char *pName, const char *pDefaultValue, int flags,
const char *pHelpString )
: ConVar(pName, pDefaultValue, flags, pHelpString, 0)
{
}
ConVar::ConVar( const char *pName, const char *pDefaultValue, int flags,
const char *pHelpString, ConCommandFn callback )
{
m_szName = pName;
m_flags = flags;
m_szValue = pDefaultValue;
m_fValue = V_atof(pDefaultValue);
m_nValue = V_atoi(pDefaultValue);
Console()->RegisterVar(this);
}
bool ConVar::IsFlagSet( int flag )
{
}
const char *ConVar::GetHelpText( void )
{
return m_szHelpString;
}
bool ConVar::IsRegistered( void )
{
}
const char *ConVar::GetName( void )
{
return m_szName;
}
void ConVar::AddFlags( int flags )
{
}
bool ConVar::IsCommand( void )
{
}
void ConVar::InstallChangeCallback( ConCommandFn )
{
}
float ConVar::GetFloat( void )
{
return m_fValue;
}
int ConVar::GetInt( void )
{
return m_nValue;
}
bool ConVar::GetBool( void )
{
return m_nValue;
}
const char *ConVar::GetString( void )
{
}
void ConVar::SetValue( const char *szValue )
{
if (!szValue)
return;
m_szValue = szValue;
m_fValue = V_atof(szValue);
m_nValue = V_atoi(szValue);
}
void ConVar::SetValue( float fValue )
{
m_fValue = fValue;
m_nValue = fValue;
m_szValue = CUtlString("%f\n",fValue);
}
void ConVar::SetValue( int iValue )
{
m_fValue = iValue;
m_nValue = iValue;
m_szValue = CUtlString("%i\n",iValue);
}
ConCommand::ConCommand(const char *pName, ConCommandFn callback,
const char *pHelpString, int flags)
{
m_szName = pName;
m_callback = callback;
m_flags = flags;
Console()->RegisterCommand(this);
};
const char *ConCommand::GetHelpText( void )
{
return m_szHelpString;
}
const char *ConCommand::GetName( void )
{
return m_szName;
}
ConCommandFn ConCommand::GetCallback( void )
{
return m_callback;
}
void IConsole_Exec( int argc, char **argv)
{
if (argc != 2)
return;
IFileHandle *f = filesystem->Open(argv[1], FILEMODE_READ);
if (!f)
return;
CUtlBuffer<char> b(f->Size()+1);
f->Read(b, b.GetSize());
b[b.GetSize()-1] = 0;
Console()->AddCommand(b);
Console()->AddCommand(";");
Console()->Execute();
}
ConCommand IConsole_ExecCmd("exec", IConsole_Exec);

View File

@@ -1,6 +1,7 @@
#include "ifilesystem.h"
#include "tier2/ifilesystem.h"
#include "igamewindow.h"
#include "materialsystem/materialsystem.h"
#include "tier1/interface.h"
#include "tier1/commandline.h"
#include "tier0/mem.h"
@@ -16,6 +17,8 @@ extern "C" void FunnyMain( int argc, char **argv )
ServerGameDLL()->Init();
Materials()->Init();
for (;;) {
gamewindow->UpdateWindow();

View File

@@ -1,81 +0,0 @@
#include "ifilesystem.h"
#include "tier1/interface.h"
#include "tier0/lib.h"
size_t IFileHandle::Write( const void *pData, size_t nDataSize )
{
return m_pFileSystem->Write(this, pData, nDataSize);
}
size_t IFileHandle::Read( void *pData, size_t nDataSize )
{
return m_pFileSystem->Read(this, pData, nDataSize);
}
size_t IFileHandle::Seek( ESeekMode eSeekMode, size_t nOffset )
{
return m_pFileSystem->Seek(this, eSeekMode, nOffset);
}
size_t IFileHandle::Tell( void )
{
return m_pFileSystem->Tell(this);
}
void IFileHandle::Close( void )
{
m_pFileSystem->Close(this);
}
class CFileSystem : public IFileSystem
{
public:
virtual void Init() override {
filesystem_backend->Init();
filesystem_pak->Init();
};
virtual void Shutdown() override {
};
virtual IFileHandle *Open( const char *szFileName, int eOpCode ) override
{
IFileHandle *pHandle = NULL;
pHandle = filesystem_pak->Open(szFileName, eOpCode);
if (pHandle) return pHandle;
pHandle = filesystem_backend->Open(szFileName, eOpCode);
if (pHandle) return pHandle;
return NULL;
}
virtual size_t Write( IFileHandle *pFile, const void *pData, size_t nDataSize ) override
{
return pFile->Write(pData, nDataSize);
}
virtual size_t Read( IFileHandle *pFile, void *pData, size_t nDataSize ) override
{
return pFile->Read(pData, nDataSize);
}
virtual size_t Seek( IFileHandle *pFile, ESeekMode eSeekMode, size_t nOffset ) override
{
return pFile->Seek(eSeekMode, nOffset);
}
virtual size_t Tell( IFileHandle *pFile ) override
{
return pFile->Tell();
}
virtual void Close( IFileHandle *pFile ) override
{
}
};
CFileSystem g_fileSystem;
IFileSystem *filesystem = &g_fileSystem;

View File

@@ -1,95 +0,0 @@
#include "ifilesystem.h"
#include "tier1/interface.h"
#include "tier0/lib.h"
class CLIBCFileHandle : public IFileHandle
{
public:
FILE *m_pFile;
};
class CLIBCFileSystem : public IFileSystem
{
public:
virtual void Init() override {};
virtual void Shutdown() override {};
virtual IFileHandle *Open( const char *szFileName, int eOpCode ) override
{
const char *szOperation;
FILE *pFile;
CLIBCFileHandle *pHandle = NULL;
switch (eOpCode)
{
case FILEMODE_READ:
szOperation = "r";
break;
default:
V_printf("Operation is not supported\n");
break;
}
pFile = V_fopen(szFileName, szOperation);
if (!pFile)
return NULL;
pHandle = new CLIBCFileHandle;
pHandle->m_pFileSystem = this;
pHandle->m_pFile = pFile;
return pHandle;
}
virtual size_t Write( IFileHandle *pFile, const void *pData, size_t nDataSize ) override
{
return 0;
}
virtual size_t Read( IFileHandle *pFile, void *pData, size_t nDataSize ) override
{
CLIBCFileHandle *pHandle = (CLIBCFileHandle*)pFile;
if (!pHandle)
return 0;
return V_fread( pData, 1, nDataSize, pHandle->m_pFile );
}
virtual size_t Seek( IFileHandle *pFile, ESeekMode eSeekMode, size_t nOffset ) override
{
CLIBCFileHandle *pHandle = (CLIBCFileHandle*)pFile;
int eLibcSeekMode = 0;
switch (eSeekMode) {
case SEEKMODE_SET:
eLibcSeekMode = SEEK_SET;
break;
case SEEKMODE_RELATIVE_END:
eLibcSeekMode = SEEK_END;
break;
case SEEKMODE_RELATIVE_START:
eLibcSeekMode = SEEK_CUR;
break;
}
if (!pHandle)
return 0;
return V_fseek( pHandle->m_pFile, eLibcSeekMode, nOffset );
}
virtual size_t Tell( IFileHandle *pFile ) override
{
CLIBCFileHandle *pHandle = (CLIBCFileHandle*)pFile;
if (!pHandle)
return 0;
return V_ftell(pHandle->m_pFile);
}
virtual void Close( IFileHandle *pFile ) override
{
CLIBCFileHandle *pHandle = (CLIBCFileHandle*)pFile;
if (!pHandle)
return;
V_fclose(pHandle->m_pFile);
delete pHandle;
}
};
CLIBCFileSystem g_libcFileSystem;
IFileSystem *filesystem_backend = &g_libcFileSystem;

View File

@@ -1,4 +1,4 @@
#include "ifilesystem.h"
#include "tier2/ifilesystem.h"
#include "tier1/interface.h"
#include "tier0/lib.h"
@@ -47,14 +47,14 @@ public:
{
int eLibcSeekMode = 0;
switch (eSeekMode) {
case SEEKMODE_SET:
eLibcSeekMode = SEEK_SET;
case SEEKMODE_RELATIVE_CURRENT:
eLibcSeekMode = SEEK_CUR;
break;
case SEEKMODE_RELATIVE_END:
eLibcSeekMode = SEEK_END;
break;
case SEEKMODE_RELATIVE_START:
eLibcSeekMode = SEEK_CUR;
eLibcSeekMode = SEEK_SET;
break;
}
CPAKFileHandle *pHandle = (CPAKFileHandle*)pFile;
@@ -68,6 +68,12 @@ public:
if (!pHandle)
return 0;
}
virtual size_t Size( IFileHandle *pFile ) override
{
CPAKFileHandle *pHandle = (CPAKFileHandle*)pFile;
if (!pHandle)
return 0;
}
virtual void Close( IFileHandle *pFile ) override
{

View File

@@ -11,7 +11,6 @@
#include "SDL3/SDL_vulkan.h"
#include "SDL3/SDL_events.h"
SDL_Window *g_pWindow;
class CSDLGameWindow: public IGameWindow
{
@@ -19,6 +18,20 @@ public:
virtual void Init() override;
virtual void Shutdown() override;
virtual void UpdateWindow() override;
virtual EGraphicsAPI GetGraphicsAPI() override;
virtual int GetVulkanInstanceExtensionCount() override;
virtual const char **GetVulkanInstanceExtensions() override;
virtual void CreateVulkanSurface( void *pInstance ) override;
virtual void DestroyVulkanSurface( void *pInstance ) override;
virtual void *GetVulkanSurface() override;
private:
SDL_Window *m_pWindow;
EGraphicsAPI m_ePreferredGraphicsAPI;
void *m_pVulkanSurface;
};
CSDLGameWindow g_sdlGameWindow;
@@ -27,12 +40,14 @@ IGameWindow *gamewindow = &g_sdlGameWindow;
void CSDLGameWindow::Init()
{
m_ePreferredGraphicsAPI = GRAPHICS_API_VULKAN;
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_GAMEPAD))
Plat_FatalErrorFunc("SDL_Init: %s\n", SDL_GetError());
g_pWindow = SDL_CreateWindow("funnygame", 1280, 720, SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE);
if (!g_pWindow)
Plat_FatalErrorFunc("SDL_Init: %s\n", SDL_GetError());
m_pWindow = SDL_CreateWindow("funnygame", 1280, 720, SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE);
if (!m_pWindow)
Plat_FatalErrorFunc("SDL_CreateWindow: %s\n", SDL_GetError());
}
void CSDLGameWindow::Shutdown()
@@ -47,6 +62,8 @@ void CSDLGameWindow::UpdateWindow()
{
switch (event.type)
{
case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED:
break;
case SDL_EVENT_QUIT:
SDL_Quit();
Plat_Exit(0);
@@ -56,3 +73,50 @@ void CSDLGameWindow::UpdateWindow()
}
}
}
EGraphicsAPI CSDLGameWindow::GetGraphicsAPI()
{
return GRAPHICS_API_VULKAN;
}
int CSDLGameWindow::GetVulkanInstanceExtensionCount()
{
if ( m_ePreferredGraphicsAPI != GRAPHICS_API_VULKAN )
return -1;
uint32_t nCount;
SDL_Vulkan_GetInstanceExtensions(&nCount);
return nCount;
}
const char **CSDLGameWindow::GetVulkanInstanceExtensions()
{
if ( m_ePreferredGraphicsAPI != GRAPHICS_API_VULKAN )
return 0;
uint32_t nCount;
return (const char **)SDL_Vulkan_GetInstanceExtensions(&nCount);
}
void CSDLGameWindow::CreateVulkanSurface( void *pInstance )
{
if ( m_ePreferredGraphicsAPI != GRAPHICS_API_VULKAN )
return;
SDL_Vulkan_CreateSurface(m_pWindow, (VkInstance)pInstance, NULL, (VkSurfaceKHR*)&m_pVulkanSurface);
}
void CSDLGameWindow::DestroyVulkanSurface( void *pInstance )
{
SDL_Vulkan_DestroySurface((VkInstance)pInstance, (VkSurfaceKHR)m_pVulkanSurface, NULL);
}
void *CSDLGameWindow::GetVulkanSurface()
{
if ( m_ePreferredGraphicsAPI != GRAPHICS_API_VULKAN )
return 0;
return m_pVulkanSurface;
}