X-Git-Url: http://git.xonotic.org/?a=blobdiff_plain;f=pr_edict.c;h=efb16b50f6cbd5b151fc2049f57526ca8f568efd;hb=af4d8d7892ace10b13321577a55dc0630e315129;hp=9f22b1e4dae18769f48bf0d44039fbab0d7a176f;hpb=ef35a088cb3ab1900acdbd5285465cd845368e79;p=xonotic%2Fdarkplaces.git diff --git a/pr_edict.c b/pr_edict.c index 9f22b1e4..efb16b50 100644 --- a/pr_edict.c +++ b/pr_edict.c @@ -39,8 +39,8 @@ mempool_t *edictstring_mempool; int type_size[8] = {1,sizeof(string_t)/4,1,3,1,1,sizeof(func_t)/4,sizeof(void *)/4}; -ddef_t *ED_FieldAtOfs (int ofs); -qboolean ED_ParseEpair (void *base, ddef_t *key, const char *s); +ddef_t *ED_FieldAtOfs(int ofs); +qboolean ED_ParseEpair(edict_t *ent, ddef_t *key, const char *s); cvar_t pr_checkextension = {0, "pr_checkextension", "1"}; cvar_t nomonsters = {0, "nomonsters", "0"}; @@ -223,7 +223,7 @@ edict_t *ED_Alloc (void) int i; edict_t *e; - for ( i=svs.maxclients+1 ; istring)); + snprintf (line, sizeof (line), "%s", PR_GetString(val->string)); break; case ev_entity: //n = NoCrash_NUM_FOR_EDICT(PROG_TO_EDICT(val->edict)); n = val->edict; if (n < 0 || n >= MAX_EDICTS) - sprintf (line, "entity %i (invalid!)", n); + snprintf (line, sizeof (line), "entity %i (invalid!)", n); else - sprintf (line, "entity %i", n); + snprintf (line, sizeof (line), "entity %i", n); break; case ev_function: f = pr_functions + val->function; - sprintf (line, "%s()", PR_GetString(f->s_name)); + snprintf (line, sizeof (line), "%s()", PR_GetString(f->s_name)); break; case ev_field: def = ED_FieldAtOfs ( val->_int ); - sprintf (line, ".%s", PR_GetString(def->s_name)); + snprintf (line, sizeof (line), ".%s", PR_GetString(def->s_name)); break; case ev_void: - sprintf (line, "void"); + snprintf (line, sizeof (line), "void"); break; case ev_float: // LordHavoc: changed from %5.1f to %10.4f - sprintf (line, "%10.4f", val->_float); + snprintf (line, sizeof (line), "%10.4f", val->_float); break; case ev_vector: // LordHavoc: changed from %5.1f to %10.4f - sprintf (line, "'%10.4f %10.4f %10.4f'", val->vector[0], val->vector[1], val->vector[2]); + snprintf (line, sizeof (line), "'%10.4f %10.4f %10.4f'", val->vector[0], val->vector[1], val->vector[2]); break; case ev_pointer: - sprintf (line, "pointer"); + snprintf (line, sizeof (line), "pointer"); break; default: - sprintf (line, "bad type %i", type); + snprintf (line, sizeof (line), "bad type %i", type); break; } @@ -454,23 +454,11 @@ char *PR_UglyValueString (etype_t type, eval_t *val) switch (type) { case ev_string: - sprintf (line, "%s", PR_GetString(val->string)); - break; - case ev_entity: - sprintf (line, "%i", NUM_FOR_EDICT(PROG_TO_EDICT(val->edict))); - break; - case ev_function: - f = pr_functions + val->function; - sprintf (line, "%s", PR_GetString(f->s_name)); - break; - case ev_field: - def = ED_FieldAtOfs ( val->_int ); - // LordHavoc: parse the string a bit to turn special characters + // Parse the string a bit to turn special characters // (like newline, specifically) into escape codes, // this fixes saving games from various mods - //sprintf (line, "%s", PR_GetString(def->s_name)); - s = PR_GetString(def->s_name); - for (i = 0;i < 4095 && *s;) + s = PR_GetString (val->string); + for (i = 0;i < (int)sizeof(line) - 2 && *s;) { if (*s == '\n') { @@ -483,22 +471,33 @@ char *PR_UglyValueString (etype_t type, eval_t *val) line[i++] = 'r'; } else - line[i] = *s; + line[i++] = *s; s++; } - line[i++] = 0; + line[i] = '\0'; + break; + case ev_entity: + snprintf (line, sizeof (line), "%i", NUM_FOR_EDICT(PROG_TO_EDICT(val->edict))); + break; + case ev_function: + f = pr_functions + val->function; + snprintf (line, sizeof (line), "%s", PR_GetString(f->s_name)); + break; + case ev_field: + def = ED_FieldAtOfs ( val->_int ); + snprintf (line, sizeof (line), ".%s", PR_GetString(def->s_name)); break; case ev_void: - sprintf (line, "void"); + snprintf (line, sizeof (line), "void"); break; case ev_float: - sprintf (line, "%f", val->_float); + snprintf (line, sizeof (line), "%f", val->_float); break; case ev_vector: - sprintf (line, "%f %f %f", val->vector[0], val->vector[1], val->vector[2]); + snprintf (line, sizeof (line), "%f %f %f", val->vector[0], val->vector[1], val->vector[2]); break; default: - sprintf (line, "bad type %i", type); + snprintf (line, sizeof (line), "bad type %i", type); break; } @@ -524,17 +523,17 @@ char *PR_GlobalString (int ofs) val = (void *)&pr_globals[ofs]; def = ED_GlobalAtOfs(ofs); if (!def) - sprintf (line,"%i(?)", ofs); + snprintf (line, sizeof (line), "%i(?)", ofs); else { s = PR_ValueString (def->type, val); - sprintf (line,"%i(%s)%s", ofs, PR_GetString(def->s_name), s); + snprintf (line, sizeof (line), "%i(%s)%s", ofs, PR_GetString(def->s_name), s); } i = strlen(line); for ( ; i<20 ; i++) - strcat (line," "); - strcat (line," "); + strlcat (line, " ", sizeof (line)); + strlcat (line, " ", sizeof (line)); return line; } @@ -547,14 +546,14 @@ char *PR_GlobalStringNoContents (int ofs) def = ED_GlobalAtOfs(ofs); if (!def) - sprintf (line,"%i(?)", ofs); + snprintf (line, sizeof (line), "%i(?)", ofs); else - sprintf (line,"%i(%s)", ofs, PR_GetString(def->s_name)); + snprintf (line, sizeof (line), "%i(%s)", ofs, PR_GetString(def->s_name)); i = strlen(line); for ( ; i<20 ; i++) - strcat (line," "); - strcat (line," "); + strlcat (line, " ", sizeof (line)); + strlcat (line, " ", sizeof (line)); return line; } @@ -586,7 +585,7 @@ void ED_Print (edict_t *ed) } tempstring[0] = 0; - sprintf(tempstring, "\nEDICT %i:\n", NUM_FOR_EDICT(ed)); + snprintf (tempstring, sizeof (tempstring), "\nEDICT %i:\n", NUM_FOR_EDICT(ed)); for (i=1 ; inumfielddefs ; i++) { d = &pr_fielddefs[i]; @@ -612,7 +611,7 @@ void ED_Print (edict_t *ed) tempstring2[259] = 0; name = tempstring2; } - strcat(tempstring, name); + strlcat (tempstring, name, sizeof (tempstring)); for (l = strlen(name);l < 14;l++) strcat(tempstring, " "); strcat(tempstring, " "); @@ -625,8 +624,8 @@ void ED_Print (edict_t *ed) tempstring2[259] = 0; name = tempstring2; } - strcat(tempstring, name); - strcat(tempstring, "\n"); + strlcat (tempstring, name, sizeof (tempstring)); + strlcat (tempstring, "\n", sizeof (tempstring)); if (strlen(tempstring) >= 4096) { Con_Printf("%s", tempstring); @@ -835,7 +834,7 @@ void ED_ParseGlobals (const char *data) continue; } - if (!ED_ParseEpair ((void *)pr_globals, key, com_token)) + if (!ED_ParseEpair(NULL, key, com_token)) Host_Error ("ED_ParseGlobals: parse error"); } } @@ -883,74 +882,81 @@ Can parse either fields or globals returns false if error ============= */ -qboolean ED_ParseEpair (void *base, ddef_t *key, const char *s) +qboolean ED_ParseEpair(edict_t *ent, ddef_t *key, const char *s) { - int i; - char string[128]; - ddef_t *def; - char *v, *w; - void *d; - mfunction_t *func; - - d = (void *)((int *)base + key->ofs); + int i; + ddef_t *def; + eval_t *val; + mfunction_t *func; + if (ent) + val = (eval_t *)((int *)ent->v + key->ofs); + else + val = (eval_t *)((int *)pr_globals + key->ofs); switch (key->type & ~DEF_SAVEGLOBAL) { case ev_string: - *(string_t *)d = PR_SetString(ED_NewString(s)); + val->string = PR_SetString(ED_NewString(s)); break; case ev_float: - *(float *)d = atof (s); + while (*s && *s <= ' ') + s++; + val->_float = atof(s); break; case ev_vector: - strcpy (string, s); - v = string; - w = string; - for (i=0 ; i<3 ; i++) + for (i = 0;i < 3;i++) { - while (*v && *v != ' ') - v++; - *v = 0; - ((float *)d)[i] = atof (w); - w = v = v+1; + while (*s && *s <= ' ') + s++; + if (!*s) + break; + val->vector[i] = atof(s); + while (*s > ' ') + s++; + if (!*s) + break; } break; case ev_entity: - i = atoi (s); + while (*s && *s <= ' ') + s++; + i = atoi(s); if (i < 0 || i >= MAX_EDICTS) - Con_DPrintf("ED_ParseEpair: ev_entity reference too large (edict %i >= MAX_EDICTS %i)\n", i, MAX_EDICTS); + Con_Printf("ED_ParseEpair: ev_entity reference too large (edict %i >= MAX_EDICTS %i)\n", i, MAX_EDICTS); while (i >= sv.max_edicts) SV_IncreaseEdicts(); - *(int *)d = EDICT_TO_PROG(EDICT_NUM(i)); + // if SV_IncreaseEdicts was called the base pointer needs to be updated + if (ent) + val = (eval_t *)((int *)ent->v + key->ofs); + val->edict = EDICT_TO_PROG(EDICT_NUM(i)); break; case ev_field: - def = ED_FindField (s); + def = ED_FindField(s); if (!def) { - // LordHavoc: don't warn about worldspawn sky/fog fields because they don't require mod support - if (strcmp(s, "sky") && strcmp(s, "fog") && strncmp(s, "fog_", 4) && strcmp(s, "farclip")) - Con_DPrintf ("Can't find field %s\n", s); + Con_DPrintf("ED_ParseEpair: Can't find field %s\n", s); return false; } - *(int *)d = G_INT(def->ofs); + val->_int = G_INT(def->ofs); break; case ev_function: - func = ED_FindFunction (s); + func = ED_FindFunction(s); if (!func) { - Con_DPrintf ("Can't find function %s\n", s); + Con_Printf ("ED_ParseEpair: Can't find function %s\n", s); return false; } - *(func_t *)d = func - pr_functions; + val->function = func - pr_functions; break; default: - break; + Con_Printf("ED_ParseEpair: Unknown key->type %i for key \"%s\"\n", key->type, PR_GetString(key->s_name)); + return false; } return true; } @@ -989,19 +995,15 @@ const char *ED_ParseEdict (const char *data, edict_t *ent) // anglehack is to allow QuakeEd to write single scalar angles // and allow them to be turned into vectors. (FIXME...) - if (!strcmp(com_token, "angle")) - { - strcpy (com_token, "angles"); - anglehack = true; - } - else - anglehack = false; + anglehack = !strcmp (com_token, "angle"); + if (anglehack) + strlcpy (com_token, "angles", sizeof (com_token)); // FIXME: change light to _light to get rid of this hack if (!strcmp(com_token, "light")) - strcpy (com_token, "light_lev"); // hack for single light def + strlcpy (com_token, "light_lev", sizeof (com_token)); // hack for single light def - strcpy (keyname, com_token); + strlcpy (keyname, com_token, sizeof (keyname)); // another hack to fix heynames with trailing spaces n = strlen(keyname); @@ -1035,11 +1037,11 @@ const char *ED_ParseEdict (const char *data, edict_t *ent) if (anglehack) { char temp[32]; - strcpy (temp, com_token); - sprintf (com_token, "0 %s 0", temp); + strlcpy (temp, com_token, sizeof (temp)); + snprintf (com_token, sizeof (com_token), "0 %s 0", temp); } - if (!ED_ParseEpair ((void *)ent->v, key, com_token)) + if (!ED_ParseEpair(ent, key, com_token)) Host_Error ("ED_ParseEdict: parse error"); } @@ -1436,7 +1438,7 @@ void PR_Fields_f (void) return; } counts = Mem_Alloc(tempmempool, progs->numfielddefs * sizeof(int)); - for (ednum = 0;ednum < MAX_EDICTS;ednum++) + for (ednum = 0;ednum < sv.max_edicts;ednum++) { ed = EDICT_NUM(ednum); if (ed->e->free) @@ -1471,32 +1473,32 @@ void PR_Fields_f (void) switch(d->type & ~DEF_SAVEGLOBAL) { case ev_string: - strcat(tempstring, "string "); + strlcat (tempstring, "string ", sizeof (tempstring)); break; case ev_entity: - strcat(tempstring, "entity "); + strlcat (tempstring, "entity ", sizeof (tempstring)); break; case ev_function: - strcat(tempstring, "function "); + strlcat (tempstring, "function ", sizeof (tempstring)); break; case ev_field: - strcat(tempstring, "field "); + strlcat (tempstring, "field ", sizeof (tempstring)); break; case ev_void: - strcat(tempstring, "void "); + strlcat (tempstring, "void ", sizeof (tempstring)); break; case ev_float: - strcat(tempstring, "float "); + strlcat (tempstring, "float ", sizeof (tempstring)); break; case ev_vector: - strcat(tempstring, "vector "); + strlcat (tempstring, "vector ", sizeof (tempstring)); break; case ev_pointer: - strcat(tempstring, "pointer "); + strlcat (tempstring, "pointer ", sizeof (tempstring)); break; default: - sprintf (tempstring2, "bad type %i ", d->type & ~DEF_SAVEGLOBAL); - strcat(tempstring, tempstring2); + snprintf (tempstring2, sizeof (tempstring2), "bad type %i ", d->type & ~DEF_SAVEGLOBAL); + strlcat (tempstring, tempstring2, sizeof (tempstring)); break; } if (strlen(name) > 256) @@ -1506,12 +1508,12 @@ void PR_Fields_f (void) tempstring2[259] = 0; name = tempstring2; } - strcat(tempstring, name); + strcat (tempstring, name); for (j = strlen(name);j < 25;j++) strcat(tempstring, " "); - sprintf(tempstring2, "%5d", counts[i]); - strcat(tempstring, tempstring2); - strcat(tempstring, "\n"); + snprintf (tempstring2, sizeof (tempstring2), "%5d", counts[i]); + strlcat (tempstring, tempstring2, sizeof (tempstring)); + strlcat (tempstring, "\n", sizeof (tempstring)); if (strlen(tempstring) >= 4096) { Con_Printf("%s", tempstring); @@ -1524,7 +1526,7 @@ void PR_Fields_f (void) } } Mem_Free(counts); - Con_Printf("%i entity fields (%i in use), totalling %i bytes per edict (%i in use), %i edicts, %i bytes total spent on edict fields (%i needed)\n", progs->entityfields, used, progs->entityfields * 4, usedamount * 4, MAX_EDICTS, progs->entityfields * 4 * MAX_EDICTS, usedamount * 4 * MAX_EDICTS); + Con_Printf("%i entity fields (%i in use), totalling %i bytes per edict (%i in use), %i edicts allocated, %i bytes total spent on edict fields (%i needed)\n", progs->entityfields, used, progs->entityfields * 4, usedamount * 4, sv.max_edicts, progs->entityfields * 4 * sv.max_edicts, usedamount * 4 * sv.max_edicts); } void PR_Globals_f (void)