LCOV - code coverage report
Current view: top level - source/ps/scripting - JSInterface_Main.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 15 58 25.9 %
Date: 2023-01-19 00:18:29 Functions: 3 15 20.0 %

          Line data    Source code
       1             : /* Copyright (C) 2022 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_Main.h"
      21             : 
      22             : #include "graphics/FontMetrics.h"
      23             : #include "graphics/MapReader.h"
      24             : #include "lib/sysdep/sysdep.h"
      25             : #include "lib/utf8.h"
      26             : #include "maths/Size2D.h"
      27             : #include "maths/MD5.h"
      28             : #include "ps/CStrIntern.h"
      29             : #include "ps/GUID.h"
      30             : #include "ps/GameSetup/Atlas.h"
      31             : #include "ps/Globals.h"
      32             : #include "ps/Hotkey.h"
      33             : #include "ps/Util.h"
      34             : #include "scriptinterface/FunctionWrapper.h"
      35             : #include "tools/atlas/GameInterface/GameLoop.h"
      36             : 
      37             : extern void QuitEngine();
      38             : extern void StartAtlas();
      39             : 
      40             : namespace JSI_Main
      41             : {
      42           0 : void QuitEngine()
      43             : {
      44           0 :     ::QuitEngine();
      45           0 : }
      46             : 
      47           0 : void StartAtlas()
      48             : {
      49           0 :     ::StartAtlas();
      50           0 : }
      51             : 
      52           0 : bool AtlasIsAvailable()
      53             : {
      54           0 :     return ATLAS_IsAvailable();
      55             : }
      56             : 
      57           0 : bool IsAtlasRunning()
      58             : {
      59           0 :     return g_AtlasGameLoop && g_AtlasGameLoop->running;
      60             : }
      61             : 
      62           0 : void OpenURL(const std::string& url)
      63             : {
      64           0 :     sys_open_url(url);
      65           0 : }
      66             : 
      67           0 : std::wstring GetSystemUsername()
      68             : {
      69           0 :     return sys_get_user_name();
      70             : }
      71             : 
      72           0 : std::wstring GetMatchID()
      73             : {
      74           0 :     return ps_generate_guid().FromUTF8();
      75             : }
      76             : 
      77           0 : JS::Value LoadMapSettings(const ScriptInterface& scriptInterface, const VfsPath& pathname)
      78             : {
      79           0 :     ScriptRequest rq(scriptInterface);
      80             : 
      81           0 :     CMapSummaryReader reader;
      82             : 
      83           0 :     if (reader.LoadMap(pathname) != PSRETURN_OK)
      84           0 :         return JS::UndefinedValue();
      85             : 
      86           0 :     JS::RootedValue settings(rq.cx);
      87           0 :     reader.GetMapSettings(scriptInterface, &settings);
      88           0 :     return settings;
      89             : }
      90             : 
      91             : // This value is recalculated once a frame. We take special care to
      92             : // filter it, so it is both accurate and free of jitter.
      93           0 : int GetFps()
      94             : {
      95           0 :     if (!g_frequencyFilter)
      96           0 :         return 0;
      97             : 
      98           0 :     return g_frequencyFilter->StableFrequency();
      99             : }
     100             : 
     101           0 : CSize2D GetTextSize(const std::string& fontName, const std::wstring& text)
     102             : {
     103           0 :     int width = 0;
     104           0 :     int height = 0;
     105           0 :     CStrIntern _fontName(fontName);
     106           0 :     CFontMetrics fontMetrics(_fontName);
     107           0 :     fontMetrics.CalculateStringSize(text.c_str(), width, height);
     108           0 :     return CSize2D(width, height);
     109             : }
     110             : 
     111           0 : int GetTextWidth(const std::string& fontName, const std::wstring& text)
     112             : {
     113           0 :     return GetTextSize(fontName, text).Width;
     114             : }
     115             : 
     116           0 : std::string CalculateMD5(const std::string& input)
     117             : {
     118             :     u8 digest[MD5::DIGESTSIZE];
     119             : 
     120           0 :     MD5 m;
     121           0 :     m.Update((const u8*)input.c_str(), input.length());
     122           0 :     m.Final(digest);
     123             : 
     124           0 :     return Hexify(digest, MD5::DIGESTSIZE);
     125             : }
     126             : 
     127          12 : void RegisterScriptFunctions(const ScriptRequest& rq)
     128             : {
     129          12 :     ScriptFunction::Register<&QuitEngine>(rq, "Exit");
     130          12 :     ScriptFunction::Register<&StartAtlas>(rq, "RestartInAtlas");
     131          12 :     ScriptFunction::Register<&AtlasIsAvailable>(rq, "AtlasIsAvailable");
     132          12 :     ScriptFunction::Register<&IsAtlasRunning>(rq, "IsAtlasRunning");
     133          12 :     ScriptFunction::Register<&OpenURL>(rq, "OpenURL");
     134          12 :     ScriptFunction::Register<&GetSystemUsername>(rq, "GetSystemUsername");
     135          12 :     ScriptFunction::Register<&GetMatchID>(rq, "GetMatchID");
     136          12 :     ScriptFunction::Register<&LoadMapSettings>(rq, "LoadMapSettings");
     137          12 :     ScriptFunction::Register<&GetFps>(rq, "GetFPS");
     138          12 :     ScriptFunction::Register<&GetTextSize>(rq, "GetTextSize");
     139          12 :     ScriptFunction::Register<&GetTextWidth>(rq, "GetTextWidth");
     140          12 :     ScriptFunction::Register<&CalculateMD5>(rq, "CalculateMD5");
     141          12 : }
     142           3 : }

Generated by: LCOV version 1.13