Line data Source code
1 : /* Copyright (C) 2021 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/svn_revision.h"
24 : #include "lib/debug.h"
25 : #include "scriptinterface/FunctionWrapper.h"
26 : #include "scriptinterface/ScriptRequest.h"
27 :
28 : #include <ctime>
29 : #include <string>
30 :
31 : namespace JSI_Debug
32 : {
33 : /**
34 : * Microseconds since the epoch.
35 : */
36 0 : double GetMicroseconds()
37 : {
38 0 : return JS_Now();
39 : }
40 :
41 : // Deliberately cause the game to crash.
42 : // Currently implemented via access violation (read of address 0).
43 : // Useful for testing the crashlog/stack trace code.
44 0 : int Crash()
45 : {
46 0 : debug_printf("Crashing at user's request.\n");
47 0 : return *(volatile int*)0;
48 : }
49 :
50 0 : void DebugWarn()
51 : {
52 0 : debug_warn(L"Warning at user's request.");
53 0 : }
54 :
55 0 : void DisplayErrorDialog(const std::wstring& msg)
56 : {
57 0 : debug_DisplayError(msg.c_str(), DE_NO_DEBUG_INFO, NULL, NULL, NULL, 0, NULL, NULL);
58 0 : }
59 :
60 : // Return the date at which the current executable was compiled.
61 : // - Displayed on main menu screen; tells non-programmers which auto-build
62 : // they are running. Could also be determined via .EXE file properties,
63 : // but that's a bit more trouble.
64 0 : std::wstring GetBuildDate()
65 : {
66 0 : UDate buildDate = g_L10n.ParseDateTime(__DATE__, "MMM d yyyy", icu::Locale::getUS());
67 0 : return wstring_from_utf8(g_L10n.LocalizeDateTime(buildDate, L10n::Date, icu::SimpleDateFormat::MEDIUM));
68 : }
69 :
70 0 : double GetBuildTimestamp()
71 : {
72 0 : UDate buildDate = g_L10n.ParseDateTime(__DATE__ " " __TIME__, "MMM d yyyy HH:mm:ss", icu::Locale::getUS());
73 0 : if (buildDate)
74 0 : return buildDate / 1000.0;
75 0 : return std::time(nullptr);
76 : }
77 :
78 : // Return the revision number at which the current executable was compiled.
79 : // - svn revision is generated by calling svnversion and cached in
80 : // lib/svn_revision.cpp. it is useful to know when attempting to
81 : // reproduce bugs (the main EXE and PDB should be temporarily reverted to
82 : // that revision so that they match user-submitted crashdumps).
83 0 : std::wstring GetBuildRevision()
84 : {
85 0 : std::wstring svnRevision(svn_revision);
86 0 : if (svnRevision == L"custom build")
87 0 : return wstring_from_utf8(g_L10n.Translate("custom build"));
88 0 : return svnRevision;
89 : }
90 :
91 12 : void RegisterScriptFunctions(const ScriptRequest& rq)
92 : {
93 12 : ScriptFunction::Register<&GetMicroseconds>(rq, "GetMicroseconds");
94 12 : ScriptFunction::Register<&Crash>(rq, "Crash");
95 12 : ScriptFunction::Register<&DebugWarn>(rq, "DebugWarn");
96 12 : ScriptFunction::Register<&DisplayErrorDialog>(rq, "DisplayErrorDialog");
97 12 : ScriptFunction::Register<&GetBuildDate>(rq, "GetBuildDate");
98 12 : ScriptFunction::Register<&GetBuildTimestamp>(rq, "GetBuildTimestamp");
99 12 : ScriptFunction::Register<&GetBuildRevision>(rq, "GetBuildRevision");
100 12 : }
101 3 : }
|