// // M_LoadCVARs // void M_LoadCVARs(char *filename) { int bindcount = 0; int cvarcount = 0; int statcount = 0; // read the file in, overriding any set defaults FILE *file = fopen(filename, "r"); if (!file) { M_CheckCVARs(); M_SaveCVARs(); C_Output("Created <b>%s</b>.", filename); cvarsloaded = true; return; } for (int i = 0; i < MAXALIASES; i++) { aliases[i].name[0] = '\0'; aliases[i].string[0] = '\0'; } // Clear all default controls before reading them from config file if (!togglingvanilla && M_StringEndsWith(filename, PACKAGE_CONFIG)) { for (int i = 0; *actions[i].action; i++) { if (actions[i].keyboard1) *(int *)actions[i].keyboard1 = 0; if (actions[i].keyboard2) *(int *)actions[i].keyboard2 = 0; if (actions[i].mouse1) *(int *)actions[i].mouse1 = -1; if (actions[i].gamepad1) *(int *)actions[i].gamepad1 = 0; if (actions[i].gamepad2) *(int *)actions[i].gamepad2 = 0; } for (int i = 0; i < NUMKEYS; i++) keyactionlist[i][0] = '\0'; } while (!feof(file)) { char cvar[64] = ""; char value[256] = ""; if (fscanf(file, "%63s %255[^\n]\n", cvar, value) != 2) continue; if (cvar[0] == ';') continue; if (M_StringCompare(cvar, "bind")) { bind_cmd_func2("bind", value); bindcount++; continue; } else if (M_StringCompare(cvar, "alias")) { if (!togglingvanilla) alias_cmd_func2("alias", value); continue; } // Strip off trailing non-printable characters (\r characters from DOS text files) while (*value && !isprint((unsigned char)value[strlen(value) - 1])) value[strlen(value) - 1] = '\0'; if (togglingvanilla) { char *value_free = uncommify(value); C_ValidateInput(M_StringJoin(cvar, " ", value_free, NULL)); free(value_free); continue; } // Find the setting in the list for (int i = 0; i < arrlen(cvars); i++) { char *s; if (!M_StringCompare(cvar, cvars[i].name)) continue; // not this one if (M_StringStartsWith(cvar, "stat_")) statcount++; else cvarcount++; // parameter found switch (cvars[i].type) { case DEFAULT_STRING: s = M_StringDuplicate(value + 1); s[strlen(s) - 1] = '\0'; *(char **)cvars[i].location = s; break; case DEFAULT_INT: { char *value_free = uncommify(value); M_StringCopy(value, value_free, sizeof(value)); *(int *)cvars[i].location = ParseIntParameter(value, cvars[i].valuealiastype); free(value_free); break; } case DEFAULT_INT_UNSIGNED: { char *value_free = uncommify(value); M_StringCopy(value, value_free, sizeof(value)); sscanf(value, "%10u", (unsigned int *)cvars[i].location); free(value_free); break; } case DEFAULT_INT_PERCENT: { char *value_free = uncommify(value); M_StringCopy(value, value_free, sizeof(value)); s = M_StringDuplicate(value); if (*s && s[strlen(s) - 1] == '%') s[strlen(s) - 1] = '\0'; *(int *)cvars[i].location = ParseIntParameter(s, cvars[i].valuealiastype); free(value_free); break; } case DEFAULT_FLOAT: { char *value_free = uncommify(value); M_StringCopy(value, value_free, sizeof(value)); *(float *)cvars[i].location = ParseFloatParameter(value, cvars[i].valuealiastype); free(value_free); break; } case DEFAULT_FLOAT_PERCENT: { char *value_free = uncommify(value); M_StringCopy(value, value_free, sizeof(value)); s = M_StringDuplicate(value); if (*s && s[strlen(s) - 1] == '%') s[strlen(s) - 1] = '\0'; *(float *)cvars[i].location = ParseFloatParameter(s, cvars[i].valuealiastype); free(value_free); break; } case DEFAULT_OTHER: *(char **)cvars[i].location = M_StringDuplicate(value); break; } // finish break; } } fclose(file); if (!togglingvanilla) { char *cvarcount_str = commify(cvarcount); char *statcount_str = commify(statcount); char *bindcount_str = commify(bindcount); C_Output("Loaded %s CVARs and %s player stats from <b>%s</b>.", cvarcount_str, statcount_str, filename); C_Output("Bound %s actions to the keyboard, mouse and gamepad.", bindcount_str); M_CheckCVARs(); cvarsloaded = true; free(cvarcount_str); free(statcount_str); free(bindcount_str); } }
static void LoadDefaultCollection(default_collection_t *collection) { default_t *def; FILE *f; char defname[80]; char strparm[100]; char *s; int intparm; // read the file in, overriding any set defaults f = fopen(collection->filename, "r"); if (f == NULL) { // File not opened, but don't complain. // It's probably just the first time they ran the game. return; } while (!feof(f)) { if (fscanf (f, "%79s %[^\n]\n", defname, strparm) != 2) { // This line doesn't match continue; } // Strip off trailing non-printable characters (\r characters // from DOS text files) while (strlen(strparm) > 0 && !isprint(strparm[strlen(strparm)-1])) { strparm[strlen(strparm)-1] = '\0'; } // Find the setting in the list def = SearchCollection(collection, defname); if (def == NULL || !def->bound) { // Unknown variable? Unbound variables are also treated // as unknown. continue; } // parameter found switch (def->type) { case DEFAULT_STRING: s = strdup(strparm + 1); s[strlen(s) - 1] = '\0'; * (char **) def->location = s; break; case DEFAULT_INT: case DEFAULT_INT_HEX: * (int *) def->location = ParseIntParameter(strparm); break; case DEFAULT_KEY: // translate scancodes read from config // file (save the old value in untranslated) intparm = ParseIntParameter(strparm); def->untranslated = intparm; if (intparm >= 0 && intparm < 128) { intparm = scantokey[intparm]; } else { intparm = 0; } def->original_translated = intparm; * (int *) def->location = intparm; break; case DEFAULT_FLOAT: * (float *) def->location = (float) atof(strparm); break; } } fclose (f); }
static void LoadDefaultCollection(default_collection_t *collection) { default_t *defaults = collection->defaults; int i; FILE* f; char defname[80]; char strparm[100]; // read the file in, overriding any set defaults f = fopen(collection->filename, "r"); if (!f) { // File not opened, but don't complain return; } while (!feof(f)) { if (fscanf (f, "%79s %[^\n]\n", defname, strparm) != 2) { // This line doesn't match continue; } // Strip off trailing non-printable characters (\r characters // from DOS text files) while (strlen(strparm) > 0 && !isprint(strparm[strlen(strparm)-1])) { strparm[strlen(strparm)-1] = '\0'; } // Find the setting in the list for (i=0; i<collection->numdefaults; ++i) { default_t *def = &collection->defaults[i]; char *s; int intparm; if (strcmp(defname, def->name) != 0) { // not this one continue; } // parameter found switch (def->type) { case DEFAULT_STRING: s = strdup(strparm + 1); s[strlen(s) - 1] = '\0'; * (char **) def->location = s; break; case DEFAULT_INT: case DEFAULT_INT_HEX: * (int *) def->location = ParseIntParameter(strparm); break; case DEFAULT_KEY: // translate scancodes read from config // file (save the old value in untranslated) intparm = ParseIntParameter(strparm); defaults[i].untranslated = intparm; if (intparm >= 0 && intparm < 128) { intparm = scantokey[intparm]; } else { intparm = 0; } defaults[i].original_translated = intparm; * (int *) def->location = intparm; break; case DEFAULT_FLOAT: * (float *) def->location = (float)atof(strparm); break; } // finish break; } } fclose (f); }