File diff r3118:5a3c18a19d9f → r3119:c3eb58663989
settings.c
Show inline comments
 
@@ -8,12 +8,14 @@
 
#include "screenshot.h"
 
#include "sound.h"
 
#include "string.h"
 
#include "variables.h"
 
#include "network.h"
 
#include "settings.h"
 
#include "command.h"
 
#include "console.h"
 
#include "saveload.h"
 

	
 
typedef struct IniFile IniFile;
 
typedef struct IniItem IniItem;
 
typedef struct IniGroup IniGroup;
 
typedef struct SettingsMemoryPool SettingsMemoryPool;
 
@@ -1258,12 +1260,35 @@ void SaveToConfig(void)
 
const SettingDesc *GetSettingDescription(uint index)
 
{
 
	if (index >= lengthof(_patch_settings)) return NULL;
 
	return &_patch_settings[index];
 
}
 

	
 
/** Network-safe changing of patch-settings (server-only).
 
 * @param p1 the index of the patch in the SettingDesc array which identifies it
 
 * @param p2 the new value for the patch
 
 * The new value is properly clamped to its minimum/maximum when setting
 
 * @see _patch_settings
 
 */
 
int32 CmdChangePatchSetting(int x, int y, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	const SettingDesc *sd = GetSettingDescription(p1);
 

	
 
	if (sd == NULL) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		Patches *patches_ptr = &_patches;
 
		void *var = ini_get_variable(&sd->save, patches_ptr);
 
		Write_ValidateSetting(var, sd, (int32)p2);
 

	
 
		InvalidateWindow(WC_GAME_OPTIONS, 0);
 
	}
 

	
 
	return 0;
 
}
 

	
 
/* Top function to save the new value of an element of the Patches struct
 
 * @param index offset in the SettingDesc array of the Patches struct which
 
 * identifies the patch member we want to change
 
 * @param object pointer to a valid patches struct that has its settings change.
 
 * This only affects patch-members that are not needed to be the same on all
 
 * clients in a network game.
 
@@ -1280,12 +1305,78 @@ void SetPatchValue(uint index, const Pat
 
		Write_ValidateSetting(var, sd, value);
 
	} else {
 
		DoCommandP(0, index, value, NULL, CMD_CHANGE_PATCH_SETTING);
 
	}
 
}
 

	
 
static const SettingDesc *GetPatchFromName(const char *name, uint *i)
 
{
 
	const SettingDesc *sd;
 

	
 
	for (*i = 0, sd = _patch_settings; sd->save.cmd != SL_END; sd++, *i++) {
 
		if (strncmp(sd->desc.name, name, sizeof(sd->desc.name)) == 0) return sd;
 
	}
 

	
 
	return NULL;
 
}
 

	
 
/* Those 2 functions need to be here, else we have to make some stuff non-static
 
 * and besides, it is also better to keep stuff like this at the same place */
 
void IConsoleSetPatchSetting(const char *name, const char *value)
 
{
 
	char newval[20];
 
	int val;
 
	uint index;
 
	const SettingDesc *sd = GetPatchFromName(name, &index);
 
	const Patches *patches_ptr;
 
	void *ptr;
 

	
 
	if (sd == NULL) {
 
		IConsolePrintF(_icolour_warn, "'%s' is an unknown patch setting.", name);
 
		return;
 
	}
 

	
 
	sscanf(value, "%d", &val);
 
	patches_ptr = &_patches;
 
	ptr = ini_get_variable(&sd->save, patches_ptr);
 

	
 
	SetPatchValue(index, patches_ptr, val);
 

	
 
	if (sd->desc.cmd == SDT_BOOLX) {
 
		snprintf(newval, sizeof(newval), (*(bool*)ptr == 1) ? "on" : "off");
 
	} else {
 
		snprintf(newval, sizeof(newval), "%d", (int32)ReadValue(ptr, sd->save.conv));
 
	}
 

	
 
	IConsolePrintF(_icolour_warn, "'%s' changed to:  %s", name, newval);
 
}
 

	
 
void IConsoleGetPatchSetting(const char *name)
 
{
 
	char value[20];
 
	uint index;
 
	const SettingDesc *sd = GetPatchFromName(name, &index);
 
	const void *ptr;
 

	
 
	if (sd == NULL) {
 
		IConsolePrintF(_icolour_warn, "'%s' is an unknown patch setting.", name);
 
		return;
 
	}
 

	
 
	ptr = ini_get_variable(&sd->save, &_patches);
 

	
 
	if (sd->desc.cmd == SDT_BOOLX) {
 
		snprintf(value, sizeof(value), (*(bool*)ptr == 1) ? "on" : "off");
 
	} else {
 
		snprintf(value, sizeof(value), "%d", (int32)ReadValue(ptr, sd->save.conv));
 
	}
 

	
 
	IConsolePrintF(_icolour_warn, "Current value for '%s' is: '%s' (min: %s%d, max: %d)",
 
		name, value, (sd->desc.flags & SGF_0ISDISABLED) ? "(0) " : "", sd->desc.min, sd->desc.max);
 
}
 

	
 
/** Save and load handler for patches/settings
 
 * @param osd SettingDesc struct containing all information
 
 * @param object can be either NULL in which case we load global variables or
 
 * a pointer to a struct which is getting saved */
 
static void LoadSettings(const SettingDesc *osd, void *object)
 
{