diff --git a/engine/cvar.cpp b/engine/cvar.cpp index dada8ce..7290b50 100644 --- a/engine/cvar.cpp +++ b/engine/cvar.cpp @@ -327,8 +327,8 @@ void IConsole_Exec( int argc, char **argv) IFileHandle *f = filesystem->Open(argv[1], FILEMODE_READ); if (!f) return; - CUtlBuffer b(f->Size()+1); - f->Read(b, b.GetSize()); + CUtlBuffer b(filesystem->Size(f)+1); + filesystem->Read(f, b, b.GetSize()); b[b.GetSize()-1] = 0; Console()->AddCommand(b); Console()->AddCommand(";"); diff --git a/engine/engine.cpp b/engine/engine.cpp index 73e80ed..ec38140 100644 --- a/engine/engine.cpp +++ b/engine/engine.cpp @@ -11,10 +11,14 @@ extern "C" void FunnyMain( int argc, char **argv ) { CommandLine()->CreateCommandLine(argc, argv); + void *pFilesystem = Plat_LoadLibrary("libfilesystem.so"); + CreateInterfaceFn pFilesystemFactory = Sys_GetFactory(pFilesystem); + + filesystem = (IFileSystem*)pFilesystemFactory(FILESYSTEM_INTERFACE_VERSION, NULL); filesystem->Init(); gamewindow->Init(); - Materials()->Init(); + g_pMaterialSystem->Init(); ServerGameDLL()->Init(); diff --git a/public/tier0/platform.h b/public/tier0/platform.h index 07a12d0..e106cf6 100644 --- a/public/tier0/platform.h +++ b/public/tier0/platform.h @@ -25,7 +25,7 @@ #define DLL_CLASS_EXPORT __attribute__ ((visibility("default"))) #define DLL_CLASS_IMPORT -#define DLL_GLOBAL_EXPORT extern __attribute ((visibility("default"))) +#define DLL_GLOBAL_EXPORT __attribute ((visibility("default"))) #define DLL_GLOBAL_IMPORT extern #endif @@ -50,6 +50,11 @@ #endif +#if defined(__linux__) +#define POSIX +#endif + + #define abstract_class class PLATFORM_INTERFACE void Plat_FatalErrorFunc( const char *szFormat, ... ); diff --git a/public/tier1/interface.h b/public/tier1/interface.h index 9f7de00..e9e9e42 100644 --- a/public/tier1/interface.h +++ b/public/tier1/interface.h @@ -26,11 +26,11 @@ public: static CInterfaceRegistry __Create##interfaceName##_registry( functionName, versionName ); #define EXPOSE_INTERFACE_GLOBALVAR( className, interfaceName, versionName, globalVarName ) \ - static void *__Create##interfaceName##_interface() { return ( interfaceName* )( &globalVarName ); }; \ + static void *__Create##className##_interface() { return ( interfaceName* )( &globalVarName ); }; \ static CInterfaceRegistry __Create##interfaceName##_registry( __Create##className##_interface, versionName ); -void *CreateInterface( const char *szName, int *pReturnCode ); +DLL_EXPORT void *CreateInterface( const char *szName, int *pReturnCode ); CreateInterfaceFn Sys_GetFactory( void *pLibrary ); diff --git a/public/tier2/ifilesystem.h b/public/tier2/ifilesystem.h index 8b787f5..2ece1b2 100644 --- a/public/tier2/ifilesystem.h +++ b/public/tier2/ifilesystem.h @@ -25,15 +25,6 @@ class IFileHandle { public: IFileSystem *m_pFileSystem; - - size_t Write( const void *pData, size_t nDataSize ); - size_t Read( void *pData, size_t nDataSize ); - size_t Seek( ESeekMode eSeekMode, size_t nOffset ); - size_t Tell( void ); - size_t Size( void ); - void Close( void ); - - }; @@ -68,19 +59,8 @@ public: void RegisterFileSystem(); }; +#define FILESYSTEM_INTERFACE_VERSION "FileSystem001" + extern IFileSystem *filesystem; -extern IFileSystem *g_pFileSystems; - -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 diff --git a/stdfilesystems/build.cpp b/stdfilesystems/build.cpp new file mode 100644 index 0000000..e69de29 diff --git a/tier2/filesystem_libc.cpp b/stdfilesystems/filesystem_libc.cpp similarity index 92% rename from tier2/filesystem_libc.cpp rename to stdfilesystems/filesystem_libc.cpp index 83144b3..1abaf1d 100644 --- a/tier2/filesystem_libc.cpp +++ b/stdfilesystems/filesystem_libc.cpp @@ -47,9 +47,9 @@ public: pHandle = new CLIBCFileHandle; pHandle->m_pFileSystem = this; pHandle->m_pFile = pFile; - pHandle->Seek(SEEKMODE_RELATIVE_END, 0); - pHandle->m_nSize = pHandle->Tell(); - pHandle->Seek(SEEKMODE_RELATIVE_START, 0); + Seek(pHandle,SEEKMODE_RELATIVE_END, 0); + pHandle->m_nSize = Tell(pHandle); + Seek(pHandle, SEEKMODE_RELATIVE_START, 0); return pHandle; } virtual size_t Write( IFileHandle *pFile, const void *pData, size_t nDataSize ) override @@ -121,4 +121,4 @@ public: virtual const char *ReadString( IFileHandle *pFile ) override { return NULL; }; }; -EXPOSE_FILESYSTEM(CLIBCFileSystem, "sysfs"); +EXPOSE_INTERFACE(CLIBCFileSystem, IFileSystem, FILESYSTEM_INTERFACE_VERSION) diff --git a/tier0/build.cpp b/tier0/build.cpp new file mode 100644 index 0000000..8e842e6 --- /dev/null +++ b/tier0/build.cpp @@ -0,0 +1,32 @@ +#include "helper.h" +#include "c.h" +#include "ld.h" +#include "tier1/utlstring.h" +#include "tier1/commandline.h" + +CUtlVector tier0_CompiledFiles = { + "lib.cpp", + "mem.cpp", + "platform.cpp", + "network.cpp", +}; + +DECLARE_BUILD_STAGE(tier0) +{ + CProject_t compileProject = {}; + LinkProject_t ldProject = {}; + CUtlString szOutputProject = ""; + + compileProject.m_szName = "tier0"; + compileProject.files = tier0_CompiledFiles; + compileProject.includeDirectories = {"../public"}; + compileProject.bFPIC = true; + ldProject = ccompiler->Compile(&compileProject); + + ldProject.linkType = ELINK_DYNAMIC_LIBRARY; + szOutputProject = linker->Link(&ldProject); + + ADD_OUTPUT_LIBRARY(szOutputProject); + + return 0; +}; diff --git a/tier0/network.cpp b/tier0/network.cpp index 9808862..a07363e 100644 --- a/tier0/network.cpp +++ b/tier0/network.cpp @@ -1,7 +1,4 @@ #include "tier0/network.h" -#include "steam/isteamnetworkingsockets.h" -#include "steam/isteamnetworkingutils.h" -#include "steam/steamnetworkingtypes.h" #include "tier1/commandline.h" diff --git a/tier0/platform.cpp b/tier0/platform.cpp index fd0abf9..19e39cc 100644 --- a/tier0/platform.cpp +++ b/tier0/platform.cpp @@ -214,18 +214,20 @@ PLATFORM_INTERFACE const char *Plat_GetEnv( const char *szVar ) PLATFORM_INTERFACE void Plat_SetWorkingDir( const char *psz ) { - + chdir(psz); } - -PLATFORM_INTERFACE const char *Plat_GetWorkingDir( void ) -{ - -} - #ifndef MAX_PATH #define MAX_PATH 4096 #endif +PLATFORM_INTERFACE const char *Plat_GetWorkingDir( void ) +{ + static char szCwd[MAX_PATH]; + getcwd(szCwd, MAX_PATH); + return szCwd; +} + + static char s_szExecutablePath[MAX_PATH]; #ifdef __linux__ static ssize_t s_iExecutablePathSize = readlink("/proc/self/exe", s_szExecutablePath, MAX_PATH); diff --git a/tier1/__build.cpp b/tier1/__build.cpp index 14422e5..4cfb04e 100644 --- a/tier1/__build.cpp +++ b/tier1/__build.cpp @@ -13,7 +13,6 @@ CUtlVector tier1_CompiledFiles = { "tier1/utlstring.cpp", "tier1/utlvector.cpp", }; -CUtlString tier1_lib; DECLARE_BUILD_STAGE(tier1) { @@ -22,13 +21,15 @@ DECLARE_BUILD_STAGE(tier1) compileProject.m_szName = "tier1"; compileProject.files = tier1_CompiledFiles; - compileProject.includeDirectories = all_IncludeDirectories; + compileProject.includeDirectories = {"../public"}; compileProject.bFPIC = true; ldProject = ccompiler->Compile(&compileProject); ldProject.linkType = ELINK_STATIC_LIBRARY; - CUtlString outputProject = linker->Link(&ldProject); - tier1_lib = outputProject; + CUtlString szOutputDir = linker->Link(&ldProject); + + ADD_OUTPUT_LIBRARY(szOutputDir) + return 0; }; diff --git a/tier1/build.cpp b/tier1/build.cpp new file mode 100644 index 0000000..402b04b --- /dev/null +++ b/tier1/build.cpp @@ -0,0 +1,34 @@ +#include "helper.h" +#include "c.h" +#include "ld.h" +#include "tier1/utlstring.h" +#include "tier1/commandline.h" + +CUtlVector tier1_CompiledFiles = { + "interface.cpp", + "appinit.cpp", + "commandline.cpp", + "utlbuffer.cpp", + "utlmap.cpp", + "utlstring.cpp", + "utlvector.cpp", +}; + +DECLARE_BUILD_STAGE(tier1) +{ + CProject_t compileProject = {}; + LinkProject_t ldProject = {}; + + compileProject.m_szName = "tier1"; + compileProject.files = tier1_CompiledFiles; + compileProject.includeDirectories = {"../public"}; + compileProject.bFPIC = true; + ldProject = ccompiler->Compile(&compileProject); + ldProject.linkType = ELINK_STATIC_LIBRARY; + + CUtlString szOutputDir = linker->Link(&ldProject); + + ADD_OUTPUT_LIBRARY(szOutputDir) + + return 0; +}; diff --git a/tier1/commandline.o b/tier1/commandline.o new file mode 100644 index 0000000..1604b9c Binary files /dev/null and b/tier1/commandline.o differ diff --git a/tier1/interface.cpp b/tier1/interface.cpp index 180777d..69d5d44 100644 --- a/tier1/interface.cpp +++ b/tier1/interface.cpp @@ -1,18 +1,23 @@ #include "tier1/interface.h" #include "tier1/utlvector.h" +#include "dlfcn.h" + +static CInterfaceRegistry *s_pInterfaceRegistries; -CInterfaceRegistry *g_pInterfaceRegistries = NULL; CInterfaceRegistry::CInterfaceRegistry( InstantiateInterfaceFn fn, const char *szName ) : m_szName(szName) { m_CreateFn = fn; - m_pNext = g_pInterfaceRegistries; - g_pInterfaceRegistries = this; + m_pNext = s_pInterfaceRegistries; + s_pInterfaceRegistries = this; + Dl_info info = {}; + dladdr((void *)&s_pInterfaceRegistries, &info); + printf("%p: %s in %s\n",&s_pInterfaceRegistries, m_szName, info.dli_fname); }; -void *CreateInterface( const char *szName, int *pReturnCode ) +DLL_EXPORT void *CreateInterface( const char *szName, int *pReturnCode ) { - CInterfaceRegistry *pRegistry = g_pInterfaceRegistries; + CInterfaceRegistry *pRegistry = s_pInterfaceRegistries; while (pRegistry) { if (!V_strcmp(szName, pRegistry->m_szName)) @@ -30,5 +35,8 @@ void *CreateInterface( const char *szName, int *pReturnCode ) CreateInterfaceFn Sys_GetFactory( void *lib ) { +#ifdef POSIX + return CreateInterface; +#endif return (CreateInterfaceFn)Plat_GetProc(lib, "CreateInterface"); } diff --git a/tier1/interface.o b/tier1/interface.o new file mode 100644 index 0000000..7ad5ef1 Binary files /dev/null and b/tier1/interface.o differ diff --git a/tier1/utlbuffer.o b/tier1/utlbuffer.o new file mode 100644 index 0000000..e8a5f8d Binary files /dev/null and b/tier1/utlbuffer.o differ diff --git a/tier1/utlmap.o b/tier1/utlmap.o new file mode 100644 index 0000000..bcad1bc Binary files /dev/null and b/tier1/utlmap.o differ diff --git a/tier1/utlstring.o b/tier1/utlstring.o new file mode 100644 index 0000000..67f00a9 Binary files /dev/null and b/tier1/utlstring.o differ diff --git a/tier1/utlvector.o b/tier1/utlvector.o new file mode 100644 index 0000000..05041b8 Binary files /dev/null and b/tier1/utlvector.o differ diff --git a/tier2/__build.cpp b/tier2/__build.cpp index b13ac9e..bbb42ee 100644 --- a/tier2/__build.cpp +++ b/tier2/__build.cpp @@ -5,10 +5,10 @@ #include "tier1/commandline.h" CUtlVector tier2_CompiledFiles = { - "tier2/fileformats/ini.cpp", - "tier2/fileformats/json.cpp", - "tier2/filesystem.cpp", - "tier2/filesystem_libc.cpp", + "fileformats/ini.cpp", + "fileformats/json.cpp", + "filesystem.cpp", + "filesystem_libc.cpp", }; CUtlString tier2_lib; diff --git a/tier2/build.cpp b/tier2/build.cpp new file mode 100644 index 0000000..c047f96 --- /dev/null +++ b/tier2/build.cpp @@ -0,0 +1,32 @@ +#include "helper.h" +#include "c.h" +#include "ld.h" +#include "tier1/utlstring.h" +#include "tier1/commandline.h" + +CUtlVector tier2_CompiledFiles = { + "fileformats/ini.cpp", + "fileformats/json.cpp", + "filesystem.cpp", +}; + + +DECLARE_BUILD_STAGE(tier2) +{ + V_printf("%s\n", Plat_GetWorkingDir()); + CProject_t compileProject = {}; + LinkProject_t ldProject = {}; + + compileProject.m_szName = "tier2"; + compileProject.files = tier2_CompiledFiles; + compileProject.includeDirectories = {"../public"}; + compileProject.bFPIC = true; + ldProject = ccompiler->Compile(&compileProject); + ldProject.linkType = ELINK_STATIC_LIBRARY; + + CUtlString szOutputProject = linker->Link(&ldProject); + + ADD_OUTPUT_LIBRARY(szOutputProject) + + return 0; +}; diff --git a/tier2/fileformats/ini.cpp b/tier2/fileformats/ini.cpp index b7278b1..0c0416b 100644 --- a/tier2/fileformats/ini.cpp +++ b/tier2/fileformats/ini.cpp @@ -92,16 +92,15 @@ public: IINIFile *CINIManager::ReadFile( const char *psz ) { IFileHandle *pFile; - CUtlBuffer data; + const char *szData; pFile = filesystem->Open(psz, FILEMODE_READ); if (!pFile) return NULL; - data = CUtlBuffer(pFile->Size()+1); - uint32_t nSize = pFile->Read(data.GetMemory(), pFile->Size()); - pFile->Close(); + szData = filesystem->ReadString(pFile); + filesystem->Close(pFile); - return ReadString(data.GetMemory()); + return ReadString(szData); } IINIFile *CINIManager::ReadString( const char *psz ) diff --git a/tier2/fileformats/ini.o b/tier2/fileformats/ini.o new file mode 100644 index 0000000..f2b00f0 Binary files /dev/null and b/tier2/fileformats/ini.o differ diff --git a/tier2/filesystem.cpp b/tier2/filesystem.cpp index e31c151..64c0e18 100644 --- a/tier2/filesystem.cpp +++ b/tier2/filesystem.cpp @@ -1,121 +1,4 @@ -#include "tier1/utlbuffer.h" #include "tier2/ifilesystem.h" -#include "tier1/interface.h" -#include "tier0/lib.h" -#include -size_t IFileHandle::Write( const void *pData, size_t nDataSize ) -{ - return m_pFileSystem->Write(this, pData, nDataSize); -} +IFileSystem *filesystem; -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); -} - -size_t IFileHandle::Size( void ) -{ - return m_pFileSystem->Size(this); -} - -void IFileHandle::Close( void ) -{ - m_pFileSystem->Close(this); -} - - -class CFileSystem : public IFileSystem -{ -public: - virtual void Init() override { - }; - virtual void Shutdown() override { - - }; - - virtual IFileHandle *Open( const char *szFileName, int eOpCode ) override - { - IFileSystem *pFileSystem; - IFileHandle *pHandle = NULL; - - for ( pFileSystem = g_pFileSystems; pFileSystem; pFileSystem = pFileSystem->m_pNext ) - { - pHandle = pFileSystem->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 size_t Size( IFileHandle *pFile ) override - { - return pFile->Size(); - } - virtual void Close( IFileHandle *pFile ) override - { - - } - virtual CUtlBuffer Read( IFileHandle *pFile ) override - { - CUtlBuffer buffer; - buffer = CUtlBuffer(Size(pFile)); - - Read(pFile, buffer.GetMemory(), buffer.GetSize()); - - return buffer; - } - virtual const char *ReadString( IFileHandle *pFile ) override { - char *szString = (char*)V_malloc(Size(pFile)+1); - Read(pFile, szString, Size(pFile)); - szString[Size(pFile)] = 0; - return szString; - }; -}; - - -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; -}; diff --git a/tier2/filesystem.o b/tier2/filesystem.o new file mode 100644 index 0000000..059b69d Binary files /dev/null and b/tier2/filesystem.o differ