Changeset View
Changeset View
Standalone View
Standalone View
source/ps/scripting/JSInterface_Main.cpp
Show All 25 Lines | |||||
#include "maths/MD5.h" | #include "maths/MD5.h" | ||||
#include "ps/CStrIntern.h" | #include "ps/CStrIntern.h" | ||||
#include "ps/GUID.h" | #include "ps/GUID.h" | ||||
#include "ps/GameSetup/Atlas.h" | #include "ps/GameSetup/Atlas.h" | ||||
#include "ps/Globals.h" | #include "ps/Globals.h" | ||||
#include "ps/Hotkey.h" | #include "ps/Hotkey.h" | ||||
#include "ps/Util.h" | #include "ps/Util.h" | ||||
#include "scriptinterface/ScriptInterface.h" | #include "scriptinterface/ScriptInterface.h" | ||||
#include "scriptinterface/FunctionWrapper.h" | |||||
#include "tools/atlas/GameInterface/GameLoop.h" | #include "tools/atlas/GameInterface/GameLoop.h" | ||||
extern void QuitEngine(); | extern void QuitEngine(); | ||||
extern void StartAtlas(); | extern void StartAtlas(); | ||||
void JSI_Main::QuitEngine(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | void JSI_Main::QuitEngine() | ||||
{ | { | ||||
::QuitEngine(); | ::QuitEngine(); | ||||
} | } | ||||
void JSI_Main::StartAtlas(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | void JSI_Main::StartAtlas() | ||||
{ | { | ||||
::StartAtlas(); | ::StartAtlas(); | ||||
} | } | ||||
bool JSI_Main::AtlasIsAvailable(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | bool JSI_Main::AtlasIsAvailable() | ||||
{ | { | ||||
return ATLAS_IsAvailable(); | return ATLAS_IsAvailable(); | ||||
} | } | ||||
bool JSI_Main::IsAtlasRunning(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | bool JSI_Main::IsAtlasRunning() | ||||
{ | { | ||||
return g_AtlasGameLoop && g_AtlasGameLoop->running; | return g_AtlasGameLoop && g_AtlasGameLoop->running; | ||||
} | } | ||||
void JSI_Main::OpenURL(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::string& url) | void JSI_Main::OpenURL(const std::string& url) | ||||
{ | { | ||||
sys_open_url(url); | sys_open_url(url); | ||||
} | } | ||||
std::wstring JSI_Main::GetSystemUsername(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | std::wstring JSI_Main::GetSystemUsername() | ||||
{ | { | ||||
return sys_get_user_name(); | return sys_get_user_name(); | ||||
} | } | ||||
std::wstring JSI_Main::GetMatchID(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | std::wstring JSI_Main::GetMatchID() | ||||
{ | { | ||||
return ps_generate_guid().FromUTF8(); | return ps_generate_guid().FromUTF8(); | ||||
} | } | ||||
JS::Value JSI_Main::LoadMapSettings(ScriptInterface::CxPrivate* pCxPrivate, const VfsPath& pathname) | JS::Value JSI_Main::LoadMapSettings(ScriptInterface::CxPrivate* pCxPrivate, const VfsPath& pathname) | ||||
{ | { | ||||
JSContext* cx = pCxPrivate->pScriptInterface->GetContext(); | JSContext* cx = pCxPrivate->pScriptInterface->GetContext(); | ||||
JSAutoRequest rq(cx); | JSAutoRequest rq(cx); | ||||
CMapSummaryReader reader; | CMapSummaryReader reader; | ||||
if (reader.LoadMap(pathname) != PSRETURN_OK) | if (reader.LoadMap(pathname) != PSRETURN_OK) | ||||
return JS::UndefinedValue(); | return JS::UndefinedValue(); | ||||
JS::RootedValue settings(cx); | JS::RootedValue settings(cx); | ||||
reader.GetMapSettings(*(pCxPrivate->pScriptInterface), &settings); | reader.GetMapSettings(*(pCxPrivate->pScriptInterface), &settings); | ||||
return settings; | return settings; | ||||
} | } | ||||
bool JSI_Main::HotkeyIsPressed_(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::string& hotkeyName) | bool JSI_Main::HotkeyIsPressed_(const std::string& hotkeyName) | ||||
{ | { | ||||
return HotkeyIsPressed(hotkeyName); | return HotkeyIsPressed(hotkeyName); | ||||
} | } | ||||
// This value is recalculated once a frame. We take special care to | // This value is recalculated once a frame. We take special care to | ||||
// filter it, so it is both accurate and free of jitter. | // filter it, so it is both accurate and free of jitter. | ||||
int JSI_Main::GetFps(ScriptInterface::CxPrivate* UNUSED(pCxPrivate)) | int JSI_Main::GetFps() | ||||
{ | { | ||||
if (!g_frequencyFilter) | if (!g_frequencyFilter) | ||||
return 0; | return 0; | ||||
return g_frequencyFilter->StableFrequency(); | return g_frequencyFilter->StableFrequency(); | ||||
} | } | ||||
int JSI_Main::GetTextWidth(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::string& fontName, const std::wstring& text) | int JSI_Main::GetTextWidth(const std::string& fontName, const std::wstring& text) | ||||
{ | { | ||||
int width = 0; | int width = 0; | ||||
int height = 0; | int height = 0; | ||||
CStrIntern _fontName(fontName); | CStrIntern _fontName(fontName); | ||||
CFontMetrics fontMetrics(_fontName); | CFontMetrics fontMetrics(_fontName); | ||||
fontMetrics.CalculateStringSize(text.c_str(), width, height); | fontMetrics.CalculateStringSize(text.c_str(), width, height); | ||||
return width; | return width; | ||||
} | } | ||||
std::string JSI_Main::CalculateMD5(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::string& input) | std::string JSI_Main::CalculateMD5(const std::string& input) | ||||
{ | { | ||||
u8 digest[MD5::DIGESTSIZE]; | u8 digest[MD5::DIGESTSIZE]; | ||||
MD5 m; | MD5 m; | ||||
m.Update((const u8*)input.c_str(), input.length()); | m.Update((const u8*)input.c_str(), input.length()); | ||||
m.Final(digest); | m.Final(digest); | ||||
return Hexify(digest, MD5::DIGESTSIZE); | return Hexify(digest, MD5::DIGESTSIZE); | ||||
} | } | ||||
void JSI_Main::RegisterScriptFunctions(const ScriptInterface& scriptInterface) | void JSI_Main::RegisterScriptFunctions(const ScriptInterface& scriptInterface) | ||||
{ | { | ||||
scriptInterface.RegisterFunction<void, &QuitEngine>("Exit"); | ScriptWrapper::WrapAndRegister<decltype(&QuitEngine), &QuitEngine>(scriptInterface, "Exit"); | ||||
scriptInterface.RegisterFunction<void, &StartAtlas>("RestartInAtlas"); | ScriptWrapper::WrapAndRegister<decltype(&StartAtlas), &StartAtlas>(scriptInterface, "RestartInAtlas"); | ||||
scriptInterface.RegisterFunction<bool, &AtlasIsAvailable>("AtlasIsAvailable"); | ScriptWrapper::WrapAndRegister<decltype(&GetFps), &GetFps>(scriptInterface, "GetFPS"); | ||||
scriptInterface.RegisterFunction<bool, &IsAtlasRunning>("IsAtlasRunning"); | #define Register(name) ScriptWrapper::WrapAndRegister<decltype(&name), &name>(scriptInterface, #name) | ||||
scriptInterface.RegisterFunction<void, std::string, &OpenURL>("OpenURL"); | Register(AtlasIsAvailable); | ||||
scriptInterface.RegisterFunction<std::wstring, &GetSystemUsername>("GetSystemUsername"); | Register(IsAtlasRunning); | ||||
scriptInterface.RegisterFunction<std::wstring, &GetMatchID>("GetMatchID"); | Register(OpenURL); | ||||
scriptInterface.RegisterFunction<JS::Value, VfsPath, &LoadMapSettings>("LoadMapSettings"); | Register(GetSystemUsername); | ||||
scriptInterface.RegisterFunction<bool, std::string, &HotkeyIsPressed_>("HotkeyIsPressed"); | Register(GetMatchID); | ||||
scriptInterface.RegisterFunction<int, &GetFps>("GetFPS"); | Register(LoadMapSettings); | ||||
scriptInterface.RegisterFunction<int, std::string, std::wstring, &GetTextWidth>("GetTextWidth"); | Register(HotkeyIsPressed); | ||||
scriptInterface.RegisterFunction<std::string, std::string, &CalculateMD5>("CalculateMD5"); | Register(GetTextWidth); | ||||
Register(CalculateMD5); | |||||
#undef Register | |||||
} | } |
Wildfire Games · Phabricator