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_Main.h" 21 : 22 : #include "graphics/FontMetrics.h" 23 : #include "graphics/MapReader.h" 24 : #include "lib/code_generation.h" 25 : #include "lib/file/vfs/vfs_path.h" 26 : #include "lib/frequency_filter.h" 27 : #include "lib/sysdep/sysdep.h" 28 : #include "maths/MD5.h" 29 : #include "maths/Size2D.h" 30 : #include "ps/CLogger.h" 31 : #include "ps/CStr.h" 32 : #include "ps/CStrIntern.h" 33 : #include "ps/Errors.h" 34 : #include "ps/GUID.h" 35 : #include "ps/GameSetup/Atlas.h" 36 : #include "ps/Globals.h" 37 : #include "ps/Util.h" 38 : #include "scriptinterface/FunctionWrapper.h" 39 : #include "scriptinterface/Request.h" 40 : #include "tools/atlas/GameInterface/GameLoop.h" 41 : 42 : #include <js/RootingAPI.h> 43 : #include <js/TypeDecls.h> 44 : #include <js/Value.h> 45 : #include <string> 46 : 47 : namespace Script { class Interface; } 48 : 49 : extern void QuitEngine(int exitStatus); 50 : 51 : namespace JSI_Main 52 : { 53 0 : bool AtlasIsAvailable() 54 : { 55 0 : return ATLAS_IsAvailable(); 56 : } 57 : 58 0 : bool IsAtlasRunning() 59 : { 60 0 : return g_AtlasGameLoop && g_AtlasGameLoop->running; 61 : } 62 : 63 0 : void OpenURL(const std::string& url) 64 : { 65 0 : sys_open_url(url); 66 0 : } 67 : 68 0 : std::wstring GetSystemUsername() 69 : { 70 0 : return sys_get_user_name(); 71 : } 72 : 73 0 : std::wstring GetMatchID() 74 : { 75 0 : return ps_generate_guid().FromUTF8(); 76 : } 77 : 78 0 : JS::Value LoadMapSettings(const Script::Interface& scriptInterface, const VfsPath& pathname) 79 : { 80 0 : Script::Request rq(scriptInterface); 81 : 82 0 : CMapSummaryReader reader; 83 : 84 0 : if (reader.LoadMap(pathname) != PSRETURN_OK) 85 0 : return JS::UndefinedValue(); 86 : 87 0 : JS::RootedValue settings(rq.cx); 88 0 : reader.GetMapSettings(scriptInterface, &settings); 89 0 : return settings; 90 0 : } 91 : 92 : // This value is recalculated once a frame. We take special care to 93 : // filter it, so it is both accurate and free of jitter. 94 0 : int GetFps() 95 : { 96 0 : if (!g_frequencyFilter) 97 : return 0; 98 : 99 0 : return g_frequencyFilter->StableFrequency(); 100 : } 101 : 102 0 : CSize2D CalculateTextSize(const std::string& fontName, const std::wstring& text) 103 : { 104 0 : float width = 0; 105 0 : float height = 0; 106 0 : CStrIntern _fontName(fontName); 107 0 : CFontMetrics fontMetrics(_fontName); 108 0 : fontMetrics.CalculateStringSize(text.c_str(), width, height); 109 0 : return CSize2D(width, height); 110 : } 111 : 112 : 113 0 : CSize2D GetTextSize(const std::string& fontName, const std::wstring& text) 114 : { 115 0 : ONCE(LOGWARNING("Engine.GetTextSize is deprecated and will be removed in a future version. Instead use guiObject.getPreferredTextSize and dropdown.getPreferredHeaderTextSize for more convenient text sizing or guiObject.getTextSize for sizing within an object.")); 116 0 : return CalculateTextSize(fontName, text); 117 : } 118 : 119 0 : int GetTextWidth(const std::string& fontName, const std::wstring& text) 120 : { 121 0 : ONCE(LOGWARNING("Engine.GetTextWidth is deprecated and will be removed in a future version. Instead use guiObject.getPreferredTextSize and dropdown.getPreferredHeaderTextSize for more convenient text sizing or guiObject.getTextSize for sizing within an object.")); 122 0 : return CalculateTextSize(fontName, text).Width; 123 : } 124 : 125 0 : std::string CalculateMD5(const std::string& input) 126 : { 127 0 : std::uint8_t digest[MD5::DIGESTSIZE]; 128 : 129 0 : MD5 m; 130 0 : m.Update(reinterpret_cast<const std::uint8_t*>(input.c_str()), input.length()); 131 0 : m.Final(digest); 132 : 133 0 : return Hexify(digest, MD5::DIGESTSIZE); 134 : } 135 : 136 26 : void RegisterScriptFunctions(const Script::Request& rq) 137 : { 138 26 : Script::Function::Register<&QuitEngine>(rq, "Exit"); 139 26 : Script::Function::Register<&AtlasIsAvailable>(rq, "AtlasIsAvailable"); 140 26 : Script::Function::Register<&IsAtlasRunning>(rq, "IsAtlasRunning"); 141 26 : Script::Function::Register<&OpenURL>(rq, "OpenURL"); 142 26 : Script::Function::Register<&GetSystemUsername>(rq, "GetSystemUsername"); 143 26 : Script::Function::Register<&GetMatchID>(rq, "GetMatchID"); 144 26 : Script::Function::Register<&LoadMapSettings>(rq, "LoadMapSettings"); 145 26 : Script::Function::Register<&GetFps>(rq, "GetFPS"); 146 26 : Script::Function::Register<&GetTextSize>(rq, "GetTextSize"); 147 26 : Script::Function::Register<&GetTextWidth>(rq, "GetTextWidth"); 148 26 : Script::Function::Register<&CalculateMD5>(rq, "CalculateMD5"); 149 26 : } 150 : }