Changeset - r27435:a26109e60d6d
[Not reviewed]
master
0 4 0
Rubidium - 13 months ago 2023-05-24 20:46:22
rubidium@openttd.org
Codechange: use std::string to create the GRF parameter list
4 files changed with 10 insertions and 18 deletions:
0 comments (0 inline, 0 general)
src/newgrf_config.cpp
Show inline comments
 
@@ -705,35 +705,31 @@ GRFConfig *GetGRFConfig(uint32 grfid, ui
 
{
 
	GRFConfig *c;
 

	
 
	for (c = _grfconfig; c != nullptr; c = c->next) {
 
		if ((c->ident.grfid & mask) == (grfid & mask)) return c;
 
	}
 

	
 
	return nullptr;
 
}
 

	
 

	
 
/** Build a string containing space separated parameter values, and terminate */
 
char *GRFBuildParamList(char *dst, const GRFConfig *c, const char *last)
 
std::string GRFBuildParamList(const GRFConfig *c)
 
{
 
	uint i;
 

	
 
	/* Return an empty string if there are no parameters */
 
	if (c->num_params == 0) return strecpy(dst, "", last);
 

	
 
	for (i = 0; i < c->num_params; i++) {
 
		if (i > 0) dst = strecpy(dst, " ", last);
 
		dst += seprintf(dst, last, "%d", c->param[i]);
 
	std::string result;
 
	for (uint i = 0; i < c->num_params; i++) {
 
		if (!result.empty()) result += ' ';
 
		result += std::to_string(c->param[i]);
 
	}
 
	return dst;
 
	return result;
 
}
 

	
 
/**
 
 * Search a textfile file next to this NewGRF.
 
 * @param type The type of the textfile to search for.
 
 * @return The filename for the textfile.
 
 */
 
std::optional<std::string> GRFConfig::GetTextfile(TextfileType type) const
 
{
 
	return ::GetTextfile(type, NEWGRF_DIR, this->filename);
 
}
src/newgrf_config.h
Show inline comments
 
@@ -211,21 +211,21 @@ struct NewGRFScanCallback {
 
size_t GRFGetSizeOfDataSection(FILE *f);
 

	
 
void ScanNewGRFFiles(NewGRFScanCallback *callback);
 
const GRFConfig *FindGRFConfig(uint32 grfid, FindGRFConfigMode mode, const MD5Hash *md5sum = nullptr, uint32 desired_version = 0);
 
GRFConfig *GetGRFConfig(uint32 grfid, uint32 mask = 0xFFFFFFFF);
 
GRFConfig **CopyGRFConfigList(GRFConfig **dst, const GRFConfig *src, bool init_only);
 
void AppendStaticGRFConfigs(GRFConfig **dst);
 
void AppendToGRFConfigList(GRFConfig **dst, GRFConfig *el);
 
void ClearGRFConfigList(GRFConfig **config);
 
void ResetGRFConfig(bool defaults);
 
GRFListCompatibility IsGoodGRFConfigList(GRFConfig *grfconfig);
 
bool FillGRFDetails(GRFConfig *config, bool is_static, Subdirectory subdir = NEWGRF_DIR);
 
char *GRFBuildParamList(char *dst, const GRFConfig *c, const char *last);
 
std::string GRFBuildParamList(const GRFConfig *c);
 

	
 
/* In newgrf_gui.cpp */
 
void ShowNewGRFSettings(bool editable, bool show_params, bool exec_changes, GRFConfig **config);
 

	
 
void UpdateNewGRFScanStatus(uint num, const char *name);
 
void UpdateNewGRFConfigPalette(int32 new_value = 0);
 

	
 
#endif /* NEWGRF_CONFIG_H */
src/newgrf_gui.cpp
Show inline comments
 
@@ -103,28 +103,27 @@ static void ShowNewGRFInfo(const GRFConf
 
		SetDParam(0, c->min_loadable_version);
 
		tr.top = DrawStringMultiLine(tr, STR_NEWGRF_SETTINGS_MIN_VERSION);
 
	}
 

	
 
	/* Prepare and draw MD5 sum */
 
	tmp = FormatArrayAsHex(c->ident.md5sum);
 
	SetDParamStr(0, tmp);
 
	tr.top = DrawStringMultiLine(tr, STR_NEWGRF_SETTINGS_MD5SUM);
 

	
 
	/* Show GRF parameter list */
 
	if (show_params) {
 
		if (c->num_params > 0) {
 
			char buff[256];
 
			GRFBuildParamList(buff, c, lastof(buff));
 
			std::string params = GRFBuildParamList(c);
 
			SetDParam(0, STR_JUST_RAW_STRING);
 
			SetDParamStr(1, buff);
 
			SetDParamStr(1, params);
 
		} else {
 
			SetDParam(0, STR_NEWGRF_SETTINGS_PARAMETER_NONE);
 
		}
 
		tr.top = DrawStringMultiLine(tr, STR_NEWGRF_SETTINGS_PARAMETER);
 

	
 
		/* Draw the palette of the NewGRF */
 
		if (c->palette & GRFP_BLT_32BPP) {
 
			SetDParam(0, (c->palette & GRFP_USE_WINDOWS) ? STR_NEWGRF_SETTINGS_PALETTE_LEGACY_32BPP : STR_NEWGRF_SETTINGS_PALETTE_DEFAULT_32BPP);
 
		} else {
 
			SetDParam(0, (c->palette & GRFP_USE_WINDOWS) ? STR_NEWGRF_SETTINGS_PALETTE_LEGACY : STR_NEWGRF_SETTINGS_PALETTE_DEFAULT);
 
		}
 
		tr.top = DrawStringMultiLine(tr, STR_NEWGRF_SETTINGS_PALETTE);
src/settings.cpp
Show inline comments
 
@@ -1125,30 +1125,27 @@ static void SaveVersionInConfig(IniFile 
 
	group->GetItem("version_number", true)->SetValue(fmt::format("{:08X}", _openttd_newgrf_version));
 
	group->GetItem("ini_version", true)->SetValue(std::to_string(INIFILE_VERSION));
 
}
 

	
 
/* Save a GRF configuration to the given group name */
 
static void GRFSaveConfig(IniFile &ini, const char *grpname, const GRFConfig *list)
 
{
 
	ini.RemoveGroup(grpname);
 
	IniGroup *group = ini.GetGroup(grpname);
 
	const GRFConfig *c;
 

	
 
	for (c = list; c != nullptr; c = c->next) {
 
		char params[512];
 
		GRFBuildParamList(params, c, lastof(params));
 

	
 
		std::string key = fmt::format("{:08X}|{}|{}", BSWAP32(c->ident.grfid),
 
				FormatArrayAsHex(c->ident.md5sum), c->filename);
 
		group->GetItem(key, true)->SetValue(params);
 
		group->GetItem(key, true)->SetValue(GRFBuildParamList(c));
 
	}
 
}
 

	
 
/* Common handler for saving/loading variables to the configuration file */
 
static void HandleSettingDescs(IniFile &generic_ini, IniFile &private_ini, IniFile &secrets_ini, SettingDescProc *proc, SettingDescProcList *proc_list, bool only_startup = false)
 
{
 
	proc(generic_ini, _misc_settings, "misc", nullptr, only_startup);
 
#if defined(_WIN32) && !defined(DEDICATED)
 
	proc(generic_ini, _win32_settings, "win32", nullptr, only_startup);
 
#endif /* _WIN32 */
 

	
 
	/* The name "patches" is a fallback, as every setting should sets its own group. */
0 comments (0 inline, 0 general)