Changeset - r25579:86a9c48defe8
[Not reviewed]
master
0 3 0
Patric Stout - 3 years ago 2021-05-30 08:55:52
truebrain@openttd.org
Codechange: add a wrapper function to find all settings based on prefix (#9312)
3 files changed with 19 insertions and 23 deletions:
0 comments (0 inline, 0 general)
src/saveload/linkgraph_sl.cpp
Show inline comments
 
@@ -20,8 +20,6 @@
 
typedef LinkGraph::BaseNode Node;
 
typedef LinkGraph::BaseEdge Edge;
 

	
 
const SettingDesc *GetSettingDescription(uint index);
 

	
 
static uint16 _num_nodes;
 

	
 
/**
 
@@ -70,17 +68,10 @@ const SaveLoad *GetLinkGraphJobDesc()
 

	
 
	/* Build the SaveLoad array on first call and don't touch it later on */
 
	if (saveloads.size() == 0) {
 
		size_t prefixlen = strlen(prefix);
 
		GetSettingSaveLoadByPrefix(prefix, saveloads);
 

	
 
		int setting = 0;
 
		const SettingDesc *desc = GetSettingDescription(setting);
 
		while (desc != nullptr) {
 
			if (desc->name != nullptr && strncmp(desc->name, prefix, prefixlen) == 0) {
 
				SaveLoad sl = desc->save;
 
				sl.address_proc = proc;
 
				saveloads.push_back(sl);
 
			}
 
			desc = GetSettingDescription(++setting);
 
		for (auto &sl : saveloads) {
 
			sl.address_proc = proc;
 
		}
 

	
 
		int i = 0;
src/settings.cpp
Show inline comments
 
@@ -94,17 +94,6 @@ typedef void SettingDescProcList(IniFile
 
static bool IsSignedVarMemType(VarType vt);
 

	
 
/**
 
 * Get the setting at the given index into the settings table.
 
 * @param index The index to look for.
 
 * @return The setting at the given index, or nullptr when the index is invalid.
 
 */
 
const SettingDesc *GetSettingDescription(uint index)
 
{
 
	if (index >= _settings.size()) return nullptr;
 
	return _settings.begin()[index].get();
 
}
 

	
 
/**
 
 * Groups in openttd.cfg that are actually lists.
 
 */
 
static const char * const _list_group_names[] = {
 
@@ -1740,6 +1729,21 @@ static const SettingDesc *GetSettingFrom
 
}
 

	
 
/**
 
 * Get the SaveLoad from all settings matching the prefix.
 
 * @param prefix The prefix to look for.
 
 * @param saveloads A vector to store the result in.
 
 */
 
void GetSettingSaveLoadByPrefix(const char *prefix, std::vector<SaveLoad> &saveloads)
 
{
 
	size_t prefixlen = strlen(prefix);
 

	
 
	for (auto &sd : _settings) {
 
		if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
 
		if (strncmp(sd->name, prefix, prefixlen) == 0) saveloads.push_back(sd->save);
 
	}
 
}
 

	
 
/**
 
 * Given a name of setting, return a company setting description of it.
 
 * @param name  Name of the company setting to return a setting description of.
 
 * @return Pointer to the setting description of setting \a name if it can be found,
src/settings_internal.h
Show inline comments
 
@@ -300,6 +300,7 @@ struct NullSettingDesc : SettingDesc {
 
typedef std::initializer_list<std::unique_ptr<const SettingDesc>> SettingTable;
 

	
 
const SettingDesc *GetSettingFromName(const char *name);
 
void GetSettingSaveLoadByPrefix(const char *prefix, std::vector<SaveLoad> &saveloads);
 
bool SetSettingValue(const IntSettingDesc *sd, int32 value, bool force_newgame = false);
 
bool SetSettingValue(const StringSettingDesc *sd, const std::string value, bool force_newgame = false);
 

	
0 comments (0 inline, 0 general)