OpenTTD Source  20241108-master-g80f628063a
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 <http://www.gnu.org/licenses/>.
6  */
7 
10 #include "../stdafx.h"
11 #include "../settings_type.h"
12 #include "../core/random_func.hpp"
13 #include "script_info.hpp"
14 #include "api/script_object.hpp"
15 #include "../textfile_gui.h"
16 #include "../string_func.h"
17 #include <charconv>
18 
19 #include "../safeguards.h"
20 
21 void ScriptConfig::Change(std::optional<const std::string> name, int version, bool force_exact_match)
22 {
23  if (name.has_value()) {
24  this->name = std::move(name.value());
25  this->info = this->FindInfo(this->name, version, force_exact_match);
26  } else {
27  this->info = nullptr;
28  }
29  this->version = (info == nullptr) ? -1 : info->GetVersion();
30  this->config_list.reset();
31  this->to_load_data.reset();
32 
33  this->ClearConfigList();
34 }
35 
36 ScriptConfig::ScriptConfig(const ScriptConfig *config)
37 {
38  this->name = config->name;
39  this->info = config->info;
40  this->version = config->version;
41  this->to_load_data.reset();
42 
43  for (const auto &item : config->settings) {
44  this->settings[item.first] = item.second;
45  }
46 }
47 
49 {
50  this->ResetSettings();
51  this->to_load_data.reset();
52 }
53 
55 {
56  return this->info;
57 }
58 
60 {
61  if (this->info != nullptr) return this->info->GetConfigList();
62  if (this->config_list == nullptr) {
63  this->config_list = std::make_unique<ScriptConfigItemList>();
64  }
65  return this->config_list.get();
66 }
67 
69 {
70  this->settings.clear();
71 }
72 
74 {
75  for (const auto &item : *this->GetConfigList()) {
76  if ((item.flags & SCRIPTCONFIG_INGAME) == 0) {
77  this->SetSetting(item.name, this->GetSetting(item.name));
78  }
79  }
80 }
81 
82 int ScriptConfig::GetSetting(const std::string &name) const
83 {
84  const auto it = this->settings.find(name);
85  if (it == this->settings.end()) return this->info->GetSettingDefaultValue(name);
86  return (*it).second;
87 }
88 
89 void ScriptConfig::SetSetting(const std::string_view name, int value)
90 {
91  /* You can only set Script specific settings if an Script is selected. */
92  if (this->info == nullptr) return;
93 
94  const ScriptConfigItem *config_item = this->info->GetConfigItem(name);
95  if (config_item == nullptr) return;
96 
97  value = Clamp(value, config_item->min_value, config_item->max_value);
98 
99  this->settings[std::string{name}] = value;
100 }
101 
103 {
104  this->settings.clear();
105 }
106 
107 void ScriptConfig::ResetEditableSettings(bool yet_to_start)
108 {
109  if (this->info == nullptr) return ResetSettings();
110 
111  for (SettingValueList::iterator it = this->settings.begin(); it != this->settings.end();) {
112  const ScriptConfigItem *config_item = this->info->GetConfigItem(it->first);
113  assert(config_item != nullptr);
114 
115  bool editable = yet_to_start || (config_item->flags & SCRIPTCONFIG_INGAME) != 0;
116  bool visible = _settings_client.gui.ai_developer_tools || (config_item->flags & SCRIPTCONFIG_DEVELOPER) == 0;
117 
118  if (editable && visible) {
119  it = this->settings.erase(it);
120  } else {
121  it++;
122  }
123  }
124 }
125 
127 {
128  return this->info != nullptr;
129 }
130 
131 const std::string &ScriptConfig::GetName() const
132 {
133  return this->name;
134 }
135 
137 {
138  return this->version;
139 }
140 
141 void ScriptConfig::StringToSettings(const std::string &value)
142 {
143  std::string_view to_process = value;
144  for (;;) {
145  /* Analyze the string ('name=value,name=value\0') */
146  size_t pos = to_process.find_first_of('=');
147  if (pos == std::string_view::npos) return;
148 
149  std::string_view item_name = to_process.substr(0, pos);
150 
151  to_process.remove_prefix(pos + 1);
152  pos = to_process.find_first_of(',');
153  int item_value = 0;
154  std::from_chars(to_process.data(), to_process.data() + std::min(pos, to_process.size()), item_value);
155 
156  this->SetSetting(item_name, item_value);
157 
158  if (pos == std::string_view::npos) return;
159  to_process.remove_prefix(pos + 1);
160  }
161 }
162 
164 {
165  if (this->settings.empty()) return {};
166 
167  std::string result;
168  for (const auto &item : this->settings) {
169  fmt::format_to(std::back_inserter(result), "{}={},", item.first, item.second);
170  }
171 
172  /* Remove the last ','. */
173  result.resize(result.size() - 1);
174  return result;
175 }
176 
177 std::optional<std::string> ScriptConfig::GetTextfile(TextfileType type, CompanyID slot) const
178 {
179  if (slot == INVALID_COMPANY || this->GetInfo() == nullptr) return std::nullopt;
180 
181  return ::GetTextfile(type, (slot == OWNER_DEITY) ? GAME_DIR : AI_DIR, this->GetInfo()->GetMainScript());
182 }
183 
184 void ScriptConfig::SetToLoadData(ScriptInstance::ScriptData *data)
185 {
186  this->to_load_data.reset(data);
187 }
188 
189 ScriptInstance::ScriptData *ScriptConfig::GetToLoadData()
190 {
191  return this->to_load_data.get();
192 }
193 
Script settings.
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? In other words, is there a Script that is assigned to this slot...
virtual ScriptInfo * FindInfo(const std::string &name, int version, bool force_exact_match)=0
This function should call back to the Scanner in charge of this Config, to find the ScriptInfo belong...
void Change(std::optional< const std::string > name, int version=-1, bool force_exact_match=false)
Set another Script to be loaded in this slot.
virtual ~ScriptConfig()
Delete an Script configuration.
int version
Version of the Script.
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 SetSetting(const std::string_view name, int value)
Set the value of a setting for this config.
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.
void StringToSettings(const std::string &value)
Convert a string which is stored in the config file or savegames to custom settings of this Script.
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.
Definition: script_info.hpp:30
const ScriptConfigItemList * GetConfigList() const
Get the config list for this Script.
int GetVersion() const
Get the version of the script.
Definition: script_info.hpp:61
int GetSettingDefaultValue(const std::string &name) const
Get the default value for a setting.
const ScriptConfigItem * GetConfigItem(const std::string_view name) const
Get the description of a certain Script config option.
Owner
Enum for all companies/owners.
Definition: company_type.h:18
@ INVALID_COMPANY
An invalid company.
Definition: company_type.h:30
@ OWNER_DEITY
The object is owned by a superuser / goal script.
Definition: company_type.h:27
@ AI_DIR
Subdirectory for all AI files.
Definition: fileio_type.h:126
@ GAME_DIR
Subdirectory for all game scripts.
Definition: fileio_type.h:128
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:79
@ SCRIPTCONFIG_INGAME
This setting can be changed while the Script is running.
@ SCRIPTCONFIG_DEVELOPER
This setting will only be visible when the Script development tools are active.
std::vector< ScriptConfigItem > ScriptConfigItemList
List of ScriptConfig items.
ScriptInfo keeps track of all information of a script, like Author, Description, ....
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:56
GUISettings gui
settings related to the GUI
bool ai_developer_tools
activate AI/GS developer tools
Info about a single Script setting.
ScriptConfigFlags flags
Flags for the configuration setting.
int min_value
The minimal value this configuration setting can have.
int max_value
The maximal value this configuration setting can have.
std::optional< std::string > GetTextfile(TextfileType type, Subdirectory dir, const std::string &filename)
Search a textfile file next to the given content.
TextfileType
Additional text files accompanying Tar archives.
Definition: textfile_type.h:14