OpenTTD Source 20261112-master-gbcbec5e63f
script_config.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "../stdafx.h"
11#include "../settings_type.h"
12#include "script_info.hpp"
13#include "../textfile_gui.h"
14#include <charconv>
15
16#include "table/strings.h"
17
18#include "../safeguards.h"
19
20void ScriptConfig::Change(std::optional<std::string_view> name, int version, bool force_exact_match)
21{
22 if (name.has_value()) {
23 this->name = name.value();
24 this->info = this->FindInfo(this->name, version, force_exact_match);
25 } else {
26 this->info = nullptr;
27 }
28 this->version = (this->info == nullptr) ? -1 : this->info->GetVersion();
29 this->config_list.reset();
30 this->to_load_data.reset();
31
32 this->ClearConfigList();
33}
34
36{
37 this->name = config.name;
38 this->info = config.info;
39 this->version = config.version;
40 this->to_load_data.reset();
41
42 for (const auto &item : config.settings) {
43 this->settings[item.first] = item.second;
44 }
45}
46
48{
49 return this->info;
50}
51
53{
54 if (this->info != nullptr) return this->info->GetConfigList();
55 if (this->config_list == nullptr) {
56 this->config_list = std::make_unique<ScriptConfigItemList>();
57 }
58 return this->config_list.get();
59}
60
62{
63 this->settings.clear();
64}
65
67{
68 for (const auto &item : *this->GetConfigList()) {
69 if (!item.flags.Test(ScriptConfigFlag::InGame)) {
70 this->SetSetting(item.name, this->GetSetting(item.name));
71 }
72 }
73}
74
75int ScriptConfig::GetSetting(const std::string &name) const
76{
77 const auto it = this->settings.find(name);
78 if (it == this->settings.end()) return this->info->GetSettingDefaultValue(name);
79 return it->second;
80}
81
82void ScriptConfig::SetSetting(std::string_view name, int value)
83{
84 /* You can only set Script specific settings if an Script is selected. */
85 if (this->info == nullptr) return;
86
87 const ScriptConfigItem *config_item = this->info->GetConfigItem(name);
88 if (config_item == nullptr) return;
89
90 value = Clamp(value, config_item->min_value, config_item->max_value);
91
92 this->settings[std::string{name}] = value;
93}
94
96{
97 this->settings.clear();
98}
99
101{
102 if (this->info == nullptr) return ResetSettings();
103
104 for (SettingValueList::iterator it = this->settings.begin(); it != this->settings.end();) {
105 const ScriptConfigItem *config_item = this->info->GetConfigItem(it->first);
106 assert(config_item != nullptr);
107
108 bool editable = yet_to_start || config_item->flags.Test(ScriptConfigFlag::InGame);
109 bool visible = _settings_client.gui.ai_developer_tools || !config_item->flags.Test(ScriptConfigFlag::Developer);
110
111 if (editable && visible) {
112 it = this->settings.erase(it);
113 } else {
114 it++;
115 }
116 }
117}
118
120{
121 return this->info != nullptr;
122}
123
124const std::string &ScriptConfig::GetName() const
125{
126 return this->name;
127}
128
130{
131 return this->version;
132}
133
134void ScriptConfig::StringToSettings(std::string_view value)
135{
136 std::string_view to_process = value;
137 for (;;) {
138 /* Analyze the string ('name=value,name=value\0') */
139 size_t pos = to_process.find_first_of('=');
140 if (pos == std::string_view::npos) return;
141
142 std::string_view item_name = to_process.substr(0, pos);
143
144 to_process.remove_prefix(pos + 1);
145 pos = to_process.find_first_of(',');
146 int item_value = 0;
147 std::from_chars(to_process.data(), to_process.data() + std::min(pos, to_process.size()), item_value);
148
149 this->SetSetting(item_name, item_value);
150
151 if (pos == std::string_view::npos) return;
152 to_process.remove_prefix(pos + 1);
153 }
154}
155
157{
158 if (this->settings.empty()) return {};
159
160 std::string result;
161 for (const auto &item : this->settings) {
162 format_append(result, "{}={},", item.first, item.second);
163 }
164
165 /* Remove the last ','. */
166 result.resize(result.size() - 1);
167 return result;
168}
169
170std::optional<std::string> ScriptConfig::GetTextfile(TextfileType type, CompanyID slot) const
171{
172 if (slot == CompanyID::Invalid() || this->GetInfo() == nullptr) return std::nullopt;
173
174 return ::GetTextfile(type, (slot == OWNER_DEITY) ? Subdirectory::Gs : Subdirectory::Ai, this->GetInfo()->GetMainScript());
175}
176
177void ScriptConfig::SetToLoadData(ScriptInstance::ScriptData *data)
178{
179 this->to_load_data.reset(data);
180}
181
182ScriptInstance::ScriptData *ScriptConfig::GetToLoadData()
183{
184 return this->to_load_data.get();
185}
186
187static std::pair<StringParameter, StringParameter> GetValueParams(const ScriptConfigItem &config_item, int value)
188{
189 if (config_item.flags.Test(ScriptConfigFlag::Boolean)) return {value != 0 ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF, {}};
190
191 auto it = config_item.labels.find(value);
192 if (it != std::end(config_item.labels)) return {STR_JUST_RAW_STRING, it->second};
193
194 return {STR_JUST_INT, value};
195}
196
202std::string ScriptConfigItem::GetString(int value) const
203{
204 auto [param1, param2] = GetValueParams(*this, value);
205 return this->description.empty()
206 ? ::GetString(STR_JUST_STRING1, param1, param2)
207 : ::GetString(STR_AI_SETTINGS_SETTING, this->description, param1, param2);
208}
209
218
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
SettingValueList settings
List with all setting=>value pairs that are configure for this Script.
std::optional< std::string > GetTextfile(TextfileType type, CompanyID slot) const
Search a textfile file next to this script.
void AnchorUnchangeableSettings()
As long as the default of a setting has not been changed, the value of the setting is not stored.
bool HasScript() const
Is this config attached to an Script?
ScriptConfig()=default
Default constructor.
void SetSetting(std::string_view name, int value)
Set the value of a setting for this config.
void Change(std::optional< std::string_view > name, int version=-1, bool force_exact_match=false)
Set another Script to be loaded in this slot.
void StringToSettings(std::string_view value)
Convert a string which is stored in the config file or savegames to custom settings of this Script.
int version
Version of the Script.
virtual ScriptInfo * FindInfo(const std::string &name, int version, bool force_exact_match)=0
Finds the appropriate ScriptInfo for a given script name and version.
const std::string & GetName() const
Get the name of the Script.
class ScriptInfo * info
ScriptInfo object for related to this Script version.
std::unique_ptr< ScriptConfigItemList > config_list
List with all settings defined by this Script.
int GetVersion() const
Get the version of the Script.
void ClearConfigList()
Routine that clears the config list.
std::unique_ptr< ScriptInstance::ScriptData > to_load_data
Data to load after the Script start.
class ScriptInfo * GetInfo() const
Get the ScriptInfo linked to this ScriptConfig.
void ResetSettings()
Reset all settings to their default value.
std::string name
Name of the Script.
const ScriptConfigItemList * GetConfigList()
Get the config list for this ScriptConfig.
int GetSetting(const std::string &name) const
Get the value of a setting for this config.
std::string SettingsToString() const
Convert the custom settings to a string that can be stored in the config file or savegames.
void ResetEditableSettings(bool yet_to_start)
Reset only editable and visible settings to their default value.
All static information from an Script like name, version, etc.
static constexpr Owner OWNER_DEITY
The object is owned by a superuser / goal script.
@ Ai
Subdirectory for all AI files.
Definition fileio_type.h:98
@ Gs
Subdirectory for all game scripts.
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition gfx_type.h:315
@ LightBlue
Light blue colour.
Definition gfx_type.h:331
@ Orange
Orange colour.
Definition gfx_type.h:324
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition math_func.hpp:79
A number of safeguards to prevent using unsafe methods.
std::vector< ScriptConfigItem > ScriptConfigItemList
List of ScriptConfig items.
@ Boolean
This value is a boolean (either 0 (false) or 1 (true) ).
@ Developer
This setting will only be visible when the Script development tools are active.
@ InGame
This setting can be changed while the Script is running.
ScriptInfo keeps track of all information of a script, like Author, Description, ....
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:60
Types related to global configuration settings.
Definition of base types and functions in a cross-platform compatible way.
Info about a single Script setting.
ScriptConfigFlags flags
Flags for the configuration setting.
std::string GetString(int value) const
Get string to display this setting in the configuration interface.
LabelMapping labels
Text labels for the integer values.
int min_value
The minimal value this configuration setting can have.
int max_value
The maximal value this configuration setting can have.
std::string description
The description of the configuration setting.
TextColour GetColour() const
Get text colour to display this setting in the configuration interface.
GUI functions related to textfiles.
TextfileType
Additional text files accompanying Tar archives.