]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - prvm_edict.c
renamed prvm_startupreuseedicttime to prvm_reuseedicts_startuptime
[xonotic/darkplaces.git] / prvm_edict.c
index e0f3773275c14789a983cb7dbdab01e7e6a06aa6..da9dec068eef78b5a2932d1eef942e33939378a7 100644 (file)
@@ -39,6 +39,8 @@ cvar_t prvm_backtraceforwarnings = {0, "prvm_backtraceforwarnings", "0", "print
 cvar_t prvm_leaktest = {0, "prvm_leaktest", "0", "try to detect memory leaks in strings or entities"};
 cvar_t prvm_leaktest_ignore_classnames = {0, "prvm_leaktest_ignore_classnames", "", "classnames of entities to NOT leak check because they are found by find(world, classname, ...) but are actually spawned by QC code (NOT map entities)"};
 cvar_t prvm_errordump = {0, "prvm_errordump", "0", "write a savegame on crash to crash-server.dmp"};
+cvar_t prvm_reuseedicts_startuptime = {0, "prvm_reuseedicts_startuptime", "2", "allows immediate re-use of freed entity slots during start of new level (value in seconds)"};
+cvar_t prvm_reuseedicts_neverinsameframe = {0, "prvm_reuseedicts_neverinsameframe", "1", "never allows re-use of freed entity slots during same frame"};
 
 qboolean prvm_runawaycheck = true;
 
@@ -224,7 +226,7 @@ void PRVM_ED_ClearEdict (prvm_edict_t *e)
        PRVM_GCALL(init_edict)(e);
 }
 
-const char *PRVM_AllocationOrigin()
+const char *PRVM_AllocationOrigin(void)
 {
        char *buf = NULL;
        if(prog->leaktest_active)
@@ -247,7 +249,9 @@ qboolean PRVM_ED_CanAlloc(prvm_edict_t *e)
 {
        if(!e->priv.required->free)
                return false;
-       if(e->priv.required->freetime < prog->starttime + 2)
+       if(realtime <= e->priv.required->freetime && prvm_reuseedicts_neverinsameframe.integer)
+               return false; // never allow reuse in same frame (causes networking trouble)
+       if(e->priv.required->freetime < prog->starttime + prvm_reuseedicts_startuptime.value)
                return true;
        if(realtime > e->priv.required->freetime + 1)
                return true;
@@ -1148,6 +1152,105 @@ void PRVM_GameCommand_Menu_f(void)
        PRVM_GameCommand("menu", "menu_cmd");
 }
 
+/*
+=============
+PRVM_ED_EdictGet_f
+
+Console command to load a field of a specified edict
+=============
+*/
+void PRVM_ED_EdictGet_f(void)
+{
+       prvm_edict_t *ed;
+       ddef_t *key;
+       const char *s;
+       prvm_eval_t *v;
+
+       if(Cmd_Argc() != 4 && Cmd_Argc() != 5)
+       {
+               Con_Print("prvm_edictget <program name> <edict number> <field> [<cvar>]\n");
+               return;
+       }
+
+       PRVM_Begin;
+       if(!PRVM_SetProgFromString(Cmd_Argv(1)))
+       {
+               Con_Printf("Wrong program name %s !\n", Cmd_Argv(1));
+               return;
+       }
+
+       ed = PRVM_EDICT_NUM(atoi(Cmd_Argv(2)));
+
+       if((key = PRVM_ED_FindField(Cmd_Argv(3))) == 0)
+       {
+               Con_Printf("Key %s not found !\n", Cmd_Argv(3));
+               goto fail;
+       }
+
+       v = (prvm_eval_t *)((char *)ed->fields.vp + key->ofs*4);
+       s = PRVM_UglyValueString(key->type, v);
+       if(Cmd_Argc() == 5)
+       {
+               cvar_t *cvar = Cvar_FindVar(Cmd_Argv(4));
+               if (cvar && cvar->flags & CVAR_READONLY)
+               {
+                       Con_Printf("prvm_edictget: %s is read-only\n", cvar->name);
+                       goto fail;
+               }
+               Cvar_Get(Cmd_Argv(4), s, 0, NULL);
+       }
+       else
+               Con_Printf("%s\n", s);
+
+fail:
+       PRVM_End;
+}
+
+void PRVM_ED_GlobalGet_f(void)
+{
+       ddef_t *key;
+       const char *s;
+       prvm_eval_t *v;
+
+       if(Cmd_Argc() != 3 && Cmd_Argc() != 4)
+       {
+               Con_Print("prvm_globalget <program name> <global> [<cvar>]\n");
+               return;
+       }
+
+       PRVM_Begin;
+       if(!PRVM_SetProgFromString(Cmd_Argv(1)))
+       {
+               Con_Printf("Wrong program name %s !\n", Cmd_Argv(1));
+               return;
+       }
+
+       key = PRVM_ED_FindGlobal(Cmd_Argv(2));
+       if(!key)
+       {
+               Con_Printf( "No global '%s' in %s!\n", Cmd_Argv(2), Cmd_Argv(1) );
+               goto fail;
+       }
+
+       v = (prvm_eval_t *) &prog->globals.generic[key->ofs];
+       s = PRVM_UglyValueString(key->type, v);
+       if(Cmd_Argc() == 4)
+       {
+               cvar_t *cvar = Cvar_FindVar(Cmd_Argv(3));
+               if (cvar && cvar->flags & CVAR_READONLY)
+               {
+                       Con_Printf("prvm_globalget: %s is read-only\n", cvar->name);
+                       goto fail;
+               }
+               Cvar_Get(Cmd_Argv(3), s, 0, NULL);
+       }
+       else
+               Con_Printf("%s\n", s);
+
+fail:
+       PRVM_End;
+}
+
 /*
 =============
 PRVM_ED_EdictSet_f
@@ -1630,8 +1733,8 @@ PRVM_ResetProg
 ===============
 */
 
-void PRVM_LeakTest();
-void PRVM_ResetProg()
+void PRVM_LeakTest(void);
+void PRVM_ResetProg(void)
 {
        PRVM_LeakTest();
        PRVM_GCALL(reset_cmd)();
@@ -1707,6 +1810,7 @@ void PRVM_LoadProgs (const char * filename, int numrequiredfunc, char **required
        prog->progs = (dprograms_t *)FS_LoadFile (filename, prog->progs_mempool, false, &filesize);
        if (prog->progs == NULL || filesize < (fs_offset_t)sizeof(dprograms_t))
                PRVM_ERROR ("PRVM_LoadProgs: couldn't load %s for %s", filename, PRVM_NAME);
+       // TODO bounds check header fields (e.g. numstatements), they must never go behind end of file
 
        Con_DPrintf("%s programs occupy %iK.\n", PRVM_NAME, (int)(filesize/1024));
 
@@ -1772,6 +1876,9 @@ void PRVM_LoadProgs (const char * filename, int numrequiredfunc, char **required
                prog->functions[i].numparms = LittleLong (dfunctions[i].numparms);
                prog->functions[i].locals = LittleLong (dfunctions[i].locals);
                memcpy(prog->functions[i].parm_size, dfunctions[i].parm_size, sizeof(dfunctions[i].parm_size));
+               if(prog->functions[i].first_statement >= prog->progs->numstatements)
+                       PRVM_ERROR("PRVM_LoadProgs: out of bounds function statement (function %d) in %s", i, PRVM_NAME);
+               // TODO bounds check parm_start, s_name, s_file, numparms, locals, parm_size
        }
 
        for (i=0 ; i<prog->progs->numglobaldefs ; i++)
@@ -1779,6 +1886,7 @@ void PRVM_LoadProgs (const char * filename, int numrequiredfunc, char **required
                prog->globaldefs[i].type = LittleShort (prog->globaldefs[i].type);
                prog->globaldefs[i].ofs = LittleShort (prog->globaldefs[i].ofs);
                prog->globaldefs[i].s_name = LittleLong (prog->globaldefs[i].s_name);
+               // TODO bounds check ofs, s_name
        }
 
        // copy the progs fields to the new fields list
@@ -1789,6 +1897,7 @@ void PRVM_LoadProgs (const char * filename, int numrequiredfunc, char **required
                        PRVM_ERROR ("PRVM_LoadProgs: prog->fielddefs[i].type & DEF_SAVEGLOBAL in %s", PRVM_NAME);
                prog->fielddefs[i].ofs = LittleShort (infielddefs[i].ofs);
                prog->fielddefs[i].s_name = LittleLong (infielddefs[i].s_name);
+               // TODO bounds check ofs, s_name
        }
 
        // append the required fields
@@ -1797,6 +1906,7 @@ void PRVM_LoadProgs (const char * filename, int numrequiredfunc, char **required
                prog->fielddefs[prog->progs->numfielddefs].type = required_field[i].type;
                prog->fielddefs[prog->progs->numfielddefs].ofs = prog->progs->entityfields;
                prog->fielddefs[prog->progs->numfielddefs].s_name = PRVM_SetEngineString(required_field[i].name);
+               // TODO bounds check ofs, s_name
                if (prog->fielddefs[prog->progs->numfielddefs].type == ev_vector)
                        prog->progs->entityfields += 3;
                else
@@ -1920,6 +2030,20 @@ void PRVM_LoadProgs (const char * filename, int numrequiredfunc, char **required
                        break;
                }
        }
+       if(prog->progs->numstatements < 1)
+       {
+               PRVM_ERROR("PRVM_LoadProgs: empty program in %s", PRVM_NAME);
+       }
+       else switch(prog->statements[prog->progs->numstatements - 1].op)
+       {
+               case OP_RETURN:
+               case OP_GOTO:
+               case OP_DONE:
+                       break;
+               default:
+                       PRVM_ERROR("PRVM_LoadProgs: program may fall off the edge (does not end with RETURN, GOTO or DONE) in %s", PRVM_NAME);
+                       break;
+       }
 
        PRVM_LoadLNO(filename);
 
@@ -2168,12 +2292,15 @@ void PRVM_Init (void)
        Cmd_AddCommand ("prvm_edicts", PRVM_ED_PrintEdicts_f, "prints all data about all entities in the selected VM (server, client, menu)");
        Cmd_AddCommand ("prvm_edictcount", PRVM_ED_Count_f, "prints number of active entities in the selected VM (server, client, menu)");
        Cmd_AddCommand ("prvm_profile", PRVM_Profile_f, "prints execution statistics about the most used QuakeC functions in the selected VM (server, client, menu)");
+       Cmd_AddCommand ("prvm_childprofile", PRVM_ChildProfile_f, "prints execution statistics about the most used QuakeC functions in the selected VM (server, client, menu), sorted by time taken in function with child calls");
        Cmd_AddCommand ("prvm_callprofile", PRVM_CallProfile_f, "prints execution statistics about the most time consuming QuakeC calls from the engine in the selected VM (server, client, menu)");
        Cmd_AddCommand ("prvm_fields", PRVM_Fields_f, "prints usage statistics on properties (how many entities have non-zero values) in the selected VM (server, client, menu)");
        Cmd_AddCommand ("prvm_globals", PRVM_Globals_f, "prints all global variables in the selected VM (server, client, menu)");
        Cmd_AddCommand ("prvm_global", PRVM_Global_f, "prints value of a specified global variable in the selected VM (server, client, menu)");
        Cmd_AddCommand ("prvm_globalset", PRVM_GlobalSet_f, "sets value of a specified global variable in the selected VM (server, client, menu)");
        Cmd_AddCommand ("prvm_edictset", PRVM_ED_EdictSet_f, "changes value of a specified property of a specified entity in the selected VM (server, client, menu)");
+       Cmd_AddCommand ("prvm_edictget", PRVM_ED_EdictGet_f, "retrieves the value of a specified property of a specified entity in the selected VM (server, client menu) into a cvar or to the console");
+       Cmd_AddCommand ("prvm_globalget", PRVM_ED_GlobalGet_f, "retrieves the value of a specified global variable in the selected VM (server, client menu) into a cvar or to the console");
        Cmd_AddCommand ("prvm_printfunction", PRVM_PrintFunction_f, "prints a disassembly (QuakeC instructions) of the specified function in the selected VM (server, client, menu)");
        Cmd_AddCommand ("cl_cmd", PRVM_GameCommand_Client_f, "calls the client QC function GameCommand with the supplied string as argument");
        Cmd_AddCommand ("menu_cmd", PRVM_GameCommand_Menu_f, "calls the menu QC function GameCommand with the supplied string as argument");
@@ -2188,6 +2315,8 @@ void PRVM_Init (void)
        Cvar_RegisterVariable (&prvm_leaktest);
        Cvar_RegisterVariable (&prvm_leaktest_ignore_classnames);
        Cvar_RegisterVariable (&prvm_errordump);
+       Cvar_RegisterVariable (&prvm_reuseedicts_startuptime);
+       Cvar_RegisterVariable (&prvm_reuseedicts_neverinsameframe);
 
        // COMMANDLINEOPTION: PRVM: -norunaway disables the runaway loop check (it might be impossible to exit DarkPlaces if used!)
        prvm_runawaycheck = !COM_CheckParm("-norunaway");
@@ -2214,10 +2343,10 @@ void PRVM_InitProg(int prognr)
        prog->starttime = Sys_DoubleTime();
 
        prog->error_cmd = Host_Error;
-       prog->leaktest_active = prvm_leaktest.integer;
+       prog->leaktest_active = prvm_leaktest.integer != 0;
 }
 
-int PRVM_GetProgNr()
+int PRVM_GetProgNr(void)
 {
        return prog - prog_list;
 }
@@ -2338,7 +2467,10 @@ const char *PRVM_GetString(int num)
                if (num < prog->numknownstrings)
                {
                        if (!prog->knownstrings[num])
+                       {
                                VM_Warning("PRVM_GetString: Invalid zone-string offset (%i has been freed)\n", num);
+                               return "";
+                       }
                        return prog->knownstrings[num];
                }
                else
@@ -2651,7 +2783,7 @@ static qboolean PRVM_IsEdictReferenced(prvm_edict_t *edict, int mark)
        return false;
 }
 
-static void PRVM_MarkReferencedEdicts()
+static void PRVM_MarkReferencedEdicts(void)
 {
        int j;
        qboolean found_new;
@@ -2688,7 +2820,7 @@ static void PRVM_MarkReferencedEdicts()
        Con_DPrintf("leak check used %d stages to find all references\n", stage);
 }
 
-void PRVM_LeakTest()
+void PRVM_LeakTest(void)
 {
        int i, j;
        qboolean leaked = false;