2024-10-28 13:34:20 -07:00
|
|
|
/* Copyright (C) 2025 Wildfire Games.
|
2023-12-02 16:30:12 -08:00
|
|
|
* This file is part of 0 A.D.
|
2017-09-11 20:11:33 -07:00
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2017-09-11 20:11:33 -07:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2017-09-11 20:11:33 -07:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2023-12-02 16:30:12 -08:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2017-09-11 20:11:33 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
|
|
#include "JSInterface_Main.h"
|
|
|
|
|
|
|
|
|
|
#include "graphics/FontMetrics.h"
|
|
|
|
|
#include "graphics/MapReader.h"
|
|
|
|
|
#include "lib/sysdep/sysdep.h"
|
|
|
|
|
#include "lib/utf8.h"
|
2022-03-02 08:27:11 -08:00
|
|
|
#include "maths/Size2D.h"
|
2018-06-21 09:38:08 -07:00
|
|
|
#include "maths/MD5.h"
|
2017-09-11 20:11:33 -07:00
|
|
|
#include "ps/CStrIntern.h"
|
|
|
|
|
#include "ps/GUID.h"
|
|
|
|
|
#include "ps/GameSetup/Atlas.h"
|
|
|
|
|
#include "ps/Globals.h"
|
|
|
|
|
#include "ps/Hotkey.h"
|
2018-08-08 05:59:05 -07:00
|
|
|
#include "ps/Util.h"
|
2021-03-02 12:01:14 -08:00
|
|
|
#include "scriptinterface/FunctionWrapper.h"
|
2017-09-11 20:11:33 -07:00
|
|
|
#include "tools/atlas/GameInterface/GameLoop.h"
|
|
|
|
|
|
2021-03-02 12:01:14 -08:00
|
|
|
namespace JSI_Main
|
|
|
|
|
{
|
|
|
|
|
bool AtlasIsAvailable()
|
2017-09-11 20:11:33 -07:00
|
|
|
{
|
|
|
|
|
return ATLAS_IsAvailable();
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-02 12:01:14 -08:00
|
|
|
bool IsAtlasRunning()
|
2017-09-11 20:11:33 -07:00
|
|
|
{
|
|
|
|
|
return g_AtlasGameLoop && g_AtlasGameLoop->running;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-02 12:01:14 -08:00
|
|
|
void OpenURL(const std::string& url)
|
2017-09-11 20:11:33 -07:00
|
|
|
{
|
|
|
|
|
sys_open_url(url);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-02 12:01:14 -08:00
|
|
|
std::wstring GetSystemUsername()
|
2017-09-11 20:11:33 -07:00
|
|
|
{
|
|
|
|
|
return sys_get_user_name();
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-02 12:01:14 -08:00
|
|
|
std::wstring GetMatchID()
|
2017-09-11 20:11:33 -07:00
|
|
|
{
|
|
|
|
|
return ps_generate_guid().FromUTF8();
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-02 12:01:14 -08:00
|
|
|
JS::Value LoadMapSettings(const ScriptInterface& scriptInterface, const VfsPath& pathname)
|
2017-09-11 20:11:33 -07:00
|
|
|
{
|
2021-03-02 12:01:14 -08:00
|
|
|
ScriptRequest rq(scriptInterface);
|
2017-09-11 20:11:33 -07:00
|
|
|
|
|
|
|
|
CMapSummaryReader reader;
|
|
|
|
|
|
|
|
|
|
if (reader.LoadMap(pathname) != PSRETURN_OK)
|
|
|
|
|
return JS::UndefinedValue();
|
|
|
|
|
|
2020-11-13 05:18:22 -08:00
|
|
|
JS::RootedValue settings(rq.cx);
|
2021-03-02 12:01:14 -08:00
|
|
|
reader.GetMapSettings(scriptInterface, &settings);
|
2017-09-11 20:11:33 -07:00
|
|
|
return settings;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This value is recalculated once a frame. We take special care to
|
|
|
|
|
// filter it, so it is both accurate and free of jitter.
|
2021-03-02 12:01:14 -08:00
|
|
|
int GetFps()
|
2017-09-11 20:11:33 -07:00
|
|
|
{
|
|
|
|
|
if (!g_frequencyFilter)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
return g_frequencyFilter->StableFrequency();
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-02 08:27:11 -08:00
|
|
|
CSize2D GetTextSize(const std::string& fontName, const std::wstring& text)
|
2017-09-11 20:11:33 -07:00
|
|
|
{
|
|
|
|
|
int width = 0;
|
|
|
|
|
int height = 0;
|
|
|
|
|
CStrIntern _fontName(fontName);
|
|
|
|
|
CFontMetrics fontMetrics(_fontName);
|
|
|
|
|
fontMetrics.CalculateStringSize(text.c_str(), width, height);
|
2022-03-02 08:27:11 -08:00
|
|
|
return CSize2D(width, height);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int GetTextWidth(const std::string& fontName, const std::wstring& text)
|
|
|
|
|
{
|
|
|
|
|
return GetTextSize(fontName, text).Width;
|
2017-09-11 20:11:33 -07:00
|
|
|
}
|
|
|
|
|
|
2021-03-02 12:01:14 -08:00
|
|
|
std::string CalculateMD5(const std::string& input)
|
2018-06-21 09:38:08 -07:00
|
|
|
{
|
|
|
|
|
u8 digest[MD5::DIGESTSIZE];
|
|
|
|
|
|
|
|
|
|
MD5 m;
|
2018-06-21 09:52:52 -07:00
|
|
|
m.Update((const u8*)input.c_str(), input.length());
|
2018-06-21 09:38:08 -07:00
|
|
|
m.Final(digest);
|
|
|
|
|
|
2018-08-08 05:59:05 -07:00
|
|
|
return Hexify(digest, MD5::DIGESTSIZE);
|
2018-06-21 09:38:08 -07:00
|
|
|
}
|
|
|
|
|
|
2021-03-02 12:01:14 -08:00
|
|
|
void RegisterScriptFunctions(const ScriptRequest& rq)
|
2017-09-11 20:11:33 -07:00
|
|
|
{
|
2021-03-02 12:01:14 -08:00
|
|
|
ScriptFunction::Register<&AtlasIsAvailable>(rq, "AtlasIsAvailable");
|
|
|
|
|
ScriptFunction::Register<&IsAtlasRunning>(rq, "IsAtlasRunning");
|
|
|
|
|
ScriptFunction::Register<&OpenURL>(rq, "OpenURL");
|
|
|
|
|
ScriptFunction::Register<&GetSystemUsername>(rq, "GetSystemUsername");
|
|
|
|
|
ScriptFunction::Register<&GetMatchID>(rq, "GetMatchID");
|
|
|
|
|
ScriptFunction::Register<&LoadMapSettings>(rq, "LoadMapSettings");
|
|
|
|
|
ScriptFunction::Register<&GetFps>(rq, "GetFPS");
|
2022-03-02 08:27:11 -08:00
|
|
|
ScriptFunction::Register<&GetTextSize>(rq, "GetTextSize");
|
2021-03-02 12:01:14 -08:00
|
|
|
ScriptFunction::Register<&GetTextWidth>(rq, "GetTextWidth");
|
|
|
|
|
ScriptFunction::Register<&CalculateMD5>(rq, "CalculateMD5");
|
|
|
|
|
}
|
2017-09-11 20:11:33 -07:00
|
|
|
}
|