Doc: Prepare for 14.0-RC2 release (#12248)
[openttd-github.git] / src / survey.cpp
blobf9310c976d2e0b391378737d43b9e9fde5810d3a
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 */
8 /** @file survey.cpp Functions to survey the current game / system, for crashlog and network-survey. */
10 #include "stdafx.h"
12 #include "survey.h"
14 #include "settings_table.h"
15 #include "network/network.h"
16 #include "rev.h"
17 #include "settings_type.h"
18 #include "timer/timer_game_tick.h"
19 #include "timer/timer_game_calendar.h"
20 #include "timer/timer_game_economy.h"
22 #include "currency.h"
23 #include "fontcache.h"
24 #include "language.h"
26 #include "ai/ai_info.hpp"
27 #include "game/game.hpp"
28 #include "game/game_info.hpp"
30 #include "music/music_driver.hpp"
31 #include "sound/sound_driver.hpp"
32 #include "video/video_driver.hpp"
34 #include "base_media_base.h"
35 #include "blitter/factory.hpp"
37 #include "social_integration.h"
39 #ifdef WITH_ALLEGRO
40 # include <allegro.h>
41 #endif /* WITH_ALLEGRO */
42 #ifdef WITH_FONTCONFIG
43 # include <fontconfig/fontconfig.h>
44 #endif /* WITH_FONTCONFIG */
45 #ifdef WITH_PNG
46 /* pngconf.h, included by png.h doesn't like something in the
47 * freetype headers. As such it's not alphabetically sorted. */
48 # include <png.h>
49 #endif /* WITH_PNG */
50 #ifdef WITH_FREETYPE
51 # include <ft2build.h>
52 # include FT_FREETYPE_H
53 #endif /* WITH_FREETYPE */
54 #ifdef WITH_HARFBUZZ
55 # include <hb.h>
56 #endif /* WITH_HARFBUZZ */
57 #ifdef WITH_ICU_I18N
58 # include <unicode/uversion.h>
59 #endif /* WITH_ICU_I18N */
60 #ifdef WITH_LIBLZMA
61 # include <lzma.h>
62 #endif
63 #ifdef WITH_LZO
64 #include <lzo/lzo1x.h>
65 #endif
66 #if defined(WITH_SDL) || defined(WITH_SDL2)
67 # include <SDL.h>
68 #endif /* WITH_SDL || WITH_SDL2 */
69 #ifdef WITH_ZLIB
70 # include <zlib.h>
71 #endif
72 #ifdef WITH_CURL
73 # include <curl/curl.h>
74 #endif
76 #include "safeguards.h"
78 NLOHMANN_JSON_SERIALIZE_ENUM(GRFStatus, {
79 {GRFStatus::GCS_UNKNOWN, "unknown"},
80 {GRFStatus::GCS_DISABLED, "disabled"},
81 {GRFStatus::GCS_NOT_FOUND, "not found"},
82 {GRFStatus::GCS_INITIALISED, "initialised"},
83 {GRFStatus::GCS_ACTIVATED, "activated"},
86 NLOHMANN_JSON_SERIALIZE_ENUM(SocialIntegrationPlugin::State, {
87 {SocialIntegrationPlugin::State::RUNNING, "running"},
88 {SocialIntegrationPlugin::State::FAILED, "failed"},
89 {SocialIntegrationPlugin::State::PLATFORM_NOT_RUNNING, "platform_not_running"},
90 {SocialIntegrationPlugin::State::UNLOADED, "unloaded"},
91 {SocialIntegrationPlugin::State::DUPLICATE, "duplicate"},
92 {SocialIntegrationPlugin::State::UNSUPPORTED_API, "unsupported_api"},
93 {SocialIntegrationPlugin::State::INVALID_SIGNATURE, "invalid_signature"},
97 /** Lookup table to convert a VehicleType to a string. */
98 static const std::string _vehicle_type_to_string[] = {
99 "train",
100 "roadveh",
101 "ship",
102 "aircraft",
106 * List of all the generic setting tables.
108 * There are a few tables that are special and not processed like the rest:
109 * - _currency_settings
110 * - _misc_settings
111 * - _company_settings
112 * - _win32_settings
113 * As such, they are not part of this list.
115 static auto &GenericSettingTables()
117 static const SettingTable _generic_setting_tables[] = {
118 _difficulty_settings,
119 _economy_settings,
120 _game_settings,
121 _gui_settings,
122 _linkgraph_settings,
123 _locale_settings,
124 _multimedia_settings,
125 _network_settings,
126 _news_display_settings,
127 _pathfinding_settings,
128 _script_settings,
129 _world_settings,
131 return _generic_setting_tables;
135 * Convert a settings table to JSON.
137 * @param survey The JSON object.
138 * @param table The settings table to convert.
139 * @param object The object to get the settings from.
140 * @param skip_if_default If true, skip any settings that are on their default value.
142 static void SurveySettingsTable(nlohmann::json &survey, const SettingTable &table, void *object, bool skip_if_default)
144 for (auto &desc : table) {
145 const SettingDesc *sd = GetSettingDesc(desc);
146 /* Skip any old settings we no longer save/load. */
147 if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
149 auto name = sd->GetName();
150 if (skip_if_default && sd->IsDefaultValue(object)) continue;
151 survey[name] = sd->FormatValue(object);
156 * Convert settings to JSON.
158 * @param survey The JSON object.
160 void SurveySettings(nlohmann::json &survey, bool skip_if_default)
162 SurveySettingsTable(survey, _misc_settings, nullptr, skip_if_default);
163 #if defined(_WIN32) && !defined(DEDICATED)
164 SurveySettingsTable(survey, _win32_settings, nullptr, skip_if_default);
165 #endif
166 for (auto &table : GenericSettingTables()) {
167 SurveySettingsTable(survey, table, &_settings_game, skip_if_default);
169 SurveySettingsTable(survey, _currency_settings, &_custom_currency, skip_if_default);
170 SurveySettingsTable(survey, _company_settings, &_settings_client.company, skip_if_default);
174 * Convert compiler information to JSON.
176 * @param survey The JSON object.
178 void SurveyCompiler(nlohmann::json &survey)
180 #if defined(_MSC_VER)
181 survey["name"] = "MSVC";
182 survey["version"] = _MSC_VER;
183 #elif defined(__ICC) && defined(__GNUC__)
184 survey["name"] = "ICC";
185 survey["version"] = __ICC;
186 # if defined(__GNUC__)
187 survey["extra"] = fmt::format("GCC {}.{}.{} mode", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__);
188 # endif
189 #elif defined(__GNUC__)
190 survey["name"] = "GCC";
191 survey["version"] = fmt::format("{}.{}.{}", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__);
192 #else
193 survey["name"] = "unknown";
194 #endif
196 #if defined(__VERSION__)
197 survey["extra"] = __VERSION__;
198 #endif
202 * Convert generic OpenTTD information to JSON.
204 * @param survey The JSON object.
206 void SurveyOpenTTD(nlohmann::json &survey)
208 survey["version"]["revision"] = std::string(_openttd_revision);
209 survey["version"]["modified"] = _openttd_revision_modified;
210 survey["version"]["tagged"] = _openttd_revision_tagged;
211 survey["version"]["hash"] = std::string(_openttd_revision_hash);
212 survey["version"]["newgrf"] = fmt::format("{:X}", _openttd_newgrf_version);
213 survey["version"]["content"] = std::string(_openttd_content_version);
214 survey["build_date"] = std::string(_openttd_build_date);
215 survey["bits"] =
216 #ifdef POINTER_IS_64BIT
218 #else
220 #endif
222 survey["endian"] =
223 #if (TTD_ENDIAN == TTD_LITTLE_ENDIAN)
224 "little"
225 #else
226 "big"
227 #endif
229 survey["dedicated_build"] =
230 #ifdef DEDICATED
231 "yes"
232 #else
233 "no"
234 #endif
239 * Convert game session information to JSON.
241 * @param survey The JSON object.
243 void SurveyGameSession(nlohmann::json &survey)
245 survey["id"] = _game_session_stats.savegame_id;
246 survey["seconds"] = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now() - _game_session_stats.start_time).count();
247 if (_game_session_stats.savegame_size.has_value()) {
248 survey["savegame_size"] = _game_session_stats.savegame_size.value();
253 * Convert generic game information to JSON.
255 * @param survey The JSON object.
257 void SurveyConfiguration(nlohmann::json &survey)
259 survey["network"] = _networking ? (_network_server ? "server" : "client") : "no";
260 if (_current_language != nullptr) {
261 survey["language"]["filename"] = _current_language->file.filename().string();
262 survey["language"]["name"] = _current_language->name;
263 survey["language"]["isocode"] = _current_language->isocode;
265 if (BlitterFactory::GetCurrentBlitter() != nullptr) {
266 survey["blitter"] = BlitterFactory::GetCurrentBlitter()->GetName();
268 if (MusicDriver::GetInstance() != nullptr) {
269 survey["music_driver"] = MusicDriver::GetInstance()->GetName();
271 if (SoundDriver::GetInstance() != nullptr) {
272 survey["sound_driver"] = SoundDriver::GetInstance()->GetName();
274 if (VideoDriver::GetInstance() != nullptr) {
275 survey["video_driver"] = VideoDriver::GetInstance()->GetName();
276 survey["video_info"] = VideoDriver::GetInstance()->GetInfoString();
278 if (BaseGraphics::GetUsedSet() != nullptr) {
279 survey["graphics_set"] = fmt::format("{}.{}", BaseGraphics::GetUsedSet()->name, BaseGraphics::GetUsedSet()->version);
280 const GRFConfig *extra_cfg = BaseGraphics::GetUsedSet()->GetExtraConfig();
281 if (extra_cfg != nullptr && extra_cfg->num_params > 0) {
282 survey["graphics_set_parameters"] = std::span<const uint32_t>(extra_cfg->param.data(), extra_cfg->num_params);
283 } else {
284 survey["graphics_set_parameters"] = std::span<const uint32_t>();
287 if (BaseMusic::GetUsedSet() != nullptr) {
288 survey["music_set"] = fmt::format("{}.{}", BaseMusic::GetUsedSet()->name, BaseMusic::GetUsedSet()->version);
290 if (BaseSounds::GetUsedSet() != nullptr) {
291 survey["sound_set"] = fmt::format("{}.{}", BaseSounds::GetUsedSet()->name, BaseSounds::GetUsedSet()->version);
296 * Convert font information to JSON.
298 * @param survey The JSON object.
300 void SurveyFont(nlohmann::json &survey)
302 survey["small"] = FontCache::Get(FS_SMALL)->GetFontName();
303 survey["medium"] = FontCache::Get(FS_NORMAL)->GetFontName();
304 survey["large"] = FontCache::Get(FS_LARGE)->GetFontName();
305 survey["mono"] = FontCache::Get(FS_MONO)->GetFontName();
309 * Convert company information to JSON.
311 * @param survey The JSON object.
313 void SurveyCompanies(nlohmann::json &survey)
315 for (const Company *c : Company::Iterate()) {
316 auto &company = survey[std::to_string(c->index)];
317 if (c->ai_info == nullptr) {
318 company["type"] = "human";
319 } else {
320 company["type"] = "ai";
321 company["script"] = fmt::format("{}.{}", c->ai_info->GetName(), c->ai_info->GetVersion());
324 for (VehicleType type = VEH_BEGIN; type < VEH_COMPANY_END; type++) {
325 uint amount = c->group_all[type].num_vehicle;
326 company["vehicles"][_vehicle_type_to_string[type]] = amount;
329 company["infrastructure"]["road"] = c->infrastructure.GetRoadTotal();
330 company["infrastructure"]["tram"] = c->infrastructure.GetTramTotal();
331 company["infrastructure"]["rail"] = c->infrastructure.GetRailTotal();
332 company["infrastructure"]["signal"] = c->infrastructure.signal;
333 company["infrastructure"]["water"] = c->infrastructure.water;
334 company["infrastructure"]["station"] = c->infrastructure.station;
335 company["infrastructure"]["airport"] = c->infrastructure.airport;
340 * Convert timer information to JSON.
342 * @param survey The JSON object.
344 void SurveyTimers(nlohmann::json &survey)
346 survey["ticks"] = TimerGameTick::counter;
348 TimerGameEconomy::YearMonthDay economy_ymd = TimerGameEconomy::ConvertDateToYMD(TimerGameEconomy::date);
349 survey["economy"] = fmt::format("{:04}-{:02}-{:02} ({})", economy_ymd.year, economy_ymd.month + 1, economy_ymd.day, TimerGameEconomy::date_fract);
351 TimerGameCalendar::YearMonthDay ymd = TimerGameCalendar::ConvertDateToYMD(TimerGameCalendar::date);
352 survey["calendar"] = fmt::format("{:04}-{:02}-{:02} ({})", ymd.year, ymd.month + 1, ymd.day, TimerGameCalendar::date_fract);
356 * Convert GRF information to JSON.
358 * @param survey The JSON object.
360 void SurveyGrfs(nlohmann::json &survey)
362 for (GRFConfig *c = _grfconfig; c != nullptr; c = c->next) {
363 auto grfid = fmt::format("{:08x}", BSWAP32(c->ident.grfid));
364 auto &grf = survey[grfid];
366 grf["md5sum"] = FormatArrayAsHex(c->ident.md5sum);
367 grf["status"] = c->status;
369 if ((c->palette & GRFP_GRF_MASK) == GRFP_GRF_UNSET) grf["palette"] = "unset";
370 if ((c->palette & GRFP_GRF_MASK) == GRFP_GRF_DOS) grf["palette"] = "dos";
371 if ((c->palette & GRFP_GRF_MASK) == GRFP_GRF_WINDOWS) grf["palette"] = "windows";
372 if ((c->palette & GRFP_GRF_MASK) == GRFP_GRF_ANY) grf["palette"] = "any";
374 if ((c->palette & GRFP_BLT_MASK) == GRFP_BLT_UNSET) grf["blitter"] = "unset";
375 if ((c->palette & GRFP_BLT_MASK) == GRFP_BLT_32BPP) grf["blitter"] = "32bpp";
377 grf["is_static"] = HasBit(c->flags, GCF_STATIC);
378 grf["parameters"] = std::span<const uint32_t>(c->param.data(), c->num_params);
383 * Convert game-script information to JSON.
385 * @param survey The JSON object.
387 void SurveyGameScript(nlohmann::json &survey)
389 if (Game::GetInfo() == nullptr) return;
391 survey = fmt::format("{}.{}", Game::GetInfo()->GetName(), Game::GetInfo()->GetVersion());
395 * Convert compiled libraries information to JSON.
397 * @param survey The JSON object.
399 void SurveyLibraries(nlohmann::json &survey)
401 #ifdef WITH_ALLEGRO
402 survey["allegro"] = std::string(allegro_id);
403 #endif /* WITH_ALLEGRO */
405 #ifdef WITH_FONTCONFIG
406 int version = FcGetVersion();
407 survey["fontconfig"] = fmt::format("{}.{}.{}", version / 10000, (version / 100) % 100, version % 100);
408 #endif /* WITH_FONTCONFIG */
410 #ifdef WITH_FREETYPE
411 FT_Library library;
412 int major, minor, patch;
413 FT_Init_FreeType(&library);
414 FT_Library_Version(library, &major, &minor, &patch);
415 FT_Done_FreeType(library);
416 survey["freetype"] = fmt::format("{}.{}.{}", major, minor, patch);
417 #endif /* WITH_FREETYPE */
419 #if defined(WITH_HARFBUZZ)
420 survey["harfbuzz"] = hb_version_string();
421 #endif /* WITH_HARFBUZZ */
423 #if defined(WITH_ICU_I18N)
424 /* 4 times 0-255, separated by dots (.) and a trailing '\0' */
425 char buf[4 * 3 + 3 + 1];
426 UVersionInfo ver;
427 u_getVersion(ver);
428 u_versionToString(ver, buf);
429 survey["icu_i18n"] = buf;
430 #endif /* WITH_ICU_I18N */
432 #ifdef WITH_LIBLZMA
433 survey["lzma"] = lzma_version_string();
434 #endif
436 #ifdef WITH_LZO
437 survey["lzo"] = lzo_version_string();
438 #endif
440 #ifdef WITH_PNG
441 survey["png"] = png_get_libpng_ver(nullptr);
442 #endif /* WITH_PNG */
444 #ifdef WITH_SDL
445 const SDL_version *sdl_v = SDL_Linked_Version();
446 survey["sdl"] = fmt::format("{}.{}.{}", sdl_v->major, sdl_v->minor, sdl_v->patch);
447 #elif defined(WITH_SDL2)
448 SDL_version sdl2_v;
449 SDL_GetVersion(&sdl2_v);
450 survey["sdl2"] = fmt::format("{}.{}.{}", sdl2_v.major, sdl2_v.minor, sdl2_v.patch);
451 #endif
453 #ifdef WITH_ZLIB
454 survey["zlib"] = zlibVersion();
455 #endif
457 #ifdef WITH_CURL
458 auto *curl_v = curl_version_info(CURLVERSION_NOW);
459 survey["curl"] = curl_v->version;
460 survey["curl_ssl"] = curl_v->ssl_version == nullptr ? "none" : curl_v->ssl_version;
461 #endif
465 * Convert plugin information to JSON.
467 * @param survey The JSON object.
469 void SurveyPlugins(nlohmann::json &survey)
471 auto _plugins = SocialIntegration::GetPlugins();
473 for (auto &plugin : _plugins) {
474 auto &platform = survey[plugin->social_platform];
475 platform.push_back({
476 {"name", plugin->name},
477 {"version", plugin->version},
478 {"basepath", plugin->basepath},
479 {"state", plugin->state},
485 * Change the bytes of memory into a textual version rounded up to the biggest unit.
487 * For example, 16751108096 would become 16 GiB.
489 * @param memory The bytes of memory.
490 * @return std::string A textual representation.
492 std::string SurveyMemoryToText(uint64_t memory)
494 memory = memory / 1024; // KiB
495 memory = CeilDiv(memory, 1024); // MiB
497 /* Anything above 512 MiB we represent in GiB. */
498 if (memory > 512) {
499 return fmt::format("{} GiB", CeilDiv(memory, 1024));
502 /* Anything above 64 MiB we represent in a multiplier of 128 MiB. */
503 if (memory > 64) {
504 return fmt::format("{} MiB", Ceil(memory, 128));
507 /* Anything else in a multiplier of 4 MiB. */
508 return fmt::format("{} MiB", Ceil(memory, 4));