From 9c3f81f065ba8254bff69a548753eb8fc63f9f71 Mon Sep 17 00:00:00 2001 From: frog Date: Sun, 16 Mar 2025 17:05:29 -0500 Subject: [PATCH 01/46] dummy change --- code/server/sv_game.c | 1 + 1 file changed, 1 insertion(+) diff --git a/code/server/sv_game.c b/code/server/sv_game.c index f8d05651e5..62d618a0f6 100644 --- a/code/server/sv_game.c +++ b/code/server/sv_game.c @@ -361,6 +361,7 @@ The module is making a system call static intptr_t SV_GameSystemCalls( intptr_t *args ) { switch( args[0] ) { case G_PRINT: + Com_Printf("Hello world"); Com_Printf( "%s", (const char*)VMA(1) ); return 0; case G_ERROR: From 3b9247b0d0ebf00b9dbb9701814f0192547ee3fa Mon Sep 17 00:00:00 2001 From: frog Date: Sun, 16 Mar 2025 19:12:01 -0500 Subject: [PATCH 02/46] print on finish --- code/client/cl_cgame.c | 8 +++++++- code/server/sv_game.c | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/code/client/cl_cgame.c b/code/client/cl_cgame.c index ad700af383..d5fe0209e0 100644 --- a/code/client/cl_cgame.c +++ b/code/client/cl_cgame.c @@ -450,8 +450,13 @@ static void CL_ForceFixedDlights( void ) { } } - /* +================= +startsWith + +Returns qtrue if the string begins with the given prefix +================= + ==================== CL_CgameSystemCalls @@ -461,6 +466,7 @@ The cgame module is making a system call static intptr_t CL_CgameSystemCalls( intptr_t *args ) { switch( args[0] ) { case CG_PRINT: + // StoreRecordIfNecessary((const char *)VMA(1)); Com_Printf( "%s", (const char*)VMA(1) ); return 0; case CG_ERROR: diff --git a/code/server/sv_game.c b/code/server/sv_game.c index 62d618a0f6..7356b84ccd 100644 --- a/code/server/sv_game.c +++ b/code/server/sv_game.c @@ -350,6 +350,38 @@ static qboolean SV_GetValue( char* value, int valueSize, const char* key ) return qfalse; } +/* +================= +startsWith + +Returns qtrue if the string begins with the given prefix +================= +*/ +qboolean s_startsWith(const char *string, const char *prefix) { + if (!string || !prefix) { + return qfalse; + } + + size_t prefixLen = strlen(prefix); + size_t stringLen = strlen(string); + + if (prefixLen > stringLen) { + return qfalse; + } + + return (strncmp(string, prefix, prefixLen) == 0) ? qtrue : qfalse; +} + +static void StoreRecordIfNecessary(const char *s) { + if (!s_startsWith(s, "ClientTimerStop: ")) return; + + SV_GameSendServerCommand( -1, "print \"^5You have finished\"\n" ); + // parse the string for client num, + // check client is logged in + if (Cvar_VariableIntegerValue("sv_cheats") != 0) return; + // blah blah spawn a new thread or send a message somewhere blah blah +} + /* ==================== @@ -361,7 +393,8 @@ The module is making a system call static intptr_t SV_GameSystemCalls( intptr_t *args ) { switch( args[0] ) { case G_PRINT: - Com_Printf("Hello world"); + StoreRecordIfNecessary((const char *)VMA(1)); + // Com_Printf( "server ^5> ^7"); Com_Printf( "%s", (const char*)VMA(1) ); return 0; case G_ERROR: From 9e6e130167bf59cdac83e9d84e1084c35b278950 Mon Sep 17 00:00:00 2001 From: frog Date: Sun, 16 Mar 2025 19:42:11 -0500 Subject: [PATCH 03/46] separate thread --- code/server/sv_game.c | 45 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/code/server/sv_game.c b/code/server/sv_game.c index 7356b84ccd..07889b18ce 100644 --- a/code/server/sv_game.c +++ b/code/server/sv_game.c @@ -22,6 +22,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA // sv_game.c -- interface to the game dll #include "server.h" +#include #include "../botlib/botlib.h" @@ -350,6 +351,36 @@ static qboolean SV_GetValue( char* value, int valueSize, const char* key ) return qfalse; } +/* +=============== +Sys_CreateThread + +Create a new thread of execution +=============== +*/ +void Sys_CreateThread(void (*function)(void)) { + // POSIX implementation (Linux, macOS, etc.) + pthread_t threadHandle; + pthread_attr_t attr; + int result; + + pthread_attr_init(&attr); + // Create the thread in detached state so its resources are automatically + // freed when it exits + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + + result = pthread_create(&threadHandle, + &attr, + (void*(*)(void*))function, + NULL); + + pthread_attr_destroy(&attr); + + if (result != 0) { + Com_Error(ERR_FATAL, "Sys_CreateThread: pthread_create failed with error %d", result); + } +} + /* ================= startsWith @@ -372,10 +403,16 @@ qboolean s_startsWith(const char *string, const char *prefix) { return (strncmp(string, prefix, prefixLen) == 0) ? qtrue : qfalse; } -static void StoreRecordIfNecessary(const char *s) { - if (!s_startsWith(s, "ClientTimerStop: ")) return; - +static void RS_TimerStop(){ + Sys_Sleep(5000); SV_GameSendServerCommand( -1, "print \"^5You have finished\"\n" ); +} + +static void CheckForRS(const char *s) { + if (!s_startsWith(s, "ClientBegin: ")) return; + + Sys_CreateThread((void (*)(void))RS_TimerStop); +// SV_GameSendServerCommand( -1, "print \"^5You have finished\"\n" ); // parse the string for client num, // check client is logged in if (Cvar_VariableIntegerValue("sv_cheats") != 0) return; @@ -393,7 +430,7 @@ The module is making a system call static intptr_t SV_GameSystemCalls( intptr_t *args ) { switch( args[0] ) { case G_PRINT: - StoreRecordIfNecessary((const char *)VMA(1)); + CheckForRS((const char *)VMA(1)); // Com_Printf( "server ^5> ^7"); Com_Printf( "%s", (const char*)VMA(1) ); return 0; From da1fa29344720f1da2e6e43fe38e10b555498af6 Mon Sep 17 00:00:00 2001 From: frog Date: Mon, 17 Mar 2025 11:22:02 -0500 Subject: [PATCH 04/46] file structure --- Makefile | 11 +++++ code/client/cl_cgame.c | 8 +--- code/recordsystem/auth.c | 0 code/recordsystem/recent.c | 0 code/recordsystem/recordsystem.h | 40 +++++++++++++++++ code/recordsystem/rs_main.c | 57 +++++++++++++++++++++++++ code/recordsystem/rs_record.c | 26 ++++++++++++ code/recordsystem/top.c | 0 code/server/sv_game.c | 73 +------------------------------- 9 files changed, 137 insertions(+), 78 deletions(-) create mode 100644 code/recordsystem/auth.c create mode 100644 code/recordsystem/recent.c create mode 100644 code/recordsystem/recordsystem.h create mode 100644 code/recordsystem/rs_main.c create mode 100644 code/recordsystem/rs_record.c create mode 100644 code/recordsystem/top.c diff --git a/Makefile b/Makefile index 2182e1d60c..b3de3ea589 100644 --- a/Makefile +++ b/Makefile @@ -214,6 +214,7 @@ RVDIR=$(MOUNT_DIR)/renderervk SDLDIR=$(MOUNT_DIR)/sdl SDLHDIR=$(MOUNT_DIR)/libsdl/include/SDL2 +RSDIR=$(MOUNT_DIR)/recordsystem CMDIR=$(MOUNT_DIR)/qcommon UDIR=$(MOUNT_DIR)/unix W32DIR=$(MOUNT_DIR)/win32 @@ -1068,6 +1069,8 @@ Q3OBJ = \ \ $(B)/client/cmd.o \ $(B)/client/common.o \ + $(B)/client/rs_main.o \ + $(B)/client/rs_record.o \ $(B)/client/cvar.o \ $(B)/client/files.o \ $(B)/client/history.o \ @@ -1305,6 +1308,8 @@ Q3DOBJ = \ $(B)/ded/cm_test.o \ $(B)/ded/cm_trace.o \ $(B)/ded/cmd.o \ + $(B)/ded/rs_main.o \ + $(B)/ded/rs_record.o \ $(B)/ded/common.o \ $(B)/ded/cvar.o \ $(B)/ded/files.o \ @@ -1399,6 +1404,9 @@ $(B)/client/%.o: $(CDIR)/%.c $(B)/client/%.o: $(SDIR)/%.c $(DO_CC) +$(B)/client/%.o: $(RSDIR)/%.c + $(DO_CC) + $(B)/client/%.o: $(CMDIR)/%.c $(DO_CC) @@ -1465,6 +1473,9 @@ $(B)/ded/%.o: $(ADIR)/%.s $(B)/ded/%.o: $(SDIR)/%.c $(DO_DED_CC) +$(B)/ded/%.o: $(RSDIR)/%.c + $(DO_DED_CC) + $(B)/ded/%.o: $(CMDIR)/%.c $(DO_DED_CC) diff --git a/code/client/cl_cgame.c b/code/client/cl_cgame.c index d5fe0209e0..ad700af383 100644 --- a/code/client/cl_cgame.c +++ b/code/client/cl_cgame.c @@ -450,13 +450,8 @@ static void CL_ForceFixedDlights( void ) { } } -/* -================= -startsWith - -Returns qtrue if the string begins with the given prefix -================= +/* ==================== CL_CgameSystemCalls @@ -466,7 +461,6 @@ The cgame module is making a system call static intptr_t CL_CgameSystemCalls( intptr_t *args ) { switch( args[0] ) { case CG_PRINT: - // StoreRecordIfNecessary((const char *)VMA(1)); Com_Printf( "%s", (const char*)VMA(1) ); return 0; case CG_ERROR: diff --git a/code/recordsystem/auth.c b/code/recordsystem/auth.c new file mode 100644 index 0000000000..e69de29bb2 diff --git a/code/recordsystem/recent.c b/code/recordsystem/recent.c new file mode 100644 index 0000000000..e69de29bb2 diff --git a/code/recordsystem/recordsystem.h b/code/recordsystem/recordsystem.h new file mode 100644 index 0000000000..858bbb4da2 --- /dev/null +++ b/code/recordsystem/recordsystem.h @@ -0,0 +1,40 @@ +// recordsystem.h -- thread and string utility functions for record system + +#ifndef __RECORDSYSTEM_H__ +#define __RECORDSYSTEM_H__ + +#include +#include +#include "../qcommon/q_shared.h" +#include "../qcommon/qcommon.h" + +/* +=============== +startsWith + +Returns qtrue if the string begins with the given prefix +=============== +*/ +qboolean startsWith(const char *string, const char *prefix); + +/* +=============== +CheckForRS + +Checks if a string starts with "ClientBegin: " and performs actions +=============== +*/ +void RS_Gateway(const char *s); + +/* +=============== +Sys_CreateThread + +Create a new thread of execution with no arguments +=============== +*/ +void Sys_CreateThread(void (*function)(void)); + +void RS_CreateRecord(void); + +#endif // __RECORDSYSTEM_H__ \ No newline at end of file diff --git a/code/recordsystem/rs_main.c b/code/recordsystem/rs_main.c new file mode 100644 index 0000000000..0d4b21388d --- /dev/null +++ b/code/recordsystem/rs_main.c @@ -0,0 +1,57 @@ +#include "recordsystem.h" + +qboolean startsWith(const char *string, const char *prefix) { + if (!string || !prefix) { + return qfalse; + } + + size_t prefixLen = strlen(prefix); + size_t stringLen = strlen(string); + + if (prefixLen > stringLen) { + return qfalse; + } + + return (strncmp(string, prefix, prefixLen) == 0) ? qtrue : qfalse; +} + +void RS_Gateway(const char *s) { + if (!startsWith(s, "ClientBegin: ")) return; + + Sys_CreateThread((void (*)(void))RS_CreateRecord); +// SV_GameSendServerCommand( -1, "print \"^5You have finished\"\n" ); + // parse the string for client num, + // check client is logged in + if (Cvar_VariableIntegerValue("sv_cheats") != 0) return; + // blah blah spawn a new thread or send a message somewhere blah blah +} + +/* +=============== +Sys_CreateThread + +Create a new thread of execution +=============== +*/ +void Sys_CreateThread(void (*function)(void)) { + // POSIX implementation (Linux, macOS, etc.) + pthread_t threadHandle; + pthread_attr_t attr; + int result; + + pthread_attr_init(&attr); + // Create the thread in detached state so its resources are automatically + // freed when it exits + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + + result = pthread_create(&threadHandle, + &attr, + (void*(*)(void*))function, + NULL); + + pthread_attr_destroy(&attr); + + if (result != 0) { + Com_Error(ERR_FATAL, "Sys_CreateThread: pthread_create failed with error %d", result); + } +} \ No newline at end of file diff --git a/code/recordsystem/rs_record.c b/code/recordsystem/rs_record.c new file mode 100644 index 0000000000..8e6fced379 --- /dev/null +++ b/code/recordsystem/rs_record.c @@ -0,0 +1,26 @@ +#include "recordsystem.h" +#include "../server/server.h" + +/* +=============== +SV_GameSendServerCommand + +Sends a command string to a client +=============== +*/ +static void RS_GameSendServerCommand( int clientNum, const char *text ) { + if ( clientNum == -1 ) { + SV_SendServerCommand( NULL, "%s", text ); + } else { + if ( clientNum < 0 || clientNum >= sv.maxclients ) { + return; + } + SV_SendServerCommand( svs.clients + clientNum, "%s", text ); + } +} + +void RS_CreateRecord(void){ + Sys_Sleep(5000); + RS_GameSendServerCommand( -1, "print \"^5You have finished\"\n" ); +} + diff --git a/code/recordsystem/top.c b/code/recordsystem/top.c new file mode 100644 index 0000000000..e69de29bb2 diff --git a/code/server/sv_game.c b/code/server/sv_game.c index 07889b18ce..4cb5b057bb 100644 --- a/code/server/sv_game.c +++ b/code/server/sv_game.c @@ -23,7 +23,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include "server.h" #include - +#include "../recordsystem/recordsystem.h" #include "../botlib/botlib.h" botlib_export_t *botlib_export; @@ -351,74 +351,6 @@ static qboolean SV_GetValue( char* value, int valueSize, const char* key ) return qfalse; } -/* -=============== -Sys_CreateThread - -Create a new thread of execution -=============== -*/ -void Sys_CreateThread(void (*function)(void)) { - // POSIX implementation (Linux, macOS, etc.) - pthread_t threadHandle; - pthread_attr_t attr; - int result; - - pthread_attr_init(&attr); - // Create the thread in detached state so its resources are automatically - // freed when it exits - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); - - result = pthread_create(&threadHandle, - &attr, - (void*(*)(void*))function, - NULL); - - pthread_attr_destroy(&attr); - - if (result != 0) { - Com_Error(ERR_FATAL, "Sys_CreateThread: pthread_create failed with error %d", result); - } -} - -/* -================= -startsWith - -Returns qtrue if the string begins with the given prefix -================= -*/ -qboolean s_startsWith(const char *string, const char *prefix) { - if (!string || !prefix) { - return qfalse; - } - - size_t prefixLen = strlen(prefix); - size_t stringLen = strlen(string); - - if (prefixLen > stringLen) { - return qfalse; - } - - return (strncmp(string, prefix, prefixLen) == 0) ? qtrue : qfalse; -} - -static void RS_TimerStop(){ - Sys_Sleep(5000); - SV_GameSendServerCommand( -1, "print \"^5You have finished\"\n" ); -} - -static void CheckForRS(const char *s) { - if (!s_startsWith(s, "ClientBegin: ")) return; - - Sys_CreateThread((void (*)(void))RS_TimerStop); -// SV_GameSendServerCommand( -1, "print \"^5You have finished\"\n" ); - // parse the string for client num, - // check client is logged in - if (Cvar_VariableIntegerValue("sv_cheats") != 0) return; - // blah blah spawn a new thread or send a message somewhere blah blah -} - /* ==================== @@ -430,8 +362,7 @@ The module is making a system call static intptr_t SV_GameSystemCalls( intptr_t *args ) { switch( args[0] ) { case G_PRINT: - CheckForRS((const char *)VMA(1)); - // Com_Printf( "server ^5> ^7"); + RS_Gateway((const char *)VMA(1)); Com_Printf( "%s", (const char*)VMA(1) ); return 0; case G_ERROR: From 35dd2ce2a852b5c50eae2b27848d609c53f6083d Mon Sep 17 00:00:00 2001 From: frog Date: Mon, 17 Mar 2025 11:22:36 -0500 Subject: [PATCH 05/46] record -> records --- Makefile | 4 ++-- code/recordsystem/rs_record.c | 26 -------------------------- 2 files changed, 2 insertions(+), 28 deletions(-) delete mode 100644 code/recordsystem/rs_record.c diff --git a/Makefile b/Makefile index b3de3ea589..a32da99f4a 100644 --- a/Makefile +++ b/Makefile @@ -1070,7 +1070,7 @@ Q3OBJ = \ $(B)/client/cmd.o \ $(B)/client/common.o \ $(B)/client/rs_main.o \ - $(B)/client/rs_record.o \ + $(B)/client/rs_records.o \ $(B)/client/cvar.o \ $(B)/client/files.o \ $(B)/client/history.o \ @@ -1309,7 +1309,7 @@ Q3DOBJ = \ $(B)/ded/cm_trace.o \ $(B)/ded/cmd.o \ $(B)/ded/rs_main.o \ - $(B)/ded/rs_record.o \ + $(B)/ded/rs_records.o \ $(B)/ded/common.o \ $(B)/ded/cvar.o \ $(B)/ded/files.o \ diff --git a/code/recordsystem/rs_record.c b/code/recordsystem/rs_record.c deleted file mode 100644 index 8e6fced379..0000000000 --- a/code/recordsystem/rs_record.c +++ /dev/null @@ -1,26 +0,0 @@ -#include "recordsystem.h" -#include "../server/server.h" - -/* -=============== -SV_GameSendServerCommand - -Sends a command string to a client -=============== -*/ -static void RS_GameSendServerCommand( int clientNum, const char *text ) { - if ( clientNum == -1 ) { - SV_SendServerCommand( NULL, "%s", text ); - } else { - if ( clientNum < 0 || clientNum >= sv.maxclients ) { - return; - } - SV_SendServerCommand( svs.clients + clientNum, "%s", text ); - } -} - -void RS_CreateRecord(void){ - Sys_Sleep(5000); - RS_GameSendServerCommand( -1, "print \"^5You have finished\"\n" ); -} - From 180b1edc74791a34746c925b07ff731ef7e0545c Mon Sep 17 00:00:00 2001 From: frog Date: Mon, 17 Mar 2025 11:22:54 -0500 Subject: [PATCH 06/46] record -> records --- code/recordsystem/rs_records.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 code/recordsystem/rs_records.c diff --git a/code/recordsystem/rs_records.c b/code/recordsystem/rs_records.c new file mode 100644 index 0000000000..8e6fced379 --- /dev/null +++ b/code/recordsystem/rs_records.c @@ -0,0 +1,26 @@ +#include "recordsystem.h" +#include "../server/server.h" + +/* +=============== +SV_GameSendServerCommand + +Sends a command string to a client +=============== +*/ +static void RS_GameSendServerCommand( int clientNum, const char *text ) { + if ( clientNum == -1 ) { + SV_SendServerCommand( NULL, "%s", text ); + } else { + if ( clientNum < 0 || clientNum >= sv.maxclients ) { + return; + } + SV_SendServerCommand( svs.clients + clientNum, "%s", text ); + } +} + +void RS_CreateRecord(void){ + Sys_Sleep(5000); + RS_GameSendServerCommand( -1, "print \"^5You have finished\"\n" ); +} + From f8f0d2acbc5af5cc9b31b9ac6f32930ec407e959 Mon Sep 17 00:00:00 2001 From: frog Date: Mon, 17 Mar 2025 12:58:47 -0500 Subject: [PATCH 07/46] command recognition --- Makefile | 4 +++ code/recordsystem/auth.c | 0 code/recordsystem/recent.c | 0 code/recordsystem/recordsystem.h | 8 +++++ code/recordsystem/rs_commands.c | 10 ++++++ code/recordsystem/rs_common.c | 58 ++++++++++++++++++++++++++++++ code/recordsystem/rs_main.c | 61 ++++++-------------------------- code/recordsystem/rs_records.c | 26 ++++---------- 8 files changed, 97 insertions(+), 70 deletions(-) delete mode 100644 code/recordsystem/auth.c delete mode 100644 code/recordsystem/recent.c create mode 100644 code/recordsystem/rs_commands.c create mode 100644 code/recordsystem/rs_common.c diff --git a/Makefile b/Makefile index a32da99f4a..1fa58586bb 100644 --- a/Makefile +++ b/Makefile @@ -1071,6 +1071,8 @@ Q3OBJ = \ $(B)/client/common.o \ $(B)/client/rs_main.o \ $(B)/client/rs_records.o \ + $(B)/client/rs_common.o \ + $(B)/client/rs_commands.o \ $(B)/client/cvar.o \ $(B)/client/files.o \ $(B)/client/history.o \ @@ -1310,6 +1312,8 @@ Q3DOBJ = \ $(B)/ded/cmd.o \ $(B)/ded/rs_main.o \ $(B)/ded/rs_records.o \ + $(B)/ded/rs_common.o \ + $(B)/ded/rs_commands.o \ $(B)/ded/common.o \ $(B)/ded/cvar.o \ $(B)/ded/files.o \ diff --git a/code/recordsystem/auth.c b/code/recordsystem/auth.c deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/code/recordsystem/recent.c b/code/recordsystem/recent.c deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/code/recordsystem/recordsystem.h b/code/recordsystem/recordsystem.h index 858bbb4da2..00031c8949 100644 --- a/code/recordsystem/recordsystem.h +++ b/code/recordsystem/recordsystem.h @@ -37,4 +37,12 @@ void Sys_CreateThread(void (*function)(void)); void RS_CreateRecord(void); +void RS_GameSendServerCommand( int clientNum, const char *text ); + +qboolean RS_IsCommand(const char *s); + +void RS_CommandGateway(void); + +qboolean RS_IsClientTimerStop( const char *s); + #endif // __RECORDSYSTEM_H__ \ No newline at end of file diff --git a/code/recordsystem/rs_commands.c b/code/recordsystem/rs_commands.c new file mode 100644 index 0000000000..16389a9383 --- /dev/null +++ b/code/recordsystem/rs_commands.c @@ -0,0 +1,10 @@ +#include "recordsystem.h" + +qboolean RS_IsCommand(const char *s) { + // parse string for client commands + return startsWith(s, "say: frog: !") ? qtrue: qfalse; +} + +void RS_CommandGateway(void) { + return RS_GameSendServerCommand( -1, "print \"^5Command Received\n\"" ); +} \ No newline at end of file diff --git a/code/recordsystem/rs_common.c b/code/recordsystem/rs_common.c new file mode 100644 index 0000000000..652f8dc80a --- /dev/null +++ b/code/recordsystem/rs_common.c @@ -0,0 +1,58 @@ +#include "recordsystem.h" +#include "../server/server.h" + +qboolean startsWith(const char *string, const char *prefix) { + if (!string || !prefix) { + return qfalse; + } + + size_t prefixLen = strlen(prefix); + size_t stringLen = strlen(string); + + if (prefixLen > stringLen) { + return qfalse; + } + + return (strncmp(string, prefix, prefixLen) == 0) ? qtrue : qfalse; +} + +/* +=============== +Sys_CreateThread + +Create a new thread of execution +=============== +*/ +void Sys_CreateThread(void (*function)(void)) { + // POSIX implementation (Linux, macOS, etc.) + pthread_t threadHandle; + pthread_attr_t attr; + int result; + + pthread_attr_init(&attr); + // Create the thread in detached state so its resources are automatically + // freed when it exits + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + + result = pthread_create(&threadHandle, + &attr, + (void*(*)(void*))function, + NULL); + + pthread_attr_destroy(&attr); + + if (result != 0) { + Com_Error(ERR_FATAL, "Sys_CreateThread: pthread_create failed with error %d", result); + } +} + +void RS_GameSendServerCommand( int clientNum, const char *text ) { + if ( clientNum == -1 ) { + SV_SendServerCommand( NULL, "%s", text ); + } else { + if ( clientNum < 0 || clientNum >= sv.maxclients ) { + return; + } + SV_SendServerCommand( svs.clients + clientNum, "%s", text ); + } +} diff --git a/code/recordsystem/rs_main.c b/code/recordsystem/rs_main.c index 0d4b21388d..330ec7782b 100644 --- a/code/recordsystem/rs_main.c +++ b/code/recordsystem/rs_main.c @@ -1,57 +1,18 @@ #include "recordsystem.h" -qboolean startsWith(const char *string, const char *prefix) { - if (!string || !prefix) { - return qfalse; - } +void RS_Gateway(const char *s) { - size_t prefixLen = strlen(prefix); - size_t stringLen = strlen(string); + if (RS_IsClientTimerStop(s)) { + if (Cvar_VariableIntegerValue("sv_cheats") != 0) { + return; + } + Sys_CreateThread((void (*)(void))RS_CreateRecord); + } - if (prefixLen > stringLen) { - return qfalse; + if (RS_IsCommand(s)) { + // Additional checks or logic for this case + Sys_CreateThread((void (*)(void))RS_CommandGateway); } - return (strncmp(string, prefix, prefixLen) == 0) ? qtrue : qfalse; + return; } - -void RS_Gateway(const char *s) { - if (!startsWith(s, "ClientBegin: ")) return; - - Sys_CreateThread((void (*)(void))RS_CreateRecord); -// SV_GameSendServerCommand( -1, "print \"^5You have finished\"\n" ); - // parse the string for client num, - // check client is logged in - if (Cvar_VariableIntegerValue("sv_cheats") != 0) return; - // blah blah spawn a new thread or send a message somewhere blah blah -} - -/* -=============== -Sys_CreateThread - -Create a new thread of execution -=============== -*/ -void Sys_CreateThread(void (*function)(void)) { - // POSIX implementation (Linux, macOS, etc.) - pthread_t threadHandle; - pthread_attr_t attr; - int result; - - pthread_attr_init(&attr); - // Create the thread in detached state so its resources are automatically - // freed when it exits - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); - - result = pthread_create(&threadHandle, - &attr, - (void*(*)(void*))function, - NULL); - - pthread_attr_destroy(&attr); - - if (result != 0) { - Com_Error(ERR_FATAL, "Sys_CreateThread: pthread_create failed with error %d", result); - } -} \ No newline at end of file diff --git a/code/recordsystem/rs_records.c b/code/recordsystem/rs_records.c index 8e6fced379..a581bc9aab 100644 --- a/code/recordsystem/rs_records.c +++ b/code/recordsystem/rs_records.c @@ -1,26 +1,12 @@ #include "recordsystem.h" -#include "../server/server.h" -/* -=============== -SV_GameSendServerCommand - -Sends a command string to a client -=============== -*/ -static void RS_GameSendServerCommand( int clientNum, const char *text ) { - if ( clientNum == -1 ) { - SV_SendServerCommand( NULL, "%s", text ); - } else { - if ( clientNum < 0 || clientNum >= sv.maxclients ) { - return; - } - SV_SendServerCommand( svs.clients + clientNum, "%s", text ); - } +qboolean RS_IsClientTimerStop( const char *s) { + // extra logic here to make sure it's a true timer stop + // potential: compare playerstates between this and last frame: + // check for timer state bit and that it's non-zero + return startsWith(s, "ClientTimerStop: ") ? qtrue: qfalse; } void RS_CreateRecord(void){ - Sys_Sleep(5000); - RS_GameSendServerCommand( -1, "print \"^5You have finished\"\n" ); + RS_GameSendServerCommand( -1, "print \"^5You have finished\n\"" ); } - From 00687dfd545c8e989bd873dfd8cd916b31fb4949 Mon Sep 17 00:00:00 2001 From: frog Date: Mon, 17 Mar 2025 17:43:31 -0500 Subject: [PATCH 08/46] set up handling of modules --- code/recordsystem/recordsystem.h | 6 +-- code/recordsystem/rs_commands.c | 87 +++++++++++++++++++++++++++++--- code/recordsystem/rs_common.c | 8 +-- code/recordsystem/rs_main.c | 4 +- code/recordsystem/rs_records.c | 2 +- 5 files changed, 91 insertions(+), 16 deletions(-) diff --git a/code/recordsystem/recordsystem.h b/code/recordsystem/recordsystem.h index 00031c8949..f3becb2115 100644 --- a/code/recordsystem/recordsystem.h +++ b/code/recordsystem/recordsystem.h @@ -33,15 +33,15 @@ Sys_CreateThread Create a new thread of execution with no arguments =============== */ -void Sys_CreateThread(void (*function)(void)); +void Sys_CreateThread(void (*function)(const char *), const char *arg); -void RS_CreateRecord(void); +void RS_CreateRecord(const char *s); void RS_GameSendServerCommand( int clientNum, const char *text ); qboolean RS_IsCommand(const char *s); -void RS_CommandGateway(void); +void RS_CommandGateway(const char *s); qboolean RS_IsClientTimerStop( const char *s); diff --git a/code/recordsystem/rs_commands.c b/code/recordsystem/rs_commands.c index 16389a9383..4c6a383c4a 100644 --- a/code/recordsystem/rs_commands.c +++ b/code/recordsystem/rs_commands.c @@ -1,10 +1,85 @@ #include "recordsystem.h" -qboolean RS_IsCommand(const char *s) { - // parse string for client commands - return startsWith(s, "say: frog: !") ? qtrue: qfalse; +/* +=============== +endsWith + +Returns qtrue if the string ends with the given suffix +=============== +*/ + +static void RS_Top(const char *map) { + return RS_GameSendServerCommand( -1, "print \"^5Top\n\"" ); +} + +static void RS_Recent(const char *map) { + return RS_GameSendServerCommand( -1, "print \"^5Recent maps: st1\n\"" ); +} + +static void RS_Login(const char *map) { + return RS_GameSendServerCommand( -1, "print \"^5You are now logged in\n\"" ); +} + +static void RS_Logout(const char *map) { + return RS_GameSendServerCommand( -1, "print \"^5You are now logged out\n\"" ); } -void RS_CommandGateway(void) { - return RS_GameSendServerCommand( -1, "print \"^5Command Received\n\"" ); -} \ No newline at end of file +typedef struct { + const char *suffix; + void (*handler)(const char *); +} Module; + +Module modules[] = { + {": !top\n", RS_Top}, + {": !recent\n", RS_Recent}, + {": !login\n", RS_Login}, + {": !logout\n", RS_Logout} +}; + +static qboolean endsWith(const char *string, const char *suffix) { + if (!string || !suffix) { + return qfalse; + } + + size_t stringLen = strlen(string); + size_t suffixLen = strlen(suffix); + + if (suffixLen > stringLen) { + return qfalse; + } + + return (strcmp(string + stringLen - suffixLen, suffix) == 0) ? qtrue : qfalse; +} + +qboolean RS_IsCommand(const char *string) { + if (!string) { + return qfalse; + } + + // Check if it starts with "say:" + if (!startsWith(string, "say:")) { + return qfalse; + } + + int numModules= sizeof(modules) / sizeof(modules[0]); + for (int i = 0; i < numModules; i++) { + if (endsWith(string, modules[i].suffix)) { + return qtrue; + } + } + + return qfalse; +} + +void RS_CommandGateway(const char *string) { + // Check each command pattern + int numModules= sizeof(modules) / sizeof(modules[0]); + for (int i = 0; i < numModules; i++) { + if (endsWith(string, modules[i].suffix)) { + // Call the appropriate handler function + modules[i].handler(string); + return; + } + } + return RS_GameSendServerCommand( -1, "print \"^5Command Received\n\"" ); +} diff --git a/code/recordsystem/rs_common.c b/code/recordsystem/rs_common.c index 652f8dc80a..166c5701f5 100644 --- a/code/recordsystem/rs_common.c +++ b/code/recordsystem/rs_common.c @@ -23,8 +23,7 @@ Sys_CreateThread Create a new thread of execution =============== */ -void Sys_CreateThread(void (*function)(void)) { - // POSIX implementation (Linux, macOS, etc.) +void Sys_CreateThread(void (*function)(const char *), const char *arg) { pthread_t threadHandle; pthread_attr_t attr; int result; @@ -34,10 +33,11 @@ void Sys_CreateThread(void (*function)(void)) { // freed when it exits pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + // Cast the function pointer and pass the character as an integer cast to void* result = pthread_create(&threadHandle, &attr, - (void*(*)(void*))function, - NULL); + (void*(*)(void*))function, + (void*)(intptr_t)arg); pthread_attr_destroy(&attr); diff --git a/code/recordsystem/rs_main.c b/code/recordsystem/rs_main.c index 330ec7782b..ac262a3da5 100644 --- a/code/recordsystem/rs_main.c +++ b/code/recordsystem/rs_main.c @@ -6,12 +6,12 @@ void RS_Gateway(const char *s) { if (Cvar_VariableIntegerValue("sv_cheats") != 0) { return; } - Sys_CreateThread((void (*)(void))RS_CreateRecord); + Sys_CreateThread(RS_CreateRecord, s); } if (RS_IsCommand(s)) { // Additional checks or logic for this case - Sys_CreateThread((void (*)(void))RS_CommandGateway); + Sys_CreateThread(RS_CommandGateway, s); } return; diff --git a/code/recordsystem/rs_records.c b/code/recordsystem/rs_records.c index a581bc9aab..779defa0e6 100644 --- a/code/recordsystem/rs_records.c +++ b/code/recordsystem/rs_records.c @@ -7,6 +7,6 @@ qboolean RS_IsClientTimerStop( const char *s) { return startsWith(s, "ClientTimerStop: ") ? qtrue: qfalse; } -void RS_CreateRecord(void){ +void RS_CreateRecord(const char *s){ RS_GameSendServerCommand( -1, "print \"^5You have finished\n\"" ); } From ce6f6206ce9e4d0685de00f4a22c5e00512663ec Mon Sep 17 00:00:00 2001 From: frog Date: Mon, 17 Mar 2025 23:36:28 -0500 Subject: [PATCH 09/46] add http helper functions, hit API for top --- Makefile | 6 +-- code/recordsystem/recordsystem.h | 26 +++++++++++ code/recordsystem/rs_commands.c | 12 ++++- code/recordsystem/rs_common.c | 80 ++++++++++++++++++++++++++++++++ code/server/sv_game.c | 2 + 5 files changed, 120 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 1fa58586bb..c97e78d2c9 100644 --- a/Makefile +++ b/Makefile @@ -1069,10 +1069,6 @@ Q3OBJ = \ \ $(B)/client/cmd.o \ $(B)/client/common.o \ - $(B)/client/rs_main.o \ - $(B)/client/rs_records.o \ - $(B)/client/rs_common.o \ - $(B)/client/rs_commands.o \ $(B)/client/cvar.o \ $(B)/client/files.o \ $(B)/client/history.o \ @@ -1393,7 +1389,7 @@ endif $(B)/$(TARGET_SERVER): $(Q3DOBJ) $(echo_cmd) "LD $@" - $(Q)$(CC) -o $@ $(Q3DOBJ) $(LDFLAGS) + $(Q)$(CC) -o $@ $(Q3DOBJ) $(LDFLAGS) -lcurl ############################################################################# ## CLIENT/SERVER RULES diff --git a/code/recordsystem/recordsystem.h b/code/recordsystem/recordsystem.h index f3becb2115..f0d2fbaef5 100644 --- a/code/recordsystem/recordsystem.h +++ b/code/recordsystem/recordsystem.h @@ -8,6 +8,7 @@ #include "../qcommon/q_shared.h" #include "../qcommon/qcommon.h" + /* =============== startsWith @@ -45,4 +46,29 @@ void RS_CommandGateway(const char *s); qboolean RS_IsClientTimerStop( const char *s); +typedef struct { + char *memory; + size_t size; +} MemoryStruct; + +/* +=============== +WriteMemoryCallback + +Callback function for storing HTTP response data +=============== +*/ +// size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp); + +/* +=============== +RS_HttpGet + +Performs an HTTP GET request to the specified URL +Returns the response as a null-terminated string that must be freed by the caller +Returns NULL if the request failed +=============== +*/ +char* RS_HttpGet(const char *url); + #endif // __RECORDSYSTEM_H__ \ No newline at end of file diff --git a/code/recordsystem/rs_commands.c b/code/recordsystem/rs_commands.c index 4c6a383c4a..f2a10e0cec 100644 --- a/code/recordsystem/rs_commands.c +++ b/code/recordsystem/rs_commands.c @@ -9,7 +9,17 @@ Returns qtrue if the string ends with the given suffix */ static void RS_Top(const char *map) { - return RS_GameSendServerCommand( -1, "print \"^5Top\n\"" ); + char *response; + + // Make the HTTP request + response = RS_HttpGet("http://localhost:8000/api/records/st1"); + + if (response) { + return RS_GameSendServerCommand( -1, va("print %s", response)); + free(response); + } else { + return RS_GameSendServerCommand( -1, "print \"Failed to get response\n\"" ); + } } static void RS_Recent(const char *map) { diff --git a/code/recordsystem/rs_common.c b/code/recordsystem/rs_common.c index 166c5701f5..e1d7c5247d 100644 --- a/code/recordsystem/rs_common.c +++ b/code/recordsystem/rs_common.c @@ -1,5 +1,7 @@ #include "recordsystem.h" #include "../server/server.h" +#include + qboolean startsWith(const char *string, const char *prefix) { if (!string || !prefix) { @@ -56,3 +58,81 @@ void RS_GameSendServerCommand( int clientNum, const char *text ) { SV_SendServerCommand( svs.clients + clientNum, "%s", text ); } } + +static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) { + size_t realsize = size * nmemb; + MemoryStruct *mem = (MemoryStruct *)userp; + + char *ptr = realloc(mem->memory, mem->size + realsize + 1); + if(!ptr) { + // Out of memory + Com_Printf("RS: Not enough memory for HTTP response\n"); + return 0; + } + + mem->memory = ptr; + memcpy(&(mem->memory[mem->size]), contents, realsize); + mem->size += realsize; + mem->memory[mem->size] = 0; // Null terminate + + return realsize; +} + +/* +=============== +RS_HttpGet + +Performs an HTTP GET request to the specified URL +Returns the response as a null-terminated string that must be freed by the caller +Returns NULL if the request failed +=============== +*/ +char* RS_HttpGet(const char *url) { + CURL *curl; + CURLcode res; + MemoryStruct chunk; + char *response = NULL; + + // Initialize memory structure + chunk.memory = malloc(1); + chunk.size = 0; + + // Initialize cURL + curl_global_init(CURL_GLOBAL_ALL); + curl = curl_easy_init(); + + if (!curl) { + Com_Printf("RS: Failed to initialize cURL handle\n"); + free(chunk.memory); + curl_global_cleanup(); + return NULL; + } + + // Set options + curl_easy_setopt(curl, CURLOPT_URL, url); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk); + curl_easy_setopt(curl, CURLOPT_USERAGENT, "Quake III Record System"); + curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1); + curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10); // 10 second timeout + + // Perform the request + Com_Printf("RS: Making HTTP GET request to %s\n", url); + res = curl_easy_perform(curl); + + // Check for errors + if (res != CURLE_OK) { + Com_Printf("RS: HTTP GET failed: %s\n", curl_easy_strerror(res)); + free(chunk.memory); + } else { + // Request successful + Com_Printf("RS: HTTP GET successful (%lu bytes)\n", (unsigned long)chunk.size); + response = chunk.memory; + } + + // Clean up + curl_easy_cleanup(curl); + curl_global_cleanup(); + + return response; +} \ No newline at end of file diff --git a/code/server/sv_game.c b/code/server/sv_game.c index 4cb5b057bb..b089bb51a3 100644 --- a/code/server/sv_game.c +++ b/code/server/sv_game.c @@ -362,7 +362,9 @@ The module is making a system call static intptr_t SV_GameSystemCalls( intptr_t *args ) { switch( args[0] ) { case G_PRINT: + #ifdef DEDICATED RS_Gateway((const char *)VMA(1)); + #endif Com_Printf( "%s", (const char*)VMA(1) ); return 0; case G_ERROR: From 61b34f62a8b4c0ac14b2e2cc977e96856ec100bf Mon Sep 17 00:00:00 2001 From: frog Date: Fri, 21 Mar 2025 00:13:05 -0500 Subject: [PATCH 10/46] send url-encoded say str to API /commands endpoint --- code/recordsystem/recordsystem.h | 19 ++++++---- code/recordsystem/rs_commands.c | 44 +++++++++------------ code/recordsystem/rs_common.c | 65 ++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 34 deletions(-) diff --git a/code/recordsystem/recordsystem.h b/code/recordsystem/recordsystem.h index f0d2fbaef5..53eeecdaf8 100644 --- a/code/recordsystem/recordsystem.h +++ b/code/recordsystem/recordsystem.h @@ -9,15 +9,10 @@ #include "../qcommon/qcommon.h" -/* -=============== -startsWith - -Returns qtrue if the string begins with the given prefix -=============== -*/ qboolean startsWith(const char *string, const char *prefix); +qboolean endsWith(const char *string, const char *suffix); + /* =============== CheckForRS @@ -71,4 +66,14 @@ Returns NULL if the request failed */ char* RS_HttpGet(const char *url); +/* +=============== +RS_UrlEncode + +Encodes a string for use in a URL +The returned string must be freed by the caller +=============== +*/ +char* RS_UrlEncode(const char *str); + #endif // __RECORDSYSTEM_H__ \ No newline at end of file diff --git a/code/recordsystem/rs_commands.c b/code/recordsystem/rs_commands.c index f2a10e0cec..4a80c9a9e4 100644 --- a/code/recordsystem/rs_commands.c +++ b/code/recordsystem/rs_commands.c @@ -1,29 +1,33 @@ #include "recordsystem.h" +#include "../server/server.h" -/* -=============== -endsWith -Returns qtrue if the string ends with the given suffix -=============== -*/ - -static void RS_Top(const char *map) { +static void RS_Top(const char *str) { char *response; // Make the HTTP request - response = RS_HttpGet("http://localhost:8000/api/records/st1"); + response = RS_HttpGet(va("http://localhost:8000/api/commands/top?saystr=%s&curr_map=%s", RS_UrlEncode(str), sv_mapname->string)); if (response) { - return RS_GameSendServerCommand( -1, va("print %s", response)); + return RS_GameSendServerCommand( -1, va("print \"%s\"", response)); free(response); } else { return RS_GameSendServerCommand( -1, "print \"Failed to get response\n\"" ); } } -static void RS_Recent(const char *map) { - return RS_GameSendServerCommand( -1, "print \"^5Recent maps: st1\n\"" ); +static void RS_Recent(const char *str) { + char *response; + + // Make the HTTP request + response = RS_HttpGet(va("http://localhost:8000/api/commands/recent?saystr=%s", RS_UrlEncode(str))); + + if (response) { + return RS_GameSendServerCommand( -1, va("print \"%s\"", response)); + free(response); + } else { + return RS_GameSendServerCommand( -1, "print \"Failed to get response\n\"" ); + } } static void RS_Login(const char *map) { @@ -46,21 +50,6 @@ Module modules[] = { {": !logout\n", RS_Logout} }; -static qboolean endsWith(const char *string, const char *suffix) { - if (!string || !suffix) { - return qfalse; - } - - size_t stringLen = strlen(string); - size_t suffixLen = strlen(suffix); - - if (suffixLen > stringLen) { - return qfalse; - } - - return (strcmp(string + stringLen - suffixLen, suffix) == 0) ? qtrue : qfalse; -} - qboolean RS_IsCommand(const char *string) { if (!string) { return qfalse; @@ -81,6 +70,7 @@ qboolean RS_IsCommand(const char *string) { return qfalse; } + void RS_CommandGateway(const char *string) { // Check each command pattern int numModules= sizeof(modules) / sizeof(modules[0]); diff --git a/code/recordsystem/rs_common.c b/code/recordsystem/rs_common.c index e1d7c5247d..3eb01f2135 100644 --- a/code/recordsystem/rs_common.c +++ b/code/recordsystem/rs_common.c @@ -18,6 +18,21 @@ qboolean startsWith(const char *string, const char *prefix) { return (strncmp(string, prefix, prefixLen) == 0) ? qtrue : qfalse; } +qboolean endsWith(const char *string, const char *suffix) { + if (!string || !suffix) { + return qfalse; + } + + size_t stringLen = strlen(string); + size_t suffixLen = strlen(suffix); + + if (suffixLen > stringLen) { + return qfalse; + } + + return (strcmp(string + stringLen - suffixLen, suffix) == 0) ? qtrue : qfalse; +} + /* =============== Sys_CreateThread @@ -135,4 +150,54 @@ char* RS_HttpGet(const char *url) { curl_global_cleanup(); return response; +} + +/* +=============== +RS_UrlEncode + +Encodes a string for use in a URL +The returned string must be freed by the caller +=============== +*/ +char* RS_UrlEncode(const char *str) { + if (!str) { + return NULL; + } + + // Characters that don't need encoding + const char *safe = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~"; + + // Count the number of characters that need encoding + int len = strlen(str); + int encoded_len = 0; + int i; + + for (i = 0; i < len; i++) { + if (strchr(safe, str[i])) { + encoded_len++; + } else { + encoded_len += 3; // %XX format for each unsafe character + } + } + + // Allocate memory for the encoded string + char *encoded = (char *)malloc(encoded_len + 1); + if (!encoded) { + return NULL; + } + + // Encode the string + char *p = encoded; + for (i = 0; i < len; i++) { + if (strchr(safe, str[i])) { + *p++ = str[i]; + } else { + sprintf(p, "%%%02X", (unsigned char)str[i]); + p += 3; + } + } + *p = '\0'; + + return encoded; } \ No newline at end of file From 9d739342212d37ca0d9d6cc0ddf95d0f9746678a Mon Sep 17 00:00:00 2001 From: frog Date: Fri, 21 Mar 2025 00:46:56 -0500 Subject: [PATCH 11/46] send top command --- code/recordsystem/rs_commands.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/code/recordsystem/rs_commands.c b/code/recordsystem/rs_commands.c index 4a80c9a9e4..ece503ad35 100644 --- a/code/recordsystem/rs_commands.c +++ b/code/recordsystem/rs_commands.c @@ -39,12 +39,12 @@ static void RS_Logout(const char *map) { } typedef struct { - const char *suffix; + const char *pattern; void (*handler)(const char *); } Module; Module modules[] = { - {": !top\n", RS_Top}, + {": !top", RS_Top}, {": !recent\n", RS_Recent}, {": !login\n", RS_Login}, {": !logout\n", RS_Logout} @@ -62,7 +62,7 @@ qboolean RS_IsCommand(const char *string) { int numModules= sizeof(modules) / sizeof(modules[0]); for (int i = 0; i < numModules; i++) { - if (endsWith(string, modules[i].suffix)) { + if (endsWith(string, modules[i].pattern)) { return qtrue; } } @@ -75,7 +75,7 @@ void RS_CommandGateway(const char *string) { // Check each command pattern int numModules= sizeof(modules) / sizeof(modules[0]); for (int i = 0; i < numModules; i++) { - if (endsWith(string, modules[i].suffix)) { + if (endsWith(string, modules[i].pattern)) { // Call the appropriate handler function modules[i].handler(string); return; From 9efeefb77abb5262e2f2df63c50d09796a8e0657 Mon Sep 17 00:00:00 2001 From: frog Date: Sun, 23 Mar 2025 23:39:12 -0500 Subject: [PATCH 12/46] clean up and use clientCommand instead of say --- code/recordsystem/recordsystem.h | 62 +++++++++---- code/recordsystem/rs_commands.c | 147 ++++++++++++++++++++----------- code/recordsystem/rs_common.c | 129 +++++++++++++++++++++++---- code/recordsystem/rs_main.c | 10 +-- code/recordsystem/rs_records.c | 16 ++-- code/recordsystem/top.c | 0 code/server/sv_client.c | 9 ++ 7 files changed, 276 insertions(+), 97 deletions(-) delete mode 100644 code/recordsystem/top.c diff --git a/code/recordsystem/recordsystem.h b/code/recordsystem/recordsystem.h index 53eeecdaf8..2f4c7ae474 100644 --- a/code/recordsystem/recordsystem.h +++ b/code/recordsystem/recordsystem.h @@ -8,16 +8,15 @@ #include "../qcommon/q_shared.h" #include "../qcommon/qcommon.h" - +// String utility functions qboolean startsWith(const char *string, const char *prefix); - qboolean endsWith(const char *string, const char *suffix); /* =============== -CheckForRS +RS_Gateway -Checks if a string starts with "ClientBegin: " and performs actions +Main entry point for record system events =============== */ void RS_Gateway(const char *s); @@ -26,21 +25,52 @@ void RS_Gateway(const char *s); =============== Sys_CreateThread -Create a new thread of execution with no arguments +Create a new thread of execution with a string argument =============== */ void Sys_CreateThread(void (*function)(const char *), const char *arg); +/* +=============== +RS_CreateRecord + +Creates a record entry from a ClientTimerStop event +=============== +*/ void RS_CreateRecord(const char *s); -void RS_GameSendServerCommand( int clientNum, const char *text ); +/* +=============== +RS_GameSendServerCommand -qboolean RS_IsCommand(const char *s); +Wrapper for SV_SendServerCommand +=============== +*/ +void RS_GameSendServerCommand(int clientNum, const char *text); -void RS_CommandGateway(const char *s); +/* +=============== +RS_CommandGateway -qboolean RS_IsClientTimerStop( const char *s); +Routes commands to their appropriate handlers +=============== +*/ +void RS_CommandGateway(int clientNum, const char *plyrName, const char *s); +/* +=============== +RS_IsClientTimerStop + +Checks if a string represents a client timer stop event +=============== +*/ +qboolean RS_IsClientTimerStop(const char *s); + +/* +=============== +Memory structure for HTTP responses +=============== +*/ typedef struct { char *memory; size_t size; @@ -48,23 +78,25 @@ typedef struct { /* =============== -WriteMemoryCallback +RS_HttpGet -Callback function for storing HTTP response data +Performs an HTTP GET request to the specified URL +Returns the response as a null-terminated string that must be freed by the caller +Returns NULL if the request failed =============== */ -// size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp); +char* RS_HttpGet(const char *url); /* =============== -RS_HttpGet +RS_HttpPost -Performs an HTTP GET request to the specified URL +Performs an HTTP POST request to the specified URL with the given payload Returns the response as a null-terminated string that must be freed by the caller Returns NULL if the request failed =============== */ -char* RS_HttpGet(const char *url); +char* RS_HttpPost(const char *url, const char *contentType, const char *payload); /* =============== diff --git a/code/recordsystem/rs_commands.c b/code/recordsystem/rs_commands.c index ece503ad35..cdd8eecb26 100644 --- a/code/recordsystem/rs_commands.c +++ b/code/recordsystem/rs_commands.c @@ -1,85 +1,134 @@ #include "recordsystem.h" #include "../server/server.h" - -static void RS_Top(const char *str) { +static void RS_Top(int clientNum, const char *plyrName, const char *str) { char *response; + char *encoded_str; + char *encoded_map; + char url[512]; + + // Encode the command string + encoded_str = RS_UrlEncode(str); + if (!encoded_str) { + RS_GameSendServerCommand(-1, "print \"^1Error encoding command\n\""); + return; + } + + // Encode the map name + encoded_map = RS_UrlEncode(sv_mapname->string); + if (!encoded_map) { + free(encoded_str); // Free already allocated memory + RS_GameSendServerCommand(-1, "print \"^1Error encoding map name\n\""); + return; + } + + // Build the URL + Com_sprintf(url, sizeof(url), "http://localhost:8000/api/commands/top?client_num=%d&cmd_string=%s&curr_map=%s", + clientNum, encoded_str, encoded_map); + + // Free encoded strings when done + free(encoded_str); + free(encoded_map); // Make the HTTP request - response = RS_HttpGet(va("http://localhost:8000/api/commands/top?saystr=%s&curr_map=%s", RS_UrlEncode(str), sv_mapname->string)); + response = RS_HttpGet(url); if (response) { - return RS_GameSendServerCommand( -1, va("print \"%s\"", response)); - free(response); + RS_GameSendServerCommand(-1, va("print \"%s\"", response)); + free(response); // Free the response } else { - return RS_GameSendServerCommand( -1, "print \"Failed to get response\n\"" ); + RS_GameSendServerCommand(-1, "print \"^1Failed to get response\n\""); } } -static void RS_Recent(const char *str) { +static void RS_Recent(int clientNum, const char *plyrName, const char *str) { char *response; + char *encoded_str; + char url[512]; + + encoded_str = RS_UrlEncode(str); + if (!encoded_str) { + RS_GameSendServerCommand(-1, "print \"^1Error encoding command\n\""); + return; + } + + // Build the URL + Com_sprintf(url, sizeof(url), "http://localhost:8000/api/commands/recent?saystr=%s", encoded_str); + free(encoded_str); // Free encoded string when done // Make the HTTP request - response = RS_HttpGet(va("http://localhost:8000/api/commands/recent?saystr=%s", RS_UrlEncode(str))); + response = RS_HttpGet(url); if (response) { - return RS_GameSendServerCommand( -1, va("print \"%s\"", response)); - free(response); + RS_GameSendServerCommand(-1, va("print \"%s\"", response)); + free(response); // Free the response } else { - return RS_GameSendServerCommand( -1, "print \"Failed to get response\n\"" ); + RS_GameSendServerCommand(-1, "print \"^1Failed to get response\n\""); } } -static void RS_Login(const char *map) { - return RS_GameSendServerCommand( -1, "print \"^5You are now logged in\n\"" ); +static void RS_Login(int clientNum, const char *plyrName, const char *str) { + char *response; + char payload[512]; + + // Create JSON payload with the entire command string + Com_sprintf(payload, sizeof(payload), + "{\"saystr\":\"%s\"}", str); + + // Make the HTTP request + response = RS_HttpPost("http://localhost:8000/api/commands/login", + "application/json", payload); + + if (response) { + RS_GameSendServerCommand(-1, va("print \"%s\"", response)); + free(response); + } else { + RS_GameSendServerCommand(-1, "print \"^1Failed to connect to server\n\""); + } } -static void RS_Logout(const char *map) { - return RS_GameSendServerCommand( -1, "print \"^5You are now logged out\n\"" ); +static void RS_Logout(int clientNum, const char *plyrName, const char *str) { + char *response; + char payload[512]; + + // Create JSON payload with the entire command string + Com_sprintf(payload, sizeof(payload), + "{\"saystr\":\"%s\"}", str); + + // Make the HTTP request + response = RS_HttpPost("http://localhost:8000/api/commands/logout", + "application/json", payload); + + if (response) { + RS_GameSendServerCommand(-1, va("print \"%s\"", response)); + free(response); + } else { + RS_GameSendServerCommand(-1, "print \"^1Failed to connect to server\n\""); + } } - typedef struct { const char *pattern; - void (*handler)(const char *); + void (*handler)(int clientNum, const char *plyrName, const char *str); } Module; -Module modules[] = { - {": !top", RS_Top}, - {": !recent\n", RS_Recent}, - {": !login\n", RS_Login}, - {": !logout\n", RS_Logout} +static Module modules[] = { + {"top", RS_Top}, + {"recent", RS_Recent}, + {"login", RS_Login}, + {"logout", RS_Logout} }; -qboolean RS_IsCommand(const char *string) { - if (!string) { - return qfalse; - } - - // Check if it starts with "say:" - if (!startsWith(string, "say:")) { - return qfalse; - } - - int numModules= sizeof(modules) / sizeof(modules[0]); - for (int i = 0; i < numModules; i++) { - if (endsWith(string, modules[i].pattern)) { - return qtrue; - } - } - - return qfalse; -} - - -void RS_CommandGateway(const char *string) { +void RS_CommandGateway(int clientNum, const char *plyrName, const char *s) { // Check each command pattern - int numModules= sizeof(modules) / sizeof(modules[0]); + int numModules = sizeof(modules) / sizeof(modules[0]); for (int i = 0; i < numModules; i++) { - if (endsWith(string, modules[i].pattern)) { + if (startsWith(s, modules[i].pattern)) { // Call the appropriate handler function - modules[i].handler(string); + modules[i].handler(clientNum, plyrName, s); return; } } - return RS_GameSendServerCommand( -1, "print \"^5Command Received\n\"" ); -} + + // If we reach here, no command matched + RS_GameSendServerCommand(-1, "print \"^5Command Received but not recognized\n\""); +} \ No newline at end of file diff --git a/code/recordsystem/rs_common.c b/code/recordsystem/rs_common.c index 3eb01f2135..e79a033c56 100644 --- a/code/recordsystem/rs_common.c +++ b/code/recordsystem/rs_common.c @@ -2,7 +2,6 @@ #include "../server/server.h" #include - qboolean startsWith(const char *string, const char *prefix) { if (!string || !prefix) { return qfalse; @@ -37,10 +36,22 @@ qboolean endsWith(const char *string, const char *suffix) { =============== Sys_CreateThread -Create a new thread of execution +Create a new thread of execution with a string argument =============== */ void Sys_CreateThread(void (*function)(const char *), const char *arg) { + // We need to duplicate the string argument to ensure it remains valid + // for the duration of the thread + char *arg_copy = NULL; + + if (arg) { + arg_copy = strdup(arg); + if (!arg_copy) { + Com_Error(ERR_FATAL, "Sys_CreateThread: Failed to allocate memory for thread argument"); + return; + } + } + pthread_t threadHandle; pthread_attr_t attr; int result; @@ -50,28 +61,30 @@ void Sys_CreateThread(void (*function)(const char *), const char *arg) { // freed when it exits pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); - // Cast the function pointer and pass the character as an integer cast to void* result = pthread_create(&threadHandle, &attr, - (void*(*)(void*))function, - (void*)(intptr_t)arg); + (void *(*)(void *))function, + arg_copy); pthread_attr_destroy(&attr); if (result != 0) { + if (arg_copy) { + free(arg_copy); + } Com_Error(ERR_FATAL, "Sys_CreateThread: pthread_create failed with error %d", result); } } -void RS_GameSendServerCommand( int clientNum, const char *text ) { - if ( clientNum == -1 ) { - SV_SendServerCommand( NULL, "%s", text ); - } else { - if ( clientNum < 0 || clientNum >= sv.maxclients ) { - return; - } - SV_SendServerCommand( svs.clients + clientNum, "%s", text ); - } +void RS_GameSendServerCommand(int clientNum, const char *text) { + if (clientNum == -1) { + SV_SendServerCommand(NULL, "%s", text); + } else { + if (clientNum < 0 || clientNum >= sv.maxclients) { + return; + } + SV_SendServerCommand(svs.clients + clientNum, "%s", text); + } } static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) { @@ -79,7 +92,7 @@ static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, voi MemoryStruct *mem = (MemoryStruct *)userp; char *ptr = realloc(mem->memory, mem->size + realsize + 1); - if(!ptr) { + if (!ptr) { // Out of memory Com_Printf("RS: Not enough memory for HTTP response\n"); return 0; @@ -110,6 +123,11 @@ char* RS_HttpGet(const char *url) { // Initialize memory structure chunk.memory = malloc(1); + if (!chunk.memory) { + Com_Printf("RS: Failed to allocate memory for HTTP response\n"); + return NULL; + } + chunk.size = 0; // Initialize cURL @@ -200,4 +218,83 @@ char* RS_UrlEncode(const char *str) { *p = '\0'; return encoded; -} \ No newline at end of file +} + +/* +=============== +RS_HttpPost + +Performs an HTTP POST request to the specified URL with the given payload +Parameters: + url - The target URL for the POST request + contentType - The content type of the payload (e.g., "application/json") + payload - The data to send in the POST request +Returns the response as a null-terminated string that must be freed by the caller +Returns NULL if the request failed +=============== +*/ +char* RS_HttpPost(const char *url, const char *contentType, const char *payload) { + CURL *curl; + CURLcode res; + MemoryStruct chunk; + char *response = NULL; + + // Initialize memory structure + chunk.memory = malloc(1); + if (!chunk.memory) { + Com_Printf("RS: Failed to allocate memory for HTTP response\n"); + return NULL; + } + + chunk.size = 0; + + // Initialize cURL + curl_global_init(CURL_GLOBAL_ALL); + curl = curl_easy_init(); + + if (!curl) { + Com_Printf("RS: Failed to initialize cURL handle\n"); + free(chunk.memory); + curl_global_cleanup(); + return NULL; + } + + // Create headers list + struct curl_slist *headers = NULL; + if (contentType) { + char contentTypeHeader[256]; + Com_sprintf(contentTypeHeader, sizeof(contentTypeHeader), "Content-Type: %s", contentType); + headers = curl_slist_append(headers, contentTypeHeader); + } + + // Set options + curl_easy_setopt(curl, CURLOPT_URL, url); + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, payload); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk); + curl_easy_setopt(curl, CURLOPT_USERAGENT, "Quake III Record System"); + curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1); + curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10); // 10 second timeout + + // Perform the request + Com_Printf("RS: Making HTTP POST request to %s\n", url); + res = curl_easy_perform(curl); + + // Check for errors + if (res != CURLE_OK) { + Com_Printf("RS: HTTP POST failed: %s\n", curl_easy_strerror(res)); + free(chunk.memory); + } else { + // Request successful + Com_Printf("RS: HTTP POST successful (%lu bytes)\n", (unsigned long)chunk.size); + response = chunk.memory; + } + + // Clean up + curl_slist_free_all(headers); + curl_easy_cleanup(curl); + curl_global_cleanup(); + + return response; +}; \ No newline at end of file diff --git a/code/recordsystem/rs_main.c b/code/recordsystem/rs_main.c index ac262a3da5..08ebde8f4d 100644 --- a/code/recordsystem/rs_main.c +++ b/code/recordsystem/rs_main.c @@ -1,18 +1,10 @@ #include "recordsystem.h" void RS_Gateway(const char *s) { - if (RS_IsClientTimerStop(s)) { if (Cvar_VariableIntegerValue("sv_cheats") != 0) { return; } Sys_CreateThread(RS_CreateRecord, s); } - - if (RS_IsCommand(s)) { - // Additional checks or logic for this case - Sys_CreateThread(RS_CommandGateway, s); - } - - return; -} +} \ No newline at end of file diff --git a/code/recordsystem/rs_records.c b/code/recordsystem/rs_records.c index 779defa0e6..0bedf2f972 100644 --- a/code/recordsystem/rs_records.c +++ b/code/recordsystem/rs_records.c @@ -1,12 +1,12 @@ #include "recordsystem.h" -qboolean RS_IsClientTimerStop( const char *s) { - // extra logic here to make sure it's a true timer stop - // potential: compare playerstates between this and last frame: - // check for timer state bit and that it's non-zero - return startsWith(s, "ClientTimerStop: ") ? qtrue: qfalse; +qboolean RS_IsClientTimerStop(const char *s) { + // extra logic here to make sure it's a true timer stop + // potential: compare playerstates between this and last frame: + // check for timer state bit and that it's non-zero + return startsWith(s, "ClientTimerStop: ") ? qtrue : qfalse; } -void RS_CreateRecord(const char *s){ - RS_GameSendServerCommand( -1, "print \"^5You have finished\n\"" ); -} +void RS_CreateRecord(const char *s) { + RS_GameSendServerCommand(-1, "print \"^5You have finished\n\""); +} \ No newline at end of file diff --git a/code/recordsystem/top.c b/code/recordsystem/top.c deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/code/server/sv_client.c b/code/server/sv_client.c index 5e8458cd74..8e2fc9780d 100644 --- a/code/server/sv_client.c +++ b/code/server/sv_client.c @@ -23,6 +23,10 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include "server.h" +#ifdef DEDICATED +#include "../recordsystem/recordsystem.h" +#endif + static void SV_CloseDownload( client_t *cl ); // @@ -2077,6 +2081,11 @@ static qboolean SV_ClientCommand( client_t *cl, msg_t *msg ) { Com_DPrintf( "clientCommand: %s : %i : %s\n", cl->name, seq, s ); +#ifdef DEDICATED + int clientNum; + clientNum = cl - svs.clients; + RS_CommandGateway(clientNum, cl->name, s); +#endif // drop the connection if we have somehow lost commands if ( seq - cl->lastClientCommand > 1 ) { Com_Printf( "Client %s lost %i clientCommands\n", cl->name, seq - cl->lastClientCommand - 1 ); From af047912f574496f33d609a2472023fd1a34d3b4 Mon Sep 17 00:00:00 2001 From: frog Date: Mon, 24 Mar 2025 00:25:59 -0500 Subject: [PATCH 13/46] don't execute command if RS command --- code/recordsystem/recordsystem.h | 5 ++- code/recordsystem/rs_commands.c | 28 ++++++------- code/recordsystem/rs_common.c | 67 +++++++++++++++++++++++++++++++- code/server/sv_client.c | 16 +++++--- 4 files changed, 94 insertions(+), 22 deletions(-) diff --git a/code/recordsystem/recordsystem.h b/code/recordsystem/recordsystem.h index 2f4c7ae474..eb3e1cfa36 100644 --- a/code/recordsystem/recordsystem.h +++ b/code/recordsystem/recordsystem.h @@ -55,7 +55,7 @@ RS_CommandGateway Routes commands to their appropriate handlers =============== */ -void RS_CommandGateway(int clientNum, const char *plyrName, const char *s); +qboolean RS_CommandGateway(int clientNum, const char *plyrName, const char *s); /* =============== @@ -108,4 +108,7 @@ The returned string must be freed by the caller */ char* RS_UrlEncode(const char *str); + +void RS_PrintAPIResponse(const char *jsonString); + #endif // __RECORDSYSTEM_H__ \ No newline at end of file diff --git a/code/recordsystem/rs_commands.c b/code/recordsystem/rs_commands.c index cdd8eecb26..09d6cd4ca4 100644 --- a/code/recordsystem/rs_commands.c +++ b/code/recordsystem/rs_commands.c @@ -10,7 +10,7 @@ static void RS_Top(int clientNum, const char *plyrName, const char *str) { // Encode the command string encoded_str = RS_UrlEncode(str); if (!encoded_str) { - RS_GameSendServerCommand(-1, "print \"^1Error encoding command\n\""); + RS_GameSendServerCommand(clientNum, "print \"^1Error encoding command\n\""); return; } @@ -18,7 +18,7 @@ static void RS_Top(int clientNum, const char *plyrName, const char *str) { encoded_map = RS_UrlEncode(sv_mapname->string); if (!encoded_map) { free(encoded_str); // Free already allocated memory - RS_GameSendServerCommand(-1, "print \"^1Error encoding map name\n\""); + RS_GameSendServerCommand(clientNum, "print \"^1Error encoding map name\n\""); return; } @@ -34,10 +34,10 @@ static void RS_Top(int clientNum, const char *plyrName, const char *str) { response = RS_HttpGet(url); if (response) { - RS_GameSendServerCommand(-1, va("print \"%s\"", response)); + RS_PrintAPIResponse(response); free(response); // Free the response } else { - RS_GameSendServerCommand(-1, "print \"^1Failed to get response\n\""); + RS_GameSendServerCommand(clientNum, "print \"^1Failed to get response\n\""); } } @@ -48,7 +48,7 @@ static void RS_Recent(int clientNum, const char *plyrName, const char *str) { encoded_str = RS_UrlEncode(str); if (!encoded_str) { - RS_GameSendServerCommand(-1, "print \"^1Error encoding command\n\""); + RS_GameSendServerCommand(clientNum, "print \"^1Error encoding command\n\""); return; } @@ -60,10 +60,10 @@ static void RS_Recent(int clientNum, const char *plyrName, const char *str) { response = RS_HttpGet(url); if (response) { - RS_GameSendServerCommand(-1, va("print \"%s\"", response)); + RS_PrintAPIResponse(response); free(response); // Free the response } else { - RS_GameSendServerCommand(-1, "print \"^1Failed to get response\n\""); + RS_GameSendServerCommand(clientNum, "print \"^1Failed to get response\n\""); } } @@ -80,10 +80,10 @@ static void RS_Login(int clientNum, const char *plyrName, const char *str) { "application/json", payload); if (response) { - RS_GameSendServerCommand(-1, va("print \"%s\"", response)); + RS_PrintAPIResponse(response); free(response); } else { - RS_GameSendServerCommand(-1, "print \"^1Failed to connect to server\n\""); + RS_GameSendServerCommand(clientNum, "print \"^1Failed to connect to server\n\""); } } @@ -100,10 +100,10 @@ static void RS_Logout(int clientNum, const char *plyrName, const char *str) { "application/json", payload); if (response) { - RS_GameSendServerCommand(-1, va("print \"%s\"", response)); + RS_PrintAPIResponse(response); free(response); } else { - RS_GameSendServerCommand(-1, "print \"^1Failed to connect to server\n\""); + RS_GameSendServerCommand(clientNum, "print \"^1Failed to connect to server\n\""); } } typedef struct { @@ -118,17 +118,17 @@ static Module modules[] = { {"logout", RS_Logout} }; -void RS_CommandGateway(int clientNum, const char *plyrName, const char *s) { +qboolean RS_CommandGateway(int clientNum, const char *plyrName, const char *s) { // Check each command pattern int numModules = sizeof(modules) / sizeof(modules[0]); for (int i = 0; i < numModules; i++) { if (startsWith(s, modules[i].pattern)) { // Call the appropriate handler function modules[i].handler(clientNum, plyrName, s); - return; + return qtrue; } } // If we reach here, no command matched - RS_GameSendServerCommand(-1, "print \"^5Command Received but not recognized\n\""); + return qfalse; } \ No newline at end of file diff --git a/code/recordsystem/rs_common.c b/code/recordsystem/rs_common.c index e79a033c56..5717cbbbb4 100644 --- a/code/recordsystem/rs_common.c +++ b/code/recordsystem/rs_common.c @@ -297,4 +297,69 @@ char* RS_HttpPost(const char *url, const char *contentType, const char *payload) curl_global_cleanup(); return response; -}; \ No newline at end of file +}; + +/* +=============== +RS_PrintAPIResponse + +Parses a JSON API response and sends the message to the target client +Expected format: "{"targetClient":-1,"message":"Map 'df_castle' not found"}" +=============== +*/ +/* +=============== +RS_PrintAPIResponse + +Parses a JSON API response and sends the message to the target client +Expected format: {"targetClient":-1,"message":"Map 'df_castle' not found"} +=============== +*/ +void RS_PrintAPIResponse(const char *jsonString) { + char message[1024] = {0}; + int targetClient = -1; + const char *startMsg, *endMsg; + const char *startClient; + + // Sanity check + if (!jsonString || !*jsonString) { + Com_Printf("RS: Empty API response\n"); + return; + } + + // Parse targetClient field - look for "targetClient":X + startClient = strstr(jsonString, "\"targetClient\":"); + if (startClient) { + startClient += 15; // Length of "\"targetClient\":" + + // Skip whitespace + while (*startClient && (*startClient == ' ' || *startClient == '\t')) + startClient++; + + // Read the client number + targetClient = atoi(startClient); + } + + // Parse message field - look for "message":"X" + startMsg = strstr(jsonString, "\"message\":\""); + if (startMsg) { + startMsg += 11; // Length of "\"message\":\"" + + // Find the closing quote + endMsg = strchr(startMsg, '\"'); + if (endMsg) { + // Extract the message text + int msgLen = endMsg - startMsg; + if (msgLen > 0 && msgLen < sizeof(message) - 1) { + Q_strncpyz(message, startMsg, msgLen + 1); + } + } + } + + // If we successfully parsed a message, send it to the target client + if (message[0] != '\0') { + RS_GameSendServerCommand(targetClient, va("print \"%s\n\"", message)); + } else { + Com_Printf("RS: Failed to parse message from API response: %s\n", jsonString); + } +} \ No newline at end of file diff --git a/code/server/sv_client.c b/code/server/sv_client.c index 8e2fc9780d..b1fc010a03 100644 --- a/code/server/sv_client.c +++ b/code/server/sv_client.c @@ -2081,11 +2081,6 @@ static qboolean SV_ClientCommand( client_t *cl, msg_t *msg ) { Com_DPrintf( "clientCommand: %s : %i : %s\n", cl->name, seq, s ); -#ifdef DEDICATED - int clientNum; - clientNum = cl - svs.clients; - RS_CommandGateway(clientNum, cl->name, s); -#endif // drop the connection if we have somehow lost commands if ( seq - cl->lastClientCommand > 1 ) { Com_Printf( "Client %s lost %i clientCommands\n", cl->name, seq - cl->lastClientCommand - 1 ); @@ -2093,10 +2088,19 @@ static qboolean SV_ClientCommand( client_t *cl, msg_t *msg ) { return qfalse; } +#ifdef DEDICATED + int clientNum; + clientNum = cl - svs.clients; + if (!RS_CommandGateway(clientNum, cl->name, s)) { + if ( !SV_ExecuteClientCommand( cl, s ) ) { + return qfalse; + } + } +#else if ( !SV_ExecuteClientCommand( cl, s ) ) { return qfalse; } - +#endif cl->lastClientCommand = seq; Q_strncpyz( cl->lastClientCommandString, s, sizeof( cl->lastClientCommandString ) ); From 3846a833a5810d0f2b3284ca0028dd35d6fc3b9e Mon Sep 17 00:00:00 2001 From: frog Date: Mon, 24 Mar 2025 23:11:25 -0500 Subject: [PATCH 14/46] login/logout, records --- Makefile | 1 + code/recordsystem/cJSON.c | 3143 ++++++++++++++++++++++++++++++ code/recordsystem/cJSON.h | 300 +++ code/recordsystem/recordsystem.h | 8 +- code/recordsystem/rs_commands.c | 69 +- code/recordsystem/rs_common.c | 76 +- code/recordsystem/rs_main.c | 2 +- code/recordsystem/rs_records.c | 54 +- 8 files changed, 3576 insertions(+), 77 deletions(-) create mode 100644 code/recordsystem/cJSON.c create mode 100644 code/recordsystem/cJSON.h diff --git a/Makefile b/Makefile index c97e78d2c9..b83885f0d0 100644 --- a/Makefile +++ b/Makefile @@ -1306,6 +1306,7 @@ Q3DOBJ = \ $(B)/ded/cm_test.o \ $(B)/ded/cm_trace.o \ $(B)/ded/cmd.o \ + $(B)/ded/cJSON.o \ $(B)/ded/rs_main.o \ $(B)/ded/rs_records.o \ $(B)/ded/rs_common.o \ diff --git a/code/recordsystem/cJSON.c b/code/recordsystem/cJSON.c new file mode 100644 index 0000000000..48a401a69d --- /dev/null +++ b/code/recordsystem/cJSON.c @@ -0,0 +1,3143 @@ +/* + Copyright (c) 2009-2017 Dave Gamble and cJSON contributors + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +/* cJSON */ +/* JSON parser in C. */ + +/* disable warnings about old C89 functions in MSVC */ +#if !defined(_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER) +#define _CRT_SECURE_NO_DEPRECATE +#endif + +#ifdef __GNUC__ +#pragma GCC visibility push(default) +#endif +#if defined(_MSC_VER) +#pragma warning (push) +/* disable warning about single line comments in system headers */ +#pragma warning (disable : 4001) +#endif + +#include +#include +#include +#include +#include +#include +#include + +#ifdef ENABLE_LOCALES +#include +#endif + +#if defined(_MSC_VER) +#pragma warning (pop) +#endif +#ifdef __GNUC__ +#pragma GCC visibility pop +#endif + +#include "cJSON.h" + +/* define our own boolean type */ +#ifdef true +#undef true +#endif +#define true ((cJSON_bool)1) + +#ifdef false +#undef false +#endif +#define false ((cJSON_bool)0) + +/* define isnan and isinf for ANSI C, if in C99 or above, isnan and isinf has been defined in math.h */ +#ifndef isinf +#define isinf(d) (isnan((d - d)) && !isnan(d)) +#endif +#ifndef isnan +#define isnan(d) (d != d) +#endif + +#ifndef NAN +#ifdef _WIN32 +#define NAN sqrt(-1.0) +#else +#define NAN 0.0/0.0 +#endif +#endif + +typedef struct { + const unsigned char *json; + size_t position; +} error; +static error global_error = { NULL, 0 }; + +CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void) +{ + return (const char*) (global_error.json + global_error.position); +} + +CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item) +{ + if (!cJSON_IsString(item)) + { + return NULL; + } + + return item->valuestring; +} + +CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const item) +{ + if (!cJSON_IsNumber(item)) + { + return (double) NAN; + } + + return item->valuedouble; +} + +/* This is a safeguard to prevent copy-pasters from using incompatible C and header files */ +#if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 7) || (CJSON_VERSION_PATCH != 18) + #error cJSON.h and cJSON.c have different versions. Make sure that both have the same. +#endif + +CJSON_PUBLIC(const char*) cJSON_Version(void) +{ + static char version[15]; + sprintf(version, "%i.%i.%i", CJSON_VERSION_MAJOR, CJSON_VERSION_MINOR, CJSON_VERSION_PATCH); + + return version; +} + +/* Case insensitive string comparison, doesn't consider two NULL pointers equal though */ +static int case_insensitive_strcmp(const unsigned char *string1, const unsigned char *string2) +{ + if ((string1 == NULL) || (string2 == NULL)) + { + return 1; + } + + if (string1 == string2) + { + return 0; + } + + for(; tolower(*string1) == tolower(*string2); (void)string1++, string2++) + { + if (*string1 == '\0') + { + return 0; + } + } + + return tolower(*string1) - tolower(*string2); +} + +typedef struct internal_hooks +{ + void *(CJSON_CDECL *allocate)(size_t size); + void (CJSON_CDECL *deallocate)(void *pointer); + void *(CJSON_CDECL *reallocate)(void *pointer, size_t size); +} internal_hooks; + +#if defined(_MSC_VER) +/* work around MSVC error C2322: '...' address of dllimport '...' is not static */ +static void * CJSON_CDECL internal_malloc(size_t size) +{ + return malloc(size); +} +static void CJSON_CDECL internal_free(void *pointer) +{ + free(pointer); +} +static void * CJSON_CDECL internal_realloc(void *pointer, size_t size) +{ + return realloc(pointer, size); +} +#else +#define internal_malloc malloc +#define internal_free free +#define internal_realloc realloc +#endif + +/* strlen of character literals resolved at compile time */ +#define static_strlen(string_literal) (sizeof(string_literal) - sizeof("")) + +static internal_hooks global_hooks = { internal_malloc, internal_free, internal_realloc }; + +static unsigned char* cJSON_strdup(const unsigned char* string, const internal_hooks * const hooks) +{ + size_t length = 0; + unsigned char *copy = NULL; + + if (string == NULL) + { + return NULL; + } + + length = strlen((const char*)string) + sizeof(""); + copy = (unsigned char*)hooks->allocate(length); + if (copy == NULL) + { + return NULL; + } + memcpy(copy, string, length); + + return copy; +} + +CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks) +{ + if (hooks == NULL) + { + /* Reset hooks */ + global_hooks.allocate = malloc; + global_hooks.deallocate = free; + global_hooks.reallocate = realloc; + return; + } + + global_hooks.allocate = malloc; + if (hooks->malloc_fn != NULL) + { + global_hooks.allocate = hooks->malloc_fn; + } + + global_hooks.deallocate = free; + if (hooks->free_fn != NULL) + { + global_hooks.deallocate = hooks->free_fn; + } + + /* use realloc only if both free and malloc are used */ + global_hooks.reallocate = NULL; + if ((global_hooks.allocate == malloc) && (global_hooks.deallocate == free)) + { + global_hooks.reallocate = realloc; + } +} + +/* Internal constructor. */ +static cJSON *cJSON_New_Item(const internal_hooks * const hooks) +{ + cJSON* node = (cJSON*)hooks->allocate(sizeof(cJSON)); + if (node) + { + memset(node, '\0', sizeof(cJSON)); + } + + return node; +} + +/* Delete a cJSON structure. */ +CJSON_PUBLIC(void) cJSON_Delete(cJSON *item) +{ + cJSON *next = NULL; + while (item != NULL) + { + next = item->next; + if (!(item->type & cJSON_IsReference) && (item->child != NULL)) + { + cJSON_Delete(item->child); + } + if (!(item->type & cJSON_IsReference) && (item->valuestring != NULL)) + { + global_hooks.deallocate(item->valuestring); + item->valuestring = NULL; + } + if (!(item->type & cJSON_StringIsConst) && (item->string != NULL)) + { + global_hooks.deallocate(item->string); + item->string = NULL; + } + global_hooks.deallocate(item); + item = next; + } +} + +/* get the decimal point character of the current locale */ +static unsigned char get_decimal_point(void) +{ +#ifdef ENABLE_LOCALES + struct lconv *lconv = localeconv(); + return (unsigned char) lconv->decimal_point[0]; +#else + return '.'; +#endif +} + +typedef struct +{ + const unsigned char *content; + size_t length; + size_t offset; + size_t depth; /* How deeply nested (in arrays/objects) is the input at the current offset. */ + internal_hooks hooks; +} parse_buffer; + +/* check if the given size is left to read in a given parse buffer (starting with 1) */ +#define can_read(buffer, size) ((buffer != NULL) && (((buffer)->offset + size) <= (buffer)->length)) +/* check if the buffer can be accessed at the given index (starting with 0) */ +#define can_access_at_index(buffer, index) ((buffer != NULL) && (((buffer)->offset + index) < (buffer)->length)) +#define cannot_access_at_index(buffer, index) (!can_access_at_index(buffer, index)) +/* get a pointer to the buffer at the position */ +#define buffer_at_offset(buffer) ((buffer)->content + (buffer)->offset) + +/* Parse the input text to generate a number, and populate the result into item. */ +static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_buffer) +{ + double number = 0; + unsigned char *after_end = NULL; + unsigned char number_c_string[64]; + unsigned char decimal_point = get_decimal_point(); + size_t i = 0; + + if ((input_buffer == NULL) || (input_buffer->content == NULL)) + { + return false; + } + + /* copy the number into a temporary buffer and replace '.' with the decimal point + * of the current locale (for strtod) + * This also takes care of '\0' not necessarily being available for marking the end of the input */ + for (i = 0; (i < (sizeof(number_c_string) - 1)) && can_access_at_index(input_buffer, i); i++) + { + switch (buffer_at_offset(input_buffer)[i]) + { + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + case '+': + case '-': + case 'e': + case 'E': + number_c_string[i] = buffer_at_offset(input_buffer)[i]; + break; + + case '.': + number_c_string[i] = decimal_point; + break; + + default: + goto loop_end; + } + } +loop_end: + number_c_string[i] = '\0'; + + number = strtod((const char*)number_c_string, (char**)&after_end); + if (number_c_string == after_end) + { + return false; /* parse_error */ + } + + item->valuedouble = number; + + /* use saturation in case of overflow */ + if (number >= INT_MAX) + { + item->valueint = INT_MAX; + } + else if (number <= (double)INT_MIN) + { + item->valueint = INT_MIN; + } + else + { + item->valueint = (int)number; + } + + item->type = cJSON_Number; + + input_buffer->offset += (size_t)(after_end - number_c_string); + return true; +} + +/* don't ask me, but the original cJSON_SetNumberValue returns an integer or double */ +CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number) +{ + if (number >= INT_MAX) + { + object->valueint = INT_MAX; + } + else if (number <= (double)INT_MIN) + { + object->valueint = INT_MIN; + } + else + { + object->valueint = (int)number; + } + + return object->valuedouble = number; +} + +/* Note: when passing a NULL valuestring, cJSON_SetValuestring treats this as an error and return NULL */ +CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring) +{ + char *copy = NULL; + /* if object's type is not cJSON_String or is cJSON_IsReference, it should not set valuestring */ + if ((object == NULL) || !(object->type & cJSON_String) || (object->type & cJSON_IsReference)) + { + return NULL; + } + /* return NULL if the object is corrupted or valuestring is NULL */ + if (object->valuestring == NULL || valuestring == NULL) + { + return NULL; + } + if (strlen(valuestring) <= strlen(object->valuestring)) + { + strcpy(object->valuestring, valuestring); + return object->valuestring; + } + copy = (char*) cJSON_strdup((const unsigned char*)valuestring, &global_hooks); + if (copy == NULL) + { + return NULL; + } + if (object->valuestring != NULL) + { + cJSON_free(object->valuestring); + } + object->valuestring = copy; + + return copy; +} + +typedef struct +{ + unsigned char *buffer; + size_t length; + size_t offset; + size_t depth; /* current nesting depth (for formatted printing) */ + cJSON_bool noalloc; + cJSON_bool format; /* is this print a formatted print */ + internal_hooks hooks; +} printbuffer; + +/* realloc printbuffer if necessary to have at least "needed" bytes more */ +static unsigned char* ensure(printbuffer * const p, size_t needed) +{ + unsigned char *newbuffer = NULL; + size_t newsize = 0; + + if ((p == NULL) || (p->buffer == NULL)) + { + return NULL; + } + + if ((p->length > 0) && (p->offset >= p->length)) + { + /* make sure that offset is valid */ + return NULL; + } + + if (needed > INT_MAX) + { + /* sizes bigger than INT_MAX are currently not supported */ + return NULL; + } + + needed += p->offset + 1; + if (needed <= p->length) + { + return p->buffer + p->offset; + } + + if (p->noalloc) { + return NULL; + } + + /* calculate new buffer size */ + if (needed > (INT_MAX / 2)) + { + /* overflow of int, use INT_MAX if possible */ + if (needed <= INT_MAX) + { + newsize = INT_MAX; + } + else + { + return NULL; + } + } + else + { + newsize = needed * 2; + } + + if (p->hooks.reallocate != NULL) + { + /* reallocate with realloc if available */ + newbuffer = (unsigned char*)p->hooks.reallocate(p->buffer, newsize); + if (newbuffer == NULL) + { + p->hooks.deallocate(p->buffer); + p->length = 0; + p->buffer = NULL; + + return NULL; + } + } + else + { + /* otherwise reallocate manually */ + newbuffer = (unsigned char*)p->hooks.allocate(newsize); + if (!newbuffer) + { + p->hooks.deallocate(p->buffer); + p->length = 0; + p->buffer = NULL; + + return NULL; + } + + memcpy(newbuffer, p->buffer, p->offset + 1); + p->hooks.deallocate(p->buffer); + } + p->length = newsize; + p->buffer = newbuffer; + + return newbuffer + p->offset; +} + +/* calculate the new length of the string in a printbuffer and update the offset */ +static void update_offset(printbuffer * const buffer) +{ + const unsigned char *buffer_pointer = NULL; + if ((buffer == NULL) || (buffer->buffer == NULL)) + { + return; + } + buffer_pointer = buffer->buffer + buffer->offset; + + buffer->offset += strlen((const char*)buffer_pointer); +} + +/* securely comparison of floating-point variables */ +static cJSON_bool compare_double(double a, double b) +{ + double maxVal = fabs(a) > fabs(b) ? fabs(a) : fabs(b); + return (fabs(a - b) <= maxVal * DBL_EPSILON); +} + +/* Render the number nicely from the given item into a string. */ +static cJSON_bool print_number(const cJSON * const item, printbuffer * const output_buffer) +{ + unsigned char *output_pointer = NULL; + double d = item->valuedouble; + int length = 0; + size_t i = 0; + unsigned char number_buffer[26] = {0}; /* temporary buffer to print the number into */ + unsigned char decimal_point = get_decimal_point(); + double test = 0.0; + + if (output_buffer == NULL) + { + return false; + } + + /* This checks for NaN and Infinity */ + if (isnan(d) || isinf(d)) + { + length = sprintf((char*)number_buffer, "null"); + } + else if(d == (double)item->valueint) + { + length = sprintf((char*)number_buffer, "%d", item->valueint); + } + else + { + /* Try 15 decimal places of precision to avoid nonsignificant nonzero digits */ + length = sprintf((char*)number_buffer, "%1.15g", d); + + /* Check whether the original double can be recovered */ + if ((sscanf((char*)number_buffer, "%lg", &test) != 1) || !compare_double((double)test, d)) + { + /* If not, print with 17 decimal places of precision */ + length = sprintf((char*)number_buffer, "%1.17g", d); + } + } + + /* sprintf failed or buffer overrun occurred */ + if ((length < 0) || (length > (int)(sizeof(number_buffer) - 1))) + { + return false; + } + + /* reserve appropriate space in the output */ + output_pointer = ensure(output_buffer, (size_t)length + sizeof("")); + if (output_pointer == NULL) + { + return false; + } + + /* copy the printed number to the output and replace locale + * dependent decimal point with '.' */ + for (i = 0; i < ((size_t)length); i++) + { + if (number_buffer[i] == decimal_point) + { + output_pointer[i] = '.'; + continue; + } + + output_pointer[i] = number_buffer[i]; + } + output_pointer[i] = '\0'; + + output_buffer->offset += (size_t)length; + + return true; +} + +/* parse 4 digit hexadecimal number */ +static unsigned parse_hex4(const unsigned char * const input) +{ + unsigned int h = 0; + size_t i = 0; + + for (i = 0; i < 4; i++) + { + /* parse digit */ + if ((input[i] >= '0') && (input[i] <= '9')) + { + h += (unsigned int) input[i] - '0'; + } + else if ((input[i] >= 'A') && (input[i] <= 'F')) + { + h += (unsigned int) 10 + input[i] - 'A'; + } + else if ((input[i] >= 'a') && (input[i] <= 'f')) + { + h += (unsigned int) 10 + input[i] - 'a'; + } + else /* invalid */ + { + return 0; + } + + if (i < 3) + { + /* shift left to make place for the next nibble */ + h = h << 4; + } + } + + return h; +} + +/* converts a UTF-16 literal to UTF-8 + * A literal can be one or two sequences of the form \uXXXX */ +static unsigned char utf16_literal_to_utf8(const unsigned char * const input_pointer, const unsigned char * const input_end, unsigned char **output_pointer) +{ + long unsigned int codepoint = 0; + unsigned int first_code = 0; + const unsigned char *first_sequence = input_pointer; + unsigned char utf8_length = 0; + unsigned char utf8_position = 0; + unsigned char sequence_length = 0; + unsigned char first_byte_mark = 0; + + if ((input_end - first_sequence) < 6) + { + /* input ends unexpectedly */ + goto fail; + } + + /* get the first utf16 sequence */ + first_code = parse_hex4(first_sequence + 2); + + /* check that the code is valid */ + if (((first_code >= 0xDC00) && (first_code <= 0xDFFF))) + { + goto fail; + } + + /* UTF16 surrogate pair */ + if ((first_code >= 0xD800) && (first_code <= 0xDBFF)) + { + const unsigned char *second_sequence = first_sequence + 6; + unsigned int second_code = 0; + sequence_length = 12; /* \uXXXX\uXXXX */ + + if ((input_end - second_sequence) < 6) + { + /* input ends unexpectedly */ + goto fail; + } + + if ((second_sequence[0] != '\\') || (second_sequence[1] != 'u')) + { + /* missing second half of the surrogate pair */ + goto fail; + } + + /* get the second utf16 sequence */ + second_code = parse_hex4(second_sequence + 2); + /* check that the code is valid */ + if ((second_code < 0xDC00) || (second_code > 0xDFFF)) + { + /* invalid second half of the surrogate pair */ + goto fail; + } + + + /* calculate the unicode codepoint from the surrogate pair */ + codepoint = 0x10000 + (((first_code & 0x3FF) << 10) | (second_code & 0x3FF)); + } + else + { + sequence_length = 6; /* \uXXXX */ + codepoint = first_code; + } + + /* encode as UTF-8 + * takes at maximum 4 bytes to encode: + * 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */ + if (codepoint < 0x80) + { + /* normal ascii, encoding 0xxxxxxx */ + utf8_length = 1; + } + else if (codepoint < 0x800) + { + /* two bytes, encoding 110xxxxx 10xxxxxx */ + utf8_length = 2; + first_byte_mark = 0xC0; /* 11000000 */ + } + else if (codepoint < 0x10000) + { + /* three bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx */ + utf8_length = 3; + first_byte_mark = 0xE0; /* 11100000 */ + } + else if (codepoint <= 0x10FFFF) + { + /* four bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx 10xxxxxx */ + utf8_length = 4; + first_byte_mark = 0xF0; /* 11110000 */ + } + else + { + /* invalid unicode codepoint */ + goto fail; + } + + /* encode as utf8 */ + for (utf8_position = (unsigned char)(utf8_length - 1); utf8_position > 0; utf8_position--) + { + /* 10xxxxxx */ + (*output_pointer)[utf8_position] = (unsigned char)((codepoint | 0x80) & 0xBF); + codepoint >>= 6; + } + /* encode first byte */ + if (utf8_length > 1) + { + (*output_pointer)[0] = (unsigned char)((codepoint | first_byte_mark) & 0xFF); + } + else + { + (*output_pointer)[0] = (unsigned char)(codepoint & 0x7F); + } + + *output_pointer += utf8_length; + + return sequence_length; + +fail: + return 0; +} + +/* Parse the input text into an unescaped cinput, and populate item. */ +static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_buffer) +{ + const unsigned char *input_pointer = buffer_at_offset(input_buffer) + 1; + const unsigned char *input_end = buffer_at_offset(input_buffer) + 1; + unsigned char *output_pointer = NULL; + unsigned char *output = NULL; + + /* not a string */ + if (buffer_at_offset(input_buffer)[0] != '\"') + { + goto fail; + } + + { + /* calculate approximate size of the output (overestimate) */ + size_t allocation_length = 0; + size_t skipped_bytes = 0; + while (((size_t)(input_end - input_buffer->content) < input_buffer->length) && (*input_end != '\"')) + { + /* is escape sequence */ + if (input_end[0] == '\\') + { + if ((size_t)(input_end + 1 - input_buffer->content) >= input_buffer->length) + { + /* prevent buffer overflow when last input character is a backslash */ + goto fail; + } + skipped_bytes++; + input_end++; + } + input_end++; + } + if (((size_t)(input_end - input_buffer->content) >= input_buffer->length) || (*input_end != '\"')) + { + goto fail; /* string ended unexpectedly */ + } + + /* This is at most how much we need for the output */ + allocation_length = (size_t) (input_end - buffer_at_offset(input_buffer)) - skipped_bytes; + output = (unsigned char*)input_buffer->hooks.allocate(allocation_length + sizeof("")); + if (output == NULL) + { + goto fail; /* allocation failure */ + } + } + + output_pointer = output; + /* loop through the string literal */ + while (input_pointer < input_end) + { + if (*input_pointer != '\\') + { + *output_pointer++ = *input_pointer++; + } + /* escape sequence */ + else + { + unsigned char sequence_length = 2; + if ((input_end - input_pointer) < 1) + { + goto fail; + } + + switch (input_pointer[1]) + { + case 'b': + *output_pointer++ = '\b'; + break; + case 'f': + *output_pointer++ = '\f'; + break; + case 'n': + *output_pointer++ = '\n'; + break; + case 'r': + *output_pointer++ = '\r'; + break; + case 't': + *output_pointer++ = '\t'; + break; + case '\"': + case '\\': + case '/': + *output_pointer++ = input_pointer[1]; + break; + + /* UTF-16 literal */ + case 'u': + sequence_length = utf16_literal_to_utf8(input_pointer, input_end, &output_pointer); + if (sequence_length == 0) + { + /* failed to convert UTF16-literal to UTF-8 */ + goto fail; + } + break; + + default: + goto fail; + } + input_pointer += sequence_length; + } + } + + /* zero terminate the output */ + *output_pointer = '\0'; + + item->type = cJSON_String; + item->valuestring = (char*)output; + + input_buffer->offset = (size_t) (input_end - input_buffer->content); + input_buffer->offset++; + + return true; + +fail: + if (output != NULL) + { + input_buffer->hooks.deallocate(output); + output = NULL; + } + + if (input_pointer != NULL) + { + input_buffer->offset = (size_t)(input_pointer - input_buffer->content); + } + + return false; +} + +/* Render the cstring provided to an escaped version that can be printed. */ +static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffer * const output_buffer) +{ + const unsigned char *input_pointer = NULL; + unsigned char *output = NULL; + unsigned char *output_pointer = NULL; + size_t output_length = 0; + /* numbers of additional characters needed for escaping */ + size_t escape_characters = 0; + + if (output_buffer == NULL) + { + return false; + } + + /* empty string */ + if (input == NULL) + { + output = ensure(output_buffer, sizeof("\"\"")); + if (output == NULL) + { + return false; + } + strcpy((char*)output, "\"\""); + + return true; + } + + /* set "flag" to 1 if something needs to be escaped */ + for (input_pointer = input; *input_pointer; input_pointer++) + { + switch (*input_pointer) + { + case '\"': + case '\\': + case '\b': + case '\f': + case '\n': + case '\r': + case '\t': + /* one character escape sequence */ + escape_characters++; + break; + default: + if (*input_pointer < 32) + { + /* UTF-16 escape sequence uXXXX */ + escape_characters += 5; + } + break; + } + } + output_length = (size_t)(input_pointer - input) + escape_characters; + + output = ensure(output_buffer, output_length + sizeof("\"\"")); + if (output == NULL) + { + return false; + } + + /* no characters have to be escaped */ + if (escape_characters == 0) + { + output[0] = '\"'; + memcpy(output + 1, input, output_length); + output[output_length + 1] = '\"'; + output[output_length + 2] = '\0'; + + return true; + } + + output[0] = '\"'; + output_pointer = output + 1; + /* copy the string */ + for (input_pointer = input; *input_pointer != '\0'; (void)input_pointer++, output_pointer++) + { + if ((*input_pointer > 31) && (*input_pointer != '\"') && (*input_pointer != '\\')) + { + /* normal character, copy */ + *output_pointer = *input_pointer; + } + else + { + /* character needs to be escaped */ + *output_pointer++ = '\\'; + switch (*input_pointer) + { + case '\\': + *output_pointer = '\\'; + break; + case '\"': + *output_pointer = '\"'; + break; + case '\b': + *output_pointer = 'b'; + break; + case '\f': + *output_pointer = 'f'; + break; + case '\n': + *output_pointer = 'n'; + break; + case '\r': + *output_pointer = 'r'; + break; + case '\t': + *output_pointer = 't'; + break; + default: + /* escape and print as unicode codepoint */ + sprintf((char*)output_pointer, "u%04x", *input_pointer); + output_pointer += 4; + break; + } + } + } + output[output_length + 1] = '\"'; + output[output_length + 2] = '\0'; + + return true; +} + +/* Invoke print_string_ptr (which is useful) on an item. */ +static cJSON_bool print_string(const cJSON * const item, printbuffer * const p) +{ + return print_string_ptr((unsigned char*)item->valuestring, p); +} + +/* Predeclare these prototypes. */ +static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer); +static cJSON_bool print_value(const cJSON * const item, printbuffer * const output_buffer); +static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer); +static cJSON_bool print_array(const cJSON * const item, printbuffer * const output_buffer); +static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer); +static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer); + +/* Utility to jump whitespace and cr/lf */ +static parse_buffer *buffer_skip_whitespace(parse_buffer * const buffer) +{ + if ((buffer == NULL) || (buffer->content == NULL)) + { + return NULL; + } + + if (cannot_access_at_index(buffer, 0)) + { + return buffer; + } + + while (can_access_at_index(buffer, 0) && (buffer_at_offset(buffer)[0] <= 32)) + { + buffer->offset++; + } + + if (buffer->offset == buffer->length) + { + buffer->offset--; + } + + return buffer; +} + +/* skip the UTF-8 BOM (byte order mark) if it is at the beginning of a buffer */ +static parse_buffer *skip_utf8_bom(parse_buffer * const buffer) +{ + if ((buffer == NULL) || (buffer->content == NULL) || (buffer->offset != 0)) + { + return NULL; + } + + if (can_access_at_index(buffer, 4) && (strncmp((const char*)buffer_at_offset(buffer), "\xEF\xBB\xBF", 3) == 0)) + { + buffer->offset += 3; + } + + return buffer; +} + +CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated) +{ + size_t buffer_length; + + if (NULL == value) + { + return NULL; + } + + /* Adding null character size due to require_null_terminated. */ + buffer_length = strlen(value) + sizeof(""); + + return cJSON_ParseWithLengthOpts(value, buffer_length, return_parse_end, require_null_terminated); +} + +/* Parse an object - create a new root, and populate. */ +CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, cJSON_bool require_null_terminated) +{ + parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } }; + cJSON *item = NULL; + + /* reset error position */ + global_error.json = NULL; + global_error.position = 0; + + if (value == NULL || 0 == buffer_length) + { + goto fail; + } + + buffer.content = (const unsigned char*)value; + buffer.length = buffer_length; + buffer.offset = 0; + buffer.hooks = global_hooks; + + item = cJSON_New_Item(&global_hooks); + if (item == NULL) /* memory fail */ + { + goto fail; + } + + if (!parse_value(item, buffer_skip_whitespace(skip_utf8_bom(&buffer)))) + { + /* parse failure. ep is set. */ + goto fail; + } + + /* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */ + if (require_null_terminated) + { + buffer_skip_whitespace(&buffer); + if ((buffer.offset >= buffer.length) || buffer_at_offset(&buffer)[0] != '\0') + { + goto fail; + } + } + if (return_parse_end) + { + *return_parse_end = (const char*)buffer_at_offset(&buffer); + } + + return item; + +fail: + if (item != NULL) + { + cJSON_Delete(item); + } + + if (value != NULL) + { + error local_error; + local_error.json = (const unsigned char*)value; + local_error.position = 0; + + if (buffer.offset < buffer.length) + { + local_error.position = buffer.offset; + } + else if (buffer.length > 0) + { + local_error.position = buffer.length - 1; + } + + if (return_parse_end != NULL) + { + *return_parse_end = (const char*)local_error.json + local_error.position; + } + + global_error = local_error; + } + + return NULL; +} + +/* Default options for cJSON_Parse */ +CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value) +{ + return cJSON_ParseWithOpts(value, 0, 0); +} + +CJSON_PUBLIC(cJSON *) cJSON_ParseWithLength(const char *value, size_t buffer_length) +{ + return cJSON_ParseWithLengthOpts(value, buffer_length, 0, 0); +} + +#define cjson_min(a, b) (((a) < (b)) ? (a) : (b)) + +static unsigned char *print(const cJSON * const item, cJSON_bool format, const internal_hooks * const hooks) +{ + static const size_t default_buffer_size = 256; + printbuffer buffer[1]; + unsigned char *printed = NULL; + + memset(buffer, 0, sizeof(buffer)); + + /* create buffer */ + buffer->buffer = (unsigned char*) hooks->allocate(default_buffer_size); + buffer->length = default_buffer_size; + buffer->format = format; + buffer->hooks = *hooks; + if (buffer->buffer == NULL) + { + goto fail; + } + + /* print the value */ + if (!print_value(item, buffer)) + { + goto fail; + } + update_offset(buffer); + + /* check if reallocate is available */ + if (hooks->reallocate != NULL) + { + printed = (unsigned char*) hooks->reallocate(buffer->buffer, buffer->offset + 1); + if (printed == NULL) { + goto fail; + } + buffer->buffer = NULL; + } + else /* otherwise copy the JSON over to a new buffer */ + { + printed = (unsigned char*) hooks->allocate(buffer->offset + 1); + if (printed == NULL) + { + goto fail; + } + memcpy(printed, buffer->buffer, cjson_min(buffer->length, buffer->offset + 1)); + printed[buffer->offset] = '\0'; /* just to be sure */ + + /* free the buffer */ + hooks->deallocate(buffer->buffer); + buffer->buffer = NULL; + } + + return printed; + +fail: + if (buffer->buffer != NULL) + { + hooks->deallocate(buffer->buffer); + buffer->buffer = NULL; + } + + if (printed != NULL) + { + hooks->deallocate(printed); + printed = NULL; + } + + return NULL; +} + +/* Render a cJSON item/entity/structure to text. */ +CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item) +{ + return (char*)print(item, true, &global_hooks); +} + +CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item) +{ + return (char*)print(item, false, &global_hooks); +} + +CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt) +{ + printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; + + if (prebuffer < 0) + { + return NULL; + } + + p.buffer = (unsigned char*)global_hooks.allocate((size_t)prebuffer); + if (!p.buffer) + { + return NULL; + } + + p.length = (size_t)prebuffer; + p.offset = 0; + p.noalloc = false; + p.format = fmt; + p.hooks = global_hooks; + + if (!print_value(item, &p)) + { + global_hooks.deallocate(p.buffer); + p.buffer = NULL; + return NULL; + } + + return (char*)p.buffer; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format) +{ + printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; + + if ((length < 0) || (buffer == NULL)) + { + return false; + } + + p.buffer = (unsigned char*)buffer; + p.length = (size_t)length; + p.offset = 0; + p.noalloc = true; + p.format = format; + p.hooks = global_hooks; + + return print_value(item, &p); +} + +/* Parser core - when encountering text, process appropriately. */ +static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer) +{ + if ((input_buffer == NULL) || (input_buffer->content == NULL)) + { + return false; /* no input */ + } + + /* parse the different types of values */ + /* null */ + if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "null", 4) == 0)) + { + item->type = cJSON_NULL; + input_buffer->offset += 4; + return true; + } + /* false */ + if (can_read(input_buffer, 5) && (strncmp((const char*)buffer_at_offset(input_buffer), "false", 5) == 0)) + { + item->type = cJSON_False; + input_buffer->offset += 5; + return true; + } + /* true */ + if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "true", 4) == 0)) + { + item->type = cJSON_True; + item->valueint = 1; + input_buffer->offset += 4; + return true; + } + /* string */ + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '\"')) + { + return parse_string(item, input_buffer); + } + /* number */ + if (can_access_at_index(input_buffer, 0) && ((buffer_at_offset(input_buffer)[0] == '-') || ((buffer_at_offset(input_buffer)[0] >= '0') && (buffer_at_offset(input_buffer)[0] <= '9')))) + { + return parse_number(item, input_buffer); + } + /* array */ + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '[')) + { + return parse_array(item, input_buffer); + } + /* object */ + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '{')) + { + return parse_object(item, input_buffer); + } + + return false; +} + +/* Render a value to text. */ +static cJSON_bool print_value(const cJSON * const item, printbuffer * const output_buffer) +{ + unsigned char *output = NULL; + + if ((item == NULL) || (output_buffer == NULL)) + { + return false; + } + + switch ((item->type) & 0xFF) + { + case cJSON_NULL: + output = ensure(output_buffer, 5); + if (output == NULL) + { + return false; + } + strcpy((char*)output, "null"); + return true; + + case cJSON_False: + output = ensure(output_buffer, 6); + if (output == NULL) + { + return false; + } + strcpy((char*)output, "false"); + return true; + + case cJSON_True: + output = ensure(output_buffer, 5); + if (output == NULL) + { + return false; + } + strcpy((char*)output, "true"); + return true; + + case cJSON_Number: + return print_number(item, output_buffer); + + case cJSON_Raw: + { + size_t raw_length = 0; + if (item->valuestring == NULL) + { + return false; + } + + raw_length = strlen(item->valuestring) + sizeof(""); + output = ensure(output_buffer, raw_length); + if (output == NULL) + { + return false; + } + memcpy(output, item->valuestring, raw_length); + return true; + } + + case cJSON_String: + return print_string(item, output_buffer); + + case cJSON_Array: + return print_array(item, output_buffer); + + case cJSON_Object: + return print_object(item, output_buffer); + + default: + return false; + } +} + +/* Build an array from input text. */ +static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer) +{ + cJSON *head = NULL; /* head of the linked list */ + cJSON *current_item = NULL; + + if (input_buffer->depth >= CJSON_NESTING_LIMIT) + { + return false; /* to deeply nested */ + } + input_buffer->depth++; + + if (buffer_at_offset(input_buffer)[0] != '[') + { + /* not an array */ + goto fail; + } + + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ']')) + { + /* empty array */ + goto success; + } + + /* check if we skipped to the end of the buffer */ + if (cannot_access_at_index(input_buffer, 0)) + { + input_buffer->offset--; + goto fail; + } + + /* step back to character in front of the first element */ + input_buffer->offset--; + /* loop through the comma separated array elements */ + do + { + /* allocate next item */ + cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks)); + if (new_item == NULL) + { + goto fail; /* allocation failure */ + } + + /* attach next item to list */ + if (head == NULL) + { + /* start the linked list */ + current_item = head = new_item; + } + else + { + /* add to the end and advance */ + current_item->next = new_item; + new_item->prev = current_item; + current_item = new_item; + } + + /* parse next value */ + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (!parse_value(current_item, input_buffer)) + { + goto fail; /* failed to parse value */ + } + buffer_skip_whitespace(input_buffer); + } + while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ',')); + + if (cannot_access_at_index(input_buffer, 0) || buffer_at_offset(input_buffer)[0] != ']') + { + goto fail; /* expected end of array */ + } + +success: + input_buffer->depth--; + + if (head != NULL) { + head->prev = current_item; + } + + item->type = cJSON_Array; + item->child = head; + + input_buffer->offset++; + + return true; + +fail: + if (head != NULL) + { + cJSON_Delete(head); + } + + return false; +} + +/* Render an array to text */ +static cJSON_bool print_array(const cJSON * const item, printbuffer * const output_buffer) +{ + unsigned char *output_pointer = NULL; + size_t length = 0; + cJSON *current_element = item->child; + + if (output_buffer == NULL) + { + return false; + } + + /* Compose the output array. */ + /* opening square bracket */ + output_pointer = ensure(output_buffer, 1); + if (output_pointer == NULL) + { + return false; + } + + *output_pointer = '['; + output_buffer->offset++; + output_buffer->depth++; + + while (current_element != NULL) + { + if (!print_value(current_element, output_buffer)) + { + return false; + } + update_offset(output_buffer); + if (current_element->next) + { + length = (size_t) (output_buffer->format ? 2 : 1); + output_pointer = ensure(output_buffer, length + 1); + if (output_pointer == NULL) + { + return false; + } + *output_pointer++ = ','; + if(output_buffer->format) + { + *output_pointer++ = ' '; + } + *output_pointer = '\0'; + output_buffer->offset += length; + } + current_element = current_element->next; + } + + output_pointer = ensure(output_buffer, 2); + if (output_pointer == NULL) + { + return false; + } + *output_pointer++ = ']'; + *output_pointer = '\0'; + output_buffer->depth--; + + return true; +} + +/* Build an object from the text. */ +static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer) +{ + cJSON *head = NULL; /* linked list head */ + cJSON *current_item = NULL; + + if (input_buffer->depth >= CJSON_NESTING_LIMIT) + { + return false; /* to deeply nested */ + } + input_buffer->depth++; + + if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '{')) + { + goto fail; /* not an object */ + } + + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '}')) + { + goto success; /* empty object */ + } + + /* check if we skipped to the end of the buffer */ + if (cannot_access_at_index(input_buffer, 0)) + { + input_buffer->offset--; + goto fail; + } + + /* step back to character in front of the first element */ + input_buffer->offset--; + /* loop through the comma separated array elements */ + do + { + /* allocate next item */ + cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks)); + if (new_item == NULL) + { + goto fail; /* allocation failure */ + } + + /* attach next item to list */ + if (head == NULL) + { + /* start the linked list */ + current_item = head = new_item; + } + else + { + /* add to the end and advance */ + current_item->next = new_item; + new_item->prev = current_item; + current_item = new_item; + } + + if (cannot_access_at_index(input_buffer, 1)) + { + goto fail; /* nothing comes after the comma */ + } + + /* parse the name of the child */ + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (!parse_string(current_item, input_buffer)) + { + goto fail; /* failed to parse name */ + } + buffer_skip_whitespace(input_buffer); + + /* swap valuestring and string, because we parsed the name */ + current_item->string = current_item->valuestring; + current_item->valuestring = NULL; + + if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != ':')) + { + goto fail; /* invalid object */ + } + + /* parse the value */ + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (!parse_value(current_item, input_buffer)) + { + goto fail; /* failed to parse value */ + } + buffer_skip_whitespace(input_buffer); + } + while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ',')); + + if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '}')) + { + goto fail; /* expected end of object */ + } + +success: + input_buffer->depth--; + + if (head != NULL) { + head->prev = current_item; + } + + item->type = cJSON_Object; + item->child = head; + + input_buffer->offset++; + return true; + +fail: + if (head != NULL) + { + cJSON_Delete(head); + } + + return false; +} + +/* Render an object to text. */ +static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer) +{ + unsigned char *output_pointer = NULL; + size_t length = 0; + cJSON *current_item = item->child; + + if (output_buffer == NULL) + { + return false; + } + + /* Compose the output: */ + length = (size_t) (output_buffer->format ? 2 : 1); /* fmt: {\n */ + output_pointer = ensure(output_buffer, length + 1); + if (output_pointer == NULL) + { + return false; + } + + *output_pointer++ = '{'; + output_buffer->depth++; + if (output_buffer->format) + { + *output_pointer++ = '\n'; + } + output_buffer->offset += length; + + while (current_item) + { + if (output_buffer->format) + { + size_t i; + output_pointer = ensure(output_buffer, output_buffer->depth); + if (output_pointer == NULL) + { + return false; + } + for (i = 0; i < output_buffer->depth; i++) + { + *output_pointer++ = '\t'; + } + output_buffer->offset += output_buffer->depth; + } + + /* print key */ + if (!print_string_ptr((unsigned char*)current_item->string, output_buffer)) + { + return false; + } + update_offset(output_buffer); + + length = (size_t) (output_buffer->format ? 2 : 1); + output_pointer = ensure(output_buffer, length); + if (output_pointer == NULL) + { + return false; + } + *output_pointer++ = ':'; + if (output_buffer->format) + { + *output_pointer++ = '\t'; + } + output_buffer->offset += length; + + /* print value */ + if (!print_value(current_item, output_buffer)) + { + return false; + } + update_offset(output_buffer); + + /* print comma if not last */ + length = ((size_t)(output_buffer->format ? 1 : 0) + (size_t)(current_item->next ? 1 : 0)); + output_pointer = ensure(output_buffer, length + 1); + if (output_pointer == NULL) + { + return false; + } + if (current_item->next) + { + *output_pointer++ = ','; + } + + if (output_buffer->format) + { + *output_pointer++ = '\n'; + } + *output_pointer = '\0'; + output_buffer->offset += length; + + current_item = current_item->next; + } + + output_pointer = ensure(output_buffer, output_buffer->format ? (output_buffer->depth + 1) : 2); + if (output_pointer == NULL) + { + return false; + } + if (output_buffer->format) + { + size_t i; + for (i = 0; i < (output_buffer->depth - 1); i++) + { + *output_pointer++ = '\t'; + } + } + *output_pointer++ = '}'; + *output_pointer = '\0'; + output_buffer->depth--; + + return true; +} + +/* Get Array size/item / object item. */ +CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array) +{ + cJSON *child = NULL; + size_t size = 0; + + if (array == NULL) + { + return 0; + } + + child = array->child; + + while(child != NULL) + { + size++; + child = child->next; + } + + /* FIXME: Can overflow here. Cannot be fixed without breaking the API */ + + return (int)size; +} + +static cJSON* get_array_item(const cJSON *array, size_t index) +{ + cJSON *current_child = NULL; + + if (array == NULL) + { + return NULL; + } + + current_child = array->child; + while ((current_child != NULL) && (index > 0)) + { + index--; + current_child = current_child->next; + } + + return current_child; +} + +CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index) +{ + if (index < 0) + { + return NULL; + } + + return get_array_item(array, (size_t)index); +} + +static cJSON *get_object_item(const cJSON * const object, const char * const name, const cJSON_bool case_sensitive) +{ + cJSON *current_element = NULL; + + if ((object == NULL) || (name == NULL)) + { + return NULL; + } + + current_element = object->child; + if (case_sensitive) + { + while ((current_element != NULL) && (current_element->string != NULL) && (strcmp(name, current_element->string) != 0)) + { + current_element = current_element->next; + } + } + else + { + while ((current_element != NULL) && (case_insensitive_strcmp((const unsigned char*)name, (const unsigned char*)(current_element->string)) != 0)) + { + current_element = current_element->next; + } + } + + if ((current_element == NULL) || (current_element->string == NULL)) { + return NULL; + } + + return current_element; +} + +CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string) +{ + return get_object_item(object, string, false); +} + +CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string) +{ + return get_object_item(object, string, true); +} + +CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string) +{ + return cJSON_GetObjectItem(object, string) ? 1 : 0; +} + +/* Utility for array list handling. */ +static void suffix_object(cJSON *prev, cJSON *item) +{ + prev->next = item; + item->prev = prev; +} + +/* Utility for handling references. */ +static cJSON *create_reference(const cJSON *item, const internal_hooks * const hooks) +{ + cJSON *reference = NULL; + if (item == NULL) + { + return NULL; + } + + reference = cJSON_New_Item(hooks); + if (reference == NULL) + { + return NULL; + } + + memcpy(reference, item, sizeof(cJSON)); + reference->string = NULL; + reference->type |= cJSON_IsReference; + reference->next = reference->prev = NULL; + return reference; +} + +static cJSON_bool add_item_to_array(cJSON *array, cJSON *item) +{ + cJSON *child = NULL; + + if ((item == NULL) || (array == NULL) || (array == item)) + { + return false; + } + + child = array->child; + /* + * To find the last item in array quickly, we use prev in array + */ + if (child == NULL) + { + /* list is empty, start new one */ + array->child = item; + item->prev = item; + item->next = NULL; + } + else + { + /* append to the end */ + if (child->prev) + { + suffix_object(child->prev, item); + array->child->prev = item; + } + } + + return true; +} + +/* Add item to array/object. */ +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item) +{ + return add_item_to_array(array, item); +} + +#if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) + #pragma GCC diagnostic push +#endif +#ifdef __GNUC__ +#pragma GCC diagnostic ignored "-Wcast-qual" +#endif +/* helper function to cast away const */ +static void* cast_away_const(const void* string) +{ + return (void*)string; +} +#if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) + #pragma GCC diagnostic pop +#endif + + +static cJSON_bool add_item_to_object(cJSON * const object, const char * const string, cJSON * const item, const internal_hooks * const hooks, const cJSON_bool constant_key) +{ + char *new_key = NULL; + int new_type = cJSON_Invalid; + + if ((object == NULL) || (string == NULL) || (item == NULL) || (object == item)) + { + return false; + } + + if (constant_key) + { + new_key = (char*)cast_away_const(string); + new_type = item->type | cJSON_StringIsConst; + } + else + { + new_key = (char*)cJSON_strdup((const unsigned char*)string, hooks); + if (new_key == NULL) + { + return false; + } + + new_type = item->type & ~cJSON_StringIsConst; + } + + if (!(item->type & cJSON_StringIsConst) && (item->string != NULL)) + { + hooks->deallocate(item->string); + } + + item->string = new_key; + item->type = new_type; + + return add_item_to_array(object, item); +} + +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item) +{ + return add_item_to_object(object, string, item, &global_hooks, false); +} + +/* Add an item to an object with constant string as key */ +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item) +{ + return add_item_to_object(object, string, item, &global_hooks, true); +} + +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item) +{ + if (array == NULL) + { + return false; + } + + return add_item_to_array(array, create_reference(item, &global_hooks)); +} + +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item) +{ + if ((object == NULL) || (string == NULL)) + { + return false; + } + + return add_item_to_object(object, string, create_reference(item, &global_hooks), &global_hooks, false); +} + +CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name) +{ + cJSON *null = cJSON_CreateNull(); + if (add_item_to_object(object, name, null, &global_hooks, false)) + { + return null; + } + + cJSON_Delete(null); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name) +{ + cJSON *true_item = cJSON_CreateTrue(); + if (add_item_to_object(object, name, true_item, &global_hooks, false)) + { + return true_item; + } + + cJSON_Delete(true_item); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name) +{ + cJSON *false_item = cJSON_CreateFalse(); + if (add_item_to_object(object, name, false_item, &global_hooks, false)) + { + return false_item; + } + + cJSON_Delete(false_item); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean) +{ + cJSON *bool_item = cJSON_CreateBool(boolean); + if (add_item_to_object(object, name, bool_item, &global_hooks, false)) + { + return bool_item; + } + + cJSON_Delete(bool_item); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number) +{ + cJSON *number_item = cJSON_CreateNumber(number); + if (add_item_to_object(object, name, number_item, &global_hooks, false)) + { + return number_item; + } + + cJSON_Delete(number_item); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string) +{ + cJSON *string_item = cJSON_CreateString(string); + if (add_item_to_object(object, name, string_item, &global_hooks, false)) + { + return string_item; + } + + cJSON_Delete(string_item); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw) +{ + cJSON *raw_item = cJSON_CreateRaw(raw); + if (add_item_to_object(object, name, raw_item, &global_hooks, false)) + { + return raw_item; + } + + cJSON_Delete(raw_item); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name) +{ + cJSON *object_item = cJSON_CreateObject(); + if (add_item_to_object(object, name, object_item, &global_hooks, false)) + { + return object_item; + } + + cJSON_Delete(object_item); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name) +{ + cJSON *array = cJSON_CreateArray(); + if (add_item_to_object(object, name, array, &global_hooks, false)) + { + return array; + } + + cJSON_Delete(array); + return NULL; +} + +CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item) +{ + if ((parent == NULL) || (item == NULL)) + { + return NULL; + } + + if (item != parent->child) + { + /* not the first element */ + item->prev->next = item->next; + } + if (item->next != NULL) + { + /* not the last element */ + item->next->prev = item->prev; + } + + if (item == parent->child) + { + /* first element */ + parent->child = item->next; + } + else if (item->next == NULL) + { + /* last element */ + parent->child->prev = item->prev; + } + + /* make sure the detached item doesn't point anywhere anymore */ + item->prev = NULL; + item->next = NULL; + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which) +{ + if (which < 0) + { + return NULL; + } + + return cJSON_DetachItemViaPointer(array, get_array_item(array, (size_t)which)); +} + +CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which) +{ + cJSON_Delete(cJSON_DetachItemFromArray(array, which)); +} + +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string) +{ + cJSON *to_detach = cJSON_GetObjectItem(object, string); + + return cJSON_DetachItemViaPointer(object, to_detach); +} + +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string) +{ + cJSON *to_detach = cJSON_GetObjectItemCaseSensitive(object, string); + + return cJSON_DetachItemViaPointer(object, to_detach); +} + +CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string) +{ + cJSON_Delete(cJSON_DetachItemFromObject(object, string)); +} + +CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string) +{ + cJSON_Delete(cJSON_DetachItemFromObjectCaseSensitive(object, string)); +} + +/* Replace array/object items with new ones. */ +CJSON_PUBLIC(cJSON_bool) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem) +{ + cJSON *after_inserted = NULL; + + if (which < 0 || newitem == NULL) + { + return false; + } + + after_inserted = get_array_item(array, (size_t)which); + if (after_inserted == NULL) + { + return add_item_to_array(array, newitem); + } + + if (after_inserted != array->child && after_inserted->prev == NULL) { + /* return false if after_inserted is a corrupted array item */ + return false; + } + + newitem->next = after_inserted; + newitem->prev = after_inserted->prev; + after_inserted->prev = newitem; + if (after_inserted == array->child) + { + array->child = newitem; + } + else + { + newitem->prev->next = newitem; + } + return true; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement) +{ + if ((parent == NULL) || (parent->child == NULL) || (replacement == NULL) || (item == NULL)) + { + return false; + } + + if (replacement == item) + { + return true; + } + + replacement->next = item->next; + replacement->prev = item->prev; + + if (replacement->next != NULL) + { + replacement->next->prev = replacement; + } + if (parent->child == item) + { + if (parent->child->prev == parent->child) + { + replacement->prev = replacement; + } + parent->child = replacement; + } + else + { /* + * To find the last item in array quickly, we use prev in array. + * We can't modify the last item's next pointer where this item was the parent's child + */ + if (replacement->prev != NULL) + { + replacement->prev->next = replacement; + } + if (replacement->next == NULL) + { + parent->child->prev = replacement; + } + } + + item->next = NULL; + item->prev = NULL; + cJSON_Delete(item); + + return true; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem) +{ + if (which < 0) + { + return false; + } + + return cJSON_ReplaceItemViaPointer(array, get_array_item(array, (size_t)which), newitem); +} + +static cJSON_bool replace_item_in_object(cJSON *object, const char *string, cJSON *replacement, cJSON_bool case_sensitive) +{ + if ((replacement == NULL) || (string == NULL)) + { + return false; + } + + /* replace the name in the replacement */ + if (!(replacement->type & cJSON_StringIsConst) && (replacement->string != NULL)) + { + cJSON_free(replacement->string); + } + replacement->string = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks); + if (replacement->string == NULL) + { + return false; + } + + replacement->type &= ~cJSON_StringIsConst; + + return cJSON_ReplaceItemViaPointer(object, get_object_item(object, string, case_sensitive), replacement); +} + +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObject(cJSON *object, const char *string, cJSON *newitem) +{ + return replace_item_in_object(object, string, newitem, false); +} + +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object, const char *string, cJSON *newitem) +{ + return replace_item_in_object(object, string, newitem, true); +} + +/* Create basic types: */ +CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_NULL; + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_True; + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_False; + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = boolean ? cJSON_True : cJSON_False; + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_Number; + item->valuedouble = num; + + /* use saturation in case of overflow */ + if (num >= INT_MAX) + { + item->valueint = INT_MAX; + } + else if (num <= (double)INT_MIN) + { + item->valueint = INT_MIN; + } + else + { + item->valueint = (int)num; + } + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_String; + item->valuestring = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks); + if(!item->valuestring) + { + cJSON_Delete(item); + return NULL; + } + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if (item != NULL) + { + item->type = cJSON_String | cJSON_IsReference; + item->valuestring = (char*)cast_away_const(string); + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if (item != NULL) { + item->type = cJSON_Object | cJSON_IsReference; + item->child = (cJSON*)cast_away_const(child); + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child) { + cJSON *item = cJSON_New_Item(&global_hooks); + if (item != NULL) { + item->type = cJSON_Array | cJSON_IsReference; + item->child = (cJSON*)cast_away_const(child); + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_Raw; + item->valuestring = (char*)cJSON_strdup((const unsigned char*)raw, &global_hooks); + if(!item->valuestring) + { + cJSON_Delete(item); + return NULL; + } + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type=cJSON_Array; + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if (item) + { + item->type = cJSON_Object; + } + + return item; +} + +/* Create Arrays: */ +CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count) +{ + size_t i = 0; + cJSON *n = NULL; + cJSON *p = NULL; + cJSON *a = NULL; + + if ((count < 0) || (numbers == NULL)) + { + return NULL; + } + + a = cJSON_CreateArray(); + + for(i = 0; a && (i < (size_t)count); i++) + { + n = cJSON_CreateNumber(numbers[i]); + if (!n) + { + cJSON_Delete(a); + return NULL; + } + if(!i) + { + a->child = n; + } + else + { + suffix_object(p, n); + } + p = n; + } + + if (a && a->child) { + a->child->prev = n; + } + + return a; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count) +{ + size_t i = 0; + cJSON *n = NULL; + cJSON *p = NULL; + cJSON *a = NULL; + + if ((count < 0) || (numbers == NULL)) + { + return NULL; + } + + a = cJSON_CreateArray(); + + for(i = 0; a && (i < (size_t)count); i++) + { + n = cJSON_CreateNumber((double)numbers[i]); + if(!n) + { + cJSON_Delete(a); + return NULL; + } + if(!i) + { + a->child = n; + } + else + { + suffix_object(p, n); + } + p = n; + } + + if (a && a->child) { + a->child->prev = n; + } + + return a; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count) +{ + size_t i = 0; + cJSON *n = NULL; + cJSON *p = NULL; + cJSON *a = NULL; + + if ((count < 0) || (numbers == NULL)) + { + return NULL; + } + + a = cJSON_CreateArray(); + + for(i = 0; a && (i < (size_t)count); i++) + { + n = cJSON_CreateNumber(numbers[i]); + if(!n) + { + cJSON_Delete(a); + return NULL; + } + if(!i) + { + a->child = n; + } + else + { + suffix_object(p, n); + } + p = n; + } + + if (a && a->child) { + a->child->prev = n; + } + + return a; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int count) +{ + size_t i = 0; + cJSON *n = NULL; + cJSON *p = NULL; + cJSON *a = NULL; + + if ((count < 0) || (strings == NULL)) + { + return NULL; + } + + a = cJSON_CreateArray(); + + for (i = 0; a && (i < (size_t)count); i++) + { + n = cJSON_CreateString(strings[i]); + if(!n) + { + cJSON_Delete(a); + return NULL; + } + if(!i) + { + a->child = n; + } + else + { + suffix_object(p,n); + } + p = n; + } + + if (a && a->child) { + a->child->prev = n; + } + + return a; +} + +/* Duplication */ +CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse) +{ + cJSON *newitem = NULL; + cJSON *child = NULL; + cJSON *next = NULL; + cJSON *newchild = NULL; + + /* Bail on bad ptr */ + if (!item) + { + goto fail; + } + /* Create new item */ + newitem = cJSON_New_Item(&global_hooks); + if (!newitem) + { + goto fail; + } + /* Copy over all vars */ + newitem->type = item->type & (~cJSON_IsReference); + newitem->valueint = item->valueint; + newitem->valuedouble = item->valuedouble; + if (item->valuestring) + { + newitem->valuestring = (char*)cJSON_strdup((unsigned char*)item->valuestring, &global_hooks); + if (!newitem->valuestring) + { + goto fail; + } + } + if (item->string) + { + newitem->string = (item->type&cJSON_StringIsConst) ? item->string : (char*)cJSON_strdup((unsigned char*)item->string, &global_hooks); + if (!newitem->string) + { + goto fail; + } + } + /* If non-recursive, then we're done! */ + if (!recurse) + { + return newitem; + } + /* Walk the ->next chain for the child. */ + child = item->child; + while (child != NULL) + { + newchild = cJSON_Duplicate(child, true); /* Duplicate (with recurse) each item in the ->next chain */ + if (!newchild) + { + goto fail; + } + if (next != NULL) + { + /* If newitem->child already set, then crosswire ->prev and ->next and move on */ + next->next = newchild; + newchild->prev = next; + next = newchild; + } + else + { + /* Set newitem->child and move to it */ + newitem->child = newchild; + next = newchild; + } + child = child->next; + } + if (newitem && newitem->child) + { + newitem->child->prev = newchild; + } + + return newitem; + +fail: + if (newitem != NULL) + { + cJSON_Delete(newitem); + } + + return NULL; +} + +static void skip_oneline_comment(char **input) +{ + *input += static_strlen("//"); + + for (; (*input)[0] != '\0'; ++(*input)) + { + if ((*input)[0] == '\n') { + *input += static_strlen("\n"); + return; + } + } +} + +static void skip_multiline_comment(char **input) +{ + *input += static_strlen("/*"); + + for (; (*input)[0] != '\0'; ++(*input)) + { + if (((*input)[0] == '*') && ((*input)[1] == '/')) + { + *input += static_strlen("*/"); + return; + } + } +} + +static void minify_string(char **input, char **output) { + (*output)[0] = (*input)[0]; + *input += static_strlen("\""); + *output += static_strlen("\""); + + + for (; (*input)[0] != '\0'; (void)++(*input), ++(*output)) { + (*output)[0] = (*input)[0]; + + if ((*input)[0] == '\"') { + (*output)[0] = '\"'; + *input += static_strlen("\""); + *output += static_strlen("\""); + return; + } else if (((*input)[0] == '\\') && ((*input)[1] == '\"')) { + (*output)[1] = (*input)[1]; + *input += static_strlen("\""); + *output += static_strlen("\""); + } + } +} + +CJSON_PUBLIC(void) cJSON_Minify(char *json) +{ + char *into = json; + + if (json == NULL) + { + return; + } + + while (json[0] != '\0') + { + switch (json[0]) + { + case ' ': + case '\t': + case '\r': + case '\n': + json++; + break; + + case '/': + if (json[1] == '/') + { + skip_oneline_comment(&json); + } + else if (json[1] == '*') + { + skip_multiline_comment(&json); + } else { + json++; + } + break; + + case '\"': + minify_string(&json, (char**)&into); + break; + + default: + into[0] = json[0]; + json++; + into++; + } + } + + /* and null-terminate. */ + *into = '\0'; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_Invalid; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_False; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xff) == cJSON_True; +} + + +CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & (cJSON_True | cJSON_False)) != 0; +} +CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_NULL; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_Number; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_String; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_Array; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_Object; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_Raw; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive) +{ + if ((a == NULL) || (b == NULL) || ((a->type & 0xFF) != (b->type & 0xFF))) + { + return false; + } + + /* check if type is valid */ + switch (a->type & 0xFF) + { + case cJSON_False: + case cJSON_True: + case cJSON_NULL: + case cJSON_Number: + case cJSON_String: + case cJSON_Raw: + case cJSON_Array: + case cJSON_Object: + break; + + default: + return false; + } + + /* identical objects are equal */ + if (a == b) + { + return true; + } + + switch (a->type & 0xFF) + { + /* in these cases and equal type is enough */ + case cJSON_False: + case cJSON_True: + case cJSON_NULL: + return true; + + case cJSON_Number: + if (compare_double(a->valuedouble, b->valuedouble)) + { + return true; + } + return false; + + case cJSON_String: + case cJSON_Raw: + if ((a->valuestring == NULL) || (b->valuestring == NULL)) + { + return false; + } + if (strcmp(a->valuestring, b->valuestring) == 0) + { + return true; + } + + return false; + + case cJSON_Array: + { + cJSON *a_element = a->child; + cJSON *b_element = b->child; + + for (; (a_element != NULL) && (b_element != NULL);) + { + if (!cJSON_Compare(a_element, b_element, case_sensitive)) + { + return false; + } + + a_element = a_element->next; + b_element = b_element->next; + } + + /* one of the arrays is longer than the other */ + if (a_element != b_element) { + return false; + } + + return true; + } + + case cJSON_Object: + { + cJSON *a_element = NULL; + cJSON *b_element = NULL; + cJSON_ArrayForEach(a_element, a) + { + /* TODO This has O(n^2) runtime, which is horrible! */ + b_element = get_object_item(b, a_element->string, case_sensitive); + if (b_element == NULL) + { + return false; + } + + if (!cJSON_Compare(a_element, b_element, case_sensitive)) + { + return false; + } + } + + /* doing this twice, once on a and b to prevent true comparison if a subset of b + * TODO: Do this the proper way, this is just a fix for now */ + cJSON_ArrayForEach(b_element, b) + { + a_element = get_object_item(a, b_element->string, case_sensitive); + if (a_element == NULL) + { + return false; + } + + if (!cJSON_Compare(b_element, a_element, case_sensitive)) + { + return false; + } + } + + return true; + } + + default: + return false; + } +} + +CJSON_PUBLIC(void *) cJSON_malloc(size_t size) +{ + return global_hooks.allocate(size); +} + +CJSON_PUBLIC(void) cJSON_free(void *object) +{ + global_hooks.deallocate(object); + object = NULL; +} \ No newline at end of file diff --git a/code/recordsystem/cJSON.h b/code/recordsystem/cJSON.h new file mode 100644 index 0000000000..a37d69e1e7 --- /dev/null +++ b/code/recordsystem/cJSON.h @@ -0,0 +1,300 @@ +/* + Copyright (c) 2009-2017 Dave Gamble and cJSON contributors + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#ifndef cJSON__h +#define cJSON__h + +#ifdef __cplusplus +extern "C" +{ +#endif + +#if !defined(__WINDOWS__) && (defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32)) +#define __WINDOWS__ +#endif + +#ifdef __WINDOWS__ + +/* When compiling for windows, we specify a specific calling convention to avoid issues where we are being called from a project with a different default calling convention. For windows you have 3 define options: + +CJSON_HIDE_SYMBOLS - Define this in the case where you don't want to ever dllexport symbols +CJSON_EXPORT_SYMBOLS - Define this on library build when you want to dllexport symbols (default) +CJSON_IMPORT_SYMBOLS - Define this if you want to dllimport symbol + +For *nix builds that support visibility attribute, you can define similar behavior by + +setting default visibility to hidden by adding +-fvisibility=hidden (for gcc) +or +-xldscope=hidden (for sun cc) +to CFLAGS + +then using the CJSON_API_VISIBILITY flag to "export" the same symbols the way CJSON_EXPORT_SYMBOLS does + +*/ + +#define CJSON_CDECL __cdecl +#define CJSON_STDCALL __stdcall + +/* export symbols by default, this is necessary for copy pasting the C and header file */ +#if !defined(CJSON_HIDE_SYMBOLS) && !defined(CJSON_IMPORT_SYMBOLS) && !defined(CJSON_EXPORT_SYMBOLS) +#define CJSON_EXPORT_SYMBOLS +#endif + +#if defined(CJSON_HIDE_SYMBOLS) +#define CJSON_PUBLIC(type) type CJSON_STDCALL +#elif defined(CJSON_EXPORT_SYMBOLS) +#define CJSON_PUBLIC(type) __declspec(dllexport) type CJSON_STDCALL +#elif defined(CJSON_IMPORT_SYMBOLS) +#define CJSON_PUBLIC(type) __declspec(dllimport) type CJSON_STDCALL +#endif +#else /* !__WINDOWS__ */ +#define CJSON_CDECL +#define CJSON_STDCALL + +#if (defined(__GNUC__) || defined(__SUNPRO_CC) || defined (__SUNPRO_C)) && defined(CJSON_API_VISIBILITY) +#define CJSON_PUBLIC(type) __attribute__((visibility("default"))) type +#else +#define CJSON_PUBLIC(type) type +#endif +#endif + +/* project version */ +#define CJSON_VERSION_MAJOR 1 +#define CJSON_VERSION_MINOR 7 +#define CJSON_VERSION_PATCH 18 + +#include + +/* cJSON Types: */ +#define cJSON_Invalid (0) +#define cJSON_False (1 << 0) +#define cJSON_True (1 << 1) +#define cJSON_NULL (1 << 2) +#define cJSON_Number (1 << 3) +#define cJSON_String (1 << 4) +#define cJSON_Array (1 << 5) +#define cJSON_Object (1 << 6) +#define cJSON_Raw (1 << 7) /* raw json */ + +#define cJSON_IsReference 256 +#define cJSON_StringIsConst 512 + +/* The cJSON structure: */ +typedef struct cJSON +{ + /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */ + struct cJSON *next; + struct cJSON *prev; + /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */ + struct cJSON *child; + + /* The type of the item, as above. */ + int type; + + /* The item's string, if type==cJSON_String and type == cJSON_Raw */ + char *valuestring; + /* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */ + int valueint; + /* The item's number, if type==cJSON_Number */ + double valuedouble; + + /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */ + char *string; +} cJSON; + +typedef struct cJSON_Hooks +{ + /* malloc/free are CDECL on Windows regardless of the default calling convention of the compiler, so ensure the hooks allow passing those functions directly. */ + void *(CJSON_CDECL *malloc_fn)(size_t sz); + void (CJSON_CDECL *free_fn)(void *ptr); +} cJSON_Hooks; + +typedef int cJSON_bool; + +/* Limits how deeply nested arrays/objects can be before cJSON rejects to parse them. + * This is to prevent stack overflows. */ +#ifndef CJSON_NESTING_LIMIT +#define CJSON_NESTING_LIMIT 1000 +#endif + +/* returns the version of cJSON as a string */ +CJSON_PUBLIC(const char*) cJSON_Version(void); + +/* Supply malloc, realloc and free functions to cJSON */ +CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks); + +/* Memory Management: the caller is always responsible to free the results from all variants of cJSON_Parse (with cJSON_Delete) and cJSON_Print (with stdlib free, cJSON_Hooks.free_fn, or cJSON_free as appropriate). The exception is cJSON_PrintPreallocated, where the caller has full responsibility of the buffer. */ +/* Supply a block of JSON, and this returns a cJSON object you can interrogate. */ +CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value); +CJSON_PUBLIC(cJSON *) cJSON_ParseWithLength(const char *value, size_t buffer_length); +/* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */ +/* If you supply a ptr in return_parse_end and parsing fails, then return_parse_end will contain a pointer to the error so will match cJSON_GetErrorPtr(). */ +CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated); +CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, cJSON_bool require_null_terminated); + +/* Render a cJSON entity to text for transfer/storage. */ +CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item); +/* Render a cJSON entity to text for transfer/storage without any formatting. */ +CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item); +/* Render a cJSON entity to text using a buffered strategy. prebuffer is a guess at the final size. guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */ +CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt); +/* Render a cJSON entity to text using a buffer already allocated in memory with given length. Returns 1 on success and 0 on failure. */ +/* NOTE: cJSON is not always 100% accurate in estimating how much memory it will use, so to be safe allocate 5 bytes more than you actually need */ +CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format); +/* Delete a cJSON entity and all subentities. */ +CJSON_PUBLIC(void) cJSON_Delete(cJSON *item); + +/* Returns the number of items in an array (or object). */ +CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array); +/* Retrieve item number "index" from array "array". Returns NULL if unsuccessful. */ +CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index); +/* Get item "string" from object. Case insensitive. */ +CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string); +CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string); +CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string); +/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */ +CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void); + +/* Check item type and return its value */ +CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item); +CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const item); + +/* These functions check the type of an item */ +CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item); + +/* These calls create a cJSON item of the appropriate type. */ +CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void); +CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void); +CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void); +CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean); +CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num); +CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string); +/* raw json */ +CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw); +CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void); +CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void); + +/* Create a string where valuestring references a string so + * it will not be freed by cJSON_Delete */ +CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string); +/* Create an object/array that only references it's elements so + * they will not be freed by cJSON_Delete */ +CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child); +CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child); + +/* These utilities create an Array of count items. + * The parameter count cannot be greater than the number of elements in the number array, otherwise array access will be out of bounds.*/ +CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count); +CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count); +CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count); +CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int count); + +/* Append item to the specified array/object. */ +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item); +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item); +/* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object. + * WARNING: When this function was used, make sure to always check that (item->type & cJSON_StringIsConst) is zero before + * writing to `item->string` */ +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item); +/* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */ +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item); +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item); + +/* Remove/Detach items from Arrays/Objects. */ +CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item); +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which); +CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which); +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string); +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string); +CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string); +CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string); + +/* Update array items. */ +CJSON_PUBLIC(cJSON_bool) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem); /* Shifts pre-existing items to the right. */ +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement); +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem); +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem); +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object,const char *string,cJSON *newitem); + +/* Duplicate a cJSON item */ +CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse); +/* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will + * need to be released. With recurse!=0, it will duplicate any children connected to the item. + * The item->next and ->prev pointers are always zero on return from Duplicate. */ +/* Recursively compare two cJSON items for equality. If either a or b is NULL or invalid, they will be considered unequal. + * case_sensitive determines if object keys are treated case sensitive (1) or case insensitive (0) */ +CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive); + +/* Minify a strings, remove blank characters(such as ' ', '\t', '\r', '\n') from strings. + * The input pointer json cannot point to a read-only address area, such as a string constant, + * but should point to a readable and writable address area. */ +CJSON_PUBLIC(void) cJSON_Minify(char *json); + +/* Helper functions for creating and adding items to an object at the same time. + * They return the added item or NULL on failure. */ +CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name); +CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name); +CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name); +CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean); +CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number); +CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string); +CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw); +CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name); +CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name); + +/* When assigning an integer value, it needs to be propagated to valuedouble too. */ +#define cJSON_SetIntValue(object, number) ((object) ? (object)->valueint = (object)->valuedouble = (number) : (number)) +/* helper for the cJSON_SetNumberValue macro */ +CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number); +#define cJSON_SetNumberValue(object, number) ((object != NULL) ? cJSON_SetNumberHelper(object, (double)number) : (number)) +/* Change the valuestring of a cJSON_String object, only takes effect when type of object is cJSON_String */ +CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring); + +/* If the object is not a boolean type this does nothing and returns cJSON_Invalid else it returns the new type*/ +#define cJSON_SetBoolValue(object, boolValue) ( \ + (object != NULL && ((object)->type & (cJSON_False|cJSON_True))) ? \ + (object)->type=((object)->type &(~(cJSON_False|cJSON_True)))|((boolValue)?cJSON_True:cJSON_False) : \ + cJSON_Invalid\ +) + +/* Macro for iterating over an array or object */ +#define cJSON_ArrayForEach(element, array) for(element = (array != NULL) ? (array)->child : NULL; element != NULL; element = element->next) + +/* malloc/free objects using the malloc/free functions that have been set with cJSON_InitHooks */ +CJSON_PUBLIC(void *) cJSON_malloc(size_t size); +CJSON_PUBLIC(void) cJSON_free(void *object); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/code/recordsystem/recordsystem.h b/code/recordsystem/recordsystem.h index eb3e1cfa36..6daaf9aea9 100644 --- a/code/recordsystem/recordsystem.h +++ b/code/recordsystem/recordsystem.h @@ -30,14 +30,8 @@ Create a new thread of execution with a string argument */ void Sys_CreateThread(void (*function)(const char *), const char *arg); -/* -=============== -RS_CreateRecord -Creates a record entry from a ClientTimerStop event -=============== -*/ -void RS_CreateRecord(const char *s); +void RS_SendTime(const char *cmdString); /* =============== diff --git a/code/recordsystem/rs_commands.c b/code/recordsystem/rs_commands.c index 09d6cd4ca4..3284636c1a 100644 --- a/code/recordsystem/rs_commands.c +++ b/code/recordsystem/rs_commands.c @@ -1,5 +1,6 @@ #include "recordsystem.h" #include "../server/server.h" +#include "cJSON.h" static void RS_Top(int clientNum, const char *plyrName, const char *str) { char *response; @@ -53,7 +54,8 @@ static void RS_Recent(int clientNum, const char *plyrName, const char *str) { } // Build the URL - Com_sprintf(url, sizeof(url), "http://localhost:8000/api/commands/recent?saystr=%s", encoded_str); + + Com_sprintf(url, sizeof(url), "http://localhost:8000/api/commands/recent?client_num=%d&cmd_string=%s", clientNum, encoded_str); free(encoded_str); // Free encoded string when done // Make the HTTP request @@ -69,15 +71,36 @@ static void RS_Recent(int clientNum, const char *plyrName, const char *str) { static void RS_Login(int clientNum, const char *plyrName, const char *str) { char *response; - char payload[512]; + char *jsonString; + cJSON *json; + + // Create a JSON object + json = cJSON_CreateObject(); + if (!json) { + RS_GameSendServerCommand(clientNum, "print \"^1Error creating JSON object\n\""); + return; + } + + // Add client number and command string to the JSON object + cJSON_AddNumberToObject(json, "clientNum", clientNum); + cJSON_AddStringToObject(json, "cmdString", str); + cJSON_AddStringToObject(json, "plyrName", plyrName); - // Create JSON payload with the entire command string - Com_sprintf(payload, sizeof(payload), - "{\"saystr\":\"%s\"}", str); + // Convert JSON object to string + jsonString = cJSON_Print(json); + cJSON_Delete(json); // Free the JSON object + + if (!jsonString) { + RS_GameSendServerCommand(clientNum, "print \"^1Error serializing JSON\n\""); + return; + } // Make the HTTP request response = RS_HttpPost("http://localhost:8000/api/commands/login", - "application/json", payload); + "application/json", jsonString); + + // Free the JSON string + free(jsonString); if (response) { RS_PrintAPIResponse(response); @@ -89,15 +112,36 @@ static void RS_Login(int clientNum, const char *plyrName, const char *str) { static void RS_Logout(int clientNum, const char *plyrName, const char *str) { char *response; - char payload[512]; + char *jsonString; + cJSON *json; - // Create JSON payload with the entire command string - Com_sprintf(payload, sizeof(payload), - "{\"saystr\":\"%s\"}", str); + // Create a JSON object + json = cJSON_CreateObject(); + if (!json) { + RS_GameSendServerCommand(clientNum, "print \"^1Error creating JSON object\n\""); + return; + } + + // Add client number and command string to the JSON object + cJSON_AddNumberToObject(json, "clientNum", clientNum); + cJSON_AddStringToObject(json, "cmdString", str); + cJSON_AddStringToObject(json, "plyrName", plyrName); + + // Convert JSON object to string + jsonString = cJSON_Print(json); + cJSON_Delete(json); // Free the JSON object + + if (!jsonString) { + RS_GameSendServerCommand(clientNum, "print \"^1Error serializing JSON\n\""); + return; + } // Make the HTTP request response = RS_HttpPost("http://localhost:8000/api/commands/logout", - "application/json", payload); + "application/json", jsonString); + + // Free the JSON string + free(jsonString); if (response) { RS_PrintAPIResponse(response); @@ -106,6 +150,7 @@ static void RS_Logout(int clientNum, const char *plyrName, const char *str) { RS_GameSendServerCommand(clientNum, "print \"^1Failed to connect to server\n\""); } } + typedef struct { const char *pattern; void (*handler)(int clientNum, const char *plyrName, const char *str); @@ -122,7 +167,7 @@ qboolean RS_CommandGateway(int clientNum, const char *plyrName, const char *s) { // Check each command pattern int numModules = sizeof(modules) / sizeof(modules[0]); for (int i = 0; i < numModules; i++) { - if (startsWith(s, modules[i].pattern)) { + if (startsWith(s, va("%s ",modules[i].pattern)) || Q_stricmp(s, modules[i].pattern) == 0) { // Call the appropriate handler function modules[i].handler(clientNum, plyrName, s); return qtrue; diff --git a/code/recordsystem/rs_common.c b/code/recordsystem/rs_common.c index 5717cbbbb4..ce2dc1fe08 100644 --- a/code/recordsystem/rs_common.c +++ b/code/recordsystem/rs_common.c @@ -1,6 +1,8 @@ #include "recordsystem.h" #include "../server/server.h" #include +#include "cJSON.h" + qboolean startsWith(const char *string, const char *prefix) { if (!string || !prefix) { @@ -299,67 +301,41 @@ char* RS_HttpPost(const char *url, const char *contentType, const char *payload) return response; }; -/* -=============== -RS_PrintAPIResponse -Parses a JSON API response and sends the message to the target client -Expected format: "{"targetClient":-1,"message":"Map 'df_castle' not found"}" -=============== -*/ -/* -=============== -RS_PrintAPIResponse - -Parses a JSON API response and sends the message to the target client -Expected format: {"targetClient":-1,"message":"Map 'df_castle' not found"} -=============== -*/ void RS_PrintAPIResponse(const char *jsonString) { - char message[1024] = {0}; + cJSON *json; + cJSON *targetClientObj; + cJSON *messageObj; int targetClient = -1; - const char *startMsg, *endMsg; - const char *startClient; + const char *message = NULL; - // Sanity check - if (!jsonString || !*jsonString) { - Com_Printf("RS: Empty API response\n"); + // Parse the JSON + json = cJSON_Parse(jsonString); + if (!json) { + Com_Printf("RS: Failed to parse JSON: %s\n", cJSON_GetErrorPtr()); return; } - // Parse targetClient field - look for "targetClient":X - startClient = strstr(jsonString, "\"targetClient\":"); - if (startClient) { - startClient += 15; // Length of "\"targetClient\":" - - // Skip whitespace - while (*startClient && (*startClient == ' ' || *startClient == '\t')) - startClient++; - - // Read the client number - targetClient = atoi(startClient); + // Extract targetClient field + targetClientObj = cJSON_GetObjectItem(json, "targetClient"); + if (targetClientObj && cJSON_IsNumber(targetClientObj)) { + targetClient = targetClientObj->valueint; } - // Parse message field - look for "message":"X" - startMsg = strstr(jsonString, "\"message\":\""); - if (startMsg) { - startMsg += 11; // Length of "\"message\":\"" - - // Find the closing quote - endMsg = strchr(startMsg, '\"'); - if (endMsg) { - // Extract the message text - int msgLen = endMsg - startMsg; - if (msgLen > 0 && msgLen < sizeof(message) - 1) { - Q_strncpyz(message, startMsg, msgLen + 1); - } - } + // Extract message field + messageObj = cJSON_GetObjectItem(json, "message"); + if (messageObj && cJSON_IsString(messageObj)) { + message = messageObj->valuestring; } - // If we successfully parsed a message, send it to the target client - if (message[0] != '\0') { - RS_GameSendServerCommand(targetClient, va("print \"%s\n\"", message)); + // Send the message to the target client + if (message && *message) { + // Send the message directly, preserving any newlines + RS_GameSendServerCommand(targetClient, va("print \"^5(^7defrag^5.^7racing^5)^7 %s\"", message)); } else { - Com_Printf("RS: Failed to parse message from API response: %s\n", jsonString); + Com_Printf("RS: Missing message in API response\n"); } + + // Clean up + cJSON_Delete(json); } \ No newline at end of file diff --git a/code/recordsystem/rs_main.c b/code/recordsystem/rs_main.c index 08ebde8f4d..c568a0d9d5 100644 --- a/code/recordsystem/rs_main.c +++ b/code/recordsystem/rs_main.c @@ -5,6 +5,6 @@ void RS_Gateway(const char *s) { if (Cvar_VariableIntegerValue("sv_cheats") != 0) { return; } - Sys_CreateThread(RS_CreateRecord, s); + Sys_CreateThread(RS_SendTime, s); } } \ No newline at end of file diff --git a/code/recordsystem/rs_records.c b/code/recordsystem/rs_records.c index 0bedf2f972..05c77d0f74 100644 --- a/code/recordsystem/rs_records.c +++ b/code/recordsystem/rs_records.c @@ -1,12 +1,52 @@ #include "recordsystem.h" +#include "recordsystem.h" +#include "cJSON.h" +#include "../server/server.h" + +/* +=============== +RS_SendTime + +Sends a time record to the API server +=============== +*/ +void RS_SendTime(const char *cmdString) { + char *response; + char *jsonString; + cJSON *json; + + + // Create a JSON object for the request + json = cJSON_CreateObject(); + if (!json) { + return; + } + + // Add properties to the JSON object + cJSON_AddStringToObject(json, "cmdString", cmdString); + + // Convert JSON object to string + jsonString = cJSON_Print(json); + cJSON_Delete(json); // Free the JSON object + + // Make the HTTP request + response = RS_HttpPost("http://localhost:8000/api/records", + "application/json", jsonString); + + // Free the JSON string + free(jsonString); + + if (response) { + RS_PrintAPIResponse(response); + free(response); + } else { + RS_GameSendServerCommand(-1, "print \"^1Failed to connect to record server\n\""); + } +} qboolean RS_IsClientTimerStop(const char *s) { - // extra logic here to make sure it's a true timer stop - // potential: compare playerstates between this and last frame: - // check for timer state bit and that it's non-zero + // Extra logic here to make sure it's a true timer stop + // Potential: compare playerstates between this and last frame + // Check for timer state bit and that it's non-zero return startsWith(s, "ClientTimerStop: ") ? qtrue : qfalse; } - -void RS_CreateRecord(const char *s) { - RS_GameSendServerCommand(-1, "print \"^5You have finished\n\""); -} \ No newline at end of file From cd0dadcc727f2a9e14996c078d4bc750edcc205e Mon Sep 17 00:00:00 2001 From: frog Date: Fri, 28 Mar 2025 23:33:08 -0500 Subject: [PATCH 15/46] server demos implementation --- Makefile | 3 +- code/recordsystem/recordsystem.h | 67 ++ code/recordsystem/rs_commands.c | 5 +- code/recordsystem/rs_common.c | 1 - code/recordsystem/rs_records.c | 2 - code/recordsystem/rs_serverdemos.c | 984 +++++++++++++++++++++++++++++ code/server/server.h | 17 +- code/server/sv_client.c | 10 +- code/server/sv_game.c | 8 +- code/server/sv_init.c | 11 +- code/server/sv_snapshot.c | 41 +- 11 files changed, 1123 insertions(+), 26 deletions(-) create mode 100644 code/recordsystem/rs_serverdemos.c diff --git a/Makefile b/Makefile index b83885f0d0..d36509183a 100644 --- a/Makefile +++ b/Makefile @@ -708,7 +708,7 @@ endef define DO_DED_CC $(echo_cmd) "DED_CC $<" -$(Q)$(CC) $(CFLAGS) -DDEDICATED -o $@ -c $< +$(Q)$(CC) $(CFLAGS) -DDEDICATED -DENABLE_RS -o $@ -c $< endef define DO_WINDRES @@ -1311,6 +1311,7 @@ Q3DOBJ = \ $(B)/ded/rs_records.o \ $(B)/ded/rs_common.o \ $(B)/ded/rs_commands.o \ + $(B)/ded/rs_serverdemos.o \ $(B)/ded/common.o \ $(B)/ded/cvar.o \ $(B)/ded/files.o \ diff --git a/code/recordsystem/recordsystem.h b/code/recordsystem/recordsystem.h index 6daaf9aea9..fff2fbcf59 100644 --- a/code/recordsystem/recordsystem.h +++ b/code/recordsystem/recordsystem.h @@ -7,6 +7,7 @@ #include #include "../qcommon/q_shared.h" #include "../qcommon/qcommon.h" +#include "../server/server.h" // String utility functions qboolean startsWith(const char *string, const char *prefix); @@ -105,4 +106,70 @@ char* RS_UrlEncode(const char *str); void RS_PrintAPIResponse(const char *jsonString); +void RS_StartRecord( int clientNum, const char *plyrName, const char *str ); + + +/* +==================== +SV_StopRecording + +stop recording a demo +==================== +*/ +void RS_StopRecord( int clientNum, const char *plyrName, const char *str ); + +/* +==================== +SV_WriteGamestate +==================== +*/ +void RS_WriteGamestate( client_t *client); + +/* +==================== +RS_WriteSnapshotToDemo +==================== +*/ +void RS_WriteSnapshot(client_t *client); +void RS_WriteDemoMessage(client_t *client, msg_t *msg); + +// Message types for demo recording +typedef enum { + DEMO_MSG_GAMESTATE, + DEMO_MSG_SNAPSHOT, + DEMO_MSG_END_RECORDING +} demoMsgType_t; + +// Structure for a demo message in the queue +typedef struct { + demoMsgType_t type; // Type of message + int clientNum; // Client index + int sequence; // Sequence number + int serverTime; // Server time for this message + byte *data; // Message data + int dataSize; // Size of the message data +} demoQueuedMsg_t; + +// Initialize the threaded demo writer system +qboolean RS_InitThreadedDemos(void); + +// Shutdown the threaded demo writer system +void RS_ShutdownThreadedDemos(void); + +// Start recording a demo for a client (threaded version) +void RS_StartThreadedRecord(int clientNum, const char *plyrName, const char *demoName); + +// Stop recording a demo for a client (threaded version) +void RS_StopThreadedRecord(int clientNum, const char *plyrName, const char *str); + +// Queue a gamestate message for writing +void RS_QueueGamestate(client_t *client); + +// Queue a snapshot message for writing +void RS_QueueSnapshot(client_t *client); + +// Get number of pending messages in queue +int RS_GetQueuedMessageCount(void); + + #endif // __RECORDSYSTEM_H__ \ No newline at end of file diff --git a/code/recordsystem/rs_commands.c b/code/recordsystem/rs_commands.c index 3284636c1a..7203f27c57 100644 --- a/code/recordsystem/rs_commands.c +++ b/code/recordsystem/rs_commands.c @@ -1,5 +1,4 @@ #include "recordsystem.h" -#include "../server/server.h" #include "cJSON.h" static void RS_Top(int clientNum, const char *plyrName, const char *str) { @@ -160,7 +159,9 @@ static Module modules[] = { {"top", RS_Top}, {"recent", RS_Recent}, {"login", RS_Login}, - {"logout", RS_Logout} + {"logout", RS_Logout}, + {"rs_record", RS_StartThreadedRecord}, + {"rs_stoprecord", RS_StopThreadedRecord} }; qboolean RS_CommandGateway(int clientNum, const char *plyrName, const char *s) { diff --git a/code/recordsystem/rs_common.c b/code/recordsystem/rs_common.c index ce2dc1fe08..2f50f03ec7 100644 --- a/code/recordsystem/rs_common.c +++ b/code/recordsystem/rs_common.c @@ -1,5 +1,4 @@ #include "recordsystem.h" -#include "../server/server.h" #include #include "cJSON.h" diff --git a/code/recordsystem/rs_records.c b/code/recordsystem/rs_records.c index 05c77d0f74..51934b7052 100644 --- a/code/recordsystem/rs_records.c +++ b/code/recordsystem/rs_records.c @@ -1,7 +1,5 @@ #include "recordsystem.h" -#include "recordsystem.h" #include "cJSON.h" -#include "../server/server.h" /* =============== diff --git a/code/recordsystem/rs_serverdemos.c b/code/recordsystem/rs_serverdemos.c new file mode 100644 index 0000000000..d90af5f9ed --- /dev/null +++ b/code/recordsystem/rs_serverdemos.c @@ -0,0 +1,984 @@ +#include "recordsystem.h" + +// Static storage for delta compression +static clientSnapshot_t saved_snap; +static entityState_t saved_entity_states[MAX_SNAPSHOT_ENTITIES]; +static entityState_t *saved_ents[MAX_SNAPSHOT_ENTITIES]; + +static void RS_EmitPacketEntities( const clientSnapshot_t *from, const clientSnapshot_t *to, msg_t *msg ); +/* +==================== +RS_StartRecord + +Begins recording a demo for a given client +==================== +*/ +void RS_StartRecord(int clientNum, const char *plyrName, const char *str) { + char demoName[MAX_OSPATH]; + client_t *cl; + + cl = &svs.clients[clientNum]; + if (sv.state != SS_GAME) { + Com_Printf("Game must be running.\n"); + return; + } + + if (cl->isRecording) { + Com_Printf("Already recording client %i\n", clientNum); + return; + } + + // Create demo name + Q_strncpyz(demoName, va("demos/[%s][%d].dm_68", sv_mapname->string, clientNum), sizeof(demoName)); + + Com_Printf("recording to %s.\n", demoName); + + // open the demo file + cl->demoFile = FS_FOpenFileWrite(demoName); + if (cl->demoFile == FS_INVALID_HANDLE) { + Com_Printf("ERROR: couldn't open file: %s.\n", demoName); + return; + } + + cl->isRecording = qtrue; + cl->demoWaiting = qtrue; + + // write out the gamestate message + RS_WriteGamestate( cl ); + + // The gamestate will be written when the client is fully active + // This happens in SV_SendClientSnapshot +} + +/* +==================== +RS_StopRecord + +stop recording a demo +==================== +*/ +void RS_StopRecord(int clientNum, const char *plyrName, const char *str) { + client_t *cl; + cl = &svs.clients[clientNum]; + + if (!cl->isRecording) { + Com_Printf("Client %i is not being recorded\n", clientNum); + return; + } + + if (cl->demoFile != FS_INVALID_HANDLE) { + int len; + + // Write proper EOF markers - TWO -1 values + len = -1; + FS_Write(&len, 4, cl->demoFile); + FS_Write(&len, 4, cl->demoFile); + + FS_FCloseFile(cl->demoFile); + cl->demoFile = FS_INVALID_HANDLE; + Com_Printf("Stopped recording client %i\n", clientNum); + } + + cl->isRecording = qfalse; +} + +/* +==================== +RS_WriteGamestate +==================== +*/ +void RS_WriteGamestate(client_t *client) { + int start; + entityState_t nullstate; + const svEntity_t *svEnt; + msg_t msg; + byte msgBuffer[ MAX_MSGLEN_BUF ]; + int len; + // entityState_t *ent; + // entityState_t nullstate; + + // accept usercmds starting from current server time only + // Com_Memset( &client->lastUsercmd, 0x0, sizeof( client->lastUsercmd ) ); + // client->lastUsercmd.serverTime = sv.time - 1; + + MSG_Init( &msg, msgBuffer, MAX_MSGLEN ); + + // NOTE, MRE: all server->client messages now acknowledge + // let the client know which reliable clientCommands we have received + MSG_WriteLong( &msg, client->lastClientCommand ); + + client->demoMessageSequence = 1; + // send any server commands waiting to be sent first. + // we have to do this cause we send the client->reliableSequence + // with a gamestate and it sets the clc.serverCommandSequence at + // the client side + SV_UpdateServerCommandsToClient( client, &msg ); + + // send the gamestate + MSG_WriteByte( &msg, svc_gamestate ); + MSG_WriteLong( &msg, client->reliableSequence ); + + // write the configstrings + for ( start = 0 ; start < MAX_CONFIGSTRINGS ; start++ ) { + if ( *sv.configstrings[ start ] != '\0' ) { + MSG_WriteByte( &msg, svc_configstring ); + MSG_WriteShort( &msg, start ); + if ( start == CS_SYSTEMINFO && sv.pure != sv_pure->integer ) { + // make sure we send latched sv.pure, not forced cvar value + char systemInfo[BIG_INFO_STRING]; + Q_strncpyz( systemInfo, sv.configstrings[ start ], sizeof( systemInfo ) ); + Info_SetValueForKey_s( systemInfo, sizeof( systemInfo ), "sv_pure", va( "%i", sv.pure ) ); + MSG_WriteBigString( &msg, systemInfo ); + } else { + MSG_WriteBigString( &msg, sv.configstrings[start] ); + } + } + } + + // write the baselines + Com_Memset( &nullstate, 0, sizeof( nullstate ) ); + for ( start = 0 ; start < MAX_GENTITIES; start++ ) { + if ( !sv.baselineUsed[ start ] ) { + continue; + } + svEnt = &sv.svEntities[ start ]; + MSG_WriteByte( &msg, svc_baseline ); + MSG_WriteDeltaEntity( &msg, &nullstate, &svEnt->baseline, qtrue ); + } + + MSG_WriteByte( &msg, svc_EOF ); + + MSG_WriteLong( &msg, client - svs.clients ); + + // write the checksum feed + MSG_WriteLong( &msg, sv.checksumFeed ); + + // it is important to handle gamestate overflow + // but at this stage client can't process any reliable commands + // so at least try to inform him in console and release connection slot + // if ( msg.overflowed ) { + // if ( client->netchan.remoteAddress.type == NA_LOOPBACK ) { + // Com_Error( ERR_DROP, "gamestate overflow" ); + // } else { + // NET_OutOfBandPrint( NS_SERVER, &client->netchan.remoteAddress, "print\n" S_COLOR_RED "SERVER ERROR: gamestate overflow\n" ); + // SV_DropClient( client, "gamestate overflow" ); + // } + // return; + // } + + // Finalize message + MSG_WriteByte(&msg, svc_EOF); + + // Write the client num - use actual client number + MSG_WriteLong(&msg, client - svs.clients); + + // Write the checksum feed + MSG_WriteLong(&msg, sv.checksumFeed); + + // End message + MSG_WriteByte(&msg, svc_EOF); + + // Write to demo file + // Sequence should be properly set to match client expectation + len = LittleLong(0); // Gamestate uses sequence 0 + FS_Write(&len, 4, client->demoFile); + + len = LittleLong(msg.cursize); + FS_Write(&len, 4, client->demoFile); + FS_Write(msg.data, msg.cursize, client->demoFile); +} + +/* +==================== +RS_WriteServerCommands +==================== +*/ +static void RS_WriteServerCommands(msg_t *msg, client_t *cl) { + int i; + + // Write all commands since the last recorded command + if (cl->reliableSequence - cl->demoCommandSequence > 0) { + // Don't write more than MAX_RELIABLE_COMMANDS + if (cl->reliableSequence - cl->demoCommandSequence > MAX_RELIABLE_COMMANDS) { + cl->demoCommandSequence = cl->reliableSequence - MAX_RELIABLE_COMMANDS; + } + + for (i = cl->demoCommandSequence + 1; i <= cl->reliableSequence; i++) { + int index = i & (MAX_RELIABLE_COMMANDS - 1); + MSG_WriteByte(msg, svc_serverCommand); + MSG_WriteLong(msg, i); + MSG_WriteString(msg, cl->reliableCommands[index]); + } + } + + cl->demoCommandSequence = cl->reliableSequence; +} + +/* +==================== +RS_WriteSnapshot +==================== +*/ +void RS_WriteSnapshot(client_t *cl) { + byte bufData[MAX_MSGLEN_BUF]; + msg_t msg; + int i, len; + + // Get current snapshot + clientSnapshot_t *frame = &cl->frames[cl->netchan.outgoingSequence & PACKET_MASK]; + + // Initialize message buffer + MSG_Init(&msg, bufData, sizeof(bufData)); + MSG_Bitstream(&msg); + + // Write reliable sequence + MSG_WriteLong(&msg, cl->reliableSequence); + + // Write server commands + RS_WriteServerCommands(&msg, cl); + + // Write snapshot header + MSG_WriteByte(&msg, svc_snapshot); + MSG_WriteLong(&msg, sv.time); // Server time + MSG_WriteByte(&msg, cl->demoDeltaNum); // 0 = no delta, 1 = delta + MSG_WriteByte(&msg, 0); // Snap flags + + // Write area info + MSG_WriteByte(&msg, frame->areabytes); + MSG_WriteData(&msg, frame->areabits, frame->areabytes); + + // Delta compress player state + if (cl->demoDeltaNum == 0) { + // First snapshot: no delta + MSG_WriteDeltaPlayerstate(&msg, NULL, &frame->ps); + } else { + // Using previous snapshot for delta + MSG_WriteDeltaPlayerstate(&msg, &saved_snap.ps, &frame->ps); + } + + RS_EmitPacketEntities(&saved_snap, frame, &msg); + // Finalize message + MSG_WriteByte(&msg, svc_EOF); + + // Write to demo file + len = LittleLong(cl->demoMessageSequence); + FS_Write(&len, 4, cl->demoFile); + + len = LittleLong(msg.cursize); + FS_Write(&len, 4, cl->demoFile); + FS_Write(msg.data, msg.cursize, cl->demoFile); + + // Save this snapshot for delta compression of next snapshot + // Create deep copies of entity pointers for delta compression + for (i = 0; i < frame->num_entities; i++) { + if (frame->ents[i]) { + // Make a copy of each entity state + saved_entity_states[i] = *(frame->ents[i]); + saved_ents[i] = &saved_entity_states[i]; + } else { + saved_ents[i] = NULL; + } + } + + // Copy the frame structure + saved_snap = *frame; + + // Update the entity pointers to our saved copies + for (i = 0; i < MAX_SNAPSHOT_ENTITIES; i++) { + saved_snap.ents[i] = saved_ents[i]; + } + + // Update tracking variables + cl->demoMessageSequence++; + cl->demoDeltaNum = 1; // All future snapshots use delta +} + +/* +================= +RS_EmitPacketEntities +================= +*/ +static void RS_EmitPacketEntities( const clientSnapshot_t *from, const clientSnapshot_t *to, msg_t *msg ) { + entityState_t *oldent, *newent; + int oldindex, newindex; + int oldnum, newnum; + int from_num_entities; + + // generate the delta update + if ( !from ) { + from_num_entities = 0; + } else { + from_num_entities = from->num_entities; + } + + newent = NULL; + oldent = NULL; + newindex = 0; + oldindex = 0; + while ( newindex < to->num_entities || oldindex < from_num_entities ) { + if ( newindex >= to->num_entities ) { + newnum = MAX_GENTITIES+1; + } else { + newent = to->ents[ newindex ]; + newnum = newent->number; + } + + if ( oldindex >= from_num_entities ) { + oldnum = MAX_GENTITIES+1; + } else { + oldent = from->ents[ oldindex ]; + oldnum = oldent->number; + } + + if ( newnum == oldnum ) { + // delta update from old position + // because the force parm is qfalse, this will not result + // in any bytes being emitted if the entity has not changed at all + MSG_WriteDeltaEntity (msg, oldent, newent, qfalse ); + oldindex++; + newindex++; + continue; + } + + if ( newnum < oldnum ) { + // this is a new entity, send it from the baseline + MSG_WriteDeltaEntity (msg, &sv.svEntities[newnum].baseline, newent, qtrue ); + newindex++; + continue; + } + + if ( newnum > oldnum ) { + // the old entity isn't present in the new message + MSG_WriteDeltaEntity (msg, oldent, NULL, qtrue ); + oldindex++; + continue; + } + } + + MSG_WriteBits( msg, (MAX_GENTITIES-1), GENTITYNUM_BITS ); // end of packetentities +} + +/* +==================== +RS_CheckDemoFormat +==================== +*/ +void RS_CheckDemoFormat(client_t *cl) { + // Verify we're not waiting for gamestate + if (cl->demoWaiting) { + Com_Printf("ERROR: Demo still waiting for gamestate!\n"); + } + + // Verify we have written at least one snapshot + if (cl->demoMessageSequence <= 1) { + Com_Printf("ERROR: No snapshots written yet!\n"); + } + + // Check the file status + fileHandle_t f = cl->demoFile; + if (f == FS_INVALID_HANDLE) { + Com_Printf("ERROR: Invalid demo file handle!\n"); + return; + } + + // Simply report that the demo file is valid + Com_Printf("Demo file appears valid, written %d snapshots\n", cl->demoMessageSequence); +} + +/* +==================== +RS_WriteDemoMessage +==================== +*/ +void RS_WriteDemoMessage(client_t *cl, msg_t *msg) { + if (!cl->isRecording) { + return; + } + + // Skip if waiting for first snapshot + if (cl->demoWaiting) { + if (cl->netchan.outgoingSequence > 0) { + cl->demoWaiting = qfalse; + } else { + return; + } + } + + // Write the packet sequence + int len = cl->netchan.outgoingSequence; + int swlen = LittleLong(len); + FS_Write(&swlen, 4, cl->demoFile); + + // Write the message size + len = LittleLong(msg->cursize); + FS_Write(&len, 4, cl->demoFile); + + // Write the message data + FS_Write(msg->data, msg->cursize, cl->demoFile); +} + +/* +==================== +RS_RecordSnapshot +Hook function to be called from SV_SendClientSnapshot +==================== +*/ +void RS_RecordSnapshot(client_t *cl) { + // If we're recording this client and they're active + if (cl->isRecording && cl->state == CS_ACTIVE) { + // If this is the first snapshot after connecting + if (cl->demoWaiting) { + cl->demoWaiting = qfalse; + + // Write the gamestate first + RS_WriteGamestate(cl); + + Com_Printf("Demo recording started for client %i\n", (int)(cl - svs.clients)); + } + + // Now record this snapshot + RS_WriteSnapshot(cl); + } +} + +#include +#include + +// Size of the message queue (must be power of 2) +#define DEMO_QUEUE_SIZE 1024 + +// Mask for queue indexing (DEMO_QUEUE_SIZE - 1) +#define DEMO_QUEUE_MASK (DEMO_QUEUE_SIZE - 1) + +// Thread and queue handling +static struct { + qboolean initialized; // Is system initialized + qboolean threadActive; // Is thread running + pthread_t threadId; // Demo writer thread ID + + // Message queue + demoQueuedMsg_t queue[DEMO_QUEUE_SIZE]; // Circular buffer of messages + int queueHead; // Index to write new messages + int queueTail; // Index to read next message + int queueCount; // Current number of messages in queue + + // Synchronization + pthread_mutex_t queueMutex; // Mutex for thread safety + pthread_cond_t dataAvailable; // Condition: data available to process + pthread_cond_t spaceAvailable; // Condition: space available in queue +} demoThread; + +// Function prototypes +static void* RS_DemoWriterThread(void *arg); +static qboolean RS_EnqueueMessage(demoQueuedMsg_t *msg); +static qboolean RS_DequeueMessage(demoQueuedMsg_t *msg); +static void RS_ProcessMessage(demoQueuedMsg_t *msg); + +/* +==================== +RS_InitThreadedDemos +==================== +*/ +qboolean RS_InitThreadedDemos(void) { + // Check if already initialized + if (demoThread.initialized) { + Com_Printf("Threaded demo system already initialized\n"); + return qtrue; + } + + // Initialize to zeros + memset(&demoThread, 0, sizeof(demoThread)); + + // Initialize mutex and condition variables + if (pthread_mutex_init(&demoThread.queueMutex, NULL) != 0) { + Com_Printf("Failed to initialize demo queue mutex\n"); + return qfalse; + } + + if (pthread_cond_init(&demoThread.dataAvailable, NULL) != 0) { + pthread_mutex_destroy(&demoThread.queueMutex); + Com_Printf("Failed to initialize data available condition\n"); + return qfalse; + } + + if (pthread_cond_init(&demoThread.spaceAvailable, NULL) != 0) { + pthread_cond_destroy(&demoThread.dataAvailable); + pthread_mutex_destroy(&demoThread.queueMutex); + Com_Printf("Failed to initialize space available condition\n"); + return qfalse; + } + + // Start the writer thread + demoThread.threadActive = qtrue; + if (pthread_create(&demoThread.threadId, NULL, RS_DemoWriterThread, NULL) != 0) { + pthread_cond_destroy(&demoThread.spaceAvailable); + pthread_cond_destroy(&demoThread.dataAvailable); + pthread_mutex_destroy(&demoThread.queueMutex); + Com_Printf("Failed to create demo writer thread\n"); + return qfalse; + } + + demoThread.initialized = qtrue; + Com_Printf("Threaded demo system initialized\n"); + return qtrue; +} + +/* +==================== +RS_ShutdownThreadedDemos +==================== +*/ +void RS_ShutdownThreadedDemos(void) { + demoQueuedMsg_t msg; + + if (!demoThread.initialized) { + return; + } + + // Signal thread to stop + pthread_mutex_lock(&demoThread.queueMutex); + demoThread.threadActive = qfalse; + pthread_cond_signal(&demoThread.dataAvailable); + pthread_mutex_unlock(&demoThread.queueMutex); + + // Wait for thread to finish + pthread_join(demoThread.threadId, NULL); + + // Clean up any remaining messages in the queue + while (RS_DequeueMessage(&msg)) { + if (msg.data) { + Z_Free(msg.data); + } + } + + // Destroy synchronization objects + pthread_cond_destroy(&demoThread.spaceAvailable); + pthread_cond_destroy(&demoThread.dataAvailable); + pthread_mutex_destroy(&demoThread.queueMutex); + + demoThread.initialized = qfalse; + Com_Printf("Threaded demo system shut down\n"); +} + +/* +==================== +RS_StartThreadedRecord +==================== +*/ +void RS_StartThreadedRecord(int clientNum, const char *plyrName, const char *demoName) { + if (!demoThread.initialized) { + Com_Printf("Threaded demo system not initialized\n"); + return; + } + + // Let the original function handle the setup + RS_StartRecord(clientNum, plyrName, demoName); + + // The gamestate will be queued when the client is fully active + // This happens in SV_SendClientSnapshot via RS_QueueGamestate +} + +/* +==================== +RS_StopThreadedRecord +==================== +*/ +void RS_StopThreadedRecord(int clientNum, const char *plyrName, const char *str) { + demoQueuedMsg_t msg; + client_t *cl; + + if (!demoThread.initialized) { + Com_Printf("Threaded demo system not initialized\n"); + return; + } + + cl = &svs.clients[clientNum]; + if (!cl->isRecording) { + Com_Printf("Client %i is not being recorded\n", clientNum); + return; + } + + // Send an end recording message to the thread + msg.type = DEMO_MSG_END_RECORDING; + msg.clientNum = clientNum; + msg.sequence = 0; + msg.serverTime = sv.time; + msg.data = NULL; + msg.dataSize = 0; + + if (!RS_EnqueueMessage(&msg)) { + Com_Printf("Failed to queue stop recording message\n"); + // Fall back to direct stop + RS_StopRecord(clientNum, plyrName, str); + return; + } + + // Mark client as not recording to prevent new messages + cl->isRecording = qfalse; + + Com_Printf("Queued stop recording for client %i\n", clientNum); +} + +/* +==================== +RS_QueueGamestate +==================== +*/ +void RS_QueueGamestate(client_t *client) { + demoQueuedMsg_t msg; + byte *buffer; + msg_t qmsg; + byte msgBuffer[MAX_MSGLEN_BUF]; + int i; + entityState_t nullstate; + const svEntity_t *svEnt; + + if (!demoThread.initialized || !client->isRecording) { + return; + } + + // Initialize message + MSG_Init(&qmsg, msgBuffer, sizeof(msgBuffer)); + + // Write the message just like RS_WriteGamestate does + MSG_WriteLong(&qmsg, client->lastClientCommand); + + // Send any server commands + SV_UpdateServerCommandsToClient(client, &qmsg); + + // Send the gamestate + MSG_WriteByte(&qmsg, svc_gamestate); + MSG_WriteLong(&qmsg, client->reliableSequence); + + // Write the configstrings + for (i = 0; i < MAX_CONFIGSTRINGS; i++) { + if (*sv.configstrings[i] != '\0') { + MSG_WriteByte(&qmsg, svc_configstring); + MSG_WriteShort(&qmsg, i); + if (i == CS_SYSTEMINFO && sv.pure != sv_pure->integer) { + // Special handling for system info + char systemInfo[BIG_INFO_STRING]; + Q_strncpyz(systemInfo, sv.configstrings[i], sizeof(systemInfo)); + Info_SetValueForKey_s(systemInfo, sizeof(systemInfo), "sv_pure", va("%i", sv.pure)); + MSG_WriteBigString(&qmsg, systemInfo); + } else { + MSG_WriteBigString(&qmsg, sv.configstrings[i]); + } + } + } + + // Write the baselines + Com_Memset(&nullstate, 0, sizeof(nullstate)); + for (i = 0; i < MAX_GENTITIES; i++) { + if (!sv.baselineUsed[i]) { + continue; + } + svEnt = &sv.svEntities[i]; + MSG_WriteByte(&qmsg, svc_baseline); + MSG_WriteDeltaEntity(&qmsg, &nullstate, &svEnt->baseline, qtrue); + } + + MSG_WriteByte(&qmsg, svc_EOF); + MSG_WriteLong(&qmsg, client - svs.clients); + MSG_WriteLong(&qmsg, sv.checksumFeed); + MSG_WriteByte(&qmsg, svc_EOF); + + // Copy the message data + buffer = Z_Malloc(qmsg.cursize); + memcpy(buffer, qmsg.data, qmsg.cursize); + + // Setup the message for the queue + msg.type = DEMO_MSG_GAMESTATE; + msg.clientNum = client - svs.clients; + msg.sequence = 0; // Gamestate uses sequence 0 + msg.serverTime = sv.time; + msg.data = buffer; + msg.dataSize = qmsg.cursize; + + // Queue the message + if (!RS_EnqueueMessage(&msg)) { + Com_Printf("Failed to queue gamestate, queue full\n"); + Z_Free(buffer); + return; + } + + // Reset client's demo sequence tracking + client->demoMessageSequence = 1; + client->demoDeltaNum = 0; + + Com_Printf("Queued gamestate for client %i\n", msg.clientNum); +} + +/* +==================== +RS_QueueSnapshot +==================== +*/ +void RS_QueueSnapshot(client_t *client) { + demoQueuedMsg_t msg; + byte *buffer; + msg_t qmsg; + byte msgBuffer[MAX_MSGLEN_BUF]; + + if (!demoThread.initialized || !client->isRecording) { + return; + } + + // Let the existing code prepare the snapshot message + MSG_Init(&qmsg, msgBuffer, sizeof(msgBuffer)); + MSG_Bitstream(&qmsg); + + // Write reliable sequence + MSG_WriteLong(&qmsg, client->reliableSequence); + + // Write server commands + if (client->reliableSequence - client->demoCommandSequence > 0) { + int i; + + // Don't write more than MAX_RELIABLE_COMMANDS + if (client->reliableSequence - client->demoCommandSequence > MAX_RELIABLE_COMMANDS) { + client->demoCommandSequence = client->reliableSequence - MAX_RELIABLE_COMMANDS; + } + + for (i = client->demoCommandSequence + 1; i <= client->reliableSequence; i++) { + int index = i & (MAX_RELIABLE_COMMANDS - 1); + MSG_WriteByte(&qmsg, svc_serverCommand); + MSG_WriteLong(&qmsg, i); + MSG_WriteString(&qmsg, client->reliableCommands[index]); + } + } + + client->demoCommandSequence = client->reliableSequence; + + // Get current snapshot + clientSnapshot_t *frame = &client->frames[client->netchan.outgoingSequence & PACKET_MASK]; + + // Write snapshot header + MSG_WriteByte(&qmsg, svc_snapshot); + MSG_WriteLong(&qmsg, sv.time); + MSG_WriteByte(&qmsg, client->demoDeltaNum); + MSG_WriteByte(&qmsg, 0); // Snap flags + + // Write area info + MSG_WriteByte(&qmsg, frame->areabytes); + MSG_WriteData(&qmsg, frame->areabits, frame->areabytes); + + // Delta compress player state + if (client->demoDeltaNum == 0) { + // First snapshot: no delta + MSG_WriteDeltaPlayerstate(&qmsg, NULL, &frame->ps); + } else { + // Using previous snapshot for delta - this will be handled in RS_WriteSnapshot + // We just prepare the basic message here + MSG_WriteDeltaPlayerstate(&qmsg, NULL, &frame->ps); + } + + // Don't attempt to delta compress entities here + // That's better handled in RS_WriteSnapshot directly + // Just send a placeholder entity message indicating we need to do it there + MSG_WriteByte(&qmsg, svc_EOF); + + // Copy the message data + buffer = Z_Malloc(qmsg.cursize); + memcpy(buffer, qmsg.data, qmsg.cursize); + + // Setup the message for the queue + msg.type = DEMO_MSG_SNAPSHOT; + msg.clientNum = client - svs.clients; + msg.sequence = client->netchan.outgoingSequence; + msg.serverTime = sv.time; + msg.data = buffer; + msg.dataSize = qmsg.cursize; + + // Queue the message + if (!RS_EnqueueMessage(&msg)) { + Com_Printf("Failed to queue snapshot, queue full\n"); + Z_Free(buffer); + return; + } +} + +/* +==================== +RS_EnqueueMessage +==================== +*/ +static qboolean RS_EnqueueMessage(demoQueuedMsg_t *msg) { + qboolean result = qfalse; + + pthread_mutex_lock(&demoThread.queueMutex); + + // Wait if queue is full (with timeout) + while (demoThread.queueCount >= DEMO_QUEUE_SIZE && demoThread.threadActive) { + struct timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + ts.tv_sec += 1; // 1 second timeout + + // Wait for space to become available + pthread_cond_timedwait(&demoThread.spaceAvailable, &demoThread.queueMutex, &ts); + } + + // Check if we can add to the queue + if (demoThread.queueCount < DEMO_QUEUE_SIZE && demoThread.threadActive) { + // Copy message to the queue + demoThread.queue[demoThread.queueHead] = *msg; + + // Update queue pointers + demoThread.queueHead = (demoThread.queueHead + 1) & DEMO_QUEUE_MASK; + demoThread.queueCount++; + + // Signal that data is available + pthread_cond_signal(&demoThread.dataAvailable); + + result = qtrue; + } + + pthread_mutex_unlock(&demoThread.queueMutex); + return result; +} + +/* +==================== +RS_DequeueMessage +==================== +*/ +static qboolean RS_DequeueMessage(demoQueuedMsg_t *msg) { + qboolean result = qfalse; + + pthread_mutex_lock(&demoThread.queueMutex); + + // Wait for data if queue is empty + while (demoThread.queueCount == 0 && demoThread.threadActive) { + // Wait for data to become available + pthread_cond_wait(&demoThread.dataAvailable, &demoThread.queueMutex); + } + + // Check if we have data + if (demoThread.queueCount > 0) { + // Copy message from the queue + *msg = demoThread.queue[demoThread.queueTail]; + + // Update queue pointers + demoThread.queueTail = (demoThread.queueTail + 1) & DEMO_QUEUE_MASK; + demoThread.queueCount--; + + // Signal that space is available + pthread_cond_signal(&demoThread.spaceAvailable); + + result = qtrue; + } + + pthread_mutex_unlock(&demoThread.queueMutex); + return result; +} + +/* +==================== +RS_ProcessMessage +==================== +*/ +static void RS_ProcessMessage(demoQueuedMsg_t *msg) { + client_t *client; + int len; + + // Get the client + if (msg->clientNum < 0 || msg->clientNum >= sv_maxclients->integer) { + Com_Printf("Invalid client number %d in demo message\n", msg->clientNum); + return; + } + + client = &svs.clients[msg->clientNum]; + + // Process based on message type + switch (msg->type) { + case DEMO_MSG_GAMESTATE: + if (client->demoFile != FS_INVALID_HANDLE) { + // Write sequence (0 for gamestate) + len = LittleLong(0); + FS_Write(&len, 4, client->demoFile); + + // Write message size + len = LittleLong(msg->dataSize); + FS_Write(&len, 4, client->demoFile); + + // Write message data + FS_Write(msg->data, msg->dataSize, client->demoFile); + + Com_Printf("Wrote gamestate for client %i\n", msg->clientNum); + } + break; + + case DEMO_MSG_SNAPSHOT: + if (client->demoFile != FS_INVALID_HANDLE) { + // Let the existing code write the snapshot + // This handles delta compression correctly + RS_WriteSnapshot(client); + + Com_Printf("Wrote snapshot %i for client %i\n", + client->demoMessageSequence - 1, msg->clientNum); + } + break; + + case DEMO_MSG_END_RECORDING: + // Close the demo file + if (client->demoFile != FS_INVALID_HANDLE) { + int len = -1; + + // Write proper EOF markers - TWO -1 values + FS_Write(&len, 4, client->demoFile); + FS_Write(&len, 4, client->demoFile); + + FS_FCloseFile(client->demoFile); + client->demoFile = FS_INVALID_HANDLE; + + Com_Printf("Finished recording demo for client %i\n", msg->clientNum); + } + break; + + default: + Com_Printf("Unknown demo message type %d\n", msg->type); + break; + } +} + +/* +==================== +RS_DemoWriterThread +==================== +*/ +static void* RS_DemoWriterThread(void *arg) { + demoQueuedMsg_t msg; + + Com_Printf("Demo writer thread started\n"); + + while (demoThread.threadActive) { + // Wait for a message + if (RS_DequeueMessage(&msg)) { + // Process the message + RS_ProcessMessage(&msg); + + // Free the message data + if (msg.data) { + Z_Free(msg.data); + } + } + } + + Com_Printf("Demo writer thread stopped\n"); + return NULL; +} + +/* +==================== +RS_GetQueuedMessageCount +==================== +*/ +int RS_GetQueuedMessageCount(void) { + int count; + + pthread_mutex_lock(&demoThread.queueMutex); + count = demoThread.queueCount; + pthread_mutex_unlock(&demoThread.queueMutex); + + return count; +} \ No newline at end of file diff --git a/code/server/server.h b/code/server/server.h index d096f685f1..ed9b6056e5 100644 --- a/code/server/server.h +++ b/code/server/server.h @@ -90,6 +90,7 @@ typedef struct { int time; byte baselineUsed[ MAX_GENTITIES ]; + } server_t; typedef struct { @@ -229,6 +230,20 @@ typedef struct client_s { char tld[3]; // "XX\0" const char *country; +#ifdef DEDICATED + qboolean isLogged; + const char *uuid; + qboolean isRecording; + fileHandle_t demoFile; + + int eventMask; + int demoCommandSequence; + int demoDeltaNum; + int demoMessageSequence; + + msg_t gamestateMsg; + qboolean demoWaiting; +#endif } client_t; //============================================================================= @@ -389,7 +404,7 @@ client_t *SV_GetPlayerByHandle( void ); void SV_AddServerCommand( client_t *client, const char *cmd ); void SV_UpdateServerCommandsToClient( client_t *client, msg_t *msg ); void SV_WriteFrameToClient( client_t *client, msg_t *msg ); -void SV_SendMessageToClient( msg_t *msg, client_t *client ); +void SV_SendMessageToClient(msg_t *msg, client_t *client, qboolean isSnapshot); void SV_SendClientMessages( void ); void SV_SendClientSnapshot( client_t *client ); diff --git a/code/server/sv_client.c b/code/server/sv_client.c index b1fc010a03..66bb93831e 100644 --- a/code/server/sv_client.c +++ b/code/server/sv_client.c @@ -21,10 +21,10 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ // sv_client.c -- server code for dealing with clients -#include "server.h" - -#ifdef DEDICATED +#ifdef ENABLE_RS #include "../recordsystem/recordsystem.h" +#else +#include "server.h" #endif static void SV_CloseDownload( client_t *cl ); @@ -1132,7 +1132,7 @@ static void SV_SendClientGameState( client_t *client ) { } // deliver this to the client - SV_SendMessageToClient( &msg, client ); + SV_SendMessageToClient( &msg, client, qfalse ); } @@ -2088,7 +2088,7 @@ static qboolean SV_ClientCommand( client_t *cl, msg_t *msg ) { return qfalse; } -#ifdef DEDICATED +#ifdef ENABLE_RS int clientNum; clientNum = cl - svs.clients; if (!RS_CommandGateway(clientNum, cl->name, s)) { diff --git a/code/server/sv_game.c b/code/server/sv_game.c index b089bb51a3..f1d40b9ca8 100644 --- a/code/server/sv_game.c +++ b/code/server/sv_game.c @@ -21,11 +21,15 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ // sv_game.c -- interface to the game dll +#ifdef DEDICATED +#include "../recordsystem/recordsystem.h" +#else #include "server.h" +#endif #include -#include "../recordsystem/recordsystem.h" #include "../botlib/botlib.h" + botlib_export_t *botlib_export; // these functions must be used instead of pointer arithmetic, because @@ -362,7 +366,7 @@ The module is making a system call static intptr_t SV_GameSystemCalls( intptr_t *args ) { switch( args[0] ) { case G_PRINT: - #ifdef DEDICATED + #ifdef ENABLE_RS RS_Gateway((const char *)VMA(1)); #endif Com_Printf( "%s", (const char*)VMA(1) ); diff --git a/code/server/sv_init.c b/code/server/sv_init.c index 1cd02b8bb7..6d6da0aab7 100644 --- a/code/server/sv_init.c +++ b/code/server/sv_init.c @@ -20,7 +20,11 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA =========================================================================== */ +#ifdef ENABLE_RS +#include "../recordsystem/recordsystem.h" +#else #include "server.h" +#endif cvar_t *sv_noReferencedPaks; @@ -832,6 +836,9 @@ void SV_Init( void ) SV_TrackCvarChanges(); SV_InitChallenger(); + #ifdef ENABLE_RS + RS_InitThreadedDemos(); + #endif } @@ -882,7 +889,9 @@ void SV_Shutdown( const char *finalmsg ) { if ( !com_sv_running || !com_sv_running->integer ) { return; } - + #ifdef ENABLE_RS + RS_ShutdownThreadedDemos(); + #endif Com_Printf( "----- Server Shutdown (%s) -----\n", finalmsg ); #ifdef USE_IPV6 diff --git a/code/server/sv_snapshot.c b/code/server/sv_snapshot.c index e790ab2598..6c28d65c44 100644 --- a/code/server/sv_snapshot.c +++ b/code/server/sv_snapshot.c @@ -20,8 +20,11 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA =========================================================================== */ +#ifdef ENABLE_RS +#include "../recordsystem/recordsystem.h" +#else #include "server.h" - +#endif /* ============================================================================= @@ -686,15 +689,31 @@ SV_SendMessageToClient Called by SV_SendClientSnapshot and SV_SendClientGameState ======================= */ -void SV_SendMessageToClient( msg_t *msg, client_t *client ) -{ - // record information about the message - client->frames[client->netchan.outgoingSequence & PACKET_MASK].messageSize = msg->cursize; - client->frames[client->netchan.outgoingSequence & PACKET_MASK].messageSent = svs.msgTime; - client->frames[client->netchan.outgoingSequence & PACKET_MASK].messageAcked = 0; - - // send the datagram - SV_Netchan_Transmit( client, msg ); +void SV_SendMessageToClient(msg_t *msg, client_t *client, qboolean isSnapshot) { + // record information about the message + client->frames[client->netchan.outgoingSequence & PACKET_MASK].messageSize = msg->cursize; + client->frames[client->netchan.outgoingSequence & PACKET_MASK].messageSent = svs.msgTime; + client->frames[client->netchan.outgoingSequence & PACKET_MASK].messageAcked = 0; + + // send the datagram + SV_Netchan_Transmit(client, msg); + +#ifdef ENABLE_RS + //If we're recording a demo for this client + if (client->isRecording) { + // If client is active and we're waiting to start recording + if (client->demoWaiting && client->state == CS_ACTIVE) { + client->demoWaiting = qfalse; + } + + // Only record after initial gamestate and when client is active + if (!client->demoWaiting && client->state == CS_ACTIVE) { + if (isSnapshot) { + RS_QueueSnapshot(client); + } + } + } +#endif } @@ -739,7 +758,7 @@ void SV_SendClientSnapshot( client_t *client ) { MSG_Clear( &msg ); } - SV_SendMessageToClient( &msg, client ); + SV_SendMessageToClient( &msg, client, qtrue ); } From 3eefabc4039568c21fa643efc97a47b1b55833ac Mon Sep 17 00:00:00 2001 From: frog Date: Mon, 31 Mar 2025 00:07:03 -0500 Subject: [PATCH 16/46] local login/logout, start serverdemo renaming --- Makefile | 1 - code/recordsystem/recordsystem.h | 57 +-- code/recordsystem/rs_commands.c | 47 +- code/recordsystem/rs_common.c | 6 +- code/recordsystem/rs_main.c | 10 - code/recordsystem/rs_records.c | 184 +++++++- code/recordsystem/rs_serverdemos.c | 724 +++-------------------------- code/server/server.h | 17 +- code/server/sv_client.c | 31 +- code/server/sv_game.c | 5 - code/server/sv_init.c | 16 +- code/server/sv_main.c | 3 + code/server/sv_snapshot.c | 15 +- 13 files changed, 341 insertions(+), 775 deletions(-) delete mode 100644 code/recordsystem/rs_main.c diff --git a/Makefile b/Makefile index d36509183a..b542979e8a 100644 --- a/Makefile +++ b/Makefile @@ -1307,7 +1307,6 @@ Q3DOBJ = \ $(B)/ded/cm_trace.o \ $(B)/ded/cmd.o \ $(B)/ded/cJSON.o \ - $(B)/ded/rs_main.o \ $(B)/ded/rs_records.o \ $(B)/ded/rs_common.o \ $(B)/ded/rs_commands.o \ diff --git a/code/recordsystem/recordsystem.h b/code/recordsystem/recordsystem.h index fff2fbcf59..a79471aeda 100644 --- a/code/recordsystem/recordsystem.h +++ b/code/recordsystem/recordsystem.h @@ -5,9 +5,9 @@ #include #include -#include "../qcommon/q_shared.h" -#include "../qcommon/qcommon.h" -#include "../server/server.h" +// #include "../qcommon/q_shared.h" +// #include "../qcommon/qcommon.h" +// #include "../server/server.h" // String utility functions qboolean startsWith(const char *string, const char *prefix); @@ -31,9 +31,6 @@ Create a new thread of execution with a string argument */ void Sys_CreateThread(void (*function)(const char *), const char *arg); - -void RS_SendTime(const char *cmdString); - /* =============== RS_GameSendServerCommand @@ -45,12 +42,12 @@ void RS_GameSendServerCommand(int clientNum, const char *text); /* =============== -RS_CommandGateway +RS_ExecuteClientCommand Routes commands to their appropriate handlers =============== */ -qboolean RS_CommandGateway(int clientNum, const char *plyrName, const char *s); +qboolean RS_ExecuteClientCommand(client_t *client, const char *s); /* =============== @@ -104,9 +101,9 @@ The returned string must be freed by the caller char* RS_UrlEncode(const char *str); -void RS_PrintAPIResponse(const char *jsonString); +void RS_ProcessAPIResponse(client_t *client, const char *jsonString); -void RS_StartRecord( int clientNum, const char *plyrName, const char *str ); +void RS_StartRecord(client_t *client); /* @@ -116,7 +113,7 @@ SV_StopRecording stop recording a demo ==================== */ -void RS_StopRecord( int clientNum, const char *plyrName, const char *str ); +void RS_StopRecord(client_t *client); /* ==================== @@ -133,43 +130,5 @@ RS_WriteSnapshotToDemo void RS_WriteSnapshot(client_t *client); void RS_WriteDemoMessage(client_t *client, msg_t *msg); -// Message types for demo recording -typedef enum { - DEMO_MSG_GAMESTATE, - DEMO_MSG_SNAPSHOT, - DEMO_MSG_END_RECORDING -} demoMsgType_t; - -// Structure for a demo message in the queue -typedef struct { - demoMsgType_t type; // Type of message - int clientNum; // Client index - int sequence; // Sequence number - int serverTime; // Server time for this message - byte *data; // Message data - int dataSize; // Size of the message data -} demoQueuedMsg_t; - -// Initialize the threaded demo writer system -qboolean RS_InitThreadedDemos(void); - -// Shutdown the threaded demo writer system -void RS_ShutdownThreadedDemos(void); - -// Start recording a demo for a client (threaded version) -void RS_StartThreadedRecord(int clientNum, const char *plyrName, const char *demoName); - -// Stop recording a demo for a client (threaded version) -void RS_StopThreadedRecord(int clientNum, const char *plyrName, const char *str); - -// Queue a gamestate message for writing -void RS_QueueGamestate(client_t *client); - -// Queue a snapshot message for writing -void RS_QueueSnapshot(client_t *client); - -// Get number of pending messages in queue -int RS_GetQueuedMessageCount(void); - #endif // __RECORDSYSTEM_H__ \ No newline at end of file diff --git a/code/recordsystem/rs_commands.c b/code/recordsystem/rs_commands.c index 7203f27c57..7c79da0f15 100644 --- a/code/recordsystem/rs_commands.c +++ b/code/recordsystem/rs_commands.c @@ -1,11 +1,12 @@ -#include "recordsystem.h" +#include "../server/server.h" #include "cJSON.h" -static void RS_Top(int clientNum, const char *plyrName, const char *str) { +static void RS_Top(client_t *client, const char *str) { char *response; char *encoded_str; char *encoded_map; char url[512]; + int clientNum = client - svs.clients; // Encode the command string encoded_str = RS_UrlEncode(str); @@ -34,17 +35,18 @@ static void RS_Top(int clientNum, const char *plyrName, const char *str) { response = RS_HttpGet(url); if (response) { - RS_PrintAPIResponse(response); + RS_ProcessAPIResponse(client, response); free(response); // Free the response } else { RS_GameSendServerCommand(clientNum, "print \"^1Failed to get response\n\""); } } -static void RS_Recent(int clientNum, const char *plyrName, const char *str) { +static void RS_Recent(client_t *client, const char *str) { char *response; char *encoded_str; char url[512]; + int clientNum = client - svs.clients; encoded_str = RS_UrlEncode(str); if (!encoded_str) { @@ -61,17 +63,18 @@ static void RS_Recent(int clientNum, const char *plyrName, const char *str) { response = RS_HttpGet(url); if (response) { - RS_PrintAPIResponse(response); + RS_ProcessAPIResponse(client, response); free(response); // Free the response } else { RS_GameSendServerCommand(clientNum, "print \"^1Failed to get response\n\""); } } -static void RS_Login(int clientNum, const char *plyrName, const char *str) { +static void RS_Login(client_t *client, const char *str) { char *response; char *jsonString; cJSON *json; + int clientNum = client - svs.clients; // Create a JSON object json = cJSON_CreateObject(); @@ -83,7 +86,7 @@ static void RS_Login(int clientNum, const char *plyrName, const char *str) { // Add client number and command string to the JSON object cJSON_AddNumberToObject(json, "clientNum", clientNum); cJSON_AddStringToObject(json, "cmdString", str); - cJSON_AddStringToObject(json, "plyrName", plyrName); + cJSON_AddStringToObject(json, "plyrName", client->name); // Convert JSON object to string jsonString = cJSON_Print(json); @@ -94,6 +97,7 @@ static void RS_Login(int clientNum, const char *plyrName, const char *str) { return; } + client->awaitingLogin = qtrue; // Make the HTTP request response = RS_HttpPost("http://localhost:8000/api/commands/login", "application/json", jsonString); @@ -102,29 +106,36 @@ static void RS_Login(int clientNum, const char *plyrName, const char *str) { free(jsonString); if (response) { - RS_PrintAPIResponse(response); + RS_ProcessAPIResponse(client, response); free(response); + client->loggedIn = qtrue; } else { RS_GameSendServerCommand(clientNum, "print \"^1Failed to connect to server\n\""); } + + client->awaitingLogin = qfalse; } -static void RS_Logout(int clientNum, const char *plyrName, const char *str) { +static void RS_Logout(client_t *client, const char *str) { char *response; char *jsonString; cJSON *json; + int clientNum = client - svs.clients; + + client->loggedIn = qfalse; // Log them out locally, don't wait for server. + RS_GameSendServerCommand(clientNum, va("print \"%s^5, ^7you are now logged out^5.^7\n\"", client->name)); // Create a JSON object json = cJSON_CreateObject(); if (!json) { - RS_GameSendServerCommand(clientNum, "print \"^1Error creating JSON object\n\""); + Com_Printf("^1Error creating JSON object\n"); return; } // Add client number and command string to the JSON object cJSON_AddNumberToObject(json, "clientNum", clientNum); cJSON_AddStringToObject(json, "cmdString", str); - cJSON_AddStringToObject(json, "plyrName", plyrName); + cJSON_AddStringToObject(json, "plyrName", client->name); // Convert JSON object to string jsonString = cJSON_Print(json); @@ -135,6 +146,7 @@ static void RS_Logout(int clientNum, const char *plyrName, const char *str) { return; } + client->awaitingLogout = qtrue; // Let game know that client is waiting for remote logout // Make the HTTP request response = RS_HttpPost("http://localhost:8000/api/commands/logout", "application/json", jsonString); @@ -143,8 +155,9 @@ static void RS_Logout(int clientNum, const char *plyrName, const char *str) { free(jsonString); if (response) { - RS_PrintAPIResponse(response); + RS_ProcessAPIResponse(client, response); free(response); + client->awaitingLogout = qfalse; } else { RS_GameSendServerCommand(clientNum, "print \"^1Failed to connect to server\n\""); } @@ -152,25 +165,23 @@ static void RS_Logout(int clientNum, const char *plyrName, const char *str) { typedef struct { const char *pattern; - void (*handler)(int clientNum, const char *plyrName, const char *str); + void (*handler)(client_t *client, const char *str); } Module; static Module modules[] = { {"top", RS_Top}, {"recent", RS_Recent}, {"login", RS_Login}, - {"logout", RS_Logout}, - {"rs_record", RS_StartThreadedRecord}, - {"rs_stoprecord", RS_StopThreadedRecord} + {"logout", RS_Logout} }; -qboolean RS_CommandGateway(int clientNum, const char *plyrName, const char *s) { +qboolean RS_ExecuteClientCommand(client_t *client, const char *s) { // Check each command pattern int numModules = sizeof(modules) / sizeof(modules[0]); for (int i = 0; i < numModules; i++) { if (startsWith(s, va("%s ",modules[i].pattern)) || Q_stricmp(s, modules[i].pattern) == 0) { // Call the appropriate handler function - modules[i].handler(clientNum, plyrName, s); + modules[i].handler(client, s); return qtrue; } } diff --git a/code/recordsystem/rs_common.c b/code/recordsystem/rs_common.c index 2f50f03ec7..ed2567571d 100644 --- a/code/recordsystem/rs_common.c +++ b/code/recordsystem/rs_common.c @@ -1,4 +1,4 @@ -#include "recordsystem.h" +#include "../server/server.h" #include #include "cJSON.h" @@ -301,7 +301,7 @@ char* RS_HttpPost(const char *url, const char *contentType, const char *payload) }; -void RS_PrintAPIResponse(const char *jsonString) { +void RS_ProcessAPIResponse(client_t *client, const char *jsonString) { cJSON *json; cJSON *targetClientObj; cJSON *messageObj; @@ -314,7 +314,7 @@ void RS_PrintAPIResponse(const char *jsonString) { Com_Printf("RS: Failed to parse JSON: %s\n", cJSON_GetErrorPtr()); return; } - + // Extract targetClient field targetClientObj = cJSON_GetObjectItem(json, "targetClient"); if (targetClientObj && cJSON_IsNumber(targetClientObj)) { diff --git a/code/recordsystem/rs_main.c b/code/recordsystem/rs_main.c deleted file mode 100644 index c568a0d9d5..0000000000 --- a/code/recordsystem/rs_main.c +++ /dev/null @@ -1,10 +0,0 @@ -#include "recordsystem.h" - -void RS_Gateway(const char *s) { - if (RS_IsClientTimerStop(s)) { - if (Cvar_VariableIntegerValue("sv_cheats") != 0) { - return; - } - Sys_CreateThread(RS_SendTime, s); - } -} \ No newline at end of file diff --git a/code/recordsystem/rs_records.c b/code/recordsystem/rs_records.c index 51934b7052..2990ceffed 100644 --- a/code/recordsystem/rs_records.c +++ b/code/recordsystem/rs_records.c @@ -1,6 +1,160 @@ -#include "recordsystem.h" +#include "../server/server.h" #include "cJSON.h" +/* +==================== +ParseTimerStop +Parses a timer stop log message into a structured format +==================== +*/ +typedef struct { + int clientNum; // Entity/client number + int time; // Timer time (in milliseconds) + char mapname[64]; // Map name + char netname[36]; // Player's name + int gametype; // Game type + int promode; // Promode enabled + int submode; // Sub-mode + int interferenceOff; // Interference off flag + int obEnabled; // Out of bounds enabled + int version; // Version number + char date[16]; // Date string (YYYY-MM-DD) +} timeInfo_t; + +static timeInfo_t* RS_ParseClientTimerStop(const char* logLine) { + timeInfo_t* info; + char buffer[1024]; + const char *token, *str; + + // Check that the line starts with "ClientTimerStop:" + if (!logLine || strncmp(logLine, "ClientTimerStop:", 16) != 0) { + return NULL; + } + + // Allocate memory for the structure + info = (timeInfo_t*)Z_Malloc(sizeof(timeInfo_t)); + if (!info) { + return NULL; + } + + // Initialize the structure + memset(info, 0, sizeof(timeInfo_t)); + + // Skip past "ClientTimerStop: " + logLine += 16; + + // Make a copy of the line to tokenize + Q_strncpyz(buffer, logLine, sizeof(buffer)); + str = buffer; + + // Parse client number + token = COM_Parse(&str); + if (!token[0]) { + Z_Free(info); + return NULL; + } + info->clientNum = atoi(token); + + // Parse time + token = COM_Parse(&str); + if (!token[0]) { + Z_Free(info); + return NULL; + } + info->time = atoi(token); + + // Parse mapname (quoted) + token = COM_ParseExt(&str, qfalse); + if (!token[0]) { + Z_Free(info); + return NULL; + } + // Remove quotes + if (token[0] == '"') { + Q_strncpyz(info->mapname, token + 1, sizeof(info->mapname) - 1); + if (info->mapname[strlen(info->mapname) - 1] == '"') { + info->mapname[strlen(info->mapname) - 1] = '\0'; + } + } else { + Q_strncpyz(info->mapname, token, sizeof(info->mapname)); + } + + // Parse netname (quoted) + token = COM_ParseExt(&str, qfalse); + if (!token[0]) { + Z_Free(info); + return NULL; + } + // Remove quotes + if (token[0] == '"') { + Q_strncpyz(info->netname, token + 1, sizeof(info->netname) - 1); + if (info->netname[strlen(info->netname) - 1] == '"') { + info->netname[strlen(info->netname) - 1] = '\0'; + } + } else { + Q_strncpyz(info->netname, token, sizeof(info->netname)); + } + + // Parse gametype + token = COM_Parse(&str); + if (!token[0]) { + Z_Free(info); + return NULL; + } + info->gametype = atoi(token); + + // Parse promode + token = COM_Parse(&str); + if (!token[0]) { + Z_Free(info); + return NULL; + } + info->promode = atoi(token); + + // Parse submode + token = COM_Parse(&str); + if (!token[0]) { + Z_Free(info); + return NULL; + } + info->submode = atoi(token); + + // Parse interference flag + token = COM_Parse(&str); + if (!token[0]) { + Z_Free(info); + return NULL; + } + info->interferenceOff = atoi(token); + + // Parse OB flag + token = COM_Parse(&str); + if (!token[0]) { + Z_Free(info); + return NULL; + } + info->obEnabled = atoi(token); + + // Parse version + token = COM_Parse(&str); + if (!token[0]) { + Z_Free(info); + return NULL; + } + info->version = atoi(token); + + // Parse date + token = COM_Parse(&str); + if (!token[0]) { + Z_Free(info); + return NULL; + } + Q_strncpyz(info->date, token, sizeof(info->date)); + + return info; +} + + /* =============== RS_SendTime @@ -8,11 +162,13 @@ RS_SendTime Sends a time record to the API server =============== */ -void RS_SendTime(const char *cmdString) { +static void RS_SendTime(const char *cmdString) { char *response; char *jsonString; cJSON *json; + timeInfo_t *timeInfo = RS_ParseClientTimerStop(cmdString); + client_t *client = &svs.clients[timeInfo->clientNum]; // Create a JSON object for the request json = cJSON_CreateObject(); @@ -35,16 +191,24 @@ void RS_SendTime(const char *cmdString) { free(jsonString); if (response) { - RS_PrintAPIResponse(response); + RS_ProcessAPIResponse(client, response); free(response); } else { - RS_GameSendServerCommand(-1, "print \"^1Failed to connect to record server\n\""); + RS_GameSendServerCommand(timeInfo->clientNum, "print \"^1Failed to connect to record server\n\""); } } -qboolean RS_IsClientTimerStop(const char *s) { - // Extra logic here to make sure it's a true timer stop - // Potential: compare playerstates between this and last frame - // Check for timer state bit and that it's non-zero - return startsWith(s, "ClientTimerStop: ") ? qtrue : qfalse; -} +void RS_Gateway(const char *s) { + timeInfo_t* timeInfo = RS_ParseClientTimerStop(s); + if (timeInfo && Cvar_VariableIntegerValue("sv_cheats") == 0) { + client_t *client = &svs.clients[timeInfo->clientNum] + if (client->loggedIn) { + RS_EndAndRenameDemo(client, timeInfo); + Sys_CreateThread(RS_SendTime, s); + } + else + RS_GameSendServerCommand(timeInfo->clientNum, "print \"^7You are not logged in^5.\n\""); + + RS_RestartDemoRecord(); + } +} \ No newline at end of file diff --git a/code/recordsystem/rs_serverdemos.c b/code/recordsystem/rs_serverdemos.c index d90af5f9ed..ef041c874b 100644 --- a/code/recordsystem/rs_serverdemos.c +++ b/code/recordsystem/rs_serverdemos.c @@ -1,4 +1,4 @@ -#include "recordsystem.h" +#include "../server/server.h" // Static storage for delta compression static clientSnapshot_t saved_snap; @@ -6,6 +6,8 @@ static entityState_t saved_entity_states[MAX_SNAPSHOT_ENTITIES]; static entityState_t *saved_ents[MAX_SNAPSHOT_ENTITIES]; static void RS_EmitPacketEntities( const clientSnapshot_t *from, const clientSnapshot_t *to, msg_t *msg ); + + /* ==================== RS_StartRecord @@ -13,41 +15,38 @@ RS_StartRecord Begins recording a demo for a given client ==================== */ -void RS_StartRecord(int clientNum, const char *plyrName, const char *str) { +void RS_StartRecord(client_t *client) { char demoName[MAX_OSPATH]; - client_t *cl; + int clientNum = client - svs.clients; - cl = &svs.clients[clientNum]; - if (sv.state != SS_GAME) { - Com_Printf("Game must be running.\n"); + if (client->state != CS_ACTIVE) { + Com_Printf("Client must be active\n"); return; } - if (cl->isRecording) { + if (client->isRecording) { Com_Printf("Already recording client %i\n", clientNum); return; } // Create demo name - Q_strncpyz(demoName, va("demos/[%s][%d].dm_68", sv_mapname->string, clientNum), sizeof(demoName)); + Q_strncpyz(demoName, va("demos/[%d].dm_68", clientNum), sizeof(demoName)); Com_Printf("recording to %s.\n", demoName); - // open the demo file - cl->demoFile = FS_FOpenFileWrite(demoName); - if (cl->demoFile == FS_INVALID_HANDLE) { + // Start writing to demo file + client->demoFile = FS_FOpenFileWrite(demoName); + if (client->demoFile == FS_INVALID_HANDLE) { Com_Printf("ERROR: couldn't open file: %s.\n", demoName); return; } - cl->isRecording = qtrue; - cl->demoWaiting = qtrue; + // Set client's demo flags + client->isRecording = qtrue; + client->demoWaiting = qtrue; // write out the gamestate message - RS_WriteGamestate( cl ); - - // The gamestate will be written when the client is fully active - // This happens in SV_SendClientSnapshot + RS_WriteGamestate( client ); } /* @@ -57,29 +56,52 @@ RS_StopRecord stop recording a demo ==================== */ -void RS_StopRecord(int clientNum, const char *plyrName, const char *str) { - client_t *cl; - cl = &svs.clients[clientNum]; +void RS_StopRecord(client_t *client, timeInfo_t timeInfo) { + if (rename) { + char finalName[MAX_OSPATH]; + } - if (!cl->isRecording) { + if (timeInfo->gametype == 1) { // run mode + Com_sprintf( finalName, sizeof( finalName ), "%s[df.%s%s]%i(%s)(%s).dm_68", \ + timeInfo->mapName, \ + timeInfo->promode ? "cpm" : "vq3", \ + timeInfo->time, \ + client->uuid, \ + client->displayName); + } + else + Com_sprintf( finalName, sizeof( finalName ), "%s[df.%s.%i]%i(%s)(%s).dm_68", \ + timeInfo->mapName, \ + timeInfo->promode ? "cpm" : "vq3", \ + timeInfo->submode \ + timeInfo->time, \ + client->uuid, \ + client->displayName); + + FS_Rename( tempName, finalName ); + int clientNum = client - svs.clients; + + if (!client->isRecording) { Com_Printf("Client %i is not being recorded\n", clientNum); return; } - if (cl->demoFile != FS_INVALID_HANDLE) { + if (client->demoFile != FS_INVALID_HANDLE) { int len; // Write proper EOF markers - TWO -1 values len = -1; - FS_Write(&len, 4, cl->demoFile); - FS_Write(&len, 4, cl->demoFile); + FS_Write(&len, 4, client->demoFile); + FS_Write(&len, 4, client->demoFile); - FS_FCloseFile(cl->demoFile); - cl->demoFile = FS_INVALID_HANDLE; + FS_FCloseFile(client->demoFile); + client->demoFile = FS_INVALID_HANDLE; Com_Printf("Stopped recording client %i\n", clientNum); } - cl->isRecording = qfalse; + client->isRecording = qfalse; + client->demoWaiting = qfalse; + client->demoDeltaNum = 0; } /* @@ -193,25 +215,25 @@ void RS_WriteGamestate(client_t *client) { RS_WriteServerCommands ==================== */ -static void RS_WriteServerCommands(msg_t *msg, client_t *cl) { +static void RS_WriteServerCommands(msg_t *msg, client_t *client) { int i; // Write all commands since the last recorded command - if (cl->reliableSequence - cl->demoCommandSequence > 0) { + if (client->reliableSequence - client->demoCommandSequence > 0) { // Don't write more than MAX_RELIABLE_COMMANDS - if (cl->reliableSequence - cl->demoCommandSequence > MAX_RELIABLE_COMMANDS) { - cl->demoCommandSequence = cl->reliableSequence - MAX_RELIABLE_COMMANDS; + if (client->reliableSequence - client->demoCommandSequence > MAX_RELIABLE_COMMANDS) { + client->demoCommandSequence = client->reliableSequence - MAX_RELIABLE_COMMANDS; } - for (i = cl->demoCommandSequence + 1; i <= cl->reliableSequence; i++) { + for (i = client->demoCommandSequence + 1; i <= client->reliableSequence; i++) { int index = i & (MAX_RELIABLE_COMMANDS - 1); MSG_WriteByte(msg, svc_serverCommand); MSG_WriteLong(msg, i); - MSG_WriteString(msg, cl->reliableCommands[index]); + MSG_WriteString(msg, client->reliableCommands[index]); } } - cl->demoCommandSequence = cl->reliableSequence; + client->demoCommandSequence = client->reliableSequence; } /* @@ -219,28 +241,28 @@ static void RS_WriteServerCommands(msg_t *msg, client_t *cl) { RS_WriteSnapshot ==================== */ -void RS_WriteSnapshot(client_t *cl) { +void RS_WriteSnapshot(client_t *client) { byte bufData[MAX_MSGLEN_BUF]; msg_t msg; int i, len; // Get current snapshot - clientSnapshot_t *frame = &cl->frames[cl->netchan.outgoingSequence & PACKET_MASK]; + clientSnapshot_t *frame = &client->frames[client->netchan.outgoingSequence & PACKET_MASK]; // Initialize message buffer MSG_Init(&msg, bufData, sizeof(bufData)); MSG_Bitstream(&msg); // Write reliable sequence - MSG_WriteLong(&msg, cl->reliableSequence); + MSG_WriteLong(&msg, client->reliableSequence); // Write server commands - RS_WriteServerCommands(&msg, cl); + RS_WriteServerCommands(&msg, client); // Write snapshot header MSG_WriteByte(&msg, svc_snapshot); MSG_WriteLong(&msg, sv.time); // Server time - MSG_WriteByte(&msg, cl->demoDeltaNum); // 0 = no delta, 1 = delta + MSG_WriteByte(&msg, client->demoDeltaNum); // 0 = no delta, 1 = delta MSG_WriteByte(&msg, 0); // Snap flags // Write area info @@ -248,7 +270,7 @@ void RS_WriteSnapshot(client_t *cl) { MSG_WriteData(&msg, frame->areabits, frame->areabytes); // Delta compress player state - if (cl->demoDeltaNum == 0) { + if (client->demoDeltaNum == 0) { // First snapshot: no delta MSG_WriteDeltaPlayerstate(&msg, NULL, &frame->ps); } else { @@ -261,12 +283,12 @@ void RS_WriteSnapshot(client_t *cl) { MSG_WriteByte(&msg, svc_EOF); // Write to demo file - len = LittleLong(cl->demoMessageSequence); - FS_Write(&len, 4, cl->demoFile); + len = LittleLong(client->demoMessageSequence); + FS_Write(&len, 4, client->demoFile); len = LittleLong(msg.cursize); - FS_Write(&len, 4, cl->demoFile); - FS_Write(msg.data, msg.cursize, cl->demoFile); + FS_Write(&len, 4, client->demoFile); + FS_Write(msg.data, msg.cursize, client->demoFile); // Save this snapshot for delta compression of next snapshot // Create deep copies of entity pointers for delta compression @@ -289,8 +311,8 @@ void RS_WriteSnapshot(client_t *cl) { } // Update tracking variables - cl->demoMessageSequence++; - cl->demoDeltaNum = 1; // All future snapshots use delta + client->demoMessageSequence++; + client->demoDeltaNum = 1; // All future snapshots use delta } /* @@ -358,627 +380,35 @@ static void RS_EmitPacketEntities( const clientSnapshot_t *from, const clientSna MSG_WriteBits( msg, (MAX_GENTITIES-1), GENTITYNUM_BITS ); // end of packetentities } -/* -==================== -RS_CheckDemoFormat -==================== -*/ -void RS_CheckDemoFormat(client_t *cl) { - // Verify we're not waiting for gamestate - if (cl->demoWaiting) { - Com_Printf("ERROR: Demo still waiting for gamestate!\n"); - } - - // Verify we have written at least one snapshot - if (cl->demoMessageSequence <= 1) { - Com_Printf("ERROR: No snapshots written yet!\n"); - } - - // Check the file status - fileHandle_t f = cl->demoFile; - if (f == FS_INVALID_HANDLE) { - Com_Printf("ERROR: Invalid demo file handle!\n"); - return; - } - - // Simply report that the demo file is valid - Com_Printf("Demo file appears valid, written %d snapshots\n", cl->demoMessageSequence); -} - /* ==================== RS_WriteDemoMessage ==================== */ -void RS_WriteDemoMessage(client_t *cl, msg_t *msg) { - if (!cl->isRecording) { +void RS_WriteDemoMessage(client_t *client, msg_t *msg) { + if (!client->isRecording) { return; } // Skip if waiting for first snapshot - if (cl->demoWaiting) { - if (cl->netchan.outgoingSequence > 0) { - cl->demoWaiting = qfalse; + if (client->demoWaiting) { + if (client->netchan.outgoingSequence > 0) { + client->demoWaiting = qfalse; } else { return; } } // Write the packet sequence - int len = cl->netchan.outgoingSequence; + int len = client->netchan.outgoingSequence; int swlen = LittleLong(len); - FS_Write(&swlen, 4, cl->demoFile); + FS_Write(&swlen, 4, client->demoFile); // Write the message size len = LittleLong(msg->cursize); - FS_Write(&len, 4, cl->demoFile); + FS_Write(&len, 4, client->demoFile); // Write the message data - FS_Write(msg->data, msg->cursize, cl->demoFile); -} - -/* -==================== -RS_RecordSnapshot -Hook function to be called from SV_SendClientSnapshot -==================== -*/ -void RS_RecordSnapshot(client_t *cl) { - // If we're recording this client and they're active - if (cl->isRecording && cl->state == CS_ACTIVE) { - // If this is the first snapshot after connecting - if (cl->demoWaiting) { - cl->demoWaiting = qfalse; - - // Write the gamestate first - RS_WriteGamestate(cl); - - Com_Printf("Demo recording started for client %i\n", (int)(cl - svs.clients)); - } - - // Now record this snapshot - RS_WriteSnapshot(cl); - } -} - -#include -#include - -// Size of the message queue (must be power of 2) -#define DEMO_QUEUE_SIZE 1024 - -// Mask for queue indexing (DEMO_QUEUE_SIZE - 1) -#define DEMO_QUEUE_MASK (DEMO_QUEUE_SIZE - 1) - -// Thread and queue handling -static struct { - qboolean initialized; // Is system initialized - qboolean threadActive; // Is thread running - pthread_t threadId; // Demo writer thread ID - - // Message queue - demoQueuedMsg_t queue[DEMO_QUEUE_SIZE]; // Circular buffer of messages - int queueHead; // Index to write new messages - int queueTail; // Index to read next message - int queueCount; // Current number of messages in queue - - // Synchronization - pthread_mutex_t queueMutex; // Mutex for thread safety - pthread_cond_t dataAvailable; // Condition: data available to process - pthread_cond_t spaceAvailable; // Condition: space available in queue -} demoThread; - -// Function prototypes -static void* RS_DemoWriterThread(void *arg); -static qboolean RS_EnqueueMessage(demoQueuedMsg_t *msg); -static qboolean RS_DequeueMessage(demoQueuedMsg_t *msg); -static void RS_ProcessMessage(demoQueuedMsg_t *msg); - -/* -==================== -RS_InitThreadedDemos -==================== -*/ -qboolean RS_InitThreadedDemos(void) { - // Check if already initialized - if (demoThread.initialized) { - Com_Printf("Threaded demo system already initialized\n"); - return qtrue; - } - - // Initialize to zeros - memset(&demoThread, 0, sizeof(demoThread)); - - // Initialize mutex and condition variables - if (pthread_mutex_init(&demoThread.queueMutex, NULL) != 0) { - Com_Printf("Failed to initialize demo queue mutex\n"); - return qfalse; - } - - if (pthread_cond_init(&demoThread.dataAvailable, NULL) != 0) { - pthread_mutex_destroy(&demoThread.queueMutex); - Com_Printf("Failed to initialize data available condition\n"); - return qfalse; - } - - if (pthread_cond_init(&demoThread.spaceAvailable, NULL) != 0) { - pthread_cond_destroy(&demoThread.dataAvailable); - pthread_mutex_destroy(&demoThread.queueMutex); - Com_Printf("Failed to initialize space available condition\n"); - return qfalse; - } - - // Start the writer thread - demoThread.threadActive = qtrue; - if (pthread_create(&demoThread.threadId, NULL, RS_DemoWriterThread, NULL) != 0) { - pthread_cond_destroy(&demoThread.spaceAvailable); - pthread_cond_destroy(&demoThread.dataAvailable); - pthread_mutex_destroy(&demoThread.queueMutex); - Com_Printf("Failed to create demo writer thread\n"); - return qfalse; - } - - demoThread.initialized = qtrue; - Com_Printf("Threaded demo system initialized\n"); - return qtrue; -} - -/* -==================== -RS_ShutdownThreadedDemos -==================== -*/ -void RS_ShutdownThreadedDemos(void) { - demoQueuedMsg_t msg; - - if (!demoThread.initialized) { - return; - } - - // Signal thread to stop - pthread_mutex_lock(&demoThread.queueMutex); - demoThread.threadActive = qfalse; - pthread_cond_signal(&demoThread.dataAvailable); - pthread_mutex_unlock(&demoThread.queueMutex); - - // Wait for thread to finish - pthread_join(demoThread.threadId, NULL); - - // Clean up any remaining messages in the queue - while (RS_DequeueMessage(&msg)) { - if (msg.data) { - Z_Free(msg.data); - } - } - - // Destroy synchronization objects - pthread_cond_destroy(&demoThread.spaceAvailable); - pthread_cond_destroy(&demoThread.dataAvailable); - pthread_mutex_destroy(&demoThread.queueMutex); - - demoThread.initialized = qfalse; - Com_Printf("Threaded demo system shut down\n"); -} - -/* -==================== -RS_StartThreadedRecord -==================== -*/ -void RS_StartThreadedRecord(int clientNum, const char *plyrName, const char *demoName) { - if (!demoThread.initialized) { - Com_Printf("Threaded demo system not initialized\n"); - return; - } - - // Let the original function handle the setup - RS_StartRecord(clientNum, plyrName, demoName); - - // The gamestate will be queued when the client is fully active - // This happens in SV_SendClientSnapshot via RS_QueueGamestate -} - -/* -==================== -RS_StopThreadedRecord -==================== -*/ -void RS_StopThreadedRecord(int clientNum, const char *plyrName, const char *str) { - demoQueuedMsg_t msg; - client_t *cl; - - if (!demoThread.initialized) { - Com_Printf("Threaded demo system not initialized\n"); - return; - } - - cl = &svs.clients[clientNum]; - if (!cl->isRecording) { - Com_Printf("Client %i is not being recorded\n", clientNum); - return; - } - - // Send an end recording message to the thread - msg.type = DEMO_MSG_END_RECORDING; - msg.clientNum = clientNum; - msg.sequence = 0; - msg.serverTime = sv.time; - msg.data = NULL; - msg.dataSize = 0; - - if (!RS_EnqueueMessage(&msg)) { - Com_Printf("Failed to queue stop recording message\n"); - // Fall back to direct stop - RS_StopRecord(clientNum, plyrName, str); - return; - } - - // Mark client as not recording to prevent new messages - cl->isRecording = qfalse; - - Com_Printf("Queued stop recording for client %i\n", clientNum); -} - -/* -==================== -RS_QueueGamestate -==================== -*/ -void RS_QueueGamestate(client_t *client) { - demoQueuedMsg_t msg; - byte *buffer; - msg_t qmsg; - byte msgBuffer[MAX_MSGLEN_BUF]; - int i; - entityState_t nullstate; - const svEntity_t *svEnt; - - if (!demoThread.initialized || !client->isRecording) { - return; - } - - // Initialize message - MSG_Init(&qmsg, msgBuffer, sizeof(msgBuffer)); - - // Write the message just like RS_WriteGamestate does - MSG_WriteLong(&qmsg, client->lastClientCommand); - - // Send any server commands - SV_UpdateServerCommandsToClient(client, &qmsg); - - // Send the gamestate - MSG_WriteByte(&qmsg, svc_gamestate); - MSG_WriteLong(&qmsg, client->reliableSequence); - - // Write the configstrings - for (i = 0; i < MAX_CONFIGSTRINGS; i++) { - if (*sv.configstrings[i] != '\0') { - MSG_WriteByte(&qmsg, svc_configstring); - MSG_WriteShort(&qmsg, i); - if (i == CS_SYSTEMINFO && sv.pure != sv_pure->integer) { - // Special handling for system info - char systemInfo[BIG_INFO_STRING]; - Q_strncpyz(systemInfo, sv.configstrings[i], sizeof(systemInfo)); - Info_SetValueForKey_s(systemInfo, sizeof(systemInfo), "sv_pure", va("%i", sv.pure)); - MSG_WriteBigString(&qmsg, systemInfo); - } else { - MSG_WriteBigString(&qmsg, sv.configstrings[i]); - } - } - } - - // Write the baselines - Com_Memset(&nullstate, 0, sizeof(nullstate)); - for (i = 0; i < MAX_GENTITIES; i++) { - if (!sv.baselineUsed[i]) { - continue; - } - svEnt = &sv.svEntities[i]; - MSG_WriteByte(&qmsg, svc_baseline); - MSG_WriteDeltaEntity(&qmsg, &nullstate, &svEnt->baseline, qtrue); - } - - MSG_WriteByte(&qmsg, svc_EOF); - MSG_WriteLong(&qmsg, client - svs.clients); - MSG_WriteLong(&qmsg, sv.checksumFeed); - MSG_WriteByte(&qmsg, svc_EOF); - - // Copy the message data - buffer = Z_Malloc(qmsg.cursize); - memcpy(buffer, qmsg.data, qmsg.cursize); - - // Setup the message for the queue - msg.type = DEMO_MSG_GAMESTATE; - msg.clientNum = client - svs.clients; - msg.sequence = 0; // Gamestate uses sequence 0 - msg.serverTime = sv.time; - msg.data = buffer; - msg.dataSize = qmsg.cursize; - - // Queue the message - if (!RS_EnqueueMessage(&msg)) { - Com_Printf("Failed to queue gamestate, queue full\n"); - Z_Free(buffer); - return; - } - - // Reset client's demo sequence tracking - client->demoMessageSequence = 1; - client->demoDeltaNum = 0; - - Com_Printf("Queued gamestate for client %i\n", msg.clientNum); -} - -/* -==================== -RS_QueueSnapshot -==================== -*/ -void RS_QueueSnapshot(client_t *client) { - demoQueuedMsg_t msg; - byte *buffer; - msg_t qmsg; - byte msgBuffer[MAX_MSGLEN_BUF]; - - if (!demoThread.initialized || !client->isRecording) { - return; - } - - // Let the existing code prepare the snapshot message - MSG_Init(&qmsg, msgBuffer, sizeof(msgBuffer)); - MSG_Bitstream(&qmsg); - - // Write reliable sequence - MSG_WriteLong(&qmsg, client->reliableSequence); - - // Write server commands - if (client->reliableSequence - client->demoCommandSequence > 0) { - int i; - - // Don't write more than MAX_RELIABLE_COMMANDS - if (client->reliableSequence - client->demoCommandSequence > MAX_RELIABLE_COMMANDS) { - client->demoCommandSequence = client->reliableSequence - MAX_RELIABLE_COMMANDS; - } - - for (i = client->demoCommandSequence + 1; i <= client->reliableSequence; i++) { - int index = i & (MAX_RELIABLE_COMMANDS - 1); - MSG_WriteByte(&qmsg, svc_serverCommand); - MSG_WriteLong(&qmsg, i); - MSG_WriteString(&qmsg, client->reliableCommands[index]); - } - } - - client->demoCommandSequence = client->reliableSequence; - - // Get current snapshot - clientSnapshot_t *frame = &client->frames[client->netchan.outgoingSequence & PACKET_MASK]; - - // Write snapshot header - MSG_WriteByte(&qmsg, svc_snapshot); - MSG_WriteLong(&qmsg, sv.time); - MSG_WriteByte(&qmsg, client->demoDeltaNum); - MSG_WriteByte(&qmsg, 0); // Snap flags - - // Write area info - MSG_WriteByte(&qmsg, frame->areabytes); - MSG_WriteData(&qmsg, frame->areabits, frame->areabytes); - - // Delta compress player state - if (client->demoDeltaNum == 0) { - // First snapshot: no delta - MSG_WriteDeltaPlayerstate(&qmsg, NULL, &frame->ps); - } else { - // Using previous snapshot for delta - this will be handled in RS_WriteSnapshot - // We just prepare the basic message here - MSG_WriteDeltaPlayerstate(&qmsg, NULL, &frame->ps); - } - - // Don't attempt to delta compress entities here - // That's better handled in RS_WriteSnapshot directly - // Just send a placeholder entity message indicating we need to do it there - MSG_WriteByte(&qmsg, svc_EOF); - - // Copy the message data - buffer = Z_Malloc(qmsg.cursize); - memcpy(buffer, qmsg.data, qmsg.cursize); - - // Setup the message for the queue - msg.type = DEMO_MSG_SNAPSHOT; - msg.clientNum = client - svs.clients; - msg.sequence = client->netchan.outgoingSequence; - msg.serverTime = sv.time; - msg.data = buffer; - msg.dataSize = qmsg.cursize; - - // Queue the message - if (!RS_EnqueueMessage(&msg)) { - Com_Printf("Failed to queue snapshot, queue full\n"); - Z_Free(buffer); - return; - } + FS_Write(msg->data, msg->cursize, client->demoFile); } -/* -==================== -RS_EnqueueMessage -==================== -*/ -static qboolean RS_EnqueueMessage(demoQueuedMsg_t *msg) { - qboolean result = qfalse; - - pthread_mutex_lock(&demoThread.queueMutex); - - // Wait if queue is full (with timeout) - while (demoThread.queueCount >= DEMO_QUEUE_SIZE && demoThread.threadActive) { - struct timespec ts; - clock_gettime(CLOCK_REALTIME, &ts); - ts.tv_sec += 1; // 1 second timeout - - // Wait for space to become available - pthread_cond_timedwait(&demoThread.spaceAvailable, &demoThread.queueMutex, &ts); - } - - // Check if we can add to the queue - if (demoThread.queueCount < DEMO_QUEUE_SIZE && demoThread.threadActive) { - // Copy message to the queue - demoThread.queue[demoThread.queueHead] = *msg; - - // Update queue pointers - demoThread.queueHead = (demoThread.queueHead + 1) & DEMO_QUEUE_MASK; - demoThread.queueCount++; - - // Signal that data is available - pthread_cond_signal(&demoThread.dataAvailable); - - result = qtrue; - } - - pthread_mutex_unlock(&demoThread.queueMutex); - return result; -} - -/* -==================== -RS_DequeueMessage -==================== -*/ -static qboolean RS_DequeueMessage(demoQueuedMsg_t *msg) { - qboolean result = qfalse; - - pthread_mutex_lock(&demoThread.queueMutex); - - // Wait for data if queue is empty - while (demoThread.queueCount == 0 && demoThread.threadActive) { - // Wait for data to become available - pthread_cond_wait(&demoThread.dataAvailable, &demoThread.queueMutex); - } - - // Check if we have data - if (demoThread.queueCount > 0) { - // Copy message from the queue - *msg = demoThread.queue[demoThread.queueTail]; - - // Update queue pointers - demoThread.queueTail = (demoThread.queueTail + 1) & DEMO_QUEUE_MASK; - demoThread.queueCount--; - - // Signal that space is available - pthread_cond_signal(&demoThread.spaceAvailable); - - result = qtrue; - } - - pthread_mutex_unlock(&demoThread.queueMutex); - return result; -} - -/* -==================== -RS_ProcessMessage -==================== -*/ -static void RS_ProcessMessage(demoQueuedMsg_t *msg) { - client_t *client; - int len; - - // Get the client - if (msg->clientNum < 0 || msg->clientNum >= sv_maxclients->integer) { - Com_Printf("Invalid client number %d in demo message\n", msg->clientNum); - return; - } - - client = &svs.clients[msg->clientNum]; - - // Process based on message type - switch (msg->type) { - case DEMO_MSG_GAMESTATE: - if (client->demoFile != FS_INVALID_HANDLE) { - // Write sequence (0 for gamestate) - len = LittleLong(0); - FS_Write(&len, 4, client->demoFile); - - // Write message size - len = LittleLong(msg->dataSize); - FS_Write(&len, 4, client->demoFile); - - // Write message data - FS_Write(msg->data, msg->dataSize, client->demoFile); - - Com_Printf("Wrote gamestate for client %i\n", msg->clientNum); - } - break; - - case DEMO_MSG_SNAPSHOT: - if (client->demoFile != FS_INVALID_HANDLE) { - // Let the existing code write the snapshot - // This handles delta compression correctly - RS_WriteSnapshot(client); - - Com_Printf("Wrote snapshot %i for client %i\n", - client->demoMessageSequence - 1, msg->clientNum); - } - break; - - case DEMO_MSG_END_RECORDING: - // Close the demo file - if (client->demoFile != FS_INVALID_HANDLE) { - int len = -1; - - // Write proper EOF markers - TWO -1 values - FS_Write(&len, 4, client->demoFile); - FS_Write(&len, 4, client->demoFile); - - FS_FCloseFile(client->demoFile); - client->demoFile = FS_INVALID_HANDLE; - - Com_Printf("Finished recording demo for client %i\n", msg->clientNum); - } - break; - - default: - Com_Printf("Unknown demo message type %d\n", msg->type); - break; - } -} - -/* -==================== -RS_DemoWriterThread -==================== -*/ -static void* RS_DemoWriterThread(void *arg) { - demoQueuedMsg_t msg; - - Com_Printf("Demo writer thread started\n"); - - while (demoThread.threadActive) { - // Wait for a message - if (RS_DequeueMessage(&msg)) { - // Process the message - RS_ProcessMessage(&msg); - - // Free the message data - if (msg.data) { - Z_Free(msg.data); - } - } - } - - Com_Printf("Demo writer thread stopped\n"); - return NULL; -} - -/* -==================== -RS_GetQueuedMessageCount -==================== -*/ -int RS_GetQueuedMessageCount(void) { - int count; - - pthread_mutex_lock(&demoThread.queueMutex); - count = demoThread.queueCount; - pthread_mutex_unlock(&demoThread.queueMutex); - - return count; -} \ No newline at end of file diff --git a/code/server/server.h b/code/server/server.h index ed9b6056e5..b850c47a89 100644 --- a/code/server/server.h +++ b/code/server/server.h @@ -230,19 +230,20 @@ typedef struct client_s { char tld[3]; // "XX\0" const char *country; -#ifdef DEDICATED - qboolean isLogged; - const char *uuid; +#ifdef ENABLE_RS + qboolean loggedIn; + qboolean demoWaiting; + qboolean awaitingLogin; + qboolean awaitingLogout; qboolean isRecording; + qboolean isSpectating; + const char *uuid; fileHandle_t demoFile; int eventMask; int demoCommandSequence; int demoDeltaNum; int demoMessageSequence; - - msg_t gamestateMsg; - qboolean demoWaiting; #endif } client_t; @@ -513,3 +514,7 @@ void SV_LoadFilters( const char *filename ); const char *SV_RunFilters( const char *userinfo, const netadr_t *addr ); void SV_AddFilter_f( void ); void SV_AddFilterCmd_f( void ); + +#ifdef ENABLE_RS +#include "../recordsystem/recordsystem.h" +#endif \ No newline at end of file diff --git a/code/server/sv_client.c b/code/server/sv_client.c index 66bb93831e..9b0c4dd177 100644 --- a/code/server/sv_client.c +++ b/code/server/sv_client.c @@ -21,11 +21,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ // sv_client.c -- server code for dealing with clients -#ifdef ENABLE_RS -#include "../recordsystem/recordsystem.h" -#else #include "server.h" -#endif static void SV_CloseDownload( client_t *cl ); @@ -871,6 +867,10 @@ void SV_DropClient( client_t *drop, const char *reason ) { return; // already dropped } + #ifdef ENABLE_RS + RS_StopRecord(drop); + #endif + isBot = drop->netchan.remoteAddress.type == NA_BOT; Q_strncpyz( name, drop->name, sizeof( name ) ); // for further DPrintf() because drop->name will be nuked in SV_SetUserinfo() @@ -1173,9 +1173,14 @@ void SV_ClientEnterWorld( client_t *client ) { client->deltaMessage = client->netchan.outgoingSequence - (PACKET_BACKUP + 1); // force delta reset client->lastSnapshotTime = svs.time - 9999; // generate a snapshot immediately - // call the game begin function VM_Call( gvm, 1, GAME_CLIENT_BEGIN, clientNum ); + #ifdef ENABLE_RS + client->isRecording = qfalse; + Com_DPrintf("-----here----\n"); + Com_DPrintf("Logged: %i, Sequence: %i\n", client->loggedIn, client->reliableSequence); + // client->loggedIn = qfalse; + #endif } @@ -2043,7 +2048,7 @@ qboolean SV_ExecuteClientCommand( client_t *cl, const char *s ) { #ifndef DEDICATED if ( !com_cl_running->integer && bFloodProtect && SV_FloodProtect( cl ) ) { #else - if ( bFloodProtect && SV_FloodProtect( cl ) ) { + if ( bFloodProtect && SV_FloodProtect( cl ) ) { #endif // ignore any other text messages from this client but let them keep playing Com_DPrintf( "client text ignored for %s: %s\n", cl->name, Cmd_Argv(0) ); @@ -2054,6 +2059,16 @@ qboolean SV_ExecuteClientCommand( client_t *cl, const char *s ) { Cmd_Args_Sanitize( "\n\r;" ); // handle ';' for OSP else Cmd_Args_Sanitize( "\n\r" ); + #ifdef ENABLE_RS + if ( Q_streq( Cmd_Argv(0), "team") && Q_streq( Cmd_Argv(1), "spectator") && Q_streq( Cmd_Argv(2), "\0")) { + cl->isSpectating = qtrue; + RS_StopRecord(cl); + } + + if ( Q_streq( Cmd_Argv(0), "team") && Q_streq( Cmd_Argv(1), "free") && Q_streq( Cmd_Argv(2), "\0")) { + cl->isSpectating = qfalse; + } + #endif VM_Call( gvm, 1, GAME_CLIENT_COMMAND, cl - svs.clients ); } } @@ -2089,9 +2104,7 @@ static qboolean SV_ClientCommand( client_t *cl, msg_t *msg ) { } #ifdef ENABLE_RS - int clientNum; - clientNum = cl - svs.clients; - if (!RS_CommandGateway(clientNum, cl->name, s)) { + if (!RS_ExecuteClientCommand(cl, s)) { if ( !SV_ExecuteClientCommand( cl, s ) ) { return qfalse; } diff --git a/code/server/sv_game.c b/code/server/sv_game.c index f1d40b9ca8..6c4a17e3d4 100644 --- a/code/server/sv_game.c +++ b/code/server/sv_game.c @@ -21,12 +21,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ // sv_game.c -- interface to the game dll -#ifdef DEDICATED -#include "../recordsystem/recordsystem.h" -#else #include "server.h" -#endif -#include #include "../botlib/botlib.h" diff --git a/code/server/sv_init.c b/code/server/sv_init.c index 6d6da0aab7..93c273924a 100644 --- a/code/server/sv_init.c +++ b/code/server/sv_init.c @@ -20,11 +20,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA =========================================================================== */ -#ifdef ENABLE_RS -#include "../recordsystem/recordsystem.h" -#else #include "server.h" -#endif cvar_t *sv_noReferencedPaks; @@ -836,9 +832,9 @@ void SV_Init( void ) SV_TrackCvarChanges(); SV_InitChallenger(); - #ifdef ENABLE_RS - RS_InitThreadedDemos(); - #endif + // #ifdef ENABLE_RS + // RS_InitThreadedDemos(); + // #endif } @@ -889,9 +885,9 @@ void SV_Shutdown( const char *finalmsg ) { if ( !com_sv_running || !com_sv_running->integer ) { return; } - #ifdef ENABLE_RS - RS_ShutdownThreadedDemos(); - #endif + // #ifdef ENABLE_RS + // RS_ShutdownThreadedDemos(); + // #endif Com_Printf( "----- Server Shutdown (%s) -----\n", finalmsg ); #ifdef USE_IPV6 diff --git a/code/server/sv_main.c b/code/server/sv_main.c index 5b46961078..63713f2608 100644 --- a/code/server/sv_main.c +++ b/code/server/sv_main.c @@ -1117,6 +1117,9 @@ static void SV_CheckTimeouts( void ) { if ( cl->state == CS_ZOMBIE && cl->lastPacketTime - zombiepoint < 0 ) { // using the client id cause the cl->name is empty at this point SV_PrintClientStateChange( cl, CS_FREE ); + #ifdef ENABLE_RS + RS_StopRecord(cl); + #endif cl->state = CS_FREE; // can now be reused continue; } diff --git a/code/server/sv_snapshot.c b/code/server/sv_snapshot.c index 6c28d65c44..c24b454677 100644 --- a/code/server/sv_snapshot.c +++ b/code/server/sv_snapshot.c @@ -20,11 +20,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA =========================================================================== */ -#ifdef ENABLE_RS -#include "../recordsystem/recordsystem.h" -#else #include "server.h" -#endif /* ============================================================================= @@ -704,15 +700,20 @@ void SV_SendMessageToClient(msg_t *msg, client_t *client, qboolean isSnapshot) { // If client is active and we're waiting to start recording if (client->demoWaiting && client->state == CS_ACTIVE) { client->demoWaiting = qfalse; - } - + } // Only record after initial gamestate and when client is active if (!client->demoWaiting && client->state == CS_ACTIVE) { if (isSnapshot) { - RS_QueueSnapshot(client); + RS_WriteSnapshot(client); } } } + + else { + if(!client->isSpectating) { + RS_StartRecord(client); + } + } #endif } From 939c9cca97d27e0654c3ef2470e825ee76c07b46 Mon Sep 17 00:00:00 2001 From: frog Date: Mon, 31 Mar 2025 23:08:48 -0500 Subject: [PATCH 17/46] save serverdemo if logged at finish time --- code/recordsystem/recordsystem.h | 17 +++++++ code/recordsystem/rs_commands.c | 2 + code/recordsystem/rs_common.c | 15 ++++++ code/recordsystem/rs_records.c | 27 +++-------- code/recordsystem/rs_serverdemos.c | 77 +++++++++++++++++++++--------- code/server/server.h | 12 +++-- 6 files changed, 103 insertions(+), 47 deletions(-) diff --git a/code/recordsystem/recordsystem.h b/code/recordsystem/recordsystem.h index a79471aeda..22560c1d44 100644 --- a/code/recordsystem/recordsystem.h +++ b/code/recordsystem/recordsystem.h @@ -13,6 +13,20 @@ qboolean startsWith(const char *string, const char *prefix); qboolean endsWith(const char *string, const char *suffix); +typedef struct { + int clientNum; // Entity/client number + int time; // Timer time (in milliseconds) + char mapname[64]; // Map name + char name[36]; // Player's name + int gametype; // Gametype + int promode; // Promode enabled + int submode; // Sub-mode + int interferenceOff; // Interference off flag + int obEnabled; // Out of bounds enabled + int version; // Version number + char date[16]; // Date string (YYYY-MM-DD) +} timeInfo_t; + /* =============== RS_Gateway @@ -68,6 +82,8 @@ typedef struct { size_t size; } MemoryStruct; +char* formatTime(int ms); + /* =============== RS_HttpGet @@ -130,5 +146,6 @@ RS_WriteSnapshotToDemo void RS_WriteSnapshot(client_t *client); void RS_WriteDemoMessage(client_t *client, msg_t *msg); +void RS_SaveDemo(client_t *client, timeInfo_t *timeInfo); #endif // __RECORDSYSTEM_H__ \ No newline at end of file diff --git a/code/recordsystem/rs_commands.c b/code/recordsystem/rs_commands.c index 7c79da0f15..dd180b0cd7 100644 --- a/code/recordsystem/rs_commands.c +++ b/code/recordsystem/rs_commands.c @@ -109,6 +109,8 @@ static void RS_Login(client_t *client, const char *str) { RS_ProcessAPIResponse(client, response); free(response); client->loggedIn = qtrue; + Com_sprintf(client->uuid, MAX_NAME_LENGTH, "1a2b3c4d-5e6f-7a8b-9c0d-1e2f3a4b5c6d"); + Com_sprintf(client->displayName, MAX_NAME_LENGTH, "Frog"); } else { RS_GameSendServerCommand(clientNum, "print \"^1Failed to connect to server\n\""); } diff --git a/code/recordsystem/rs_common.c b/code/recordsystem/rs_common.c index ed2567571d..f6fb3434b1 100644 --- a/code/recordsystem/rs_common.c +++ b/code/recordsystem/rs_common.c @@ -337,4 +337,19 @@ void RS_ProcessAPIResponse(client_t *client, const char *jsonString) { // Clean up cJSON_Delete(json); +} + +char* formatTime(int ms) { + static char timeString[10]; // Increased to 10 to handle all characters + null + + int minutes = ms / 60000; + ms %= 60000; + + int seconds = ms / 1000; + ms %= 1000; + + // Format as MM.SS.MMM (with leading zeros) + sprintf(timeString, "%02d.%02d.%03d", minutes, seconds, ms); + + return timeString; } \ No newline at end of file diff --git a/code/recordsystem/rs_records.c b/code/recordsystem/rs_records.c index 2990ceffed..2848eb4582 100644 --- a/code/recordsystem/rs_records.c +++ b/code/recordsystem/rs_records.c @@ -7,19 +7,6 @@ ParseTimerStop Parses a timer stop log message into a structured format ==================== */ -typedef struct { - int clientNum; // Entity/client number - int time; // Timer time (in milliseconds) - char mapname[64]; // Map name - char netname[36]; // Player's name - int gametype; // Game type - int promode; // Promode enabled - int submode; // Sub-mode - int interferenceOff; // Interference off flag - int obEnabled; // Out of bounds enabled - int version; // Version number - char date[16]; // Date string (YYYY-MM-DD) -} timeInfo_t; static timeInfo_t* RS_ParseClientTimerStop(const char* logLine) { timeInfo_t* info; @@ -87,12 +74,12 @@ static timeInfo_t* RS_ParseClientTimerStop(const char* logLine) { } // Remove quotes if (token[0] == '"') { - Q_strncpyz(info->netname, token + 1, sizeof(info->netname) - 1); - if (info->netname[strlen(info->netname) - 1] == '"') { - info->netname[strlen(info->netname) - 1] = '\0'; + Q_strncpyz(info->name, token + 1, sizeof(info->name) - 1); + if (info->name[strlen(info->name) - 1] == '"') { + info->name[strlen(info->name) - 1] = '\0'; } } else { - Q_strncpyz(info->netname, token, sizeof(info->netname)); + Q_strncpyz(info->name, token, sizeof(info->name)); } // Parse gametype @@ -201,14 +188,14 @@ static void RS_SendTime(const char *cmdString) { void RS_Gateway(const char *s) { timeInfo_t* timeInfo = RS_ParseClientTimerStop(s); if (timeInfo && Cvar_VariableIntegerValue("sv_cheats") == 0) { - client_t *client = &svs.clients[timeInfo->clientNum] + client_t *client = &svs.clients[timeInfo->clientNum]; if (client->loggedIn) { - RS_EndAndRenameDemo(client, timeInfo); + RS_SaveDemo(client, timeInfo); Sys_CreateThread(RS_SendTime, s); } else RS_GameSendServerCommand(timeInfo->clientNum, "print \"^7You are not logged in^5.\n\""); - RS_RestartDemoRecord(); + RS_StopRecord(client); } } \ No newline at end of file diff --git a/code/recordsystem/rs_serverdemos.c b/code/recordsystem/rs_serverdemos.c index ef041c874b..639f1333b2 100644 --- a/code/recordsystem/rs_serverdemos.c +++ b/code/recordsystem/rs_serverdemos.c @@ -41,6 +41,7 @@ void RS_StartRecord(client_t *client) { return; } + Q_strncpyz(client->demoName, demoName, sizeof(demoName)); // Set client's demo flags client->isRecording = qtrue; client->demoWaiting = qtrue; @@ -56,29 +57,59 @@ RS_StopRecord stop recording a demo ==================== */ -void RS_StopRecord(client_t *client, timeInfo_t timeInfo) { - if (rename) { - char finalName[MAX_OSPATH]; - } +void RS_SaveDemo(client_t *client, timeInfo_t *timeInfo) { + int clientNum = client - svs.clients; + char finalName[MAX_OSPATH]; - if (timeInfo->gametype == 1) { // run mode - Com_sprintf( finalName, sizeof( finalName ), "%s[df.%s%s]%i(%s)(%s).dm_68", \ - timeInfo->mapName, \ - timeInfo->promode ? "cpm" : "vq3", \ - timeInfo->time, \ - client->uuid, \ - client->displayName); - } - else - Com_sprintf( finalName, sizeof( finalName ), "%s[df.%s.%i]%i(%s)(%s).dm_68", \ - timeInfo->mapName, \ - timeInfo->promode ? "cpm" : "vq3", \ - timeInfo->submode \ - timeInfo->time, \ - client->uuid, \ - client->displayName); - - FS_Rename( tempName, finalName ); + if (!client->isRecording) { + Com_Printf("Client %i is not being recorded\n", clientNum); + return; + } + + if (client->demoFile != FS_INVALID_HANDLE) { + int len; + + // Write proper EOF markers - TWO -1 values + len = -1; + FS_Write(&len, 4, client->demoFile); + FS_Write(&len, 4, client->demoFile); + + FS_FCloseFile(client->demoFile); + client->demoFile = FS_INVALID_HANDLE; + Com_Printf("Stopped recording client %i\n", clientNum); + } + + if (timeInfo->gametype == 1) { // run mode + Com_sprintf( finalName, sizeof( finalName ), "demos/%s[df.%s]%s[%s][%s].dm_68", \ + timeInfo->mapname, \ + timeInfo->promode ? "cpm" : "vq3", \ + formatTime(timeInfo->time), \ + client->displayName, \ + client->uuid); + } + else { + Com_sprintf( finalName, sizeof( finalName ), "demos/%s[df.%s.%i]%s[%s][%s].dm_68", \ + timeInfo->mapname, \ + timeInfo->promode ? "cpm" : "vq3", \ + timeInfo->submode, \ + formatTime(timeInfo->time), \ + client->displayName, \ + client->uuid); + } + + FS_Rename( client->demoName, finalName ); + Com_Printf("Saved demo: %s\n", finalName); + client->demoFile = FS_INVALID_HANDLE; +} + +/* +==================== +RS_StopRecord + +stop recording a demo +==================== +*/ +void RS_StopRecord(client_t *client) { int clientNum = client - svs.clients; if (!client->isRecording) { @@ -206,7 +237,9 @@ void RS_WriteGamestate(client_t *client) { FS_Write(&len, 4, client->demoFile); len = LittleLong(msg.cursize); + FS_Write(&len, 4, client->demoFile); + FS_Write(msg.data, msg.cursize, client->demoFile); } diff --git a/code/server/server.h b/code/server/server.h index b850c47a89..901933e554 100644 --- a/code/server/server.h +++ b/code/server/server.h @@ -237,13 +237,15 @@ typedef struct client_s { qboolean awaitingLogout; qboolean isRecording; qboolean isSpectating; - const char *uuid; + char uuid[MAX_NAME_LENGTH]; + char displayName[MAX_NAME_LENGTH]; fileHandle_t demoFile; + char demoName[MAX_OSPATH]; - int eventMask; - int demoCommandSequence; - int demoDeltaNum; - int demoMessageSequence; + int eventMask; + int demoCommandSequence; + int demoDeltaNum; + int demoMessageSequence; #endif } client_t; From c309d86789d7a9f96607e2323f76f794b3c0f8df Mon Sep 17 00:00:00 2001 From: frog Date: Wed, 2 Apr 2025 00:09:10 -0500 Subject: [PATCH 18/46] save demo x frames after finish or on kill/spectate/disconnect --- code/recordsystem/recordsystem.h | 16 +--------------- code/recordsystem/rs_common.c | 7 ++++++- code/recordsystem/rs_records.c | 12 +++++++----- code/recordsystem/rs_serverdemos.c | 27 +++++++++++++++++++++++++-- code/server/server.h | 17 +++++++++++++++++ code/server/sv_client.c | 5 +++++ code/server/sv_main.c | 2 +- code/server/sv_snapshot.c | 6 ++++++ 8 files changed, 68 insertions(+), 24 deletions(-) diff --git a/code/recordsystem/recordsystem.h b/code/recordsystem/recordsystem.h index 22560c1d44..b8005b2ac4 100644 --- a/code/recordsystem/recordsystem.h +++ b/code/recordsystem/recordsystem.h @@ -13,20 +13,6 @@ qboolean startsWith(const char *string, const char *prefix); qboolean endsWith(const char *string, const char *suffix); -typedef struct { - int clientNum; // Entity/client number - int time; // Timer time (in milliseconds) - char mapname[64]; // Map name - char name[36]; // Player's name - int gametype; // Gametype - int promode; // Promode enabled - int submode; // Sub-mode - int interferenceOff; // Interference off flag - int obEnabled; // Out of bounds enabled - int version; // Version number - char date[16]; // Date string (YYYY-MM-DD) -} timeInfo_t; - /* =============== RS_Gateway @@ -146,6 +132,6 @@ RS_WriteSnapshotToDemo void RS_WriteSnapshot(client_t *client); void RS_WriteDemoMessage(client_t *client, msg_t *msg); -void RS_SaveDemo(client_t *client, timeInfo_t *timeInfo); +void RS_SaveDemo(client_t *client); #endif // __RECORDSYSTEM_H__ \ No newline at end of file diff --git a/code/recordsystem/rs_common.c b/code/recordsystem/rs_common.c index f6fb3434b1..a307af8e6c 100644 --- a/code/recordsystem/rs_common.c +++ b/code/recordsystem/rs_common.c @@ -352,4 +352,9 @@ char* formatTime(int ms) { sprintf(timeString, "%02d.%02d.%03d", minutes, seconds, ms); return timeString; -} \ No newline at end of file +} + +// dfState_t RS_GetDFState(client_t *client) { +// char stats[MAX_STATS]; +// stats = client->ps.stats; +// } \ No newline at end of file diff --git a/code/recordsystem/rs_records.c b/code/recordsystem/rs_records.c index 2848eb4582..47b9a1d023 100644 --- a/code/recordsystem/rs_records.c +++ b/code/recordsystem/rs_records.c @@ -186,16 +186,18 @@ static void RS_SendTime(const char *cmdString) { } void RS_Gateway(const char *s) { - timeInfo_t* timeInfo = RS_ParseClientTimerStop(s); + timeInfo_t *timeInfo = RS_ParseClientTimerStop(s); if (timeInfo && Cvar_VariableIntegerValue("sv_cheats") == 0) { client_t *client = &svs.clients[timeInfo->clientNum]; if (client->loggedIn) { - RS_SaveDemo(client, timeInfo); + client->awaitingDemoSave = qtrue; + client->timerStopTime = svs.time; + client->timerStopInfo = timeInfo; Sys_CreateThread(RS_SendTime, s); } - else + else { RS_GameSendServerCommand(timeInfo->clientNum, "print \"^7You are not logged in^5.\n\""); - - RS_StopRecord(client); + RS_StopRecord(client); + } } } \ No newline at end of file diff --git a/code/recordsystem/rs_serverdemos.c b/code/recordsystem/rs_serverdemos.c index 639f1333b2..42e2c37c1d 100644 --- a/code/recordsystem/rs_serverdemos.c +++ b/code/recordsystem/rs_serverdemos.c @@ -20,7 +20,6 @@ void RS_StartRecord(client_t *client) { int clientNum = client - svs.clients; if (client->state != CS_ACTIVE) { - Com_Printf("Client must be active\n"); return; } @@ -57,10 +56,14 @@ RS_StopRecord stop recording a demo ==================== */ -void RS_SaveDemo(client_t *client, timeInfo_t *timeInfo) { +void RS_SaveDemo(client_t *client) { int clientNum = client - svs.clients; char finalName[MAX_OSPATH]; + timeInfo_t *timeInfo = client->timerStopInfo; + clientSnapshot_t *frame = &client->frames[client->netchan.outgoingSequence & PACKET_MASK]; + Com_DPrintf("Saving demo for frame: %i\n", frame->frameNum); + Com_DPrintf("Stats: %s\n", frame->ps.stats); if (!client->isRecording) { Com_Printf("Client %i is not being recorded\n", clientNum); return; @@ -97,9 +100,26 @@ void RS_SaveDemo(client_t *client, timeInfo_t *timeInfo) { client->uuid); } + if (client->demoFile != FS_INVALID_HANDLE) { + int len; + + // Write proper EOF markers - TWO -1 values + len = -1; + FS_Write(&len, 4, client->demoFile); + FS_Write(&len, 4, client->demoFile); + + FS_FCloseFile(client->demoFile); + client->demoFile = FS_INVALID_HANDLE; + Com_Printf("Stopped recording client %i\n", clientNum); + } + FS_Rename( client->demoName, finalName ); Com_Printf("Saved demo: %s\n", finalName); + client->awaitingDemoSave = qfalse; client->demoFile = FS_INVALID_HANDLE; + client->isRecording = qfalse; + client->demoWaiting = qfalse; + client->demoDeltaNum = 0; } /* @@ -110,6 +130,8 @@ stop recording a demo ==================== */ void RS_StopRecord(client_t *client) { + if (client->awaitingDemoSave) + return RS_SaveDemo(client); int clientNum = client - svs.clients; if (!client->isRecording) { @@ -281,6 +303,7 @@ void RS_WriteSnapshot(client_t *client) { // Get current snapshot clientSnapshot_t *frame = &client->frames[client->netchan.outgoingSequence & PACKET_MASK]; + // Com_DPrintf("Writing snapshot to client: %i\n", frame->frameNum); // Initialize message buffer MSG_Init(&msg, bufData, sizeof(bufData)); diff --git a/code/server/server.h b/code/server/server.h index 901933e554..724c39e4a3 100644 --- a/code/server/server.h +++ b/code/server/server.h @@ -157,6 +157,20 @@ typedef enum { GSA_ACKED // gamestate acknowledged, no retansmissions needed } gameStateAck_t; +typedef struct { + int clientNum; // Entity/client number + int time; // Timer time (in milliseconds) + char mapname[64]; // Map name + char name[36]; // Player's name + int gametype; // Gametype + int promode; // Promode enabled + int submode; // Sub-mode + int interferenceOff; // Interference off flag + int obEnabled; // Out of bounds enabled + int version; // Version number + char date[16]; // Date string (YYYY-MM-DD) +} timeInfo_t; + typedef struct client_s { clientState_t state; char userinfo[MAX_INFO_STRING]; // name, etc @@ -237,6 +251,9 @@ typedef struct client_s { qboolean awaitingLogout; qboolean isRecording; qboolean isSpectating; + qboolean awaitingDemoSave; + int timerStopTime; + timeInfo_t *timerStopInfo; char uuid[MAX_NAME_LENGTH]; char displayName[MAX_NAME_LENGTH]; fileHandle_t demoFile; diff --git a/code/server/sv_client.c b/code/server/sv_client.c index 9b0c4dd177..9d3413e7c6 100644 --- a/code/server/sv_client.c +++ b/code/server/sv_client.c @@ -2068,7 +2068,12 @@ qboolean SV_ExecuteClientCommand( client_t *cl, const char *s ) { if ( Q_streq( Cmd_Argv(0), "team") && Q_streq( Cmd_Argv(1), "free") && Q_streq( Cmd_Argv(2), "\0")) { cl->isSpectating = qfalse; } + + if ( Q_streq( Cmd_Argv(0), "kill") && Q_streq( Cmd_Argv(1), "\0")) { + RS_StopRecord(cl); + } #endif + VM_Call( gvm, 1, GAME_CLIENT_COMMAND, cl - svs.clients ); } } diff --git a/code/server/sv_main.c b/code/server/sv_main.c index 63713f2608..6a1663f425 100644 --- a/code/server/sv_main.c +++ b/code/server/sv_main.c @@ -1388,7 +1388,7 @@ void SV_Frame( int msec ) { sv.timeResidual -= frameMsec; svs.time += frameMsec; sv.time += frameMsec; - + // let everything in the world think and move VM_Call( gvm, 1, GAME_RUN_FRAME, sv.time ); } diff --git a/code/server/sv_snapshot.c b/code/server/sv_snapshot.c index c24b454677..c4ccfb535e 100644 --- a/code/server/sv_snapshot.c +++ b/code/server/sv_snapshot.c @@ -695,6 +695,11 @@ void SV_SendMessageToClient(msg_t *msg, client_t *client, qboolean isSnapshot) { SV_Netchan_Transmit(client, msg); #ifdef ENABLE_RS + int frameMsec = 1000 / sv_fps->integer * com_timescale->value; + if (client->awaitingDemoSave) + if (svs.time - client->timerStopTime > 500*frameMsec) + RS_SaveDemo(client); // it's been max frames from timer stop + //If we're recording a demo for this client if (client->isRecording) { // If client is active and we're waiting to start recording @@ -714,6 +719,7 @@ void SV_SendMessageToClient(msg_t *msg, client_t *client, qboolean isSnapshot) { RS_StartRecord(client); } } + #endif } From dfa2982f45c6271bfd42f6e87672f62b781b000c Mon Sep 17 00:00:00 2001 From: frog Date: Sat, 5 Apr 2025 16:44:46 -0500 Subject: [PATCH 19/46] maturize api responses, logout is local --- code/recordsystem/recordsystem.h | 14 +++-- code/recordsystem/rs_commands.c | 89 +++++++++----------------- code/recordsystem/rs_common.c | 104 +++++++++++++++++++++++-------- code/recordsystem/rs_records.c | 13 ++-- code/server/server.h | 3 +- 5 files changed, 127 insertions(+), 96 deletions(-) diff --git a/code/recordsystem/recordsystem.h b/code/recordsystem/recordsystem.h index b8005b2ac4..0c8a100eea 100644 --- a/code/recordsystem/recordsystem.h +++ b/code/recordsystem/recordsystem.h @@ -5,14 +5,19 @@ #include #include -// #include "../qcommon/q_shared.h" -// #include "../qcommon/qcommon.h" -// #include "../server/server.h" // String utility functions qboolean startsWith(const char *string, const char *prefix); qboolean endsWith(const char *string, const char *suffix); +typedef struct { + int success; + int targetClientNum; + char *message; + char displayName[MAX_NAME_LENGTH]; + char uuid[UUID_LENGTH]; +} apiResponse_t; + /* =============== RS_Gateway @@ -102,8 +107,9 @@ The returned string must be freed by the caller */ char* RS_UrlEncode(const char *str); +apiResponse_t* RS_ParseAPIResponse(const char* jsonString); -void RS_ProcessAPIResponse(client_t *client, const char *jsonString); +void RS_PrintAPIResponse(apiResponse_t *response, qboolean mentionClient); void RS_StartRecord(client_t *client); diff --git a/code/recordsystem/rs_commands.c b/code/recordsystem/rs_commands.c index dd180b0cd7..258d34abfa 100644 --- a/code/recordsystem/rs_commands.c +++ b/code/recordsystem/rs_commands.c @@ -2,7 +2,7 @@ #include "cJSON.h" static void RS_Top(client_t *client, const char *str) { - char *response; + apiResponse_t *response; char *encoded_str; char *encoded_map; char url[512]; @@ -32,10 +32,10 @@ static void RS_Top(client_t *client, const char *str) { free(encoded_map); // Make the HTTP request - response = RS_HttpGet(url); + response = RS_ParseAPIResponse(RS_HttpGet(url)); if (response) { - RS_ProcessAPIResponse(client, response); + RS_PrintAPIResponse(response, qfalse); free(response); // Free the response } else { RS_GameSendServerCommand(clientNum, "print \"^1Failed to get response\n\""); @@ -43,7 +43,7 @@ static void RS_Top(client_t *client, const char *str) { } static void RS_Recent(client_t *client, const char *str) { - char *response; + apiResponse_t *response; char *encoded_str; char url[512]; int clientNum = client - svs.clients; @@ -60,10 +60,10 @@ static void RS_Recent(client_t *client, const char *str) { free(encoded_str); // Free encoded string when done // Make the HTTP request - response = RS_HttpGet(url); + response = RS_ParseAPIResponse(RS_HttpGet(url)); if (response) { - RS_ProcessAPIResponse(client, response); + RS_PrintAPIResponse(response, qfalse); free(response); // Free the response } else { RS_GameSendServerCommand(clientNum, "print \"^1Failed to get response\n\""); @@ -71,15 +71,17 @@ static void RS_Recent(client_t *client, const char *str) { } static void RS_Login(client_t *client, const char *str) { - char *response; + char *responseString; char *jsonString; cJSON *json; int clientNum = client - svs.clients; + apiResponse_t *response; // Create a JSON object json = cJSON_CreateObject(); if (!json) { - RS_GameSendServerCommand(clientNum, "print \"^1Error creating JSON object\n\""); + RS_GameSendServerCommand(clientNum, "print \"^1Internal engine error, contact server admin.\n\""); + Com_DPrintf("RS_ERROR: Couldn't create JSON Object for string: %s\n", str ); return; } @@ -93,76 +95,47 @@ static void RS_Login(client_t *client, const char *str) { cJSON_Delete(json); // Free the JSON object if (!jsonString) { - RS_GameSendServerCommand(clientNum, "print \"^1Error serializing JSON\n\""); + RS_GameSendServerCommand(clientNum, "print \"^1Internal engine error, contact server admin.\n\""); + Com_DPrintf("RS_ERROR: Couldn't convert JSON object to string for string: %s\n", str ); return; } client->awaitingLogin = qtrue; // Make the HTTP request - response = RS_HttpPost("http://localhost:8000/api/commands/login", + responseString = RS_HttpPost("http://localhost:8000/api/commands/login", "application/json", jsonString); - // Free the JSON string free(jsonString); + response = RS_ParseAPIResponse(responseString); + if (response) { - RS_ProcessAPIResponse(client, response); - free(response); - client->loggedIn = qtrue; - Com_sprintf(client->uuid, MAX_NAME_LENGTH, "1a2b3c4d-5e6f-7a8b-9c0d-1e2f3a4b5c6d"); - Com_sprintf(client->displayName, MAX_NAME_LENGTH, "Frog"); + if (response->success && strlen(response->uuid) > 0 && strlen(response->displayName) > 0) { + client->loggedIn = client->awaitingLogin; // Make sure client player is the same one that was awaiting login + strncpy(client->uuid, response->uuid, UUID_LENGTH); + strncpy(client->displayName, response->displayName, MAX_NAME_LENGTH); + } + RS_PrintAPIResponse(response, qtrue); } else { - RS_GameSendServerCommand(clientNum, "print \"^1Failed to connect to server\n\""); + RS_GameSendServerCommand(clientNum, "print \"^1Bad response from server, contact defrag.racing admins\n\""); + Com_DPrintf("RS_ERROR: Couldn't parse response json: %s\n", jsonString ); } - + client->awaitingLogin = qfalse; + free(response); } static void RS_Logout(client_t *client, const char *str) { - char *response; - char *jsonString; - cJSON *json; int clientNum = client - svs.clients; - client->loggedIn = qfalse; // Log them out locally, don't wait for server. - RS_GameSendServerCommand(clientNum, va("print \"%s^5, ^7you are now logged out^5.^7\n\"", client->name)); - - // Create a JSON object - json = cJSON_CreateObject(); - if (!json) { - Com_Printf("^1Error creating JSON object\n"); - return; - } - - // Add client number and command string to the JSON object - cJSON_AddNumberToObject(json, "clientNum", clientNum); - cJSON_AddStringToObject(json, "cmdString", str); - cJSON_AddStringToObject(json, "plyrName", client->name); - - // Convert JSON object to string - jsonString = cJSON_Print(json); - cJSON_Delete(json); // Free the JSON object - - if (!jsonString) { - RS_GameSendServerCommand(clientNum, "print \"^1Error serializing JSON\n\""); + if (client->loggedIn == qfalse) { + RS_GameSendServerCommand(clientNum, va("print \"%s^5, ^7You are not logged in^5.\n\"", client->name)); return; } - - client->awaitingLogout = qtrue; // Let game know that client is waiting for remote logout - // Make the HTTP request - response = RS_HttpPost("http://localhost:8000/api/commands/logout", - "application/json", jsonString); - - // Free the JSON string - free(jsonString); - - if (response) { - RS_ProcessAPIResponse(client, response); - free(response); - client->awaitingLogout = qfalse; - } else { - RS_GameSendServerCommand(clientNum, "print \"^1Failed to connect to server\n\""); - } + client->loggedIn = qfalse; // Log them out locally, don't wait for server. + strcpy(client->uuid, ""); + strcpy(client->displayName, ""); + RS_GameSendServerCommand(clientNum, va("print \"%s^5, ^7You are now logged out^5.\n\"", client->name)); } typedef struct { diff --git a/code/recordsystem/rs_common.c b/code/recordsystem/rs_common.c index a307af8e6c..3b1c68574e 100644 --- a/code/recordsystem/rs_common.c +++ b/code/recordsystem/rs_common.c @@ -301,42 +301,92 @@ char* RS_HttpPost(const char *url, const char *contentType, const char *payload) }; -void RS_ProcessAPIResponse(client_t *client, const char *jsonString) { - cJSON *json; - cJSON *targetClientObj; - cJSON *messageObj; - int targetClient = -1; - const char *message = NULL; - - // Parse the JSON - json = cJSON_Parse(jsonString); +#include +#include +#include + +// Assuming you have a JSON parsing library like cJSON +#include "cJSON.h" + +// Function to parse JSON response into apiResponse_t structure +apiResponse_t *RS_ParseAPIResponse(const char* jsonString) { + // Allocate memory for the response structure + apiResponse_t *response = (apiResponse_t*)malloc(sizeof(apiResponse_t)); + if (!response) { + return NULL; // Memory allocation failed + } + + // Initialize with default values + response->success = 0; + response->targetClientNum = 0; + response->message = NULL; + memset(response->displayName, 0, MAX_NAME_LENGTH); + memset(response->uuid, 0, UUID_LENGTH); + + // Parse JSON string + cJSON* json = cJSON_Parse(jsonString); if (!json) { - Com_Printf("RS: Failed to parse JSON: %s\n", cJSON_GetErrorPtr()); - return; + free(response); + return NULL; // JSON parsing failed } - - // Extract targetClient field - targetClientObj = cJSON_GetObjectItem(json, "targetClient"); - if (targetClientObj && cJSON_IsNumber(targetClientObj)) { - targetClient = targetClientObj->valueint; + + // Extract success field (integer) + cJSON* successField = cJSON_GetObjectItem(json, "success"); + if (cJSON_IsNumber(successField)) { + response->success = successField->valueint; } - // Extract message field - messageObj = cJSON_GetObjectItem(json, "message"); - if (messageObj && cJSON_IsString(messageObj)) { - message = messageObj->valuestring; + // Extract targetClientNumfield (integer) + cJSON* targetClientNumField = cJSON_GetObjectItem(json, "targetClientNum"); + if (cJSON_IsNumber(targetClientNumField)) { + response->targetClientNum = targetClientNumField->valueint; } - // Send the message to the target client - if (message && *message) { - // Send the message directly, preserving any newlines - RS_GameSendServerCommand(targetClient, va("print \"^5(^7defrag^5.^7racing^5)^7 %s\"", message)); - } else { - Com_Printf("RS: Missing message in API response\n"); + // Extract message field (string array) + cJSON* messageField = cJSON_GetObjectItem(json, "message"); + if (cJSON_IsString(messageField) && messageField->valuestring && strlen(messageField->valuestring) > 0) { + response->message = strdup(messageField->valuestring); } - // Clean up + // Extract displayName field (string array) + cJSON* displayNameField = cJSON_GetObjectItem(json, "displayName"); + if (cJSON_IsString(displayNameField) && displayNameField->valuestring) { + strncpy(response->displayName, displayNameField->valuestring, MAX_NAME_LENGTH - 1); + response->displayName[MAX_NAME_LENGTH - 1] = '\0'; // Ensure null termination + } + + // Extract uuid field (string array) + cJSON* uuidField = cJSON_GetObjectItem(json, "uuid"); + if (cJSON_IsString(uuidField) && uuidField->valuestring) { + strncpy(response->uuid, uuidField->valuestring, UUID_LENGTH - 1); + response->uuid[UUID_LENGTH - 1] = '\0'; // Ensure null termination + } + + // Clean up JSON object cJSON_Delete(json); + + return response; +} + +void RS_PrintAPIResponse(apiResponse_t *response, qboolean mentionClient) { + const char *finalMessage=""; + const char *mentionPrefix=""; + client_t *targetClient; + + if (response->targetClientNum < -1) { + Com_DPrintf("%s", response->message); + return; + } + + if (mentionClient && response->targetClientNum >= 0) { + targetClient = &svs.clients[response->targetClientNum]; + strlen(targetClient->name) > 0 ? mentionPrefix = va("%s^5, ", targetClient->name) : ""; + } + + if (response->message != NULL) { + finalMessage = va("%s%s", mentionPrefix, response->message); + RS_GameSendServerCommand(response->targetClientNum, va("print \"^5(^7defrag^5.^7racing^5)^7 %s\n\"", finalMessage)); + } } char* formatTime(int ms) { diff --git a/code/recordsystem/rs_records.c b/code/recordsystem/rs_records.c index 47b9a1d023..21e84475ee 100644 --- a/code/recordsystem/rs_records.c +++ b/code/recordsystem/rs_records.c @@ -150,12 +150,11 @@ Sends a time record to the API server =============== */ static void RS_SendTime(const char *cmdString) { - char *response; + apiResponse_t *response; char *jsonString; cJSON *json; timeInfo_t *timeInfo = RS_ParseClientTimerStop(cmdString); - client_t *client = &svs.clients[timeInfo->clientNum]; // Create a JSON object for the request json = cJSON_CreateObject(); @@ -165,20 +164,22 @@ static void RS_SendTime(const char *cmdString) { // Add properties to the JSON object cJSON_AddStringToObject(json, "cmdString", cmdString); + cJSON_AddStringToObject(json, "uuid", svs.clients[timeInfo->clientNum].uuid); // Convert JSON object to string jsonString = cJSON_Print(json); cJSON_Delete(json); // Free the JSON object + Com_DPrintf("json payload: %s\n", jsonString); // Make the HTTP request - response = RS_HttpPost("http://localhost:8000/api/records", - "application/json", jsonString); + response = RS_ParseAPIResponse(RS_HttpPost("http://localhost:8000/api/records", + "application/json", jsonString)); // Free the JSON string free(jsonString); if (response) { - RS_ProcessAPIResponse(client, response); + RS_PrintAPIResponse(response, qtrue); free(response); } else { RS_GameSendServerCommand(timeInfo->clientNum, "print \"^1Failed to connect to record server\n\""); @@ -200,4 +201,4 @@ void RS_Gateway(const char *s) { RS_StopRecord(client); } } -} \ No newline at end of file +} diff --git a/code/server/server.h b/code/server/server.h index 724c39e4a3..abf59284dd 100644 --- a/code/server/server.h +++ b/code/server/server.h @@ -33,6 +33,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA // GAME BOTH REFERENCE !!! #define MAX_ENT_CLUSTERS 16 +#define UUID_LENGTH 37 typedef struct svEntity_s { struct worldSector_s *worldSector; @@ -254,7 +255,7 @@ typedef struct client_s { qboolean awaitingDemoSave; int timerStopTime; timeInfo_t *timerStopInfo; - char uuid[MAX_NAME_LENGTH]; + char uuid[UUID_LENGTH]; char displayName[MAX_NAME_LENGTH]; fileHandle_t demoFile; char demoName[MAX_OSPATH]; From ca1091548f55bac07488f14b0df6cdd4ad1662ea Mon Sep 17 00:00:00 2001 From: frog Date: Sat, 5 Apr 2025 19:50:30 -0500 Subject: [PATCH 20/46] add rs_apiHost cvar --- code/recordsystem/recordsystem.h | 2 ++ code/recordsystem/rs_common.c | 2 +- code/recordsystem/rs_serverdemos.c | 3 --- code/server/sv_init.c | 5 +++++ code/server/sv_main.c | 4 ++++ 5 files changed, 12 insertions(+), 4 deletions(-) diff --git a/code/recordsystem/recordsystem.h b/code/recordsystem/recordsystem.h index 0c8a100eea..289d62ab5a 100644 --- a/code/recordsystem/recordsystem.h +++ b/code/recordsystem/recordsystem.h @@ -6,6 +6,8 @@ #include #include +extern cvar_t *rs_apiHost; + // String utility functions qboolean startsWith(const char *string, const char *prefix); qboolean endsWith(const char *string, const char *suffix); diff --git a/code/recordsystem/rs_common.c b/code/recordsystem/rs_common.c index 3b1c68574e..c8fb0acdf6 100644 --- a/code/recordsystem/rs_common.c +++ b/code/recordsystem/rs_common.c @@ -380,7 +380,7 @@ void RS_PrintAPIResponse(apiResponse_t *response, qboolean mentionClient) { if (mentionClient && response->targetClientNum >= 0) { targetClient = &svs.clients[response->targetClientNum]; - strlen(targetClient->name) > 0 ? mentionPrefix = va("%s^5, ", targetClient->name) : ""; + strlen(targetClient->name) > 0 ? mentionPrefix = va("%s", targetClient->name) : ""; } if (response->message != NULL) { diff --git a/code/recordsystem/rs_serverdemos.c b/code/recordsystem/rs_serverdemos.c index 42e2c37c1d..5922da89bd 100644 --- a/code/recordsystem/rs_serverdemos.c +++ b/code/recordsystem/rs_serverdemos.c @@ -61,9 +61,6 @@ void RS_SaveDemo(client_t *client) { char finalName[MAX_OSPATH]; timeInfo_t *timeInfo = client->timerStopInfo; - clientSnapshot_t *frame = &client->frames[client->netchan.outgoingSequence & PACKET_MASK]; - Com_DPrintf("Saving demo for frame: %i\n", frame->frameNum); - Com_DPrintf("Stats: %s\n", frame->ps.stats); if (!client->isRecording) { Com_Printf("Client %i is not being recorded\n", clientNum); return; diff --git a/code/server/sv_init.c b/code/server/sv_init.c index 93c273924a..6e04953b94 100644 --- a/code/server/sv_init.c +++ b/code/server/sv_init.c @@ -805,6 +805,11 @@ void SV_Init( void ) Cvar_SetDescription( sv_banFile, "Name of the file that is used for storing the server bans." ); #endif +#ifdef ENABLE_RS + rs_apiHost = Cvar_Get ("rs_apiHost", "api.q3df.run", CVAR_TEMP ); + Cvar_SetDescription( sv_rconPassword, "Fully-qualified domain name of the recordsystem API" ); +#endif + sv_levelTimeReset = Cvar_Get( "sv_levelTimeReset", "1", CVAR_ARCHIVE_ND ); Cvar_SetDescription( sv_levelTimeReset, "Whether or not to reset leveltime after new map loads." ); diff --git a/code/server/sv_main.c b/code/server/sv_main.c index 6a1663f425..e063515bb8 100644 --- a/code/server/sv_main.c +++ b/code/server/sv_main.c @@ -63,6 +63,10 @@ serverBan_t serverBans[SERVER_MAXBANS]; int serverBansCount = 0; #endif +#ifdef ENABLE_RS +cvar_t *rs_apiHost; +#endif + /* ============================================================================= From 8e742d663e8c43908d5a043585c85b733ab884ed Mon Sep 17 00:00:00 2001 From: frog Date: Sat, 5 Apr 2025 22:14:45 -0500 Subject: [PATCH 21/46] add pthreads lib --- Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Makefile b/Makefile index b542979e8a..210f2b393a 100644 --- a/Makefile +++ b/Makefile @@ -439,6 +439,8 @@ ifdef MINGW LDFLAGS += -Wl,--gc-sections -fvisibility=hidden LDFLAGS += -lwsock32 -lgdi32 -lwinmm -lole32 -lws2_32 -lpsapi -lcomctl32 LDFLAGS += -flto + LDFLAGS += -pthreads + CLIENT_LDFLAGS=$(LDFLAGS) From 323329efae9c31098c6bfd41406f9c610c083ea2 Mon Sep 17 00:00:00 2001 From: frog Date: Sat, 5 Apr 2025 22:16:22 -0500 Subject: [PATCH 22/46] add pthreads lib --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 210f2b393a..405c5719e9 100644 --- a/Makefile +++ b/Makefile @@ -439,7 +439,7 @@ ifdef MINGW LDFLAGS += -Wl,--gc-sections -fvisibility=hidden LDFLAGS += -lwsock32 -lgdi32 -lwinmm -lole32 -lws2_32 -lpsapi -lcomctl32 LDFLAGS += -flto - LDFLAGS += -pthreads + LDFLAGS += -lpthread CLIENT_LDFLAGS=$(LDFLAGS) From 7a96a9d4c076c3f279be6ad0b179e5d1d34f4ad7 Mon Sep 17 00:00:00 2001 From: frog Date: Sat, 5 Apr 2025 22:18:39 -0500 Subject: [PATCH 23/46] add pthreads lib --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 405c5719e9..ba65343848 100644 --- a/Makefile +++ b/Makefile @@ -1392,7 +1392,7 @@ endif $(B)/$(TARGET_SERVER): $(Q3DOBJ) $(echo_cmd) "LD $@" - $(Q)$(CC) -o $@ $(Q3DOBJ) $(LDFLAGS) -lcurl + $(Q)$(CC) -o $@ $(Q3DOBJ) $(LDFLAGS) -lcurl -lpthread ############################################################################# ## CLIENT/SERVER RULES From e6a609ee476e07e2aff3b34b061e7373e27bfd59 Mon Sep 17 00:00:00 2001 From: frog Date: Sat, 5 Apr 2025 23:33:25 -0500 Subject: [PATCH 24/46] hardcode api url --- code/recordsystem/recordsystem.h | 2 -- code/recordsystem/rs_commands.c | 11 ++++++----- code/recordsystem/rs_records.c | 7 +++++-- code/server/sv_init.c | 5 ----- code/server/sv_main.c | 4 ---- 5 files changed, 11 insertions(+), 18 deletions(-) diff --git a/code/recordsystem/recordsystem.h b/code/recordsystem/recordsystem.h index 289d62ab5a..0c8a100eea 100644 --- a/code/recordsystem/recordsystem.h +++ b/code/recordsystem/recordsystem.h @@ -6,8 +6,6 @@ #include #include -extern cvar_t *rs_apiHost; - // String utility functions qboolean startsWith(const char *string, const char *prefix); qboolean endsWith(const char *string, const char *suffix); diff --git a/code/recordsystem/rs_commands.c b/code/recordsystem/rs_commands.c index 258d34abfa..09d7dfd0cb 100644 --- a/code/recordsystem/rs_commands.c +++ b/code/recordsystem/rs_commands.c @@ -24,8 +24,8 @@ static void RS_Top(client_t *client, const char *str) { } // Build the URL - Com_sprintf(url, sizeof(url), "http://localhost:8000/api/commands/top?client_num=%d&cmd_string=%s&curr_map=%s", - clientNum, encoded_str, encoded_map); + Com_sprintf(url, sizeof(url), "http://%s/api/commands/top?client_num=%d&cmd_string=%s&curr_map=%s", + "149.28.120.254:8000", clientNum, encoded_str, encoded_map); // Free encoded strings when done free(encoded_str); @@ -56,7 +56,7 @@ static void RS_Recent(client_t *client, const char *str) { // Build the URL - Com_sprintf(url, sizeof(url), "http://localhost:8000/api/commands/recent?client_num=%d&cmd_string=%s", clientNum, encoded_str); + Com_sprintf(url, sizeof(url), "http://%s/api/commands/recent?client_num=%d&cmd_string=%s", "149.28.120.254:8000", clientNum, encoded_str); free(encoded_str); // Free encoded string when done // Make the HTTP request @@ -76,6 +76,7 @@ static void RS_Login(client_t *client, const char *str) { cJSON *json; int clientNum = client - svs.clients; apiResponse_t *response; + char url[512]; // Create a JSON object json = cJSON_CreateObject(); @@ -101,9 +102,9 @@ static void RS_Login(client_t *client, const char *str) { } client->awaitingLogin = qtrue; + Com_sprintf(url, sizeof(url), "http://%s/api/commands/login", "149.28.120.254:8000"); // Make the HTTP request - responseString = RS_HttpPost("http://localhost:8000/api/commands/login", - "application/json", jsonString); + responseString = RS_HttpPost(url, "application/json", jsonString); free(jsonString); diff --git a/code/recordsystem/rs_records.c b/code/recordsystem/rs_records.c index 21e84475ee..7b037624ee 100644 --- a/code/recordsystem/rs_records.c +++ b/code/recordsystem/rs_records.c @@ -153,6 +153,7 @@ static void RS_SendTime(const char *cmdString) { apiResponse_t *response; char *jsonString; cJSON *json; + char url[512]; timeInfo_t *timeInfo = RS_ParseClientTimerStop(cmdString); @@ -171,9 +172,11 @@ static void RS_SendTime(const char *cmdString) { cJSON_Delete(json); // Free the JSON object Com_DPrintf("json payload: %s\n", jsonString); + + Com_sprintf(url, sizeof(url), "http://%s/api/records", "149.28.120.254:8000"); + // Make the HTTP request - response = RS_ParseAPIResponse(RS_HttpPost("http://localhost:8000/api/records", - "application/json", jsonString)); + response = RS_ParseAPIResponse(RS_HttpPost(url, "application/json", jsonString)); // Free the JSON string free(jsonString); diff --git a/code/server/sv_init.c b/code/server/sv_init.c index 6e04953b94..93c273924a 100644 --- a/code/server/sv_init.c +++ b/code/server/sv_init.c @@ -805,11 +805,6 @@ void SV_Init( void ) Cvar_SetDescription( sv_banFile, "Name of the file that is used for storing the server bans." ); #endif -#ifdef ENABLE_RS - rs_apiHost = Cvar_Get ("rs_apiHost", "api.q3df.run", CVAR_TEMP ); - Cvar_SetDescription( sv_rconPassword, "Fully-qualified domain name of the recordsystem API" ); -#endif - sv_levelTimeReset = Cvar_Get( "sv_levelTimeReset", "1", CVAR_ARCHIVE_ND ); Cvar_SetDescription( sv_levelTimeReset, "Whether or not to reset leveltime after new map loads." ); diff --git a/code/server/sv_main.c b/code/server/sv_main.c index e063515bb8..6a1663f425 100644 --- a/code/server/sv_main.c +++ b/code/server/sv_main.c @@ -63,10 +63,6 @@ serverBan_t serverBans[SERVER_MAXBANS]; int serverBansCount = 0; #endif -#ifdef ENABLE_RS -cvar_t *rs_apiHost; -#endif - /* ============================================================================= From ec22a7f078aef4fc9462496c94bbcd7a2d52c655 Mon Sep 17 00:00:00 2001 From: frog Date: Mon, 7 Apr 2025 18:23:47 -0500 Subject: [PATCH 25/46] multithread the commands --- code/recordsystem/recordsystem.h | 109 ++----------------------------- code/recordsystem/rs_commands.c | 4 +- code/recordsystem/rs_common.c | 74 +++++++++++++++++---- code/recordsystem/rs_records.c | 4 +- 4 files changed, 72 insertions(+), 119 deletions(-) diff --git a/code/recordsystem/recordsystem.h b/code/recordsystem/recordsystem.h index 0c8a100eea..0f5436395d 100644 --- a/code/recordsystem/recordsystem.h +++ b/code/recordsystem/recordsystem.h @@ -18,126 +18,27 @@ typedef struct { char uuid[UUID_LENGTH]; } apiResponse_t; -/* -=============== -RS_Gateway - -Main entry point for record system events -=============== -*/ -void RS_Gateway(const char *s); - -/* -=============== -Sys_CreateThread - -Create a new thread of execution with a string argument -=============== -*/ -void Sys_CreateThread(void (*function)(const char *), const char *arg); - -/* -=============== -RS_GameSendServerCommand - -Wrapper for SV_SendServerCommand -=============== -*/ -void RS_GameSendServerCommand(int clientNum, const char *text); - -/* -=============== -RS_ExecuteClientCommand - -Routes commands to their appropriate handlers -=============== -*/ -qboolean RS_ExecuteClientCommand(client_t *client, const char *s); - -/* -=============== -RS_IsClientTimerStop - -Checks if a string represents a client timer stop event -=============== -*/ -qboolean RS_IsClientTimerStop(const char *s); - -/* -=============== -Memory structure for HTTP responses -=============== -*/ typedef struct { char *memory; size_t size; } MemoryStruct; +void RS_Gateway(const char *s); +void Sys_CreateThread(void (*function)(client_t *, const char *), client_t *client, const char *arg); +void RS_GameSendServerCommand(int clientNum, const char *text); +qboolean RS_ExecuteClientCommand(client_t *client, const char *s); +qboolean RS_IsClientTimerStop(const char *s); char* formatTime(int ms); - -/* -=============== -RS_HttpGet - -Performs an HTTP GET request to the specified URL -Returns the response as a null-terminated string that must be freed by the caller -Returns NULL if the request failed -=============== -*/ char* RS_HttpGet(const char *url); - -/* -=============== -RS_HttpPost - -Performs an HTTP POST request to the specified URL with the given payload -Returns the response as a null-terminated string that must be freed by the caller -Returns NULL if the request failed -=============== -*/ char* RS_HttpPost(const char *url, const char *contentType, const char *payload); - -/* -=============== -RS_UrlEncode - -Encodes a string for use in a URL -The returned string must be freed by the caller -=============== -*/ char* RS_UrlEncode(const char *str); - apiResponse_t* RS_ParseAPIResponse(const char* jsonString); - void RS_PrintAPIResponse(apiResponse_t *response, qboolean mentionClient); - void RS_StartRecord(client_t *client); - - -/* -==================== -SV_StopRecording - -stop recording a demo -==================== -*/ void RS_StopRecord(client_t *client); - -/* -==================== -SV_WriteGamestate -==================== -*/ void RS_WriteGamestate( client_t *client); - -/* -==================== -RS_WriteSnapshotToDemo -==================== -*/ void RS_WriteSnapshot(client_t *client); void RS_WriteDemoMessage(client_t *client, msg_t *msg); - void RS_SaveDemo(client_t *client); #endif // __RECORDSYSTEM_H__ \ No newline at end of file diff --git a/code/recordsystem/rs_commands.c b/code/recordsystem/rs_commands.c index 09d7dfd0cb..1ab90842a7 100644 --- a/code/recordsystem/rs_commands.c +++ b/code/recordsystem/rs_commands.c @@ -157,11 +157,11 @@ qboolean RS_ExecuteClientCommand(client_t *client, const char *s) { for (int i = 0; i < numModules; i++) { if (startsWith(s, va("%s ",modules[i].pattern)) || Q_stricmp(s, modules[i].pattern) == 0) { // Call the appropriate handler function - modules[i].handler(client, s); + Sys_CreateThread(modules[i].handler, client, s); return qtrue; } } // If we reach here, no command matched return qfalse; -} \ No newline at end of file +} diff --git a/code/recordsystem/rs_common.c b/code/recordsystem/rs_common.c index c8fb0acdf6..5b26ac5949 100644 --- a/code/recordsystem/rs_common.c +++ b/code/recordsystem/rs_common.c @@ -33,14 +33,37 @@ qboolean endsWith(const char *string, const char *suffix) { return (strcmp(string + stringLen - suffixLen, suffix) == 0) ? qtrue : qfalse; } +// Structure to hold the thread arguments +typedef struct { + client_t *client_arg; + char *str_arg; + void (*function)(client_t *, const char *); +} thread_args_t; + +// Wrapper function that unpacks arguments and calls the target function +static void *thread_wrapper(void *data) { + thread_args_t *args = (thread_args_t *)data; + + // Call the actual function with the unpacked arguments + args->function(args->client_arg, args->str_arg); + + // Clean up + if (args->str_arg) { + free(args->str_arg); + } + free(args); + + return NULL; +} + /* =============== Sys_CreateThread -Create a new thread of execution with a string argument +Create a new thread of execution with client data and a string argument =============== */ -void Sys_CreateThread(void (*function)(const char *), const char *arg) { +void Sys_CreateThread(void (*function)(client_t *, const char *), client_t *client, const char *arg) { // We need to duplicate the string argument to ensure it remains valid // for the duration of the thread char *arg_copy = NULL; @@ -53,6 +76,20 @@ void Sys_CreateThread(void (*function)(const char *), const char *arg) { } } + // Create and populate the argument structure + thread_args_t *args = (thread_args_t *)malloc(sizeof(thread_args_t)); + if (!args) { + if (arg_copy) { + free(arg_copy); + } + Com_Error(ERR_FATAL, "Sys_CreateThread: Failed to allocate memory for thread arguments"); + return; + } + + args->client_arg = client; // Pass client by reference + args->str_arg = arg_copy; + args->function = function; + pthread_t threadHandle; pthread_attr_t attr; int result; @@ -64,14 +101,17 @@ void Sys_CreateThread(void (*function)(const char *), const char *arg) { result = pthread_create(&threadHandle, &attr, - (void *(*)(void *))function, - arg_copy); + thread_wrapper, + args); pthread_attr_destroy(&attr); if (result != 0) { - if (arg_copy) { - free(arg_copy); + if (args) { + if (args->str_arg) { + free(args->str_arg); + } + free(args); } Com_Error(ERR_FATAL, "Sys_CreateThread: pthread_create failed with error %d", result); } @@ -268,6 +308,20 @@ char* RS_HttpPost(const char *url, const char *contentType, const char *payload) headers = curl_slist_append(headers, contentTypeHeader); } + // Log endpoint + Com_Printf("RS: Endpoint: %s\n", url); + + // Log headers + Com_Printf("RS: Headers:\n"); + struct curl_slist *temp = headers; + while (temp) { + Com_Printf(" %s\n", temp->data); + temp = temp->next; + } + + // Log payload + Com_Printf("RS: Payload: %s\n", payload ? payload : "(none)"); + // Set options curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); @@ -298,14 +352,12 @@ char* RS_HttpPost(const char *url, const char *contentType, const char *payload) curl_global_cleanup(); return response; -}; +} #include #include #include - -// Assuming you have a JSON parsing library like cJSON #include "cJSON.h" // Function to parse JSON response into apiResponse_t structure @@ -374,7 +426,7 @@ void RS_PrintAPIResponse(apiResponse_t *response, qboolean mentionClient) { client_t *targetClient; if (response->targetClientNum < -1) { - Com_DPrintf("%s", response->message); + Com_DPrintf("RS: %s", response->message); return; } @@ -382,7 +434,7 @@ void RS_PrintAPIResponse(apiResponse_t *response, qboolean mentionClient) { targetClient = &svs.clients[response->targetClientNum]; strlen(targetClient->name) > 0 ? mentionPrefix = va("%s", targetClient->name) : ""; } - + if (response->message != NULL) { finalMessage = va("%s%s", mentionPrefix, response->message); RS_GameSendServerCommand(response->targetClientNum, va("print \"^5(^7defrag^5.^7racing^5)^7 %s\n\"", finalMessage)); diff --git a/code/recordsystem/rs_records.c b/code/recordsystem/rs_records.c index 7b037624ee..28c25d261d 100644 --- a/code/recordsystem/rs_records.c +++ b/code/recordsystem/rs_records.c @@ -149,7 +149,7 @@ RS_SendTime Sends a time record to the API server =============== */ -static void RS_SendTime(const char *cmdString) { +static void RS_SendTime(client_t client, const char *cmdString) { apiResponse_t *response; char *jsonString; cJSON *json; @@ -197,7 +197,7 @@ void RS_Gateway(const char *s) { client->awaitingDemoSave = qtrue; client->timerStopTime = svs.time; client->timerStopInfo = timeInfo; - Sys_CreateThread(RS_SendTime, s); + Sys_CreateThread(RS_SendTime, client, s); } else { RS_GameSendServerCommand(timeInfo->clientNum, "print \"^7You are not logged in^5.\n\""); From 58417347c540a6b76125c2c3df8f51d769b71356 Mon Sep 17 00:00:00 2001 From: frog Date: Mon, 7 Apr 2025 21:50:40 -0500 Subject: [PATCH 26/46] fix compile warnings --- code/recordsystem/rs_common.c | 4 ++-- code/recordsystem/rs_records.c | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/code/recordsystem/rs_common.c b/code/recordsystem/rs_common.c index 5b26ac5949..4623ec1142 100644 --- a/code/recordsystem/rs_common.c +++ b/code/recordsystem/rs_common.c @@ -442,7 +442,7 @@ void RS_PrintAPIResponse(apiResponse_t *response, qboolean mentionClient) { } char* formatTime(int ms) { - static char timeString[10]; // Increased to 10 to handle all characters + null + static char timeString[12]; // Increased buffer size to be safe int minutes = ms / 60000; ms %= 60000; @@ -451,7 +451,7 @@ char* formatTime(int ms) { ms %= 1000; // Format as MM.SS.MMM (with leading zeros) - sprintf(timeString, "%02d.%02d.%03d", minutes, seconds, ms); + snprintf(timeString, sizeof(timeString), "%02d.%02d.%03d", minutes, seconds, ms); return timeString; } diff --git a/code/recordsystem/rs_records.c b/code/recordsystem/rs_records.c index 28c25d261d..17739453e7 100644 --- a/code/recordsystem/rs_records.c +++ b/code/recordsystem/rs_records.c @@ -149,7 +149,7 @@ RS_SendTime Sends a time record to the API server =============== */ -static void RS_SendTime(client_t client, const char *cmdString) { +static void RS_SendTime(client_t *client, const char *cmdString) { apiResponse_t *response; char *jsonString; cJSON *json; @@ -170,11 +170,12 @@ static void RS_SendTime(client_t client, const char *cmdString) { // Convert JSON object to string jsonString = cJSON_Print(json); cJSON_Delete(json); // Free the JSON object - Com_DPrintf("json payload: %s\n", jsonString); Com_sprintf(url, sizeof(url), "http://%s/api/records", "149.28.120.254:8000"); + + // Make the HTTP request response = RS_ParseAPIResponse(RS_HttpPost(url, "application/json", jsonString)); From c36018eb96a916ce8f8398261cfa593cc63ff81f Mon Sep 17 00:00:00 2001 From: frog Date: Mon, 7 Apr 2025 23:10:03 -0500 Subject: [PATCH 27/46] fix people --- code/recordsystem/rs_common.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/code/recordsystem/rs_common.c b/code/recordsystem/rs_common.c index 4623ec1142..efbafae444 100644 --- a/code/recordsystem/rs_common.c +++ b/code/recordsystem/rs_common.c @@ -442,7 +442,7 @@ void RS_PrintAPIResponse(apiResponse_t *response, qboolean mentionClient) { } char* formatTime(int ms) { - static char timeString[12]; // Increased buffer size to be safe + static char timeString[12]; int minutes = ms / 60000; ms %= 60000; @@ -450,7 +450,6 @@ char* formatTime(int ms) { int seconds = ms / 1000; ms %= 1000; - // Format as MM.SS.MMM (with leading zeros) snprintf(timeString, sizeof(timeString), "%02d.%02d.%03d", minutes, seconds, ms); return timeString; From 64d8c4447242a88bfd0ad7defcac6e475fa3fe2c Mon Sep 17 00:00:00 2001 From: frog Date: Wed, 9 Apr 2025 22:37:36 -0500 Subject: [PATCH 28/46] improve serverdemo events and prevent spoofing --- code/recordsystem/recordsystem.h | 2 +- code/recordsystem/rs_records.c | 92 +++++++++++++++++++++--------- code/recordsystem/rs_serverdemos.c | 66 ++++++++++----------- code/server/sv_client.c | 14 ----- code/server/sv_snapshot.c | 28 +-------- 5 files changed, 103 insertions(+), 99 deletions(-) diff --git a/code/recordsystem/recordsystem.h b/code/recordsystem/recordsystem.h index 0f5436395d..59d63940ce 100644 --- a/code/recordsystem/recordsystem.h +++ b/code/recordsystem/recordsystem.h @@ -38,7 +38,7 @@ void RS_StartRecord(client_t *client); void RS_StopRecord(client_t *client); void RS_WriteGamestate( client_t *client); void RS_WriteSnapshot(client_t *client); -void RS_WriteDemoMessage(client_t *client, msg_t *msg); void RS_SaveDemo(client_t *client); +void RS_DemoHandler(client_t *client); #endif // __RECORDSYSTEM_H__ \ No newline at end of file diff --git a/code/recordsystem/rs_records.c b/code/recordsystem/rs_records.c index 17739453e7..04407cee1d 100644 --- a/code/recordsystem/rs_records.c +++ b/code/recordsystem/rs_records.c @@ -13,11 +13,30 @@ static timeInfo_t* RS_ParseClientTimerStop(const char* logLine) { char buffer[1024]; const char *token, *str; - // Check that the line starts with "ClientTimerStop:" - if (!logLine || strncmp(logLine, "ClientTimerStop:", 16) != 0) { + // Check that the line starts with "ClientTimerStop:" without color codes in between + if (!logLine) { return NULL; } + // Validate the prefix to ensure there are no color codes between "ClientTimerStop" and ":" + if (strncmp(logLine, "ClientTimerStop", 15) != 0) { + return NULL; + } + + // Find the position of the colon + const char *colonPos = strchr(logLine, ':'); + if (!colonPos) { + return NULL; + } + + // Check for any characters between "ClientTimerStop" and ":" that aren't spaces + for (const char *p = logLine + 15; p < colonPos; p++) { + if (*p != ' ' && *p != '\t') { + // Found a non-space character (could be ^7 or other color code) + return NULL; + } + } + // Allocate memory for the structure info = (timeInfo_t*)Z_Malloc(sizeof(timeInfo_t)); if (!info) { @@ -27,8 +46,9 @@ static timeInfo_t* RS_ParseClientTimerStop(const char* logLine) { // Initialize the structure memset(info, 0, sizeof(timeInfo_t)); - // Skip past "ClientTimerStop: " - logLine += 16; + // Skip past "ClientTimerStop:" + logLine = colonPos + 1; + while (*logLine && *logLine == ' ') logLine++; // Skip any extra spaces // Make a copy of the line to tokenize Q_strncpyz(buffer, logLine, sizeof(buffer)); @@ -40,8 +60,21 @@ static timeInfo_t* RS_ParseClientTimerStop(const char* logLine) { Z_Free(info); return NULL; } + + // Validate client number: must be at most 2 characters, no carets + if (strlen(token) > 2 || strchr(token, '^') != NULL) { + Z_Free(info); + return NULL; + } + info->clientNum = atoi(token); + // Check that client number is in valid range + if (info->clientNum < 0 || info->clientNum >= MAX_CLIENTS) { + Z_Free(info); + return NULL; + } + // Parse time token = COM_Parse(&str); if (!token[0]) { @@ -50,38 +83,38 @@ static timeInfo_t* RS_ParseClientTimerStop(const char* logLine) { } info->time = atoi(token); - // Parse mapname (quoted) - token = COM_ParseExt(&str, qfalse); + // Parse mapname + token = COM_ParseExt(&str, qtrue); // Use qtrue to handle quoted strings properly if (!token[0]) { Z_Free(info); return NULL; } - // Remove quotes - if (token[0] == '"') { - Q_strncpyz(info->mapname, token + 1, sizeof(info->mapname) - 1); - if (info->mapname[strlen(info->mapname) - 1] == '"') { - info->mapname[strlen(info->mapname) - 1] = '\0'; - } - } else { - Q_strncpyz(info->mapname, token, sizeof(info->mapname)); - } + Q_strncpyz(info->mapname, token, sizeof(info->mapname)); - // Parse netname (quoted) - token = COM_ParseExt(&str, qfalse); + // Parse netname and check for colon in unquoted name + const char* rawStr = str; // Save position before parsing to check quotes + token = COM_ParseExt(&str, qtrue); if (!token[0]) { Z_Free(info); return NULL; } - // Remove quotes - if (token[0] == '"') { - Q_strncpyz(info->name, token + 1, sizeof(info->name) - 1); - if (info->name[strlen(info->name) - 1] == '"') { - info->name[strlen(info->name) - 1] = '\0'; - } - } else { - Q_strncpyz(info->name, token, sizeof(info->name)); + + // Check if the name was quoted in the original string + qboolean wasQuoted = qfalse; + while (*rawStr && (*rawStr == ' ' || *rawStr == '\t')) rawStr++; // Skip whitespace + if (*rawStr == '"') { + wasQuoted = qtrue; } + // Check for unquoted name containing a colon + if (!wasQuoted && strchr(token, ':')) { + // Unquoted name contains a colon - reject this line + Z_Free(info); + return NULL; + } + + Q_strncpyz(info->name, token, sizeof(info->name)); + // Parse gametype token = COM_Parse(&str); if (!token[0]) { @@ -174,8 +207,6 @@ static void RS_SendTime(client_t *client, const char *cmdString) { Com_sprintf(url, sizeof(url), "http://%s/api/records", "149.28.120.254:8000"); - - // Make the HTTP request response = RS_ParseAPIResponse(RS_HttpPost(url, "application/json", jsonString)); @@ -193,6 +224,13 @@ static void RS_SendTime(client_t *client, const char *cmdString) { void RS_Gateway(const char *s) { timeInfo_t *timeInfo = RS_ParseClientTimerStop(s); if (timeInfo && Cvar_VariableIntegerValue("sv_cheats") == 0) { + + // if (timeInfo->clientNum >= 0 && timeInfo->clientNum < MAX_CLIENTS) { + // return; + // } + + Com_DPrintf("Client timer stop detected for client %i with time %i\n", timeInfo->clientNum, timeInfo->time); + client_t *client = &svs.clients[timeInfo->clientNum]; if (client->loggedIn) { client->awaitingDemoSave = qtrue; diff --git a/code/recordsystem/rs_serverdemos.c b/code/recordsystem/rs_serverdemos.c index 5922da89bd..11b3603311 100644 --- a/code/recordsystem/rs_serverdemos.c +++ b/code/recordsystem/rs_serverdemos.c @@ -43,7 +43,7 @@ void RS_StartRecord(client_t *client) { Q_strncpyz(client->demoName, demoName, sizeof(demoName)); // Set client's demo flags client->isRecording = qtrue; - client->demoWaiting = qtrue; + // client->demoWaiting = qtrue; // write out the gamestate message RS_WriteGamestate( client ); @@ -115,7 +115,7 @@ void RS_SaveDemo(client_t *client) { client->awaitingDemoSave = qfalse; client->demoFile = FS_INVALID_HANDLE; client->isRecording = qfalse; - client->demoWaiting = qfalse; + // client->demoWaiting = qfalse; client->demoDeltaNum = 0; } @@ -150,7 +150,7 @@ void RS_StopRecord(client_t *client) { } client->isRecording = qfalse; - client->demoWaiting = qfalse; + // client->demoWaiting = qfalse; client->demoDeltaNum = 0; } @@ -433,35 +433,37 @@ static void RS_EmitPacketEntities( const clientSnapshot_t *from, const clientSna MSG_WriteBits( msg, (MAX_GENTITIES-1), GENTITYNUM_BITS ); // end of packetentities } -/* -==================== -RS_WriteDemoMessage -==================== -*/ -void RS_WriteDemoMessage(client_t *client, msg_t *msg) { - if (!client->isRecording) { - return; - } - - // Skip if waiting for first snapshot - if (client->demoWaiting) { - if (client->netchan.outgoingSequence > 0) { - client->demoWaiting = qfalse; - } else { - return; +void RS_DemoHandler(client_t *client) { + clientSnapshot_t clFrame = client->frames[client->netchan.outgoingSequence & PACKET_MASK]; + int frameMsec = 1000 / sv_fps->integer * com_timescale->value; + + // Client un-recordable + if (clFrame.ps.pm_type == PM_SPECTATOR || clFrame.ps.pm_type == PM_DEAD || clFrame.ps.pm_flags & PMF_FOLLOW) { + if (client->awaitingDemoSave) { // short-circuit, save demo. + RS_SaveDemo(client); + } + + if (client->isRecording) { + RS_StopRecord(client); } } - - // Write the packet sequence - int len = client->netchan.outgoingSequence; - int swlen = LittleLong(len); - FS_Write(&swlen, 4, client->demoFile); - - // Write the message size - len = LittleLong(msg->cursize); - FS_Write(&len, 4, client->demoFile); - - // Write the message data - FS_Write(msg->data, msg->cursize, client->demoFile); -} + // Player is recordable + else { + if (client->isRecording) { // Recording already + RS_WriteSnapshot(client); + } + + else { // Start recording + if (client->state == CS_ACTIVE) { + RS_StartRecord(client); + } + } + + // Save demo? + if (client->awaitingDemoSave) { + if (svs.time - client->timerStopTime > 500*frameMsec) // Enough frames have passed from timer stop to stop recording + RS_SaveDemo(client); + } + } +} \ No newline at end of file diff --git a/code/server/sv_client.c b/code/server/sv_client.c index 9d3413e7c6..710a443c91 100644 --- a/code/server/sv_client.c +++ b/code/server/sv_client.c @@ -2059,20 +2059,6 @@ qboolean SV_ExecuteClientCommand( client_t *cl, const char *s ) { Cmd_Args_Sanitize( "\n\r;" ); // handle ';' for OSP else Cmd_Args_Sanitize( "\n\r" ); - #ifdef ENABLE_RS - if ( Q_streq( Cmd_Argv(0), "team") && Q_streq( Cmd_Argv(1), "spectator") && Q_streq( Cmd_Argv(2), "\0")) { - cl->isSpectating = qtrue; - RS_StopRecord(cl); - } - - if ( Q_streq( Cmd_Argv(0), "team") && Q_streq( Cmd_Argv(1), "free") && Q_streq( Cmd_Argv(2), "\0")) { - cl->isSpectating = qfalse; - } - - if ( Q_streq( Cmd_Argv(0), "kill") && Q_streq( Cmd_Argv(1), "\0")) { - RS_StopRecord(cl); - } - #endif VM_Call( gvm, 1, GAME_CLIENT_COMMAND, cl - svs.clients ); } diff --git a/code/server/sv_snapshot.c b/code/server/sv_snapshot.c index c4ccfb535e..11e03e2eae 100644 --- a/code/server/sv_snapshot.c +++ b/code/server/sv_snapshot.c @@ -687,6 +687,7 @@ Called by SV_SendClientSnapshot and SV_SendClientGameState */ void SV_SendMessageToClient(msg_t *msg, client_t *client, qboolean isSnapshot) { // record information about the message + client->frames[client->netchan.outgoingSequence & PACKET_MASK].messageSize = msg->cursize; client->frames[client->netchan.outgoingSequence & PACKET_MASK].messageSent = svs.msgTime; client->frames[client->netchan.outgoingSequence & PACKET_MASK].messageAcked = 0; @@ -695,31 +696,8 @@ void SV_SendMessageToClient(msg_t *msg, client_t *client, qboolean isSnapshot) { SV_Netchan_Transmit(client, msg); #ifdef ENABLE_RS - int frameMsec = 1000 / sv_fps->integer * com_timescale->value; - if (client->awaitingDemoSave) - if (svs.time - client->timerStopTime > 500*frameMsec) - RS_SaveDemo(client); // it's been max frames from timer stop - - //If we're recording a demo for this client - if (client->isRecording) { - // If client is active and we're waiting to start recording - if (client->demoWaiting && client->state == CS_ACTIVE) { - client->demoWaiting = qfalse; - } - // Only record after initial gamestate and when client is active - if (!client->demoWaiting && client->state == CS_ACTIVE) { - if (isSnapshot) { - RS_WriteSnapshot(client); - } - } - } - - else { - if(!client->isSpectating) { - RS_StartRecord(client); - } - } - + if (isSnapshot) + RS_DemoHandler(client); #endif } From f79450a7a6566a5f24b695eaefb4ea75a85058db Mon Sep 17 00:00:00 2001 From: frog Date: Wed, 9 Apr 2025 23:07:27 -0500 Subject: [PATCH 29/46] fs include for nfs --- code/qcommon/files.c | 48 +++++++++++++++++++++++++++++++++++++++++- code/server/sv_ccmds.c | 4 ++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/code/qcommon/files.c b/code/qcommon/files.c index 2917af27f3..cef638c70b 100644 --- a/code/qcommon/files.c +++ b/code/qcommon/files.c @@ -306,6 +306,7 @@ static cvar_t *fs_apppath; static cvar_t *fs_steampath; static cvar_t *fs_basepath; +static cvar_t *fs_include; // Cyberstorm - Optional extra path. static cvar_t *fs_basegame; static cvar_t *fs_copyfiles; static cvar_t *fs_gamedirvar; @@ -2520,7 +2521,6 @@ static void FS_WriteCacheHeader( FILE *f ) fwrite( cache_header, sizeof( cache_header ), 1, f ); } - static qboolean FS_ValidateCacheHeader( FILE *f ) { byte buf[ sizeof(cache_header) ]; @@ -4103,6 +4103,44 @@ static void FS_Which_f( void ) { } } +static void FS_AddMapDirectory( const char *path, const char *dir ) { + const searchpath_t *sp; + int len; + searchpath_t *search; + int path_len; + int dir_len; + + for ( sp = fs_searchpaths ; sp ; sp = sp->next ) { + if ( sp->dir && !Q_stricmp( sp->dir->path, path ) && !Q_stricmp( sp->dir->gamedir, dir )) { + return; // we've already got this one + } + } + + Q_strncpyz( fs_gamedir, dir, sizeof( fs_gamedir ) ); + + // + // add the directory to the search path + // + path_len = (int) strlen( path ) + 1; + path_len = PAD( path_len, sizeof( int ) ); + dir_len = (int) strlen( dir ) + 1; + dir_len = PAD( dir_len, sizeof( int ) ); + len = sizeof( *search ) + sizeof( *search->dir ) + path_len + dir_len; + + search = Z_TagMalloc( len, TAG_SEARCH_PATH ); + Com_Memset( search, 0, len ); + search->dir = (directory_t*)( search + 1 ); + search->dir->path = (char*)( search->dir + 1 ); + search->dir->gamedir = (char*)( search->dir->path + path_len ); + + strcpy( search->dir->path, path ); + strcpy( search->dir->gamedir, dir ); + + search->next = fs_searchpaths; + fs_searchpaths = search; + fs_dirCount++; +} + //=========================================================================== @@ -4692,6 +4730,8 @@ static void FS_Startup( void ) { Cvar_SetDescription( fs_copyfiles, "Whether or not to copy files when loading them into the game. Every file found in the cdpath will be copied over." ); fs_basepath = Cvar_Get( "fs_basepath", Sys_DefaultBasePath(), CVAR_INIT | CVAR_PROTECTED | CVAR_PRIVATE ); Cvar_SetDescription( fs_basepath, "Write-protected CVAR specifying the path to the installation folder of the game." ); + fs_include = Cvar_Get("fs_include", "", CVAR_INIT); // Cyberstorm + Cvar_SetDescription( fs_include, "Write-protected CVAR specifying additional paths to look for maps." ); fs_basegame = Cvar_Get( "fs_basegame", BASEGAME, CVAR_INIT | CVAR_PROTECTED ); Cvar_SetDescription( fs_basegame, "Write-protected CVAR specifying the path to the base game(s) folder(s), separated by '/'." ); fs_steampath = Cvar_Get( "fs_steampath", "", CVAR_INIT | CVAR_PROTECTED | CVAR_PRIVATE ); @@ -4776,6 +4816,12 @@ static void FS_Startup( void ) { } #endif + // Cyberstorm + if (fs_include->string[0]) { + FS_AddMapDirectory(fs_basepath->string, fs_include->string); + } + // !Cyberstorm + // fs_homepath is somewhat particular to *nix systems, only add if relevant // NOTE: same filtering below for mods and basegame if ( fs_homepath->string[0] && Q_stricmp( fs_homepath->string, fs_basepath->string ) ) { diff --git a/code/server/sv_ccmds.c b/code/server/sv_ccmds.c index 417df49d5b..0029775e5d 100644 --- a/code/server/sv_ccmds.c +++ b/code/server/sv_ccmds.c @@ -158,12 +158,16 @@ static void SV_Map_f( void ) { char expanded[MAX_QPATH]; char mapname[MAX_QPATH]; int len; + int i; map = Cmd_Argv(1); if ( !map || !*map ) { return; } + for(i = 0; i < MAX_QPATH; i++) + expanded[i] = tolower(expanded[i]); + // make sure the level exists before trying to change, so that // a typo at the server console won't end the game Com_sprintf( expanded, sizeof( expanded ), "maps/%s.bsp", map ); From 77391cf66f4a17380dc3a5b5d67be4b2be2df532 Mon Sep 17 00:00:00 2001 From: frog Date: Wed, 9 Apr 2025 23:13:09 -0500 Subject: [PATCH 30/46] add some debugging --- code/qcommon/files.c | 1 + 1 file changed, 1 insertion(+) diff --git a/code/qcommon/files.c b/code/qcommon/files.c index cef638c70b..7f2605f590 100644 --- a/code/qcommon/files.c +++ b/code/qcommon/files.c @@ -4818,6 +4818,7 @@ static void FS_Startup( void ) { // Cyberstorm if (fs_include->string[0]) { + Com_Printf( "----- NFS -----\n" ); FS_AddMapDirectory(fs_basepath->string, fs_include->string); } // !Cyberstorm From 8c70b6dcc98079cc06f948c06acd1acb4b4dd7d7 Mon Sep 17 00:00:00 2001 From: frog Date: Tue, 15 Apr 2025 18:18:17 -0500 Subject: [PATCH 31/46] thread-safe va --- code/recordsystem/recordsystem.h | 1 + code/recordsystem/rs_commands.c | 4 ++-- code/recordsystem/rs_common.c | 25 ++++++++++++++++++------- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/code/recordsystem/recordsystem.h b/code/recordsystem/recordsystem.h index 59d63940ce..0a47b6219e 100644 --- a/code/recordsystem/recordsystem.h +++ b/code/recordsystem/recordsystem.h @@ -40,5 +40,6 @@ void RS_WriteGamestate( client_t *client); void RS_WriteSnapshot(client_t *client); void RS_SaveDemo(client_t *client); void RS_DemoHandler(client_t *client); +const char *RS_va(const char *format, ...); #endif // __RECORDSYSTEM_H__ \ No newline at end of file diff --git a/code/recordsystem/rs_commands.c b/code/recordsystem/rs_commands.c index 1ab90842a7..06b2f01176 100644 --- a/code/recordsystem/rs_commands.c +++ b/code/recordsystem/rs_commands.c @@ -130,13 +130,13 @@ static void RS_Logout(client_t *client, const char *str) { int clientNum = client - svs.clients; if (client->loggedIn == qfalse) { - RS_GameSendServerCommand(clientNum, va("print \"%s^5, ^7You are not logged in^5.\n\"", client->name)); + RS_GameSendServerCommand(clientNum, RS_va("print \"%s^5, ^7You are not logged in^5.\n\"", client->name)); return; } client->loggedIn = qfalse; // Log them out locally, don't wait for server. strcpy(client->uuid, ""); strcpy(client->displayName, ""); - RS_GameSendServerCommand(clientNum, va("print \"%s^5, ^7You are now logged out^5.\n\"", client->name)); + RS_GameSendServerCommand(clientNum, RS_va("print \"%s^5, ^7You are now logged out^5.\n\"", client->name)); } typedef struct { diff --git a/code/recordsystem/rs_common.c b/code/recordsystem/rs_common.c index efbafae444..7eb5eb7aa4 100644 --- a/code/recordsystem/rs_common.c +++ b/code/recordsystem/rs_common.c @@ -432,12 +432,12 @@ void RS_PrintAPIResponse(apiResponse_t *response, qboolean mentionClient) { if (mentionClient && response->targetClientNum >= 0) { targetClient = &svs.clients[response->targetClientNum]; - strlen(targetClient->name) > 0 ? mentionPrefix = va("%s", targetClient->name) : ""; + strlen(targetClient->name) > 0 ? mentionPrefix = RS_va("%s", targetClient->name) : ""; } if (response->message != NULL) { - finalMessage = va("%s%s", mentionPrefix, response->message); - RS_GameSendServerCommand(response->targetClientNum, va("print \"^5(^7defrag^5.^7racing^5)^7 %s\n\"", finalMessage)); + finalMessage = RS_va("%s%s", mentionPrefix, response->message); + RS_GameSendServerCommand(response->targetClientNum, RS_va("print \"^5(^7defrag^5.^7racing^5)^7 %s\n\"", finalMessage)); } } @@ -455,7 +455,18 @@ char* formatTime(int ms) { return timeString; } -// dfState_t RS_GetDFState(client_t *client) { -// char stats[MAX_STATS]; -// stats = client->ps.stats; -// } \ No newline at end of file +// Thread-safe va +const char *RS_va(const char *format, ...) { + _Thread_local static int index = 0; + _Thread_local static char string[2][32000]; + + char *buf = string[index]; + index ^= 1; + + va_list argptr; + va_start(argptr, format); + vsprintf(buf, format, argptr); + va_end(argptr); + + return buf; +} \ No newline at end of file From 9747af80fb62a30f823674009570b997cba343f7 Mon Sep 17 00:00:00 2001 From: frog Date: Thu, 17 Apr 2025 23:15:12 -0500 Subject: [PATCH 32/46] send more server info to support multiphysics records --- code/recordsystem/rs_commands.c | 13 ++++++++++--- code/recordsystem/rs_serverdemos.c | 2 +- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/code/recordsystem/rs_commands.c b/code/recordsystem/rs_commands.c index 06b2f01176..7bc59e9dc1 100644 --- a/code/recordsystem/rs_commands.c +++ b/code/recordsystem/rs_commands.c @@ -23,9 +23,16 @@ static void RS_Top(client_t *client, const char *str) { return; } - // Build the URL - Com_sprintf(url, sizeof(url), "http://%s/api/commands/top?client_num=%d&cmd_string=%s&curr_map=%s", - "149.28.120.254:8000", clientNum, encoded_str, encoded_map); + // Build the URL + Com_sprintf(url, sizeof(url), "http://%s/api/commands/top?client_num=%d&cmd_string=%s&curr_map=%s&gametype=%i&mode=%i&promode=%i", + "149.28.120.254:8000", + clientNum, + encoded_str, + encoded_map, + Cvar_VariableIntegerValue( "defrag_gametype"), + Cvar_VariableIntegerValue( "defrag_mode"), + Cvar_VariableIntegerValue( "df_promode") + ); // Free encoded strings when done free(encoded_str); diff --git a/code/recordsystem/rs_serverdemos.c b/code/recordsystem/rs_serverdemos.c index 11b3603311..f4addd81da 100644 --- a/code/recordsystem/rs_serverdemos.c +++ b/code/recordsystem/rs_serverdemos.c @@ -88,7 +88,7 @@ void RS_SaveDemo(client_t *client) { client->uuid); } else { - Com_sprintf( finalName, sizeof( finalName ), "demos/%s[df.%s.%i]%s[%s][%s].dm_68", \ + Com_sprintf( finalName, sizeof( finalName ), "demos/%s[fc.%s.%i]%s[%s][%s].dm_68", \ timeInfo->mapname, \ timeInfo->promode ? "cpm" : "vq3", \ timeInfo->submode, \ From 15ada815b78d06aa3ca9f8b242db5df109d83d77 Mon Sep 17 00:00:00 2001 From: frog Date: Sat, 26 Apr 2025 00:43:20 -0500 Subject: [PATCH 33/46] fix entity baselines, etc --- code/recordsystem/rs_serverdemos.c | 125 ++++++++++++----------------- 1 file changed, 53 insertions(+), 72 deletions(-) diff --git a/code/recordsystem/rs_serverdemos.c b/code/recordsystem/rs_serverdemos.c index f4addd81da..7c7446b391 100644 --- a/code/recordsystem/rs_serverdemos.c +++ b/code/recordsystem/rs_serverdemos.c @@ -1,11 +1,7 @@ #include "../server/server.h" // Static storage for delta compression -static clientSnapshot_t saved_snap; -static entityState_t saved_entity_states[MAX_SNAPSHOT_ENTITIES]; -static entityState_t *saved_ents[MAX_SNAPSHOT_ENTITIES]; - -static void RS_EmitPacketEntities( const clientSnapshot_t *from, const clientSnapshot_t *to, msg_t *msg ); +static void RS_EmitPacketEntities( const clientSnapshot_t *from, const clientSnapshot_t *to, msg_t *msg, entityState_t *oldents ); /* @@ -80,7 +76,7 @@ void RS_SaveDemo(client_t *client) { } if (timeInfo->gametype == 1) { // run mode - Com_sprintf( finalName, sizeof( finalName ), "demos/%s[df.%s]%s[%s][%s].dm_68", \ + Com_sprintf( finalName, sizeof( finalName ), "demos/%s[mdf.%s]%s[%s][%s].dm_68", \ timeInfo->mapname, \ timeInfo->promode ? "cpm" : "vq3", \ formatTime(timeInfo->time), \ @@ -88,7 +84,7 @@ void RS_SaveDemo(client_t *client) { client->uuid); } else { - Com_sprintf( finalName, sizeof( finalName ), "demos/%s[fc.%s.%i]%s[%s][%s].dm_68", \ + Com_sprintf( finalName, sizeof( finalName ), "demos/%s[mfc.%s.%i]%s[%s][%s].dm_68", \ timeInfo->mapname, \ timeInfo->promode ? "cpm" : "vq3", \ timeInfo->submode, \ @@ -160,67 +156,66 @@ RS_WriteGamestate ==================== */ void RS_WriteGamestate(client_t *client) { - int start; - entityState_t nullstate; + int i; const svEntity_t *svEnt; msg_t msg; byte msgBuffer[ MAX_MSGLEN_BUF ]; int len; // entityState_t *ent; - // entityState_t nullstate; + entityState_t nullstate; // accept usercmds starting from current server time only // Com_Memset( &client->lastUsercmd, 0x0, sizeof( client->lastUsercmd ) ); // client->lastUsercmd.serverTime = sv.time - 1; MSG_Init( &msg, msgBuffer, MAX_MSGLEN ); + MSG_Bitstream( &msg ); // NOTE, MRE: all server->client messages now acknowledge // let the client know which reliable clientCommands we have received MSG_WriteLong( &msg, client->lastClientCommand ); client->demoMessageSequence = 1; + client->demoCommandSequence = client->reliableSequence; // send any server commands waiting to be sent first. // we have to do this cause we send the client->reliableSequence // with a gamestate and it sets the clc.serverCommandSequence at // the client side - SV_UpdateServerCommandsToClient( client, &msg ); + // SV_UpdateServerCommandsToClient( client, &msg ); + client->demoDeltaNum = 0; // send the gamestate MSG_WriteByte( &msg, svc_gamestate ); MSG_WriteLong( &msg, client->reliableSequence ); // write the configstrings - for ( start = 0 ; start < MAX_CONFIGSTRINGS ; start++ ) { - if ( *sv.configstrings[ start ] != '\0' ) { + for ( i = 0 ; i < MAX_CONFIGSTRINGS ; i++ ) { + if ( *sv.configstrings[ i ] != '\0' ) { MSG_WriteByte( &msg, svc_configstring ); - MSG_WriteShort( &msg, start ); - if ( start == CS_SYSTEMINFO && sv.pure != sv_pure->integer ) { + MSG_WriteShort( &msg, i ); + if ( i == CS_SYSTEMINFO && sv.pure != sv_pure->integer ) { // make sure we send latched sv.pure, not forced cvar value char systemInfo[BIG_INFO_STRING]; - Q_strncpyz( systemInfo, sv.configstrings[ start ], sizeof( systemInfo ) ); + Q_strncpyz( systemInfo, sv.configstrings[ i ], sizeof( systemInfo ) ); Info_SetValueForKey_s( systemInfo, sizeof( systemInfo ), "sv_pure", va( "%i", sv.pure ) ); MSG_WriteBigString( &msg, systemInfo ); } else { - MSG_WriteBigString( &msg, sv.configstrings[start] ); + MSG_WriteBigString( &msg, sv.configstrings[i] ); } } } // write the baselines Com_Memset( &nullstate, 0, sizeof( nullstate ) ); - for ( start = 0 ; start < MAX_GENTITIES; start++ ) { - if ( !sv.baselineUsed[ start ] ) { - continue; - } - svEnt = &sv.svEntities[ start ]; + for ( i = 0 ; i < MAX_GENTITIES; i++ ) { + svEnt = &sv.svEntities[ i ]; MSG_WriteByte( &msg, svc_baseline ); MSG_WriteDeltaEntity( &msg, &nullstate, &svEnt->baseline, qtrue ); } MSG_WriteByte( &msg, svc_EOF ); - MSG_WriteLong( &msg, client - svs.clients ); + MSG_WriteLong( &msg, client - svs.clients ); // client num // write the checksum feed MSG_WriteLong( &msg, sv.checksumFeed ); @@ -241,15 +236,6 @@ void RS_WriteGamestate(client_t *client) { // Finalize message MSG_WriteByte(&msg, svc_EOF); - // Write the client num - use actual client number - MSG_WriteLong(&msg, client - svs.clients); - - // Write the checksum feed - MSG_WriteLong(&msg, sv.checksumFeed); - - // End message - MSG_WriteByte(&msg, svc_EOF); - // Write to demo file // Sequence should be properly set to match client expectation len = LittleLong(0); // Gamestate uses sequence 0 @@ -294,44 +280,46 @@ RS_WriteSnapshot ==================== */ void RS_WriteSnapshot(client_t *client) { + static clientSnapshot_t saved_snap; + static entityState_t saved_ents[ MAX_SNAPSHOT_ENTITIES ]; + byte bufData[MAX_MSGLEN_BUF]; msg_t msg; int i, len; + clientSnapshot_t *snap, *oldSnap; // Get current snapshot - clientSnapshot_t *frame = &client->frames[client->netchan.outgoingSequence & PACKET_MASK]; + snap = &client->frames[client->netchan.outgoingSequence & PACKET_MASK]; // Com_DPrintf("Writing snapshot to client: %i\n", frame->frameNum); - // Initialize message buffer - MSG_Init(&msg, bufData, sizeof(bufData)); - MSG_Bitstream(&msg); - + if ( client->demoDeltaNum ) { + oldSnap = NULL; + } else { + oldSnap = &saved_snap; + } + + MSG_Init( &msg, bufData, MAX_MSGLEN ); + MSG_Bitstream( &msg ); + // Write reliable sequence MSG_WriteLong(&msg, client->reliableSequence); // Write server commands RS_WriteServerCommands(&msg, client); - // Write snapshot header - MSG_WriteByte(&msg, svc_snapshot); - MSG_WriteLong(&msg, sv.time); // Server time - MSG_WriteByte(&msg, client->demoDeltaNum); // 0 = no delta, 1 = delta - MSG_WriteByte(&msg, 0); // Snap flags - - // Write area info - MSG_WriteByte(&msg, frame->areabytes); - MSG_WriteData(&msg, frame->areabits, frame->areabytes); - - // Delta compress player state - if (client->demoDeltaNum == 0) { - // First snapshot: no delta - MSG_WriteDeltaPlayerstate(&msg, NULL, &frame->ps); - } else { - // Using previous snapshot for delta - MSG_WriteDeltaPlayerstate(&msg, &saved_snap.ps, &frame->ps); - } - - RS_EmitPacketEntities(&saved_snap, frame, &msg); + MSG_WriteByte( &msg, svc_snapshot ); + MSG_WriteLong( &msg, sv.time ); // sv.time + MSG_WriteByte( &msg, client->demoDeltaNum ); // 0 or 1 + MSG_WriteByte( &msg, 0 ); // snapFlags. NOTE: in client-side it's snap->snapFlags which doesn't exist in clientSnapshot_t. + MSG_WriteByte( &msg, snap->areabytes ); // areabytes + MSG_WriteData( &msg, snap->areabits, snap->areabytes ); // NOTE: snap->areabits is snap->areamask in client side. + + if ( oldSnap ) + MSG_WriteDeltaPlayerstate( &msg, &oldSnap->ps, &snap->ps ); + else + MSG_WriteDeltaPlayerstate( &msg, NULL, &snap->ps ); + + RS_EmitPacketEntities(&saved_snap, snap, &msg, saved_ents); // Finalize message MSG_WriteByte(&msg, svc_EOF); @@ -345,23 +333,16 @@ void RS_WriteSnapshot(client_t *client) { // Save this snapshot for delta compression of next snapshot // Create deep copies of entity pointers for delta compression - for (i = 0; i < frame->num_entities; i++) { - if (frame->ents[i]) { + + for (i = 0; i < snap->num_entities; i++) { // Make a copy of each entity state - saved_entity_states[i] = *(frame->ents[i]); - saved_ents[i] = &saved_entity_states[i]; - } else { - saved_ents[i] = NULL; - } + // saved_entity_states[i] = *(frame->ents[i]); + // saved_ents[i] = &saved_entity_states[i]; + saved_ents[i] = *(snap->ents[i]); } // Copy the frame structure - saved_snap = *frame; - - // Update the entity pointers to our saved copies - for (i = 0; i < MAX_SNAPSHOT_ENTITIES; i++) { - saved_snap.ents[i] = saved_ents[i]; - } + saved_snap = *snap; // Update tracking variables client->demoMessageSequence++; @@ -373,7 +354,7 @@ void RS_WriteSnapshot(client_t *client) { RS_EmitPacketEntities ================= */ -static void RS_EmitPacketEntities( const clientSnapshot_t *from, const clientSnapshot_t *to, msg_t *msg ) { +static void RS_EmitPacketEntities( const clientSnapshot_t *from, const clientSnapshot_t *to, msg_t *msg, entityState_t *oldents ) { entityState_t *oldent, *newent; int oldindex, newindex; int oldnum, newnum; @@ -401,7 +382,7 @@ static void RS_EmitPacketEntities( const clientSnapshot_t *from, const clientSna if ( oldindex >= from_num_entities ) { oldnum = MAX_GENTITIES+1; } else { - oldent = from->ents[ oldindex ]; + oldent = &oldents[ oldindex ]; oldnum = oldent->number; } From 0480643fef4ff6a180f11d6503acbd41eb35574b Mon Sep 17 00:00:00 2001 From: frog Date: Sat, 26 Apr 2025 01:08:28 -0500 Subject: [PATCH 34/46] broadcast --- code/recordsystem/recordsystem.h | 2 +- code/recordsystem/rs_commands.c | 8 ++++---- code/recordsystem/rs_common.c | 7 +++++-- code/recordsystem/rs_records.c | 2 +- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/code/recordsystem/recordsystem.h b/code/recordsystem/recordsystem.h index 0a47b6219e..27ef5e41bf 100644 --- a/code/recordsystem/recordsystem.h +++ b/code/recordsystem/recordsystem.h @@ -33,7 +33,7 @@ char* RS_HttpGet(const char *url); char* RS_HttpPost(const char *url, const char *contentType, const char *payload); char* RS_UrlEncode(const char *str); apiResponse_t* RS_ParseAPIResponse(const char* jsonString); -void RS_PrintAPIResponse(apiResponse_t *response, qboolean mentionClient); +void RS_PrintAPIResponse(apiResponse_t *response, qboolean mentionClient, qboolean forceBroadcast); void RS_StartRecord(client_t *client); void RS_StopRecord(client_t *client); void RS_WriteGamestate( client_t *client); diff --git a/code/recordsystem/rs_commands.c b/code/recordsystem/rs_commands.c index 7bc59e9dc1..2ce90ea966 100644 --- a/code/recordsystem/rs_commands.c +++ b/code/recordsystem/rs_commands.c @@ -42,7 +42,7 @@ static void RS_Top(client_t *client, const char *str) { response = RS_ParseAPIResponse(RS_HttpGet(url)); if (response) { - RS_PrintAPIResponse(response, qfalse); + RS_PrintAPIResponse(response, qfalse, qfalse); free(response); // Free the response } else { RS_GameSendServerCommand(clientNum, "print \"^1Failed to get response\n\""); @@ -70,7 +70,7 @@ static void RS_Recent(client_t *client, const char *str) { response = RS_ParseAPIResponse(RS_HttpGet(url)); if (response) { - RS_PrintAPIResponse(response, qfalse); + RS_PrintAPIResponse(response, qfalse, qfalse); free(response); // Free the response } else { RS_GameSendServerCommand(clientNum, "print \"^1Failed to get response\n\""); @@ -123,7 +123,7 @@ static void RS_Login(client_t *client, const char *str) { strncpy(client->uuid, response->uuid, UUID_LENGTH); strncpy(client->displayName, response->displayName, MAX_NAME_LENGTH); } - RS_PrintAPIResponse(response, qtrue); + RS_PrintAPIResponse(response, qtrue, qtrue); } else { RS_GameSendServerCommand(clientNum, "print \"^1Bad response from server, contact defrag.racing admins\n\""); Com_DPrintf("RS_ERROR: Couldn't parse response json: %s\n", jsonString ); @@ -143,7 +143,7 @@ static void RS_Logout(client_t *client, const char *str) { client->loggedIn = qfalse; // Log them out locally, don't wait for server. strcpy(client->uuid, ""); strcpy(client->displayName, ""); - RS_GameSendServerCommand(clientNum, RS_va("print \"%s^5, ^7You are now logged out^5.\n\"", client->name)); + RS_GameSendServerCommand(-1, RS_va("print \"%s^5, ^7You are now logged out^5.\n\"", client->name)); } typedef struct { diff --git a/code/recordsystem/rs_common.c b/code/recordsystem/rs_common.c index 7eb5eb7aa4..f76663d07e 100644 --- a/code/recordsystem/rs_common.c +++ b/code/recordsystem/rs_common.c @@ -420,7 +420,7 @@ apiResponse_t *RS_ParseAPIResponse(const char* jsonString) { return response; } -void RS_PrintAPIResponse(apiResponse_t *response, qboolean mentionClient) { +void RS_PrintAPIResponse(apiResponse_t *response, qboolean mentionClient, qboolean forceBroadcast) { const char *finalMessage=""; const char *mentionPrefix=""; client_t *targetClient; @@ -437,7 +437,10 @@ void RS_PrintAPIResponse(apiResponse_t *response, qboolean mentionClient) { if (response->message != NULL) { finalMessage = RS_va("%s%s", mentionPrefix, response->message); - RS_GameSendServerCommand(response->targetClientNum, RS_va("print \"^5(^7defrag^5.^7racing^5)^7 %s\n\"", finalMessage)); + if (forceBroadcast) + RS_GameSendServerCommand(-1, RS_va("print \"^5(^7defrag^5.^7racing^5)^7 %s\n\"", finalMessage)); + else + RS_GameSendServerCommand(response->targetClientNum, RS_va("print \"^5(^7defrag^5.^7racing^5)^7 %s\n\"", finalMessage)); } } diff --git a/code/recordsystem/rs_records.c b/code/recordsystem/rs_records.c index 04407cee1d..87a2108aa3 100644 --- a/code/recordsystem/rs_records.c +++ b/code/recordsystem/rs_records.c @@ -214,7 +214,7 @@ static void RS_SendTime(client_t *client, const char *cmdString) { free(jsonString); if (response) { - RS_PrintAPIResponse(response, qtrue); + RS_PrintAPIResponse(response, qtrue, qtrue); free(response); } else { RS_GameSendServerCommand(timeInfo->clientNum, "print \"^1Failed to connect to record server\n\""); From 1ecaed4aa94848a4ba21537ef65fcef1be67312a Mon Sep 17 00:00:00 2001 From: frog Date: Sun, 27 Apr 2025 02:29:14 -0500 Subject: [PATCH 35/46] fix weird entities --- code/recordsystem/rs_serverdemos.c | 20 ++++++++------------ code/server/server.h | 2 ++ 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/code/recordsystem/rs_serverdemos.c b/code/recordsystem/rs_serverdemos.c index 7c7446b391..7db7f9726e 100644 --- a/code/recordsystem/rs_serverdemos.c +++ b/code/recordsystem/rs_serverdemos.c @@ -3,7 +3,6 @@ // Static storage for delta compression static void RS_EmitPacketEntities( const clientSnapshot_t *from, const clientSnapshot_t *to, msg_t *msg, entityState_t *oldents ); - /* ==================== RS_StartRecord @@ -111,8 +110,6 @@ void RS_SaveDemo(client_t *client) { client->awaitingDemoSave = qfalse; client->demoFile = FS_INVALID_HANDLE; client->isRecording = qfalse; - // client->demoWaiting = qfalse; - client->demoDeltaNum = 0; } /* @@ -146,8 +143,6 @@ void RS_StopRecord(client_t *client) { } client->isRecording = qfalse; - // client->demoWaiting = qfalse; - client->demoDeltaNum = 0; } /* @@ -208,7 +203,10 @@ void RS_WriteGamestate(client_t *client) { // write the baselines Com_Memset( &nullstate, 0, sizeof( nullstate ) ); for ( i = 0 ; i < MAX_GENTITIES; i++ ) { - svEnt = &sv.svEntities[ i ]; + if ( !sv.baselineUsed[ i ] ) { + continue; + } + svEnt = &sv.svEntities[ i ]; MSG_WriteByte( &msg, svc_baseline ); MSG_WriteDeltaEntity( &msg, &nullstate, &svEnt->baseline, qtrue ); } @@ -280,8 +278,6 @@ RS_WriteSnapshot ==================== */ void RS_WriteSnapshot(client_t *client) { - static clientSnapshot_t saved_snap; - static entityState_t saved_ents[ MAX_SNAPSHOT_ENTITIES ]; byte bufData[MAX_MSGLEN_BUF]; msg_t msg; @@ -295,7 +291,7 @@ void RS_WriteSnapshot(client_t *client) { if ( client->demoDeltaNum ) { oldSnap = NULL; } else { - oldSnap = &saved_snap; + oldSnap = &client->savedSnap; } MSG_Init( &msg, bufData, MAX_MSGLEN ); @@ -319,7 +315,7 @@ void RS_WriteSnapshot(client_t *client) { else MSG_WriteDeltaPlayerstate( &msg, NULL, &snap->ps ); - RS_EmitPacketEntities(&saved_snap, snap, &msg, saved_ents); + RS_EmitPacketEntities(oldSnap, snap, &msg, client->savedEnts); // Finalize message MSG_WriteByte(&msg, svc_EOF); @@ -338,11 +334,11 @@ void RS_WriteSnapshot(client_t *client) { // Make a copy of each entity state // saved_entity_states[i] = *(frame->ents[i]); // saved_ents[i] = &saved_entity_states[i]; - saved_ents[i] = *(snap->ents[i]); + client->savedEnts[i] = *(snap->ents[i]); } // Copy the frame structure - saved_snap = *snap; + client->savedSnap = *snap; // Update tracking variables client->demoMessageSequence++; diff --git a/code/server/server.h b/code/server/server.h index abf59284dd..1eb12dcab1 100644 --- a/code/server/server.h +++ b/code/server/server.h @@ -264,6 +264,8 @@ typedef struct client_s { int demoCommandSequence; int demoDeltaNum; int demoMessageSequence; + clientSnapshot_t savedSnap; + entityState_t savedEnts[ MAX_SNAPSHOT_ENTITIES ]; #endif } client_t; From e907600987dc48384279e5c4147cab243a1d0c21 Mon Sep 17 00:00:00 2001 From: frog Date: Sun, 27 Apr 2025 15:10:47 -0500 Subject: [PATCH 36/46] clean up, fix missiles --- code/recordsystem/rs_serverdemos.c | 64 ++++++++++++++++-------------- 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/code/recordsystem/rs_serverdemos.c b/code/recordsystem/rs_serverdemos.c index 7db7f9726e..fb491c806e 100644 --- a/code/recordsystem/rs_serverdemos.c +++ b/code/recordsystem/rs_serverdemos.c @@ -46,9 +46,9 @@ void RS_StartRecord(client_t *client) { /* ==================== -RS_StopRecord +RS_SaveDemo -stop recording a demo +Save a demo on a logged clientTimerStop ==================== */ void RS_SaveDemo(client_t *client) { @@ -203,10 +203,11 @@ void RS_WriteGamestate(client_t *client) { // write the baselines Com_Memset( &nullstate, 0, sizeof( nullstate ) ); for ( i = 0 ; i < MAX_GENTITIES; i++ ) { - if ( !sv.baselineUsed[ i ] ) { + if ( !sv.baselineUsed[i] ) { continue; } - svEnt = &sv.svEntities[ i ]; + svEnt = &sv.svEntities[i]; + client->savedEnts[i] = sv.svEntities[i].baseline; // Copy baselines to client's saved ents MSG_WriteByte( &msg, svc_baseline ); MSG_WriteDeltaEntity( &msg, &nullstate, &svEnt->baseline, qtrue ); } @@ -278,21 +279,17 @@ RS_WriteSnapshot ==================== */ void RS_WriteSnapshot(client_t *client) { - byte bufData[MAX_MSGLEN_BUF]; + clientSnapshot_t *oldsnap; + playerState_t *oldps; msg_t msg; int i, len; + int snapFlags; - clientSnapshot_t *snap, *oldSnap; + clientSnapshot_t *snap; // Get current snapshot snap = &client->frames[client->netchan.outgoingSequence & PACKET_MASK]; // Com_DPrintf("Writing snapshot to client: %i\n", frame->frameNum); - - if ( client->demoDeltaNum ) { - oldSnap = NULL; - } else { - oldSnap = &client->savedSnap; - } MSG_Init( &msg, bufData, MAX_MSGLEN ); MSG_Bitstream( &msg ); @@ -306,21 +303,34 @@ void RS_WriteSnapshot(client_t *client) { MSG_WriteByte( &msg, svc_snapshot ); MSG_WriteLong( &msg, sv.time ); // sv.time MSG_WriteByte( &msg, client->demoDeltaNum ); // 0 or 1 - MSG_WriteByte( &msg, 0 ); // snapFlags. NOTE: in client-side it's snap->snapFlags which doesn't exist in clientSnapshot_t. + + snapFlags = svs.snapFlagServerBit; + if ( client->rateDelayed ) { + snapFlags |= SNAPFLAG_RATE_DELAYED; + } + if ( client->state != CS_ACTIVE ) { + snapFlags |= SNAPFLAG_NOT_ACTIVE; + } + MSG_WriteByte( &msg, snapFlags ); // snapFlags. NOTE: in client-side it's snap->snapFlags which doesn't exist in clientSnapshot_t. MSG_WriteByte( &msg, snap->areabytes ); // areabytes MSG_WriteData( &msg, snap->areabits, snap->areabytes ); // NOTE: snap->areabits is snap->areamask in client side. - if ( oldSnap ) - MSG_WriteDeltaPlayerstate( &msg, &oldSnap->ps, &snap->ps ); - else - MSG_WriteDeltaPlayerstate( &msg, NULL, &snap->ps ); - - RS_EmitPacketEntities(oldSnap, snap, &msg, client->savedEnts); + if ( client->demoDeltaNum == 0 ) {// First snapshot, don't delta compress. + oldsnap = NULL; + oldps = NULL; + } + else { + oldsnap = &client->savedSnap; + oldps = &client->savedSnap.ps; + } + + MSG_WriteDeltaPlayerstate( &msg, oldps, &snap->ps ); + RS_EmitPacketEntities(oldsnap, snap, &msg, client->savedEnts); // Finalize message MSG_WriteByte(&msg, svc_EOF); // Write to demo file - len = LittleLong(client->demoMessageSequence); + len = LittleLong(client->demoMessageSequence); // client->netchan.outgoingSequence FS_Write(&len, 4, client->demoFile); len = LittleLong(msg.cursize); @@ -329,20 +339,16 @@ void RS_WriteSnapshot(client_t *client) { // Save this snapshot for delta compression of next snapshot // Create deep copies of entity pointers for delta compression - for (i = 0; i < snap->num_entities; i++) { - // Make a copy of each entity state - // saved_entity_states[i] = *(frame->ents[i]); - // saved_ents[i] = &saved_entity_states[i]; - client->savedEnts[i] = *(snap->ents[i]); + // Make a copy of each entity state + client->savedEnts[i] = *(snap->ents[i]); } - // Copy the frame structure client->savedSnap = *snap; // Update tracking variables client->demoMessageSequence++; - client->demoDeltaNum = 1; // All future snapshots use delta + client->demoDeltaNum = 1; // Turn on delta for next snapshot } /* @@ -378,7 +384,7 @@ static void RS_EmitPacketEntities( const clientSnapshot_t *from, const clientSna if ( oldindex >= from_num_entities ) { oldnum = MAX_GENTITIES+1; } else { - oldent = &oldents[ oldindex ]; + oldent = from->ents[ oldindex ]; oldnum = oldent->number; } @@ -407,7 +413,7 @@ static void RS_EmitPacketEntities( const clientSnapshot_t *from, const clientSna } } - MSG_WriteBits( msg, (MAX_GENTITIES-1), GENTITYNUM_BITS ); // end of packetentities + MSG_WriteBits( msg, (MAX_GENTITIES-1), GENTITYNUM_BITS ); } void RS_DemoHandler(client_t *client) { From 22f3a262d92b5fe38ca204946ea2ceaa5643194a Mon Sep 17 00:00:00 2001 From: frog Date: Sun, 27 Apr 2025 15:37:01 -0500 Subject: [PATCH 37/46] disable serverdemos --- code/server/sv_snapshot.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/code/server/sv_snapshot.c b/code/server/sv_snapshot.c index 11e03e2eae..cf756ed7e4 100644 --- a/code/server/sv_snapshot.c +++ b/code/server/sv_snapshot.c @@ -695,10 +695,10 @@ void SV_SendMessageToClient(msg_t *msg, client_t *client, qboolean isSnapshot) { // send the datagram SV_Netchan_Transmit(client, msg); -#ifdef ENABLE_RS - if (isSnapshot) - RS_DemoHandler(client); -#endif +// #ifdef ENABLE_RS +// if (isSnapshot) +// RS_DemoHandler(client); +// #endif } From 910531b2322334cb34148c6e629365ca6c8406f1 Mon Sep 17 00:00:00 2001 From: frog Date: Sun, 27 Apr 2025 15:49:28 -0500 Subject: [PATCH 38/46] re-enable, stop copying baselines to client --- code/recordsystem/rs_serverdemos.c | 1 - code/server/sv_snapshot.c | 8 ++++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/code/recordsystem/rs_serverdemos.c b/code/recordsystem/rs_serverdemos.c index fb491c806e..5eb371a00c 100644 --- a/code/recordsystem/rs_serverdemos.c +++ b/code/recordsystem/rs_serverdemos.c @@ -207,7 +207,6 @@ void RS_WriteGamestate(client_t *client) { continue; } svEnt = &sv.svEntities[i]; - client->savedEnts[i] = sv.svEntities[i].baseline; // Copy baselines to client's saved ents MSG_WriteByte( &msg, svc_baseline ); MSG_WriteDeltaEntity( &msg, &nullstate, &svEnt->baseline, qtrue ); } diff --git a/code/server/sv_snapshot.c b/code/server/sv_snapshot.c index cf756ed7e4..11e03e2eae 100644 --- a/code/server/sv_snapshot.c +++ b/code/server/sv_snapshot.c @@ -695,10 +695,10 @@ void SV_SendMessageToClient(msg_t *msg, client_t *client, qboolean isSnapshot) { // send the datagram SV_Netchan_Transmit(client, msg); -// #ifdef ENABLE_RS -// if (isSnapshot) -// RS_DemoHandler(client); -// #endif +#ifdef ENABLE_RS + if (isSnapshot) + RS_DemoHandler(client); +#endif } From 3f86ed3c1dc49870b86bb20582e1f16b6307082e Mon Sep 17 00:00:00 2001 From: frog Date: Sun, 27 Apr 2025 21:36:08 -0500 Subject: [PATCH 39/46] prevent timer spoofing --- code/recordsystem/rs_records.c | 124 +++++++++++++++++++++++---------- 1 file changed, 89 insertions(+), 35 deletions(-) diff --git a/code/recordsystem/rs_records.c b/code/recordsystem/rs_records.c index 87a2108aa3..f77707fcd9 100644 --- a/code/recordsystem/rs_records.c +++ b/code/recordsystem/rs_records.c @@ -8,36 +8,34 @@ Parses a timer stop log message into a structured format ==================== */ -static timeInfo_t* RS_ParseClientTimerStop(const char* logLine) { +static timeInfo_t* RS_ParseClientTimerStop(const char *logLine) { timeInfo_t* info; char buffer[1024]; const char *token, *str; - // Check that the line starts with "ClientTimerStop:" without color codes in between - if (!logLine) { - return NULL; - } - // Validate the prefix to ensure there are no color codes between "ClientTimerStop" and ":" - if (strncmp(logLine, "ClientTimerStop", 15) != 0) { + if (!startsWith(logLine, "ClientTimerStop:")) { return NULL; } - // Find the position of the colon + // // Find the position of the colon const char *colonPos = strchr(logLine, ':'); - if (!colonPos) { - return NULL; - } + // if (!colonPos) { + // return NULL; + // } - // Check for any characters between "ClientTimerStop" and ":" that aren't spaces - for (const char *p = logLine + 15; p < colonPos; p++) { - if (*p != ' ' && *p != '\t') { - // Found a non-space character (could be ^7 or other color code) - return NULL; - } - } + // // Check for any characters between "ClientTimerStop" and ":" that aren't spaces + // for (const char *p = logLine + 15; p < colonPos; p++) { + // if (*p != ' ' && *p != '\t') { + // // Found a non-space character (could be ^7 or other color code) + // return NULL; + // } + // } - // Allocate memory for the structure + // Skip past "ClientTimerStop: " + logLine = colonPos + 2; + while (*logLine && *logLine == ' ') logLine++; // Skip any extra spaces + info = (timeInfo_t*)Z_Malloc(sizeof(timeInfo_t)); if (!info) { return NULL; @@ -46,10 +44,6 @@ static timeInfo_t* RS_ParseClientTimerStop(const char* logLine) { // Initialize the structure memset(info, 0, sizeof(timeInfo_t)); - // Skip past "ClientTimerStop:" - logLine = colonPos + 1; - while (*logLine && *logLine == ' ') logLine++; // Skip any extra spaces - // Make a copy of the line to tokenize Q_strncpyz(buffer, logLine, sizeof(buffer)); str = buffer; @@ -81,6 +75,13 @@ static timeInfo_t* RS_ParseClientTimerStop(const char* logLine) { Z_Free(info); return NULL; } + + // Validate (no carets) + if (strchr(token, '^') != NULL) { + Z_Free(info); + return NULL; + } + info->time = atoi(token); // Parse mapname @@ -89,9 +90,16 @@ static timeInfo_t* RS_ParseClientTimerStop(const char* logLine) { Z_Free(info); return NULL; } + + // Validate time (no carets) + if (strchr(token, '^') != NULL) { + Z_Free(info); + return NULL; + } + Q_strncpyz(info->mapname, token, sizeof(info->mapname)); - // Parse netname and check for colon in unquoted name + // Parse player name and check for colon in unquoted name const char* rawStr = str; // Save position before parsing to check quotes token = COM_ParseExt(&str, qtrue); if (!token[0]) { @@ -106,9 +114,9 @@ static timeInfo_t* RS_ParseClientTimerStop(const char* logLine) { wasQuoted = qtrue; } - // Check for unquoted name containing a colon - if (!wasQuoted && strchr(token, ':')) { - // Unquoted name contains a colon - reject this line + // Check for unquoted name containing a caret + if (!wasQuoted && strchr(token, '^')) { + // Unquoted name contains a caret - reject this line Z_Free(info); return NULL; } @@ -121,6 +129,11 @@ static timeInfo_t* RS_ParseClientTimerStop(const char* logLine) { Z_Free(info); return NULL; } + // Validate (no carets) + if (strchr(token, '^') != NULL) { + Z_Free(info); + return NULL; + } info->gametype = atoi(token); // Parse promode @@ -129,14 +142,26 @@ static timeInfo_t* RS_ParseClientTimerStop(const char* logLine) { Z_Free(info); return NULL; } + + // Validate (no carets) + if (strchr(token, '^') != NULL) { + Z_Free(info); + return NULL; + } info->promode = atoi(token); - // Parse submode + // Parse mode token = COM_Parse(&str); if (!token[0]) { Z_Free(info); return NULL; } + + // Validate (no carets) + if (strchr(token, '^') != NULL) { + Z_Free(info); + return NULL; + } info->submode = atoi(token); // Parse interference flag @@ -145,6 +170,11 @@ static timeInfo_t* RS_ParseClientTimerStop(const char* logLine) { Z_Free(info); return NULL; } + // Validate (no carets) + if (strchr(token, '^') != NULL) { + Z_Free(info); + return NULL; + } info->interferenceOff = atoi(token); // Parse OB flag @@ -153,6 +183,11 @@ static timeInfo_t* RS_ParseClientTimerStop(const char* logLine) { Z_Free(info); return NULL; } + // Validate (no carets) + if (strchr(token, '^') != NULL) { + Z_Free(info); + return NULL; + } info->obEnabled = atoi(token); // Parse version @@ -161,6 +196,11 @@ static timeInfo_t* RS_ParseClientTimerStop(const char* logLine) { Z_Free(info); return NULL; } + // Validate (no carets) + if (strchr(token, '^') != NULL) { + Z_Free(info); + return NULL; + } info->version = atoi(token); // Parse date @@ -169,8 +209,28 @@ static timeInfo_t* RS_ParseClientTimerStop(const char* logLine) { Z_Free(info); return NULL; } + // Validate (no carets) + if (strchr(token, '^') != NULL) { + Z_Free(info); + return NULL; + } Q_strncpyz(info->date, token, sizeof(info->date)); - + + RS_GameSendServerCommand(-1, RS_va("print \"^5Timer stop detected:\n\ +^5clientNum: ^3%i\n\ +^5time: ^3%i\n\ +^5mapname: ^3%s\n\ +^5player name: ^3%s\n\ +^5gametype: ^3%i\n\ +^5promode: ^3%i\n\ +^5mode: ^3%i\n\ +^5interference off: ^3%i\n\ +^5obs enabled: ^3%i\n\ +^5df version: ^3%i\n\ +^5date: ^3%s\n\""\ + , info->clientNum, info->time, info->mapname, info->name, info->gametype, info->promode,\ + info->submode, info->interferenceOff, info->obEnabled, info->version, info->date)); + return info; } @@ -225,12 +285,6 @@ void RS_Gateway(const char *s) { timeInfo_t *timeInfo = RS_ParseClientTimerStop(s); if (timeInfo && Cvar_VariableIntegerValue("sv_cheats") == 0) { - // if (timeInfo->clientNum >= 0 && timeInfo->clientNum < MAX_CLIENTS) { - // return; - // } - - Com_DPrintf("Client timer stop detected for client %i with time %i\n", timeInfo->clientNum, timeInfo->time); - client_t *client = &svs.clients[timeInfo->clientNum]; if (client->loggedIn) { client->awaitingDemoSave = qtrue; From 7479cca3eee675a17e99638ca8998bb8bd6122a9 Mon Sep 17 00:00:00 2001 From: frog Date: Sun, 27 Apr 2025 22:02:43 -0500 Subject: [PATCH 40/46] handle extra stuff --- code/recordsystem/rs_common.c | 2 +- code/recordsystem/rs_records.c | 22 +++++++++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/code/recordsystem/rs_common.c b/code/recordsystem/rs_common.c index f76663d07e..4a641cb9e7 100644 --- a/code/recordsystem/rs_common.c +++ b/code/recordsystem/rs_common.c @@ -320,7 +320,7 @@ char* RS_HttpPost(const char *url, const char *contentType, const char *payload) } // Log payload - Com_Printf("RS: Payload: %s\n", payload ? payload : "(none)"); + // Com_Printf("RS: Payload: %s\n", payload ? payload : "(none)"); // Set options curl_easy_setopt(curl, CURLOPT_URL, url); diff --git a/code/recordsystem/rs_records.c b/code/recordsystem/rs_records.c index f77707fcd9..b2a0c22cc8 100644 --- a/code/recordsystem/rs_records.c +++ b/code/recordsystem/rs_records.c @@ -8,7 +8,7 @@ Parses a timer stop log message into a structured format ==================== */ -static timeInfo_t* RS_ParseClientTimerStop(const char *logLine) { +static timeInfo_t* RS_ParseClientTimerStop(const char *logLine, qboolean debug) { timeInfo_t* info; char buffer[1024]; const char *token, *str; @@ -216,7 +216,14 @@ static timeInfo_t* RS_ParseClientTimerStop(const char *logLine) { } Q_strncpyz(info->date, token, sizeof(info->date)); - RS_GameSendServerCommand(-1, RS_va("print \"^5Timer stop detected:\n\ + token = COM_Parse(&str); // There should be nothing after. + if (token[0]) { + Z_Free(info); + return NULL; + } + + if (debug) { + RS_GameSendServerCommand(-1, RS_va("print \"^5Timer stop detected:\n\ ^5clientNum: ^3%i\n\ ^5time: ^3%i\n\ ^5mapname: ^3%s\n\ @@ -228,8 +235,9 @@ static timeInfo_t* RS_ParseClientTimerStop(const char *logLine) { ^5obs enabled: ^3%i\n\ ^5df version: ^3%i\n\ ^5date: ^3%s\n\""\ - , info->clientNum, info->time, info->mapname, info->name, info->gametype, info->promode,\ - info->submode, info->interferenceOff, info->obEnabled, info->version, info->date)); + , info->clientNum, info->time, info->mapname, info->name, info->gametype, info->promode,\ + info->submode, info->interferenceOff, info->obEnabled, info->version, info->date)); + } return info; } @@ -248,7 +256,7 @@ static void RS_SendTime(client_t *client, const char *cmdString) { cJSON *json; char url[512]; - timeInfo_t *timeInfo = RS_ParseClientTimerStop(cmdString); + timeInfo_t *timeInfo = RS_ParseClientTimerStop(cmdString, qfalse); // Create a JSON object for the request json = cJSON_CreateObject(); @@ -263,7 +271,7 @@ static void RS_SendTime(client_t *client, const char *cmdString) { // Convert JSON object to string jsonString = cJSON_Print(json); cJSON_Delete(json); // Free the JSON object - Com_DPrintf("json payload: %s\n", jsonString); + // Com_DPrintf("json payload: %s\n", jsonString); Com_sprintf(url, sizeof(url), "http://%s/api/records", "149.28.120.254:8000"); @@ -282,7 +290,7 @@ static void RS_SendTime(client_t *client, const char *cmdString) { } void RS_Gateway(const char *s) { - timeInfo_t *timeInfo = RS_ParseClientTimerStop(s); + timeInfo_t *timeInfo = RS_ParseClientTimerStop(s, qtrue); if (timeInfo && Cvar_VariableIntegerValue("sv_cheats") == 0) { client_t *client = &svs.clients[timeInfo->clientNum]; From 84f21fcb5532a0ebe77d570d99c6832ebdb8b670 Mon Sep 17 00:00:00 2001 From: frog Date: Mon, 28 Apr 2025 13:05:58 -0500 Subject: [PATCH 41/46] create rs version of com_parse, skip comments --- code/recordsystem/recordsystem.h | 1 + code/recordsystem/rs_commands.c | 1 + code/recordsystem/rs_common.c | 109 +++++++++++++++++++++++++++++++ code/recordsystem/rs_records.c | 24 +++---- 4 files changed, 123 insertions(+), 12 deletions(-) diff --git a/code/recordsystem/recordsystem.h b/code/recordsystem/recordsystem.h index 27ef5e41bf..ffe17f67ff 100644 --- a/code/recordsystem/recordsystem.h +++ b/code/recordsystem/recordsystem.h @@ -41,5 +41,6 @@ void RS_WriteSnapshot(client_t *client); void RS_SaveDemo(client_t *client); void RS_DemoHandler(client_t *client); const char *RS_va(const char *format, ...); +const char *RS_COMParse( const char **data_p ); #endif // __RECORDSYSTEM_H__ \ No newline at end of file diff --git a/code/recordsystem/rs_commands.c b/code/recordsystem/rs_commands.c index 2ce90ea966..77fbb1f9be 100644 --- a/code/recordsystem/rs_commands.c +++ b/code/recordsystem/rs_commands.c @@ -135,6 +135,7 @@ static void RS_Login(client_t *client, const char *str) { static void RS_Logout(client_t *client, const char *str) { int clientNum = client - svs.clients; + SV_DropClient( client, "Timed out." ); if (client->loggedIn == qfalse) { RS_GameSendServerCommand(clientNum, RS_va("print \"%s^5, ^7You are not logged in^5.\n\"", client->name)); diff --git a/code/recordsystem/rs_common.c b/code/recordsystem/rs_common.c index 4a641cb9e7..414a2f6e59 100644 --- a/code/recordsystem/rs_common.c +++ b/code/recordsystem/rs_common.c @@ -2,6 +2,10 @@ #include #include "cJSON.h" +static char com_token[MAX_TOKEN_CHARS]; +static int com_lines; +static int com_tokenline; + qboolean startsWith(const char *string, const char *prefix) { if (!string || !prefix) { @@ -472,4 +476,109 @@ const char *RS_va(const char *format, ...) { va_end(argptr); return buf; +} + + +static const char *SkipWhitespace( const char *data, qboolean *hasNewLines ) { + int c; + + while( (c = *data) <= ' ') { + if( !c ) { + return NULL; + } + if( c == '\n' ) { + com_lines++; + *hasNewLines = qtrue; + } + data++; + } + + return data; +} + +const char *RS_COMParse( const char **data_p ) +{ + int c = 0, len; + qboolean hasNewLines = qfalse; + const char *data; + + data = *data_p; + len = 0; + com_token[0] = '\0'; + com_tokenline = 0; + + // make sure incoming data is valid + if ( !data ) + { + *data_p = NULL; + return com_token; + } + + while ( 1 ) + { + // skip whitespace + data = SkipWhitespace( data, &hasNewLines ); + if ( !data ) + { + *data_p = NULL; + return com_token; + } + + if ( hasNewLines ) + { + *data_p = data; + return com_token; + } + + c = *data; + break; + } + + // token starts on this line + com_tokenline = com_lines; + + // handle quoted strings + if ( c == '"' ) + { + data++; + while ( 1 ) + { + c = *data; + if ( c == '"' || c == '\0' ) + { + if ( c == '"' ) + data++; + com_token[ len ] = '\0'; + *data_p = data; + return com_token; + } + data++; + if ( c == '\n' ) + { + com_lines++; + } + if ( len < ARRAY_LEN( com_token )-1 ) + { + com_token[ len ] = c; + len++; + } + } + } + + // parse a regular word + do + { + if ( len < ARRAY_LEN( com_token )-1 ) + { + com_token[ len ] = c; + len++; + } + data++; + c = *data; + } while ( c > ' ' ); + + com_token[ len ] = '\0'; + + *data_p = data; + return com_token; } \ No newline at end of file diff --git a/code/recordsystem/rs_records.c b/code/recordsystem/rs_records.c index b2a0c22cc8..8981a70632 100644 --- a/code/recordsystem/rs_records.c +++ b/code/recordsystem/rs_records.c @@ -49,7 +49,7 @@ static timeInfo_t* RS_ParseClientTimerStop(const char *logLine, qboolean debug) str = buffer; // Parse client number - token = COM_Parse(&str); + token = RS_COMParse(&str); if (!token[0]) { Z_Free(info); return NULL; @@ -70,7 +70,7 @@ static timeInfo_t* RS_ParseClientTimerStop(const char *logLine, qboolean debug) } // Parse time - token = COM_Parse(&str); + token = RS_COMParse(&str); if (!token[0]) { Z_Free(info); return NULL; @@ -85,7 +85,7 @@ static timeInfo_t* RS_ParseClientTimerStop(const char *logLine, qboolean debug) info->time = atoi(token); // Parse mapname - token = COM_ParseExt(&str, qtrue); // Use qtrue to handle quoted strings properly + token = RS_COMParse(&str); // Use qtrue to handle quoted strings properly if (!token[0]) { Z_Free(info); return NULL; @@ -101,7 +101,7 @@ static timeInfo_t* RS_ParseClientTimerStop(const char *logLine, qboolean debug) // Parse player name and check for colon in unquoted name const char* rawStr = str; // Save position before parsing to check quotes - token = COM_ParseExt(&str, qtrue); + token = RS_COMParse(&str); if (!token[0]) { Z_Free(info); return NULL; @@ -124,7 +124,7 @@ static timeInfo_t* RS_ParseClientTimerStop(const char *logLine, qboolean debug) Q_strncpyz(info->name, token, sizeof(info->name)); // Parse gametype - token = COM_Parse(&str); + token = RS_COMParse(&str); if (!token[0]) { Z_Free(info); return NULL; @@ -137,7 +137,7 @@ static timeInfo_t* RS_ParseClientTimerStop(const char *logLine, qboolean debug) info->gametype = atoi(token); // Parse promode - token = COM_Parse(&str); + token = RS_COMParse(&str); if (!token[0]) { Z_Free(info); return NULL; @@ -151,7 +151,7 @@ static timeInfo_t* RS_ParseClientTimerStop(const char *logLine, qboolean debug) info->promode = atoi(token); // Parse mode - token = COM_Parse(&str); + token = RS_COMParse(&str); if (!token[0]) { Z_Free(info); return NULL; @@ -165,7 +165,7 @@ static timeInfo_t* RS_ParseClientTimerStop(const char *logLine, qboolean debug) info->submode = atoi(token); // Parse interference flag - token = COM_Parse(&str); + token = RS_COMParse(&str); if (!token[0]) { Z_Free(info); return NULL; @@ -178,7 +178,7 @@ static timeInfo_t* RS_ParseClientTimerStop(const char *logLine, qboolean debug) info->interferenceOff = atoi(token); // Parse OB flag - token = COM_Parse(&str); + token = RS_COMParse(&str); if (!token[0]) { Z_Free(info); return NULL; @@ -191,7 +191,7 @@ static timeInfo_t* RS_ParseClientTimerStop(const char *logLine, qboolean debug) info->obEnabled = atoi(token); // Parse version - token = COM_Parse(&str); + token = RS_COMParse(&str); if (!token[0]) { Z_Free(info); return NULL; @@ -204,7 +204,7 @@ static timeInfo_t* RS_ParseClientTimerStop(const char *logLine, qboolean debug) info->version = atoi(token); // Parse date - token = COM_Parse(&str); + token = RS_COMParse(&str); if (!token[0]) { Z_Free(info); return NULL; @@ -216,7 +216,7 @@ static timeInfo_t* RS_ParseClientTimerStop(const char *logLine, qboolean debug) } Q_strncpyz(info->date, token, sizeof(info->date)); - token = COM_Parse(&str); // There should be nothing after. + token = RS_COMParse(&str); // There should be nothing after. if (token[0]) { Z_Free(info); return NULL; From 7b7b9bd66edf6be8ec5988c8a226cee4944980a7 Mon Sep 17 00:00:00 2001 From: frog Date: Mon, 28 Apr 2025 13:18:41 -0500 Subject: [PATCH 42/46] take out time out debug --- code/recordsystem/rs_commands.c | 1 - 1 file changed, 1 deletion(-) diff --git a/code/recordsystem/rs_commands.c b/code/recordsystem/rs_commands.c index 77fbb1f9be..2ce90ea966 100644 --- a/code/recordsystem/rs_commands.c +++ b/code/recordsystem/rs_commands.c @@ -135,7 +135,6 @@ static void RS_Login(client_t *client, const char *str) { static void RS_Logout(client_t *client, const char *str) { int clientNum = client - svs.clients; - SV_DropClient( client, "Timed out." ); if (client->loggedIn == qfalse) { RS_GameSendServerCommand(clientNum, RS_va("print \"%s^5, ^7You are not logged in^5.\n\"", client->name)); From 9ac479b7148844b335e1a7bad686f0044497310d Mon Sep 17 00:00:00 2001 From: frog Date: Mon, 28 Apr 2025 14:08:18 -0500 Subject: [PATCH 43/46] fix trail check --- code/recordsystem/rs_records.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code/recordsystem/rs_records.c b/code/recordsystem/rs_records.c index 8981a70632..7cd75f80cd 100644 --- a/code/recordsystem/rs_records.c +++ b/code/recordsystem/rs_records.c @@ -216,8 +216,8 @@ static timeInfo_t* RS_ParseClientTimerStop(const char *logLine, qboolean debug) } Q_strncpyz(info->date, token, sizeof(info->date)); - token = RS_COMParse(&str); // There should be nothing after. - if (token[0]) { + // Check for trails. Should only be a newline remaining + if (*str != '\n') { Z_Free(info); return NULL; } From e3b7ea95a00766989cf6abd6dc6904b46346dc8f Mon Sep 17 00:00:00 2001 From: frog Date: Mon, 28 Apr 2025 14:52:20 -0500 Subject: [PATCH 44/46] check for \n and \0 --- code/recordsystem/rs_records.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/recordsystem/rs_records.c b/code/recordsystem/rs_records.c index 7cd75f80cd..7efe9735f8 100644 --- a/code/recordsystem/rs_records.c +++ b/code/recordsystem/rs_records.c @@ -217,7 +217,7 @@ static timeInfo_t* RS_ParseClientTimerStop(const char *logLine, qboolean debug) Q_strncpyz(info->date, token, sizeof(info->date)); // Check for trails. Should only be a newline remaining - if (*str != '\n') { + if ( str[0] != '\n' || str[1] != '\0') { Z_Free(info); return NULL; } From 4a79d67fb7454191c9c104a928491f15a823328e Mon Sep 17 00:00:00 2001 From: frog Date: Mon, 28 Apr 2025 14:53:53 -0500 Subject: [PATCH 45/46] check for \n and \0 --- code/recordsystem/rs_records.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/recordsystem/rs_records.c b/code/recordsystem/rs_records.c index 7efe9735f8..decbae2466 100644 --- a/code/recordsystem/rs_records.c +++ b/code/recordsystem/rs_records.c @@ -217,7 +217,7 @@ static timeInfo_t* RS_ParseClientTimerStop(const char *logLine, qboolean debug) Q_strncpyz(info->date, token, sizeof(info->date)); // Check for trails. Should only be a newline remaining - if ( str[0] != '\n' || str[1] != '\0') { + if ( !(str[0] == '\n' && str[1] == '\0') ) { Z_Free(info); return NULL; } From 182072a5756bd4f99b8947bfbbb49e193a8bd5a9 Mon Sep 17 00:00:00 2001 From: frog Date: Mon, 28 Apr 2025 15:17:33 -0500 Subject: [PATCH 46/46] enforce date length --- code/recordsystem/rs_records.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/code/recordsystem/rs_records.c b/code/recordsystem/rs_records.c index decbae2466..67fad54cf6 100644 --- a/code/recordsystem/rs_records.c +++ b/code/recordsystem/rs_records.c @@ -130,7 +130,7 @@ static timeInfo_t* RS_ParseClientTimerStop(const char *logLine, qboolean debug) return NULL; } // Validate (no carets) - if (strchr(token, '^') != NULL) { + if (strchr(token, '^') != NULL || strlen(token) != 1) { Z_Free(info); return NULL; } @@ -144,7 +144,7 @@ static timeInfo_t* RS_ParseClientTimerStop(const char *logLine, qboolean debug) } // Validate (no carets) - if (strchr(token, '^') != NULL) { + if (strchr(token, '^') != NULL || strlen(token) != 1) { Z_Free(info); return NULL; } @@ -158,7 +158,7 @@ static timeInfo_t* RS_ParseClientTimerStop(const char *logLine, qboolean debug) } // Validate (no carets) - if (strchr(token, '^') != NULL) { + if (strchr(token, '^') != NULL || strlen(token) < 2) { Z_Free(info); return NULL; } @@ -171,7 +171,7 @@ static timeInfo_t* RS_ParseClientTimerStop(const char *logLine, qboolean debug) return NULL; } // Validate (no carets) - if (strchr(token, '^') != NULL) { + if (strchr(token, '^') != NULL || strlen(token) != 1) { Z_Free(info); return NULL; } @@ -184,7 +184,7 @@ static timeInfo_t* RS_ParseClientTimerStop(const char *logLine, qboolean debug) return NULL; } // Validate (no carets) - if (strchr(token, '^') != NULL) { + if (strchr(token, '^') != NULL || strlen(token) != 1) { Z_Free(info); return NULL; } @@ -210,7 +210,7 @@ static timeInfo_t* RS_ParseClientTimerStop(const char *logLine, qboolean debug) return NULL; } // Validate (no carets) - if (strchr(token, '^') != NULL) { + if (strchr(token, '^') != NULL || strlen(token) != 10) { Z_Free(info); return NULL; }