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 "JSInterface_Debug.h" 21 : 22 : #include "i18n/L10n.h" 23 : #include "lib/build_version.h" 24 : #include "lib/debug.h" 25 : #include "lib/utf8.h" 26 : #include "scriptinterface/FunctionWrapper.h" 27 : 28 : #include <ctime> 29 : #include <jsapi.h> 30 : #include <string> 31 : #include <unicode/locid.h> 32 : #include <unicode/smpdtfmt.h> 33 : #include <unicode/utypes.h> 34 : 35 : namespace JSI_Debug 36 : { 37 : /** 38 : * Microseconds since the epoch. 39 : */ 40 0 : double GetMicroseconds() 41 : { 42 0 : return JS_Now(); 43 : } 44 : 45 : // Deliberately cause the game to crash. 46 : // Currently implemented via access violation (read of address 0). 47 : // Useful for testing the crashlog/stack trace code. 48 0 : int Crash() 49 : { 50 0 : debug_printf("Crashing at user's request.\n"); 51 0 : return *(volatile int*)0; 52 : } 53 : 54 0 : void DebugWarn() 55 : { 56 0 : debug_warn(L"Warning at user's request."); 57 0 : } 58 : 59 0 : void DisplayErrorDialog(const std::wstring& msg) 60 : { 61 0 : debug_DisplayError(msg.c_str(), DE_NO_DEBUG_INFO, NULL, NULL, NULL, 0, NULL, NULL); 62 0 : } 63 : 64 : // Return the date at which the current executable was compiled. 65 : // - Displayed on main menu screen; tells non-programmers which auto-build 66 : // they are running. Could also be determined via .EXE file properties, 67 : // but that's a bit more trouble. 68 0 : std::wstring GetBuildDate() 69 : { 70 0 : UDate buildDate = g_L10n.ParseDateTime(__DATE__, "MMM d yyyy", icu::Locale::getUS()); 71 0 : return wstring_from_utf8(g_L10n.LocalizeDateTime(buildDate, L10n::Date, icu::SimpleDateFormat::MEDIUM)); 72 : } 73 : 74 0 : double GetBuildTimestamp() 75 : { 76 0 : UDate buildDate = g_L10n.ParseDateTime(__DATE__ " " __TIME__, "MMM d yyyy HH:mm:ss", icu::Locale::getUS()); 77 0 : if (buildDate) 78 0 : return buildDate / 1000.0; 79 0 : return std::time(nullptr); 80 : } 81 : 82 : // Return the build version of the current executable. 83 : // - in nightly builds, build version is generated from the git HEAD branch and 84 : // hash and cached in lib/build_version.cpp. it is useful to know when attempting 85 : // to reproduce bugs (the main EXE and PDB should be temporarily reverted to 86 : // that commit so that they match user-submitted crashdumps). 87 : // - this function is used by the JS GUI to display the version in a size-constrained 88 : // zone. when longerHash is false, the last 5 characters of the git hash are cut off. 89 0 : std::wstring GetBuildVersion(bool longerHash = false) 90 : { 91 0 : std::wstring buildVersion(build_version); 92 0 : if (buildVersion == L"custom build") 93 0 : return wstring_from_utf8(g_L10n.Translate("custom build")); 94 : 95 : // The hash is 10-char, which is a bit long for the GUI. Reduce it to 5-char by 96 : // default for the main menu display. 97 0 : if (!longerHash && buildVersion.length() > 5) 98 0 : return buildVersion.substr(0, buildVersion.length() - 5); 99 : 100 0 : return buildVersion; 101 0 : } 102 : 103 26 : void RegisterScriptFunctions(const Script::Request& rq) 104 : { 105 26 : Script::Function::Register<&GetMicroseconds>(rq, "GetMicroseconds"); 106 26 : Script::Function::Register<&Crash>(rq, "Crash"); 107 26 : Script::Function::Register<&DebugWarn>(rq, "DebugWarn"); 108 26 : Script::Function::Register<&DisplayErrorDialog>(rq, "DisplayErrorDialog"); 109 26 : Script::Function::Register<&GetBuildDate>(rq, "GetBuildDate"); 110 26 : Script::Function::Register<&GetBuildTimestamp>(rq, "GetBuildTimestamp"); 111 26 : Script::Function::Register<&GetBuildVersion>(rq, "GetBuildVersion"); 112 26 : } 113 : }