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

6
.gitmodules vendored
View File

@@ -16,3 +16,9 @@
[submodule "external/GameNetworkingSockets"] [submodule "external/GameNetworkingSockets"]
path = external/GameNetworkingSockets path = external/GameNetworkingSockets
url = https://github.com/ValveSoftware/GameNetworkingSockets.git url = https://github.com/ValveSoftware/GameNetworkingSockets.git
[submodule "external/volk"]
path = external/volk
url = https://github.com/zeux/volk
[submodule "external/Vulkan-Utility-Libraries"]
path = external/Vulkan-Utility-Libraries
url = https://github.com/KhronosGroup/Vulkan-Utility-Libraries

View File

@@ -1,11 +1,14 @@
#include "build/tools/public/target.h"
#include "target.h" #include "target.h"
#include "tier1/utlstring.h"
#include "helper.h" #include "helper.h"
#include "tier1/utlstring.h"
CUtlVector<CUtlString> all_IncludeDirectories = { CUtlVector<CUtlString> all_IncludeDirectories = {
"public", "public",
"external", "external",
"external/Vulkan-Headers/include",
"external/Vulkan-Utility-Libraries/include",
"external/volk",
"external/cglm/include", "external/cglm/include",
"external/stb", "external/stb",
"external/SDL/include", "external/SDL/include",
@@ -71,6 +74,7 @@ CUtlString client_lib;
#include "tier0/__build.cpp" #include "tier0/__build.cpp"
#include "tier1/__build.cpp" #include "tier1/__build.cpp"
#include "tier2/__build.cpp"
#include "game/server/__build.cpp" #include "game/server/__build.cpp"
#include "game/client/__build.cpp" #include "game/client/__build.cpp"

View File

@@ -7,9 +7,7 @@
CUtlVector<CUtlString> engine_CompiledFiles = { CUtlVector<CUtlString> engine_CompiledFiles = {
"engine/engine.cpp", "engine/engine.cpp",
"engine/cvar.cpp", "engine/cvar.cpp",
"engine/filesystem.cpp",
"engine/filesystem_pak.cpp", "engine/filesystem_pak.cpp",
"engine/filesystem_libc.cpp",
"engine/gamewindow_sdl.cpp", "engine/gamewindow_sdl.cpp",
@@ -38,7 +36,9 @@ DECLARE_BUILD_STAGE(engine)
else else
{ {
ldProject.objects.AppendTail((Object_t){tier1_lib}); 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){rapier_lib});
ldProject.objects.AppendTail((Object_t){material_lib});
if (bSteam) if (bSteam)
ldProject.objects.AppendTail((Object_t){steam_lib}); ldProject.objects.AppendTail((Object_t){steam_lib});
ldProject.linkType = ELINK_DYNAMIC_LIBRARY; 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 "igamewindow.h"
#include "materialsystem/materialsystem.h"
#include "tier1/interface.h" #include "tier1/interface.h"
#include "tier1/commandline.h" #include "tier1/commandline.h"
#include "tier0/mem.h" #include "tier0/mem.h"
@@ -16,6 +17,8 @@ extern "C" void FunnyMain( int argc, char **argv )
ServerGameDLL()->Init(); ServerGameDLL()->Init();
Materials()->Init();
for (;;) { for (;;) {
gamewindow->UpdateWindow(); gamewindow->UpdateWindow();

View File

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

View File

@@ -11,7 +11,6 @@
#include "SDL3/SDL_vulkan.h" #include "SDL3/SDL_vulkan.h"
#include "SDL3/SDL_events.h" #include "SDL3/SDL_events.h"
SDL_Window *g_pWindow;
class CSDLGameWindow: public IGameWindow class CSDLGameWindow: public IGameWindow
{ {
@@ -19,6 +18,20 @@ public:
virtual void Init() override; virtual void Init() override;
virtual void Shutdown() override; virtual void Shutdown() override;
virtual void UpdateWindow() 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; CSDLGameWindow g_sdlGameWindow;
@@ -27,12 +40,14 @@ IGameWindow *gamewindow = &g_sdlGameWindow;
void CSDLGameWindow::Init() void CSDLGameWindow::Init()
{ {
m_ePreferredGraphicsAPI = GRAPHICS_API_VULKAN;
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_GAMEPAD)) if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_GAMEPAD))
Plat_FatalErrorFunc("SDL_Init: %s\n", SDL_GetError()); Plat_FatalErrorFunc("SDL_Init: %s\n", SDL_GetError());
g_pWindow = SDL_CreateWindow("funnygame", 1280, 720, SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE); m_pWindow = SDL_CreateWindow("funnygame", 1280, 720, SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE);
if (!g_pWindow) if (!m_pWindow)
Plat_FatalErrorFunc("SDL_Init: %s\n", SDL_GetError()); Plat_FatalErrorFunc("SDL_CreateWindow: %s\n", SDL_GetError());
} }
void CSDLGameWindow::Shutdown() void CSDLGameWindow::Shutdown()
@@ -47,6 +62,8 @@ void CSDLGameWindow::UpdateWindow()
{ {
switch (event.type) switch (event.type)
{ {
case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED:
break;
case SDL_EVENT_QUIT: case SDL_EVENT_QUIT:
SDL_Quit(); SDL_Quit();
Plat_Exit(0); 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;
}

1
external/volk vendored Submodule

Submodule external/volk added at d64d20b4ea

BIN
fpc/fpc_temp Normal file

Binary file not shown.

View File

@@ -34,7 +34,6 @@ LinkProject_t CClangCompiler::Compile( CProject_t *pProject )
{ {
if (!clangbackend && CommandLine()->CheckParam("-experimental_header_include")) if (!clangbackend && CommandLine()->CheckParam("-experimental_header_include"))
clangbackend = (IClangBackend*)CreateInterface(CLANG_BACKEND_INTERFACE_NAME, NULL); clangbackend = (IClangBackend*)CreateInterface(CLANG_BACKEND_INTERFACE_NAME, NULL);
V_printf("CLANG: %p\n", clangbackend);
if (pProject->m_szName == 0) if (pProject->m_szName == 0)
{ {
@@ -121,11 +120,12 @@ LinkProject_t CClangCompiler::Compile( CProject_t *pProject )
} }
if (clangbackend) if (clangbackend)
bAreDependenciesUpdated = clangbackend->AreFileDependenciesUpdated(file, szOutputFile, args); bAreDependenciesUpdated = clangbackend->AreFileDependenciesUpdated(file, 0, szOutputFile, args);
if (!filesystem2->ShouldRecompile(file, szOutputFile) && !bAreDependenciesUpdated) if (!filesystem2->ShouldRecompile(file, szOutputFile) && !bAreDependenciesUpdated)
goto skipcompile; goto skipcompile;
args.AppendTail("-c"); args.AppendTail("-c");
args.AppendTail("-g");
args.AppendTail("-o"); args.AppendTail("-o");
args.AppendTail(szOutputFile); args.AppendTail(szOutputFile);
args.AppendTail(file); args.AppendTail(file);
@@ -150,7 +150,7 @@ skipcompile:
if (pProject->m_target.kernel == TARGET_KERNEL_ANDROID) if (pProject->m_target.kernel == TARGET_KERNEL_ANDROID)
{ {
if (!pProject->m_target.szSysroot) if (!pProject->m_target.szSysroot)
Plat_FatalErrorFunc("-sysroot must be specified for android\n"); Plat_FatalErrorFunc("sysroot must be specified for android\n");
cfile.m_szArguments.AppendHead(CUtlString("%s/bin/clang",pProject->m_target.szSysroot)); cfile.m_szArguments.AppendHead(CUtlString("%s/bin/clang",pProject->m_target.szSysroot));
} }
else else

View File

@@ -10,10 +10,11 @@
class CClangBackend: public IClangBackend class CClangBackend: public IClangBackend
{ {
public: public:
virtual bool AreFileDependenciesUpdated( CUtlString szFile, CUtlString szOutputFile, CUtlVector<CUtlString> arguments ) override; virtual bool AreFileDependenciesUpdated( CUtlString szFile, CUtlString szOutputFile, CUtlString szHashFile, CUtlVector<CUtlString> arguments ) override;
bool bAnyOutdated; bool bAnyOutdated;
CUtlString szCurrentFile; CUtlString szCurrentFile;
CUtlString szHashFile;
private: private:
static void InclusionVisitor( CXFile included_file, CXSourceLocation *include_stack, unsigned include_len, CXClientData client_data ); static void InclusionVisitor( CXFile included_file, CXSourceLocation *include_stack, unsigned include_len, CXClientData client_data );
@@ -21,7 +22,7 @@ private:
EXPOSE_INTERFACE(CClangBackend, IClangBackend, CLANG_BACKEND_INTERFACE_NAME); EXPOSE_INTERFACE(CClangBackend, IClangBackend, CLANG_BACKEND_INTERFACE_NAME);
bool CClangBackend::AreFileDependenciesUpdated( CUtlString szFile, CUtlString szOutputFile, CUtlVector<CUtlString> arguments ) bool CClangBackend::AreFileDependenciesUpdated( CUtlString szFile, CUtlString szOutputFile, CUtlString szHashFile, CUtlVector<CUtlString> arguments )
{ {
szCurrentFile = szOutputFile; szCurrentFile = szOutputFile;
bAnyOutdated = false; bAnyOutdated = false;

View File

@@ -9,7 +9,7 @@
abstract_class IClangBackend abstract_class IClangBackend
{ {
public: public:
virtual bool AreFileDependenciesUpdated( CUtlString szFile, CUtlString szOutputFile, CUtlVector<CUtlString> arguments ) = 0; virtual bool AreFileDependenciesUpdated( CUtlString szFile, CUtlString szOutputFile, CUtlString szHashFile, CUtlVector<CUtlString> arguments ) = 0;
}; };
extern IClangBackend *clangbackend; extern IClangBackend *clangbackend;

View File

@@ -53,7 +53,7 @@ CUtlString CClangLinker::Link( LinkProject_t *pProject )
filesystem2->MakeDirectory(szOutputDir); filesystem2->MakeDirectory(szOutputDir);
if (pProject->linkType == ELINK_STATIC_LIBRARY) if (pProject->linkType == ELINK_STATIC_LIBRARY)
{ {
V_printf(" AR %s\n", pProject->m_szName.GetString()); V_printf(" AR %s\n", pProject->m_szName.GetString());
bool shouldRecompile = false; bool shouldRecompile = false;
CUtlVector<CUtlString> args; CUtlVector<CUtlString> args;
for (auto object: pProject->objects) for (auto object: pProject->objects)

View File

@@ -5,10 +5,10 @@
#include "tier1/commandline.h" #include "tier1/commandline.h"
CUtlVector<CUtlString> MaterialSystem_CompiledFiles = { CUtlVector<CUtlString> MaterialSystem_CompiledFiles = {
"materialsystem/materialsystem.cpp",
"materialsystem/vulkan/rendercontext.cpp",
"materialsystem/vulkan/material.cpp", "materialsystem/vulkan/material.cpp",
"materialsystem/vulkan/materialsystem.cpp", "external/volk/volk.c",
"materialsystem/vulkan/shader.cpp",
"materialsystem/vulkan/vulkan_state.cpp",
}; };
CUtlString material_lib; CUtlString material_lib;
@@ -23,6 +23,7 @@ DECLARE_BUILD_STAGE(MaterialSystem)
compileProject.bFPIC = true; compileProject.bFPIC = true;
ldProject = ccompiler->Compile(&compileProject); ldProject = ccompiler->Compile(&compileProject);
ldProject.linkType = ELINK_STATIC_LIBRARY; ldProject.linkType = ELINK_STATIC_LIBRARY;
ldProject.libraries = { "vulkan" };
CUtlString outputProject = linker->Link(&ldProject); CUtlString outputProject = linker->Link(&ldProject);
material_lib = outputProject; material_lib = outputProject;

View File

@@ -0,0 +1,41 @@
#include "materialsystem/materialsystem.h"
class CMaterialSystem: public IMaterialSystem
{
public:
virtual void Init() override;
virtual void Frame( float fTime ) override;
virtual void Shutdown() override;
virtual IRenderContext *GetRenderContext( void ) override;
};
void CMaterialSystem::Init()
{
GetRenderContext()->Init();
}
void CMaterialSystem::Frame( float fTime )
{
GetRenderContext()->Frame(fTime);
}
void CMaterialSystem::Shutdown()
{
GetRenderContext()->Shutdown();
}
extern IRenderContext *g_pVkRenderContext;
IRenderContext *CMaterialSystem::GetRenderContext( void )
{
return g_pVkRenderContext;
}
IMaterialSystem *Materials( void )
{
static CMaterialSystem s_materialSystem;
return &s_materialSystem;
}

View File

View File

@@ -0,0 +1,2 @@
REQUIRED_EXTENSION(VK_KHR_SWAPCHAIN)
OPTIONAL_EXTENSION(VK_KHR_RAY_TRACING_PIPELINE)

View File

@@ -1,40 +0,0 @@
#include "materialsystem/imaterialsystem.h"
class CVkRenderingContext
{
};
CVkRenderingContext g_vkRenderingContext;
class CVkMaterialSystem: public IMaterialSystem
{
public:
virtual void Init();
virtual void Frame( float fDeltaTime );
virtual void Shutdown();
IRenderContext *GetRenderContext();
};
CVkMaterialSystem g_vkMaterialSystem;
IMaterialSystem *materials = &g_vkMaterialSystem;
void CVkMaterialSystem::Init()
{
}
void CVkMaterialSystem::Frame( float fDeltaTime )
{
}
void CVkMaterialSystem::Shutdown()
{
}

View File

@@ -0,0 +1,274 @@
#include "SDL3/SDL_vulkan.h"
#include "materialsystem/materialsystem.h"
#include "tier0/platform.h"
#include "tier1/utlvector.h"
#define VK_NO_PROTOTYPES
#include "vulkan/vulkan_core.h"
#include "vulkan_state.h"
#include "igamewindow.h"
#define REQUIRED_EXTENSION(ext) ext##_EXTENSION_NAME,
#define OPTIONAL_EXTENSION(ext) ext##_EXTENSION_NAME,
const char *g_vkDeviceExtensions[] = {
#include "device_extensions.h"
};
#undef REQUIRED_EXTENSION
#undef OPTIONAL_EXTENSION
class CVkRenderContext: public IRenderContext
{
public:
virtual void Init() override;
virtual void Frame( float fDeltaTime ) override;
virtual void Shutdown() override;
virtual IVertexBuffer *CreateVertexBuffer( uint32_t nSize ) override;
virtual IIndexBuffer *CreateIndexBuffer( uint32_t nSize ) override;
virtual IImage *CreateRenderTarget( uint32_t x, uint32_t y, EImageFormat eFormat, EMultisampleType eMultisampleType ) override;
virtual IImage *CreateStorageImage( uint32_t x, uint32_t y, EImageFormat eFormat, EMultisampleType eMultisampleType ) override;
virtual void DestroyBuffer( IBuffer *pBuffer ) override;
virtual void DestroyImage( IImage *pImage ) override;
private:
IBuffer *CreateBuffer( uint32_t nSize, VkBufferUsageFlags2 eUsage );
IBuffer *CreateBufferAligned( uint32_t nSize, uint32_t nAlignment, VkBufferUsageFlags2 eUsage );
VkPhysicalDevice SelectPhysicalDevice( CUtlVector<VkPhysicalDevice> physicalDevices );
void CreateSwapchain();
void DestroySwapchain();
};
//-----------------------------------------------------------------------------
// Creates vertex buffer. Wrapper over CreateBuffer
//-----------------------------------------------------------------------------
IVertexBuffer *CVkRenderContext::CreateVertexBuffer( uint32_t nSize )
{
return (IVertexBuffer*)CreateBuffer(nSize, VK_BUFFER_USAGE_2_VERTEX_BUFFER_BIT);
}
//-----------------------------------------------------------------------------
// Creates index buffer. Wrapper over CreateBuffer
//-----------------------------------------------------------------------------
IIndexBuffer *CVkRenderContext::CreateIndexBuffer( uint32_t nSize )
{
return (IIndexBuffer*)CreateBuffer(nSize, VK_BUFFER_USAGE_2_INDEX_BUFFER_BIT);
}
//-----------------------------------------------------------------------------
// Creates basic vulkan buffer
//-----------------------------------------------------------------------------
IBuffer *CreateBuffer( uint32_t nSize, VkBufferUsageFlags2 eUsage )
{
}
//-----------------------------------------------------------------------------
// Creates vulkan buffer aligned to the nAlignment
// Useful for everything eg: ray tracing, which requires them to be aligned
// to the groupBaseAlignment.
//-----------------------------------------------------------------------------
IBuffer *CreateBufferAligned( uint32_t nSize, uint32_t nAlignment, VkBufferUsageFlags2 eUsage )
{
}
IImage *CVkRenderContext::CreateRenderTarget( uint32_t x, uint32_t y, EImageFormat eFormat, EMultisampleType eMultisampleType )
{
}
IImage *CVkRenderContext::CreateStorageImage( uint32_t x, uint32_t y, EImageFormat eFormat, EMultisampleType eMultisampleType )
{
}
void CVkRenderContext::DestroyBuffer( IBuffer *pBuffer )
{
}
void CVkRenderContext::DestroyImage( IImage *pImage )
{
}
CVkRenderContext s_vkRenderContext;
IRenderContext *g_pVkRenderContext = &s_vkRenderContext;
uint32_t g_iDrawFamily;
uint32_t g_iPresentFamily;
VkInstance g_vkInstance;
VkPhysicalDevice g_vkPhysicalDevice;
VkDevice g_vkDevice;
VkSwapchainKHR g_vkSwapchain;
void CVkRenderContext::Init()
{
VkResult r;
int nExtensionCount;
CUtlVector<const char *> extensions = {};
uint32_t nPhysicalDevicesCount;
CUtlVector<VkPhysicalDevice> physicalDevices;
uint32_t nNumQueueFamilies = 0;
CUtlVector<VkQueueFamilyProperties> queueFamilyProperties;
VkApplicationInfo stApplicationInfo = {};
VkInstanceCreateInfo stInstanceCreateInfo = {};
VkDeviceQueueCreateInfo stDeviceQueueCreateInfo = {};
VkDeviceCreateInfo stDeviceCreateInfo = {};
float fPriority = 1.0;
r = volkInitialize();
VULKAN_RESULT_PRINT(r, volkInitialize)
// Get extensions required by game window
nExtensionCount = gamewindow->GetVulkanInstanceExtensionCount();
extensions.Resize(nExtensionCount);
V_memcpy(extensions.GetData(), gamewindow->GetVulkanInstanceExtensions(), extensions.GetSize()*sizeof(const char*));
// Create instance
stApplicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
stApplicationInfo.apiVersion = VK_API_VERSION_1_4;
stApplicationInfo.pApplicationName = "funny";
stApplicationInfo.pEngineName = "funny";
stInstanceCreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
stInstanceCreateInfo.pApplicationInfo = &stApplicationInfo;
stInstanceCreateInfo.enabledExtensionCount = extensions.GetSize();
stInstanceCreateInfo.ppEnabledExtensionNames = extensions.GetData();
r = vkCreateInstance(&stInstanceCreateInfo, NULL, &g_vkInstance);
VULKAN_RESULT_PRINT(r, vkCreateInstance)
// volk requires to load instance this way
volkLoadInstance(g_vkInstance);
// Get amount of physical devices
r = vkEnumeratePhysicalDevices(g_vkInstance, &nPhysicalDevicesCount, NULL);
VULKAN_RESULT_PRINT(r, vkEnumeratePhysicalDevices)
physicalDevices.Resize(nPhysicalDevicesCount);
// Read all physical devices
r = vkEnumeratePhysicalDevices(g_vkInstance, &nPhysicalDevicesCount, physicalDevices.GetData());
VULKAN_RESULT_PRINT(r, vkEnumeratePhysicalDevices)
g_vkPhysicalDevice = SelectPhysicalDevice(physicalDevices);
// Get all queues
vkGetPhysicalDeviceQueueFamilyProperties(g_vkPhysicalDevice, &nNumQueueFamilies, NULL);
queueFamilyProperties.Resize(nNumQueueFamilies);
uint32_t i = 0;
vkGetPhysicalDeviceQueueFamilyProperties(g_vkPhysicalDevice, &nNumQueueFamilies, queueFamilyProperties.GetData());
for (auto &family: queueFamilyProperties)
{
if (family.queueFlags & VK_QUEUE_GRAPHICS_BIT)
{
g_iDrawFamily = i;
g_iPresentFamily = i;
}
i++;
}
// Create device
stDeviceQueueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
stDeviceQueueCreateInfo.queueCount = 1;
stDeviceQueueCreateInfo.pQueuePriorities = &fPriority;
stDeviceQueueCreateInfo.queueFamilyIndex = g_iDrawFamily;
stDeviceCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
stDeviceCreateInfo.queueCreateInfoCount = 1;
stDeviceCreateInfo.pQueueCreateInfos = &stDeviceQueueCreateInfo;
vkCreateDevice(g_vkPhysicalDevice, &stDeviceCreateInfo, NULL, &g_vkDevice);
CreateSwapchain();
}
void CVkRenderContext::Frame( float fDeltaTime )
{
}
void CVkRenderContext::CreateSwapchain()
{
gamewindow->CreateVulkanSurface(g_vkInstance);
}
void CVkRenderContext::DestroySwapchain()
{
vkDestroySwapchainKHR(g_vkDevice, g_vkSwapchain, NULL);
gamewindow->DestroyVulkanSurface(g_vkInstance);
}
//-----------------------------------------------------------------------------
// TODO: Move to the rendering context
//-----------------------------------------------------------------------------
void CVkRenderContext::Shutdown()
{
vkDestroyInstance(g_vkInstance, NULL);
}
//-----------------------------------------------------------------------------
// Selects best device to be used in rendering.
// We want to get the best perfomance out of the GPU, so we need to select the
// best driver.
//-----------------------------------------------------------------------------
VkPhysicalDevice CVkRenderContext::SelectPhysicalDevice( CUtlVector<VkPhysicalDevice> physicalDevices )
{
uint32_t uMaxScore = 0;
VkPhysicalDevice selectedDevice = 0;
VkResult r;
for (auto &device: physicalDevices)
{
uint32_t nExtensionCount;
CUtlVector<VkExtensionProperties> extensions;
VkPhysicalDeviceFeatures stPhysicalDeviceFeatures;
VkPhysicalDeviceProperties stPhysicalDeviceProperties;
uint32_t uScore = 0;
r = vkEnumerateDeviceExtensionProperties(device, NULL, &nExtensionCount, NULL);
VULKAN_RESULT_PRINT(r, vkEnumeratePhysicalDevices)
extensions.Resize(nExtensionCount);
r = vkEnumerateDeviceExtensionProperties(device, NULL, &nExtensionCount, extensions.GetData());
VULKAN_RESULT_PRINT(r, vkEnumeratePhysicalDevices)
vkGetPhysicalDeviceProperties(device, &stPhysicalDeviceProperties);
vkGetPhysicalDeviceFeatures(device, &stPhysicalDeviceFeatures);
if (stPhysicalDeviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU)
uScore += 1000;
if (stPhysicalDeviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU)
uScore += 500;
if (uScore >= uMaxScore)
{
selectedDevice = device;
uMaxScore = uScore;
}
}
return selectedDevice;
}

View File

@@ -0,0 +1,57 @@
#ifndef VULKAN_STATE_H
#define VULKAN_STATE_H
#include "volk.h"
#include "vk_mem_alloc.h"
#include "tier0/platform.h"
#include "iappsystem.h"
#include "materialsystem/materialsystem.h"
#define REQUIRED_EXTENSION(ext) bool bIsSupported_##ext;
#define OPTIONAL_EXTENSION(ext) bool bIsSupported_##ext;
struct SupportedVulkanExtensions_t
{
#include "device_extensions.h"
};
#undef REQUIRED_EXTENSION
#undef OPTIONAL_EXTENSION
enum EDependencyMode
{
DEPENDENCY_MODE_SHADER_IMAGE,
DEPENDENCY_MODE_SHADER_BUFFER,
DEPENDENCY_MODE_SHADER_ACCELERATION_STRUCTURE,
DEPENDENCY_MODE_DRAWCALL_VERTEX_BUFFER,
DEPENDENCY_MODE_DRAWCALL_INDEX_BUFFER,
DEPENDENCY_MODE_DRAWCALL_OUTPUT_IMAGE,
};
abstract_class IVkCommand
{
public:
virtual void Call() = 0;
void AddDependency( IRenderingObject *pObject, EDependencyMode eDependencyMode );
};
abstract_class IVkCommandBuffer: public IAppSystem
{
public:
void PushCommand( IVkCommand *pCommand );
void Submit();
};
extern IVkCommandBuffer *vkcommandbuffer;
#undef __cplusplus
#include "vulkan/vk_enum_string_helper.h"
#define __cplusplus
#define VULKAN_RESULT_PRINT(r, func) \
if (r != VK_SUCCESS) \
Plat_FatalErrorFunc(#func " failed: %s\n", string_VkResult(r));
#endif

View File

@@ -2,7 +2,7 @@
#define FUNNY_FORMAT_H #define FUNNY_FORMAT_H
#include "tier0/platform.h" #include "tier0/platform.h"
#include "ifilesystem.h" #include "tier2/ifilesystem.h"
#include "tier1/utlvector.h" #include "tier1/utlvector.h"
struct FunnyFormatParameter_t struct FunnyFormatParameter_t

View File

@@ -0,0 +1,119 @@
#ifndef CONSOLE_H
#define CONSOLE_H
#include "iappsystem.h"
#include "tier0/platform.h"
#include "tier1/utlstring.h"
#include "tier1/utlvector.h"
class ConVar;
class ConCommand;
typedef void(*ConCommandFn)(int argc, char **argv);
abstract_class IConsole: public IAppSystem
{
public:
// Variables
virtual void RegisterVar( ConVar *cvar ) = 0;
virtual void UnRegisterVar( ConVar *cvar ) = 0;
virtual ConVar *FindVar( const char *pName ) = 0;
// Commands
virtual void RegisterCommand( ConCommand *cvar ) = 0;
virtual void UnRegisterCommand( ConCommand *cvar ) = 0;
virtual ConCommand *FindCommand( const char *pName ) = 0;
// Command buffer
virtual void Execute( void ) = 0;
virtual void ExecuteArguments( CUtlVector<CUtlString> &args ) = 0;
virtual CUtlVector<CUtlVector<CUtlString>> ParseCommandLine( CUtlString psz ) = 0;
virtual void AddCommand( const char *psz ) = 0;
virtual void InsertCommand( const char *psz ) = 0;
CUtlVector<ConVar*> m_convars;
CUtlVector<ConCommand*> m_commands;
};
IConsole *Console();
#define FCVAR_NONE 0
#define FCVAR_DEVELOPMENTONLY 0x1
#define FCVAR_GAMEDLL 0x2
#define FCVAR_CLIENTDLL 0x4
#define FCVAR_HIDDEN 0x8
#define FCVAR_PROTECTED 0x10
#define FCVAR_SPONLY 0x20
#define FCVAR_ARCHIVE 0x40
#define FCVAR_NOTIFY 0x80
#define FCVAR_CHEAT 0x100
#define FCVAR_REPLICATED 0x200
class ConVar
{
public:
ConVar( const char *pName, const char *pDefaultValue, int flags );
ConVar( const char *pName, const char *pDefaultValue, int flags,
const char *pHelpString );
ConVar( const char *pName, const char *pDefaultValue, int flags,
const char *pHelpString, ConCommandFn callback );
bool IsFlagSet( int flag );
const char *GetHelpText( void );
bool IsRegistered( void );
const char *GetName( void );
void AddFlags( int flags );
bool IsCommand( void );
void InstallChangeCallback( ConCommandFn );
float GetFloat( void );
int GetInt( void );
bool GetBool( void );
const char *GetString( void );
void SetValue( const char *szValue );
void SetValue( float fValue );
void SetValue( int iValue );
private:
CUtlString m_szName;
CUtlString m_szHelpString;
CUtlString m_szDefaultValue;
CUtlString m_szValue;
float m_fValue;
int m_nValue;
int m_flags;
};
class ConCommand
{
public:
ConCommand(const char *pName, ConCommandFn callback,
const char *pHelpString=0, int flags=0);
const char *GetName( void );
const char *GetHelpText( void );
ConCommandFn GetCallback( void );
private:
CUtlString m_szName;
CUtlString m_szHelpString = NULL;
ConCommandFn m_callback;
int m_flags;
};
#undef V_printf
#define V_printf(...) Msg(CUtlString(__VA_ARGS__).GetString())
void Msg( const char* message );
void Warning( const char* message );
void Error( const char* message );
#endif

View File

@@ -4,10 +4,24 @@
#include "iappsystem.h" #include "iappsystem.h"
#include "tier0/platform.h" #include "tier0/platform.h"
enum EGraphicsAPI
{
GRAPHICS_API_METAL,
GRAPHICS_API_VULKAN,
};
abstract_class IGameWindow: public IAppSystem abstract_class IGameWindow: public IAppSystem
{ {
public: public:
virtual void UpdateWindow() = 0; virtual void UpdateWindow() = 0;
virtual EGraphicsAPI GetGraphicsAPI() = 0;
virtual int GetVulkanInstanceExtensionCount() = 0;
virtual const char **GetVulkanInstanceExtensions() = 0;
virtual void CreateVulkanSurface( void *pInstance ) = 0;
virtual void DestroyVulkanSurface( void *pInstance ) = 0;
virtual void *GetVulkanSurface() = 0;
}; };
extern IGameWindow *gamewindow; extern IGameWindow *gamewindow;

View File

@@ -61,31 +61,30 @@ enum EMultisampleType
abstract_class IImage : public IRenderingObject abstract_class IImage : public IRenderingObject
{ {
public: public:
virtual void BlitTo( IImage *pImage );
}; };
abstract_class IRenderContext: public IAppSystem abstract_class IRenderContext: public IAppSystem
{ {
public: public:
virtual void Frame( float fTime ) = 0;
virtual IVertexBuffer *CreateVertexBuffer( uint32_t nSize ) = 0; virtual IVertexBuffer *CreateVertexBuffer( uint32_t nSize ) = 0;
virtual IIndexBuffer *CreateIndexBuffer( uint32_t nSize ) = 0; virtual IIndexBuffer *CreateIndexBuffer( uint32_t nSize ) = 0;
virtual IImage *CreateRenderTarget( uint32_t x, uint32_t y, EImageFormat eFormat, EMultisampleType eMultisampleType ); virtual IImage *CreateRenderTarget( uint32_t x, uint32_t y, EImageFormat eFormat, EMultisampleType eMultisampleType ) = 0;
virtual IImage *CreateStorageImage( uint32_t x, uint32_t y, EImageFormat eFormat, EMultisampleType eMultisampleType ); virtual IImage *CreateStorageImage( uint32_t x, uint32_t y, EImageFormat eFormat, EMultisampleType eMultisampleType ) = 0;
virtual void DestroyBuffer( IBuffer *pBuffer ); virtual void DestroyBuffer( IBuffer *pBuffer ) = 0;
virtual void DestroyImage( IImage *pImage ); virtual void DestroyImage( IImage *pImage ) = 0;
}; };
abstract_class IMaterialSystem: public IAppSystem abstract_class IMaterialSystem: public IAppSystem
{ {
public: public:
virtual void Init(); virtual void Frame( float fTime ) = 0;
virtual void Frame( float fTime );
virtual void Shutdown();
IRenderContext *GetRenderContext(); virtual IRenderContext *GetRenderContext( void) = 0;
}; };
extern IMaterialSystem *materials; IMaterialSystem *Materials( void );
#endif #endif

View File

@@ -13,6 +13,7 @@
#include "string.h" #include "string.h"
#include "stdio.h" #include "stdio.h"
#include "stdlib.h" #include "stdlib.h"
#include "ctype.h"
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// string.h // string.h
@@ -106,4 +107,24 @@
#define V_atoi atoi #define V_atoi atoi
#define V_atof atof #define V_atof atof
//-----------------------------------------------------------------------------
// ctype.h
//-----------------------------------------------------------------------------
#define V_isalnum isalnum
#define V_isalpha isalpha
#define V_isblank isblank
#define V_iscntrl iscntrl
#define V_isdigit isdigit
#define V_isgraph isgraph
#define V_islower islower
#define V_isprint isprint
#define V_ispunct ispunct
#define V_isspace isspace
#define V_isupper isupper
#define V_isxdigit isxdigit
#define V_tolower tolower
#define V_toupper toupper
#endif #endif

View File

@@ -23,10 +23,11 @@ template <typename T>
class CUtlBuffer class CUtlBuffer
{ {
public: public:
CUtlBuffer( void ); CUtlBuffer( void );
CUtlBuffer( size_t nSize ); CUtlBuffer( size_t nSize );
CUtlBuffer( const CUtlBuffer<T>& buffer ); CUtlBuffer( const CUtlBuffer<T>& buffer );
CUtlBuffer( const CUtlResizableBuffer<T>& buffer ); CUtlBuffer( const CUtlResizableBuffer<T>& buffer );
~CUtlBuffer( void );
size_t GetSize( void ) const; size_t GetSize( void ) const;
T* GetMemory(void) const; T* GetMemory(void) const;
@@ -81,6 +82,12 @@ CUtlBuffer<T>::CUtlBuffer( const CUtlResizableBuffer<T>& buffer ) : m_nSize(buff
V_memcpy(m_pData,buffer.pData,sizeof(T)*buffer.nSize); V_memcpy(m_pData,buffer.pData,sizeof(T)*buffer.nSize);
} }
template <typename T>
CUtlBuffer<T>::~CUtlBuffer()
{
if ( m_pData != 0)
V_free(m_pData);
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Gets memory size. // Gets memory size.
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@@ -145,9 +152,10 @@ CUtlBuffer<T>& CUtlBuffer<T>::operator=(const CUtlBuffer<T>& other)
if ( m_pData != 0) if ( m_pData != 0)
V_free(m_pData); V_free(m_pData);
m_pData = (T*)V_malloc(sizeof(T)*other.m_nSize); m_pData = (T*)V_malloc(sizeof(T)*other.m_nSize);
m_nSize = other.m_nSize;
V_memcpy(m_pData, other.m_pData, sizeof(T)*other.m_nSize); V_memcpy(m_pData, other.m_pData, sizeof(T)*other.m_nSize);
} }
return this; return *this;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

View File

@@ -3,6 +3,7 @@
#include "tier0/platform.h" #include "tier0/platform.h"
#include "tier1/utlstring.h" #include "tier1/utlstring.h"
#include "tier1/utlvector.h"
abstract_class IINISection abstract_class IINISection
{ {
@@ -16,7 +17,7 @@ public:
abstract_class IINIFile abstract_class IINIFile
{ {
public: public:
virtual void GetSection( const char *szSectionName ) = 0; virtual IINISection *GetSection( const char *szSectionName ) = 0;
}; };
abstract_class IINIManager abstract_class IINIManager

View File

@@ -11,15 +11,9 @@ enum EFileMode
FILEMODE_APPEND = 0x04, FILEMODE_APPEND = 0x04,
}; };
enum EFileType {
FILETYPE_NONE,
FILETYPE_SYSTEM,
FILETYPE_PAK,
};
enum ESeekMode enum ESeekMode
{ {
SEEKMODE_SET, SEEKMODE_RELATIVE_CURRENT,
SEEKMODE_RELATIVE_START, SEEKMODE_RELATIVE_START,
SEEKMODE_RELATIVE_END, SEEKMODE_RELATIVE_END,
}; };
@@ -35,6 +29,7 @@ public:
size_t Read( void *pData, size_t nDataSize ); size_t Read( void *pData, size_t nDataSize );
size_t Seek( ESeekMode eSeekMode, size_t nOffset ); size_t Seek( ESeekMode eSeekMode, size_t nOffset );
size_t Tell( void ); size_t Tell( void );
size_t Size( void );
void Close( void ); void Close( void );
@@ -56,12 +51,28 @@ public:
virtual size_t Seek( IFileHandle *pFile, ESeekMode eSeekMode, size_t nOffset ) = 0; virtual size_t Seek( IFileHandle *pFile, ESeekMode eSeekMode, size_t nOffset ) = 0;
virtual size_t Tell( IFileHandle *pFile ) = 0; virtual size_t Tell( IFileHandle *pFile ) = 0;
virtual size_t Size( IFileHandle *pFile ) = 0;
virtual void Close( IFileHandle *pFile ) = 0; virtual void Close( IFileHandle *pFile ) = 0;
IFileSystem *m_pNext;
void RegisterFileSystem();
}; };
extern IFileSystem *filesystem; extern IFileSystem *filesystem;
extern IFileSystem *filesystem_backend; extern IFileSystem *g_pFileSystems;
extern IFileSystem *filesystem_pak;
typedef IFileSystem *( *InstantiateFileSystemFn )( void );
class CFileSystemRegistry
{
public:
CFileSystemRegistry( InstantiateFileSystemFn fn, const char *szFileSystem );
};
#define EXPOSE_FILESYSTEM( className, filesystemName ) \
static IFileSystem *__Create##className##_filesystem() { return ( IFileSystem* )( new className ); }; \
static CFileSystemRegistry __Create##className##_registry( __Create##className##_filesystem, filesystemName );
#endif #endif

35
tests/ini/build.cpp Normal file
View File

@@ -0,0 +1,35 @@
#include "c.h"
#include "target.h"
#include "helper.h"
DECLARE_BUILD_STAGE(ini_test)
{
CProject_t p = {
.files = {
"../../tier0/lib.cpp",
"../../tier0/mem.cpp",
"../../tier0/platform.cpp",
"../../tier1/interface.cpp",
"../../tier1/utlbuffer.cpp",
"../../tier1/utlstring.cpp",
"../../tier1/utlvector.cpp",
"../../tier1/utlmap.cpp",
"../../tier1/commandline.cpp",
"../../tier2/fileformats/ini.cpp",
"../../tier2/filesystem.cpp",
"../../tier2/filesystem_libc.cpp",
"main.cpp",
},
.includeDirectories =
{
"../../public"
},
};
p.m_szName = "initest";
LinkProject_t l = ccompiler->Compile(&p);
CUtlString szPath = linker->Link(&l);
filesystem2->CopyFile("./", szPath);
return 0;
}

BIN
tests/ini/initest Normal file

Binary file not shown.

49
tests/ini/main.cpp Normal file
View File

@@ -0,0 +1,49 @@
#include "tier0/lib.h"
#include "tier0/mem.h"
#include "tier1/utlbuffer.h"
#include "tier1/commandline.h"
#include "tier2/fileformats/ini.h"
#include "tier2/ifilesystem.h"
int main( int argc, char **argv )
{
const char *szFileName;
IFileHandle *pHandle = NULL;
size_t nSize = 0;
CUtlBuffer<char> szData;
CommandLine()->CreateCommandLine(argc, argv);
filesystem->Init();
// Get file
szFileName = CommandLine()->ParamValue("-f");
if (!szFileName)
return 0;
// Load it
pHandle = filesystem->Open(szFileName, FILEMODE_READ);
if (!pHandle)
return 0;
// Get size
pHandle->Seek(SEEKMODE_RELATIVE_END, 0);
nSize = pHandle->Tell();
pHandle->Seek(SEEKMODE_RELATIVE_START, 0);
// Read it
szData = CUtlBuffer<char>(nSize+1);
pHandle->Read(szData, nSize);
pHandle->Close();
szData[nSize] = 0;
IINIFile *pIniFile = INIManager()->ReadString(szData);
if (!pIniFile)
return 0;
IINISection *pTestValues = pIniFile->GetSection("Test_Values");
if (!pTestValues)
return 0;
V_printf("%s\n",pTestValues->GetStringValue("abc3d"));
};

12
tests/ini/test1.ini Normal file
View File

@@ -0,0 +1,12 @@
[Test_Values]
a = "Hello world!\n"
b = true
c = false
c = 3.1415926
d = 123456
e = -2.71828
f = -890
g = .34
abc3d = "Hello world from the funny engine, you can use \" to add quotes"

1
tests/ini/test2.ini Normal file
View File

@@ -0,0 +1 @@
[

1
tests/ini/test3.ini Normal file
View File

@@ -0,0 +1 @@
[Hello

1
tests/ini/test4.ini Normal file
View File

@@ -0,0 +1 @@
[Test Hello]

2
tests/ini/test5.ini Normal file
View File

@@ -0,0 +1,2 @@
["Hello world"]
Test

2
tests/ini/test6.ini Normal file
View File

@@ -0,0 +1,2 @@
[Test]
a b

2
tests/ini/test7.ini Normal file
View File

@@ -0,0 +1,2 @@
[Test]
a =

5
tests/ini/test8.ini Normal file
View File

@@ -0,0 +1,5 @@
[Test]
a = b
c = d
d = test

31
tier2/__build.cpp Normal file
View File

@@ -0,0 +1,31 @@
#include "helper.h"
#include "c.h"
#include "ld.h"
#include "tier1/utlstring.h"
#include "tier1/commandline.h"
CUtlVector<CUtlString> tier2_CompiledFiles = {
"tier2/fileformats/ini.cpp",
"tier2/fileformats/json.cpp",
"tier2/filesystem.cpp",
"tier2/filesystem_libc.cpp",
};
CUtlString tier2_lib;
DECLARE_BUILD_STAGE(tier2)
{
CProject_t compileProject = {};
LinkProject_t ldProject = {};
compileProject.m_szName = "tier2";
compileProject.files = tier2_CompiledFiles;
compileProject.includeDirectories = all_IncludeDirectories;
compileProject.bFPIC = true;
ldProject = ccompiler->Compile(&compileProject);
ldProject.linkType = ELINK_STATIC_LIBRARY;
CUtlString outputProject = linker->Link(&ldProject);
tier2_lib = outputProject;
return 0;
};

View File

@@ -1,7 +1,27 @@
#include "tier2/fileformats/ini.h" #include "tier2/fileformats/ini.h"
#include "tier0/platform.h"
#include "tier1/utlstring.h" #include "tier1/utlstring.h"
#include "tier1/utlvector.h" #include "tier1/utlvector.h"
#include "tier0/lib.h"
enum ESectionType
{
SECTIONTYPE_String,
SECTIONTYPE_Boolean,
SECTIONTYPE_Float,
};
struct SectionData_t
{
ESectionType m_eSectionType;
CUtlString m_szKey;
bool m_bData;
float m_fData;
CUtlString m_szData;
};
abstract_class CINISection: public IINISection abstract_class CINISection: public IINISection
{ {
@@ -11,27 +31,247 @@ public:
virtual const char *GetStringValue( const char *szKeyName ) override; virtual const char *GetStringValue( const char *szKeyName ) override;
virtual CUtlString GetUTLStringValue( const char *szKeyName ) override; virtual CUtlString GetUTLStringValue( const char *szKeyName ) override;
CUtlVector<CUtlString> m_values; CUtlVector<SectionData_t> m_values;
CUtlString m_szSectionName;
}; };
bool CINISection::GetBoolValue( const char *szKeyName )
{
}
int CINISection::GetIntValue( const char *szKeyName )
{
}
const char *CINISection::GetStringValue( const char *szKeyName )
{
return 0;
}
CUtlString CINISection::GetUTLStringValue( const char *szKeyName )
{
}
class CINIFile: public IINIFile class CINIFile: public IINIFile
{ {
virtual void GetSection( const char *szSectionName ) override; public:
virtual IINISection *GetSection( const char *szSectionName ) override;
CUtlVector<CINISection*> m_sections; CUtlVector<CINISection*> m_sections;
}; };
IINISection *CINIFile::GetSection( const char *szSectionName )
{
for ( auto &section: m_sections )
{
if ( section->m_szSectionName == szSectionName )
return (IINISection*)&section;
}
return 0;
}
class CINIManager: public IINIManager class CINIManager: public IINIManager
{ {
public: public:
virtual IINIFile *ReadString( const char *psz ) override; virtual IINIFile *ReadString( const char *psz ) override;
bool IsWordSymbol( char c );
CUtlVector<CUtlString> TokenizeString();
}; };
IINIFile *CINIManager::ReadString( const char *psz ) IINIFile *CINIManager::ReadString( const char *psz )
{ {
CINIFile *pFile = new CINIFile; CINIFile *pFile = 0;
CINIFile file = {};
bool bIsQuoted;
bool bIsSlash;
bool bIsVarName;
bool bIsVarValue;
bool bIsSectionName;
CINISection currentSection;
SectionData_t currentSectionData;
size_t i = 0;
char c;
CUtlString szStringValue;
CUtlVector<CUtlString> tokens;
uint32_t nCurrentLine = 0;
while (true)
{
c = psz[i];
i++;
if (c == '\0')
break;
if (c == '\n')
nCurrentLine++;
if (bIsQuoted)
{
if (bIsSlash)
{
bIsSlash = false;
switch (c)
{
case '\\':
szStringValue.AppendTail('\\');
continue;
case '"':
szStringValue.AppendTail('"');
continue;
case 'n':
szStringValue.AppendTail('\n');
continue;
case 't':
szStringValue.AppendTail('\n');
continue;
case '\n':
szStringValue.AppendTail('\n');
continue;
default:
szStringValue.AppendTail(c);
continue;
}
} else {
switch (c)
{
case '\\':
bIsSlash = true;
continue;
case '\n':
V_printf("line %i: new line was found but the string wasn't finished, did you forget to place \" in the end of the line?\n", nCurrentLine);
case '"':
bIsQuoted = false;
if (szStringValue == 0)
continue;
tokens.AppendTail(szStringValue);
szStringValue = 0;
continue;
default:
szStringValue.AppendTail(c);
continue;
}
}
} else {
if (c == '"')
{
bIsQuoted = true;
}
if (IsWordSymbol(c))
{
szStringValue.AppendTail(c);
continue;
} else {
if (szStringValue != 0)
tokens.AppendTail(szStringValue);
szStringValue = 0;
if (V_isgraph(c) && c != '"')
tokens.AppendTail(CUtlString("%c", c));
}
}
};
currentSectionData.m_eSectionType = SECTIONTYPE_String;
for ( int i = 0; i < tokens.GetSize(); i++ )
{
if (tokens[i] == "]")
{
V_printf("Error: unexpected ]\n");
return 0;
}
if (tokens[i] == "[")
{
i++;
if (i>=tokens.GetSize())
{
V_printf("Error: Trailing section start\n");
return 0;
}
currentSection = {};
currentSection.m_szSectionName = tokens[i];
V_printf("[%s]\n",currentSection.m_szSectionName.GetString());
i++;
if (i>=tokens.GetSize())
{
V_printf("Error: Trailing section name\n");
return 0;
}
if (tokens[i] != "]")
{
V_printf("Error: expected ]\n");
return 0;
}
continue;
}
if (tokens[i] == "=")
{
V_printf("Error: expected key\n");
return 0;
}
currentSectionData = {};
currentSectionData.m_szKey = tokens[i];
i++;
if (i>=tokens.GetSize())
{
V_printf("Error: trailing key\n");
return 0;
}
if (tokens[i] != "=")
{
V_printf("Error: expected =\n");
return 0;
}
i++;
if (i>=tokens.GetSize())
{
V_printf("Error: expected value\n");
return 0;
}
currentSectionData.m_szData = tokens[i];
currentSection.m_values.AppendTail(currentSectionData);
}
pFile = new CINIFile;
*pFile = file;
return (IINIFile*)pFile; return (IINIFile*)pFile;
} }
bool CINIManager::IsWordSymbol( char c )
{
if (V_isalnum(c))
return true;
switch (c)
{
case '-':
case '_':
case '.':
return true;
default:
return false;
}
}
CUtlVector<CUtlString> TokenizeString();
IINIManager *INIManager() IINIManager *INIManager()
{ {

View File

@@ -1,4 +1,4 @@
#include "ifilesystem.h" #include "tier2/ifilesystem.h"
#include "tier1/interface.h" #include "tier1/interface.h"
#include "tier0/lib.h" #include "tier0/lib.h"
@@ -22,6 +22,11 @@ size_t IFileHandle::Tell( void )
return m_pFileSystem->Tell(this); return m_pFileSystem->Tell(this);
} }
size_t IFileHandle::Size( void )
{
return m_pFileSystem->Size(this);
}
void IFileHandle::Close( void ) void IFileHandle::Close( void )
{ {
m_pFileSystem->Close(this); m_pFileSystem->Close(this);
@@ -32,8 +37,6 @@ class CFileSystem : public IFileSystem
{ {
public: public:
virtual void Init() override { virtual void Init() override {
filesystem_backend->Init();
filesystem_pak->Init();
}; };
virtual void Shutdown() override { virtual void Shutdown() override {
@@ -41,13 +44,14 @@ public:
virtual IFileHandle *Open( const char *szFileName, int eOpCode ) override virtual IFileHandle *Open( const char *szFileName, int eOpCode ) override
{ {
IFileSystem *pFileSystem;
IFileHandle *pHandle = NULL; IFileHandle *pHandle = NULL;
pHandle = filesystem_pak->Open(szFileName, eOpCode); for ( pFileSystem = g_pFileSystems; pFileSystem; pFileSystem = pFileSystem->m_pNext )
if (pHandle) return pHandle; {
pHandle = pFileSystem->Open(szFileName, eOpCode);
pHandle = filesystem_backend->Open(szFileName, eOpCode); if (pHandle) return pHandle;
if (pHandle) return pHandle; }
return NULL; return NULL;
} }
@@ -71,11 +75,30 @@ public:
{ {
return pFile->Tell(); return pFile->Tell();
} }
virtual size_t Size( IFileHandle *pFile ) override
{
return pFile->Size();
}
virtual void Close( IFileHandle *pFile ) override virtual void Close( IFileHandle *pFile ) override
{ {
} }
}; };
CFileSystem g_fileSystem;
IFileSystem *filesystem = &g_fileSystem; static CFileSystem s_fileSystem;
IFileSystem *filesystem = &s_fileSystem;
IFileSystem *g_pFileSystems;
CFileSystemRegistry::CFileSystemRegistry( InstantiateFileSystemFn fn, const char *szFileSystem )
{
IFileSystem *pFileSystem = fn();
pFileSystem->RegisterFileSystem();
}
void IFileSystem::RegisterFileSystem()
{
m_pNext = g_pFileSystems;
g_pFileSystems = this;
};

View File

@@ -1,4 +1,4 @@
#include "ifilesystem.h" #include "tier2/ifilesystem.h"
#include "tier1/interface.h" #include "tier1/interface.h"
#include "tier0/lib.h" #include "tier0/lib.h"
@@ -6,14 +6,16 @@ class CLIBCFileHandle : public IFileHandle
{ {
public: public:
FILE *m_pFile; FILE *m_pFile;
size_t m_nSize;
}; };
class CLIBCFileSystem : public IFileSystem class CLIBCFileSystem : public IFileSystem
{ {
public: public:
virtual void Init() override {}; virtual void Init() override {
};
virtual void Shutdown() override {}; virtual void Shutdown() override {};
virtual IFileHandle *Open( const char *szFileName, int eOpCode ) override virtual IFileHandle *Open( const char *szFileName, int eOpCode ) override
@@ -25,7 +27,7 @@ public:
switch (eOpCode) switch (eOpCode)
{ {
case FILEMODE_READ: case FILEMODE_READ:
szOperation = "r"; szOperation = "rb";
break; break;
default: default:
V_printf("Operation is not supported\n"); V_printf("Operation is not supported\n");
@@ -39,6 +41,9 @@ public:
pHandle = new CLIBCFileHandle; pHandle = new CLIBCFileHandle;
pHandle->m_pFileSystem = this; pHandle->m_pFileSystem = this;
pHandle->m_pFile = pFile; pHandle->m_pFile = pFile;
pHandle->Seek(SEEKMODE_RELATIVE_START, 0);
pHandle->m_nSize = pHandle->Tell();
pHandle->Seek(SEEKMODE_RELATIVE_END, 0);
return pHandle; return pHandle;
} }
virtual size_t Write( IFileHandle *pFile, const void *pData, size_t nDataSize ) override virtual size_t Write( IFileHandle *pFile, const void *pData, size_t nDataSize ) override
@@ -57,20 +62,23 @@ public:
{ {
CLIBCFileHandle *pHandle = (CLIBCFileHandle*)pFile; CLIBCFileHandle *pHandle = (CLIBCFileHandle*)pFile;
int eLibcSeekMode = 0; int eLibcSeekMode = 0;
if (!pHandle)
return 0;
switch (eSeekMode) { switch (eSeekMode) {
case SEEKMODE_SET: case SEEKMODE_RELATIVE_CURRENT:
eLibcSeekMode = SEEK_SET; eLibcSeekMode = SEEK_CUR;
break; break;
case SEEKMODE_RELATIVE_END: case SEEKMODE_RELATIVE_END:
eLibcSeekMode = SEEK_END; eLibcSeekMode = SEEK_END;
break; break;
case SEEKMODE_RELATIVE_START: case SEEKMODE_RELATIVE_START:
eLibcSeekMode = SEEK_CUR; eLibcSeekMode = SEEK_SET;
break; break;
} }
if (!pHandle)
return 0; return V_fseek( pHandle->m_pFile, nOffset, eLibcSeekMode );
return V_fseek( pHandle->m_pFile, eLibcSeekMode, nOffset );
} }
virtual size_t Tell( IFileHandle *pFile ) override virtual size_t Tell( IFileHandle *pFile ) override
@@ -81,6 +89,16 @@ public:
return V_ftell(pHandle->m_pFile); return V_ftell(pHandle->m_pFile);
} }
virtual size_t Size( IFileHandle *pFile ) override
{
CLIBCFileHandle *pHandle = (CLIBCFileHandle*)pFile;
if (!pHandle)
return 0;
return pHandle->m_nSize;
}
virtual void Close( IFileHandle *pFile ) override virtual void Close( IFileHandle *pFile ) override
{ {
CLIBCFileHandle *pHandle = (CLIBCFileHandle*)pFile; CLIBCFileHandle *pHandle = (CLIBCFileHandle*)pFile;
@@ -91,5 +109,4 @@ public:
} }
}; };
CLIBCFileSystem g_libcFileSystem; EXPOSE_FILESYSTEM(CLIBCFileSystem, "sysfs");
IFileSystem *filesystem_backend = &g_libcFileSystem;