Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "precompiled.h"
19 :
20 : #include "lib/build_version.h"
21 : #include "lib/code_annotation.h"
22 : #include "lib/config2.h"
23 : #include "lib/debug.h"
24 : #include "lib/external_libraries/curl.h"
25 : #include "lib/external_libraries/enet.h"
26 : #include "lib/external_libraries/png.h"
27 : #include "lib/external_libraries/libsdl.h"
28 : #include "lib/path.h"
29 : #include "lib/posix/posix_utsname.h"
30 : #include "lib/sysdep/arch.h"
31 : #include "lib/sysdep/compiler.h"
32 : #include "lib/sysdep/cpu.h"
33 : #include "lib/sysdep/os.h"
34 : #include "lib/sysdep/os_cpu.h"
35 : #include "lib/sysdep/sysdep.h" // sys_OpenFile
36 : #include "lib/timer.h"
37 : #include "ps/CLogger.h"
38 : #include "ps/CStr.h"
39 : #include "ps/Errors.h"
40 : #include "ps/Filesystem.h"
41 : #include "ps/GameSetup/Config.h"
42 : #include "ps/Profiler2.h"
43 : #include "ps/Pyrogenesis.h"
44 : #include "ps/UserReport.h"
45 : #include "ps/VideoMode.h"
46 : #include "ps/scripting/JSInterface_ConfigDB.h"
47 : #include "ps/scripting/JSInterface_Debug.h"
48 : #include "renderer/backend/IDevice.h"
49 : #include "scriptinterface/FunctionWrapper.h"
50 : #include "scriptinterface/JSON.h"
51 : #include "scriptinterface/Object.h"
52 : #include "scriptinterface/Interface.h"
53 : #include "scriptinterface/Request.h"
54 : #include "scriptinterface/StructuredClone.h"
55 : #include "soundmanager/ISoundManager.h"
56 :
57 : #include <SDL_cpuinfo.h>
58 : #include <SDL_version.h>
59 : #include <SDL_video.h>
60 : #include <boost/version.hpp>
61 : #include <cstdio>
62 : #include <ctime>
63 : #include <cwchar>
64 : #include <fmt/format.h>
65 : #include <freetype/fttypes.h>
66 : #include <iterator>
67 : #include <js/PropertyAndElement.h>
68 : #include <js/RootingAPI.h>
69 : #include <js/TypeDecls.h>
70 : #include <js/Value.h>
71 : #include <libxml/xmlversion.h>
72 : #include <random>
73 : #include <sodium.h>
74 : #include <sstream>
75 : #include <string>
76 : #include <thread>
77 : #include <unicode/uvernum.h>
78 : #include <vector>
79 : #include <zlib.h>
80 :
81 : // FreeType headers might have an include order.
82 : #include <ft2build.h>
83 : #include FT_FREETYPE_H
84 :
85 : #if ARCH_AMD64
86 : #include "lib/sysdep/arch/x86_x64/x86_x64.h"
87 : #endif
88 :
89 : #if OS_LINUX
90 : #include <fstream>
91 : #endif
92 :
93 : #if CONFIG2_LOBBY
94 : #include "lib/external_libraries/gloox.h"
95 : #endif
96 :
97 : #if CONFIG2_NVTT
98 : #include "nvtt/nvtt.h"
99 : #endif
100 :
101 : #if CONFIG2_MINIUPNPC
102 : #include <miniupnpc/miniupnpc.h>
103 : #endif
104 :
105 : #if CONFIG2_AUDIO
106 : #include <vorbis/codec.h>
107 : #endif
108 :
109 : namespace
110 : {
111 :
112 0 : class Reporter
113 : {
114 : public:
115 0 : Reporter(const Script::Request& rq)
116 0 : : m_Rq(rq), m_LibrarySettings(rq.cx)
117 : {
118 0 : Script::CreateObject(m_Rq, &m_LibrarySettings);
119 0 : }
120 :
121 : template<typename PropertyType>
122 0 : Reporter& Add(const char* propertyName, const PropertyType& propertyValue)
123 : {
124 0 : Script::SetProperty(m_Rq, m_LibrarySettings, propertyName, propertyValue);
125 0 : return *this;
126 : }
127 :
128 0 : JS::Value MakeReport()
129 : {
130 0 : return Script::DeepCopy(m_Rq, m_LibrarySettings);
131 : }
132 :
133 : private:
134 : const Script::Request& m_Rq;
135 : JS::RootedValue m_LibrarySettings;
136 : };
137 :
138 0 : class LibraryReporter : public Reporter
139 : {
140 : public:
141 0 : LibraryReporter(const Script::Request& rq, const char* name)
142 0 : : Reporter(rq)
143 : {
144 0 : Add("name", name);
145 0 : }
146 : };
147 :
148 0 : JS::Value MakeSDLReport(const Script::Request& rq)
149 : {
150 0 : LibraryReporter reporter{rq, "sdl"};
151 :
152 0 : SDL_version build, runtime;
153 0 : SDL_VERSION(&build);
154 :
155 0 : char version[16];
156 0 : snprintf(version, ARRAY_SIZE(version), "%d.%d.%d", build.major, build.minor, build.patch);
157 0 : reporter.Add("build_version", version);
158 :
159 0 : SDL_GetVersion(&runtime);
160 0 : snprintf(version, ARRAY_SIZE(version), "%d.%d.%d", runtime.major, runtime.minor, runtime.patch);
161 0 : reporter.Add("runtime_version", version);
162 :
163 : // This is null in atlas (and further the call triggers an assertion).
164 0 : const char* backend = g_VideoMode.GetWindow() ? GetSDLSubsystem(g_VideoMode.GetWindow()) : "none";
165 0 : reporter.Add("video_backend", backend ? backend : "unknown");
166 :
167 0 : reporter.Add("display_count", SDL_GetNumVideoDisplays());
168 :
169 0 : reporter.Add("cpu_count", SDL_GetCPUCount());
170 0 : reporter.Add("system_ram", SDL_GetSystemRAM());
171 :
172 0 : reporter.Add("altivec", static_cast<int>(SDL_HasAltiVec()));
173 0 : reporter.Add("avx", static_cast<int>(SDL_HasAVX()));
174 : #if SDL_VERSION_ATLEAST(2, 0, 4)
175 0 : reporter.Add("avx2", static_cast<int>(SDL_HasAVX2()));
176 : #else
177 : reporter.Add("avx2", "unknown");
178 : #endif
179 : #if SDL_VERSION_ATLEAST(2, 0, 9)
180 0 : reporter.Add("avx512f", static_cast<int>(SDL_HasAVX512F()));
181 : #else
182 : reporter.Add("avx512f", "unknown");
183 : #endif
184 : #if SDL_VERSION_ATLEAST(2, 24, 0)
185 0 : reporter.Add("lasx", static_cast<int>(SDL_HasLASX()));
186 0 : reporter.Add("lsx", static_cast<int>(SDL_HasLSX()));
187 : #else
188 : reporter.Add("lasx", "unknown");
189 : reporter.Add("lsx", "unknown");
190 : #endif
191 : #if SDL_VERSION_ATLEAST(2, 0, 6)
192 0 : reporter.Add("neon", static_cast<int>(SDL_HasNEON()));
193 : #else
194 : reporter.Add("neon", "unknown");
195 : #endif
196 0 : reporter.Add("sse", static_cast<int>(SDL_HasSSE()));
197 0 : reporter.Add("sse2", static_cast<int>(SDL_HasSSE2()));
198 0 : reporter.Add("sse3", static_cast<int>(SDL_HasSSE3()));
199 0 : reporter.Add("sse41", static_cast<int>(SDL_HasSSE41()));
200 0 : reporter.Add("sse42", static_cast<int>(SDL_HasSSE42()));
201 :
202 0 : return reporter.MakeReport();
203 0 : }
204 :
205 0 : JS::Value MakeFreeTypeReport(const Script::Request& rq)
206 : {
207 0 : FT_Library FTLibrary;
208 :
209 0 : LibraryReporter libraryReporter{rq, "freetype"};
210 0 : if (!FT_Init_FreeType(&FTLibrary))
211 : {
212 0 : FT_Int major, minor, patch;
213 0 : FT_Library_Version(FTLibrary, &major, &minor, &patch);
214 0 : FT_Done_FreeType(FTLibrary);
215 0 : std::stringstream version;
216 0 : version << major << "." << minor << "." << patch;
217 0 : libraryReporter.Add("version", version.str());
218 0 : }
219 : else
220 0 : libraryReporter.Add("version", "unavailable");
221 0 : return libraryReporter.MakeReport();
222 0 : }
223 :
224 0 : void ReportLibraries(const Script::Request& rq, JS::HandleValue settings)
225 : {
226 0 : JS::RootedValueVector librariesSettings{rq.cx};
227 :
228 0 : auto appendLibrary = [&rq, &librariesSettings](const JS::Value& librarySettings)
229 : {
230 0 : JS::RootedValue value(rq.cx, librarySettings);
231 0 : if (!librariesSettings.append(value))
232 0 : throw std::runtime_error{"Append failed"};
233 0 : };
234 :
235 0 : appendLibrary(MakeSDLReport(rq));
236 0 : appendLibrary(MakeFreeTypeReport(rq));
237 :
238 0 : appendLibrary(LibraryReporter{rq, "boost"}.Add("version", BOOST_VERSION).MakeReport());
239 0 : appendLibrary(LibraryReporter{rq, "enet"}.Add("version", std::to_string(ENET_VERSION)).MakeReport());
240 0 : appendLibrary(LibraryReporter{rq, "fmt"}.Add("version", FMT_VERSION).MakeReport());
241 : #if CONFIG2_LOBBY
242 0 : appendLibrary(LibraryReporter{rq, "gloox"}.Add("version", gloox_version()).MakeReport());
243 : #endif
244 0 : appendLibrary(LibraryReporter{rq, "libicu"}.Add("version", U_ICU_VERSION).MakeReport());
245 0 : appendLibrary(LibraryReporter{rq, "libcurl"}.Add("version", std::string(curl_version())).MakeReport());
246 : #if CONFIG2_AUDIO
247 0 : appendLibrary(LibraryReporter{rq, "libvorbis"}.Add("version", std::string(vorbis_version_string())).MakeReport());
248 : #endif
249 0 : appendLibrary(LibraryReporter{rq, "libpng"}.Add("version", std::string(png_get_libpng_ver(nullptr))).MakeReport());
250 0 : appendLibrary(LibraryReporter{rq, "libsodium"}.Add("version", std::string(sodium_version_string())).MakeReport());
251 0 : appendLibrary(LibraryReporter{rq, "libxml2"}.Add("version", LIBXML_DOTTED_VERSION).MakeReport());
252 : #if CONFIG2_MINIUPNPC
253 0 : appendLibrary(LibraryReporter{rq, "miniunpnpc"}.Add("version", MINIUPNPC_VERSION).MakeReport());
254 : #endif
255 0 : appendLibrary(LibraryReporter{rq, "zlib"}.Add("version", ZLIB_VERSION).MakeReport());
256 :
257 : #if CONFIG2_NVTT
258 0 : appendLibrary(LibraryReporter{rq, "nvtt"}
259 0 : .Add("build_version", NVTT_VERSION)
260 0 : .Add("runtime_version", nvtt::version())
261 0 : .MakeReport());
262 : #endif
263 :
264 0 : Script::SetProperty(rq, settings, "libraries",
265 0 : JS::RootedValue{rq.cx, JS::ObjectValue(*JS::NewArrayObject(rq.cx, librariesSettings))});
266 0 : }
267 :
268 0 : void WriteSystemInfo(Renderer::Backend::IDevice* device, const utsname& un)
269 : {
270 0 : PROFILE2("WriteSystemInfo");
271 :
272 0 : OsPath pathname = psLogDir() / "system_info.txt";
273 0 : FILE* f = sys_OpenFile(pathname, "w");
274 0 : if(!f)
275 0 : return;
276 :
277 : // current timestamp (redundant WRT OS timestamp, but that is not
278 : // visible when people are posting this file's contents online)
279 0 : {
280 0 : wchar_t timestampBuf[100] = {'\0'};
281 0 : time_t seconds;
282 0 : time(&seconds);
283 0 : struct tm* t = gmtime(&seconds);
284 0 : const size_t charsWritten = wcsftime(timestampBuf, ARRAY_SIZE(timestampBuf), L"(generated %Y-%m-%d %H:%M:%S UTC)", t);
285 0 : ENSURE(charsWritten != 0);
286 0 : fprintf(f, "%ls\n\n", timestampBuf);
287 : }
288 :
289 : // OS
290 0 : fprintf(f, "OS : %s %s (%s)\n", un.sysname, un.release, un.version);
291 :
292 : // CPU
293 0 : fprintf(f, "CPU : %s, %s\n", un.machine, cpu_IdentifierString());
294 :
295 : // memory
296 0 : fprintf(f, "Memory : %u MiB; %u MiB free\n",
297 0 : static_cast<std::uint32_t>(SDL_GetSystemRAM()), static_cast<std::uint32_t>(os_cpu_MemoryAvailable()));
298 :
299 : // graphics
300 0 : fprintf(f, "Video Card : %s\n", device->GetName().c_str());
301 0 : fprintf(f, "Video Driver : %s\n", device->GetDriverInformation().c_str());
302 0 : fprintf(f, "Video Mode : %dx%d:%d\n", g_VideoMode.GetWindowWidth(), g_VideoMode.GetWindowHeight(), g_VideoMode.GetBPP());
303 :
304 : #if CONFIG2_AUDIO
305 0 : if (g_SoundManager)
306 : {
307 0 : fprintf(f, "Sound Card : %s\n", g_SoundManager->GetSoundCardNames().c_str());
308 0 : fprintf(f, "Sound Drivers : %s\n", g_SoundManager->GetOpenALVersion().c_str());
309 : }
310 0 : else if(g_DisableAudio)
311 0 : fprintf(f, "Sound : Game was ran without audio\n");
312 : else
313 0 : fprintf(f, "Sound : No audio device was found\n");
314 : #else
315 : fprintf(f, "Sound : Game was compiled without audio\n");
316 : #endif
317 :
318 : // OpenGL extensions (write them last, since it's a lot of text)
319 0 : fprintf(f, "\nBackend Extensions:\n");
320 0 : if (device->GetExtensions().empty())
321 0 : fprintf(f, "{unknown}\n");
322 : else
323 0 : for (const std::string& extension : device->GetExtensions())
324 0 : fprintf(f, "%s\n", extension.c_str());
325 :
326 0 : fclose(f);
327 0 : f = 0;
328 :
329 0 : debug_printf("Hardware details written to '%s'\n", pathname.string8().c_str());
330 0 : }
331 :
332 : } // anonymous namespace
333 :
334 0 : void SetDisableAudio(bool disabled)
335 : {
336 0 : g_DisableAudio = disabled;
337 0 : }
338 :
339 0 : void RunHardwareDetection(bool writeSystemInfoBeforeDetection, Renderer::Backend::IDevice* device)
340 : {
341 0 : utsname un;
342 0 : uname(&un);
343 :
344 0 : if (writeSystemInfoBeforeDetection)
345 0 : WriteSystemInfo(device, un);
346 :
347 0 : PROFILE2("RunHardwareDetection");
348 :
349 0 : Script::Interface scriptInterface("Engine", "HWDetect", g_ScriptContext);
350 :
351 0 : Script::Request rq(scriptInterface);
352 :
353 0 : JSI_Debug::RegisterScriptFunctions(scriptInterface); // Engine.DisplayErrorDialog
354 0 : JSI_ConfigDB::RegisterScriptFunctions(scriptInterface);
355 :
356 0 : Script::Function::Register<SetDisableAudio>(rq, "SetDisableAudio");
357 :
358 : // Load the detection script:
359 :
360 0 : const wchar_t* scriptName = L"hwdetect/hwdetect.js";
361 0 : CVFSFile file;
362 0 : if (file.Load(g_VFS, scriptName) != PSRETURN_OK)
363 : {
364 0 : LOGERROR("Failed to load hardware detection script");
365 0 : return;
366 : }
367 :
368 0 : std::string code = file.DecodeUTF8(); // assume it's UTF-8
369 0 : scriptInterface.LoadScript(scriptName, code);
370 :
371 : // Collect all the settings we'll pass to the script:
372 : // (We'll use this same data for the opt-in online reporting system, so it
373 : // includes some fields that aren't directly useful for the hwdetect script)
374 :
375 0 : JS::RootedValue settings(rq.cx);
376 0 : Script::CreateObject(rq, &settings);
377 :
378 0 : Script::SetProperty(rq, settings, "os_unix", OS_UNIX);
379 0 : Script::SetProperty(rq, settings, "os_bsd", OS_BSD);
380 0 : Script::SetProperty(rq, settings, "os_linux", OS_LINUX);
381 0 : Script::SetProperty(rq, settings, "os_android", OS_ANDROID);
382 0 : Script::SetProperty(rq, settings, "os_macosx", OS_MACOSX);
383 0 : Script::SetProperty(rq, settings, "os_win", OS_WIN);
384 :
385 0 : Script::SetProperty(rq, settings, "arch_ia32", ARCH_IA32);
386 0 : Script::SetProperty(rq, settings, "arch_amd64", ARCH_AMD64);
387 0 : Script::SetProperty(rq, settings, "arch_arm", ARCH_ARM);
388 0 : Script::SetProperty(rq, settings, "arch_aarch64", ARCH_AARCH64);
389 0 : Script::SetProperty(rq, settings, "arch_e2k", ARCH_E2K);
390 0 : Script::SetProperty(rq, settings, "arch_loong64", ARCH_LOONG64);
391 0 : Script::SetProperty(rq, settings, "arch_ppc64", ARCH_PPC64);
392 0 : Script::SetProperty(rq, settings, "arch_riscv64", ARCH_RISCV64);
393 :
394 : #ifdef NDEBUG
395 0 : Script::SetProperty(rq, settings, "build_debug", 0);
396 : #else
397 : Script::SetProperty(rq, settings, "build_debug", 1);
398 : #endif
399 0 : Script::SetProperty(rq, settings, "build_opengles", CONFIG2_GLES);
400 :
401 0 : Script::SetProperty(rq, settings, "build_datetime", std::string(__DATE__ " " __TIME__));
402 0 : Script::SetProperty(rq, settings, "build_version", std::wstring(build_version));
403 :
404 0 : Script::SetProperty(rq, settings, "build_msc", (int)MSC_VERSION);
405 0 : Script::SetProperty(rq, settings, "build_gcc", (int)GCC_VERSION);
406 0 : Script::SetProperty(rq, settings, "build_clang", (int)CLANG_VERSION);
407 :
408 0 : Script::SetProperty(rq, settings, "gfx_card", device->GetName());
409 0 : Script::SetProperty(rq, settings, "gfx_drv_ver", device->GetDriverInformation());
410 : #if CONFIG2_AUDIO
411 0 : if (g_SoundManager)
412 : {
413 0 : Script::SetProperty(rq, settings, "snd_card", g_SoundManager->GetSoundCardNames());
414 0 : Script::SetProperty(rq, settings, "snd_drv_ver", g_SoundManager->GetOpenALVersion());
415 : }
416 : #endif
417 :
418 0 : ReportLibraries(rq, settings);
419 :
420 0 : JS::RootedValue backendDeviceSettings(rq.cx);
421 0 : Script::CreateObject(rq, &backendDeviceSettings);
422 :
423 0 : device->Report(rq, backendDeviceSettings);
424 0 : Script::SetProperty(rq, settings, "renderer_backend", backendDeviceSettings);
425 :
426 0 : Script::SetProperty(rq, settings, "video_desktop_xres", g_VideoMode.GetDesktopXRes());
427 0 : Script::SetProperty(rq, settings, "video_desktop_yres", g_VideoMode.GetDesktopYRes());
428 0 : Script::SetProperty(rq, settings, "video_desktop_bpp", g_VideoMode.GetDesktopBPP());
429 0 : Script::SetProperty(rq, settings, "video_desktop_freq", g_VideoMode.GetDesktopFreq());
430 :
431 0 : Script::SetProperty(rq, settings, "uname_sysname", std::string(un.sysname));
432 0 : Script::SetProperty(rq, settings, "uname_release", std::string(un.release));
433 0 : Script::SetProperty(rq, settings, "uname_version", std::string(un.version));
434 0 : Script::SetProperty(rq, settings, "uname_machine", std::string(un.machine));
435 :
436 : #if OS_LINUX
437 0 : {
438 0 : std::ifstream ifs("/etc/lsb-release");
439 0 : if (ifs.good())
440 : {
441 0 : std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
442 0 : Script::SetProperty(rq, settings, "linux_release", str);
443 0 : }
444 0 : }
445 : #endif
446 :
447 0 : Script::SetProperty(rq, settings, "cpu_identifier", std::string(cpu_IdentifierString()));
448 0 : Script::SetProperty(rq, settings, "cpu_pagesize", static_cast<std::uint32_t>(os_cpu_PageSize()));
449 0 : Script::SetProperty(rq, settings, "cpu_largepagesize", static_cast<std::uint32_t>(os_cpu_LargePageSize()));
450 :
451 0 : Script::SetProperty(rq, settings, "timer_resolution", timer_Resolution());
452 :
453 0 : Script::SetProperty(rq, settings, "hardware_concurrency", std::thread::hardware_concurrency());
454 0 : Script::SetProperty(rq, settings, "random_device_entropy", std::random_device{}.entropy());
455 :
456 : // The version should be increased for every meaningful change.
457 0 : const int reportVersion = 25;
458 :
459 : // Send the same data to the reporting system
460 0 : g_UserReporter.SubmitReport(
461 : "hwdetect",
462 : reportVersion,
463 0 : Script::StringifyJSON(rq, &settings, false),
464 0 : Script::StringifyJSON(rq, &settings, true));
465 :
466 : // Run the detection script:
467 0 : JS::RootedValue global(rq.cx, rq.globalValue());
468 0 : bool hardwareSupported{true};
469 0 : Script::Function::Call(rq, global, "RunHardwareDetection", hardwareSupported, settings);
470 :
471 0 : if (!hardwareSupported)
472 : {
473 : // It doesn't make sense to continue working here, because we're not
474 : // able to display anything.
475 0 : DEBUG_DISPLAY_FATAL_ERROR(
476 : L"Your graphics card doesn't appear to be fully compatible with OpenGL shaders."
477 : L" The game does not support pre-shader graphics cards."
478 : L" You are advised to try installing newer drivers and/or upgrade your graphics card."
479 : L" For more information, please see http://www.wildfiregames.com/forum/index.php?showtopic=16734"
480 : );
481 : }
482 0 : }
|