2017-01-23 18:04:50 -08:00
|
|
|
/* Copyright (C) 2017 Wildfire Games.
|
2010-01-09 11:20:14 -08:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
|
*
|
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
|
* 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
|
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
|
|
#include "scriptinterface/ScriptInterface.h"
|
|
|
|
|
|
2016-02-04 09:14:46 -08:00
|
|
|
#include "graphics/FontMetrics.h"
|
2010-01-09 11:20:14 -08:00
|
|
|
#include "graphics/GameView.h"
|
2010-08-04 14:15:41 -07:00
|
|
|
#include "graphics/MapReader.h"
|
2014-11-12 17:26:22 -08:00
|
|
|
#include "graphics/scripting/JSInterface_GameView.h"
|
2014-01-04 02:14:53 -08:00
|
|
|
#include "gui/IGUIObject.h"
|
2017-09-11 17:54:49 -07:00
|
|
|
#include "gui/scripting/JSInterface_GUIManager.h"
|
2014-01-04 02:14:53 -08:00
|
|
|
#include "gui/scripting/JSInterface_GUITypes.h"
|
2014-04-20 13:03:57 -07:00
|
|
|
#include "i18n/scripting/JSInterface_L10n.h"
|
2010-06-30 14:41:04 -07:00
|
|
|
#include "lib/sysdep/sysdep.h"
|
2014-04-20 13:03:57 -07:00
|
|
|
#include "lib/utf8.h"
|
2013-11-16 10:38:40 -08:00
|
|
|
#include "lobby/scripting/JSInterface_Lobby.h"
|
2017-09-11 17:29:10 -07:00
|
|
|
#include "network/scripting/JSInterface_Network.h"
|
2014-11-12 17:26:22 -08:00
|
|
|
#include "ps/GUID.h"
|
|
|
|
|
#include "ps/GameSetup/Atlas.h"
|
2014-01-04 02:14:53 -08:00
|
|
|
#include "ps/Globals.h" // g_frequencyFilter
|
2010-11-07 12:42:52 -08:00
|
|
|
#include "ps/Hotkey.h"
|
2013-09-29 11:56:50 -07:00
|
|
|
#include "ps/scripting/JSInterface_ConfigDB.h"
|
2013-09-21 07:48:19 -07:00
|
|
|
#include "ps/scripting/JSInterface_Console.h"
|
2017-09-11 19:25:28 -07:00
|
|
|
#include "ps/scripting/JSInterface_Debug.h"
|
2017-09-11 18:38:46 -07:00
|
|
|
#include "ps/scripting/JSInterface_Game.h"
|
2014-08-25 09:02:54 -07:00
|
|
|
#include "ps/scripting/JSInterface_Mod.h"
|
2017-09-11 16:22:28 -07:00
|
|
|
#include "ps/scripting/JSInterface_SavedGame.h"
|
2014-01-04 02:14:53 -08:00
|
|
|
#include "ps/scripting/JSInterface_VFS.h"
|
2015-09-21 10:00:21 -07:00
|
|
|
#include "ps/scripting/JSInterface_VisualReplay.h"
|
2013-09-21 01:24:45 -07:00
|
|
|
#include "renderer/scripting/JSInterface_Renderer.h"
|
2017-09-11 18:53:46 -07:00
|
|
|
#include "simulation2/scripting/JSInterface_Simulation.h"
|
2014-01-04 02:14:53 -08:00
|
|
|
#include "soundmanager/scripting/JSInterface_Sound.h"
|
2014-11-12 17:26:22 -08:00
|
|
|
#include "tools/atlas/GameInterface/GameLoop.h"
|
2010-01-09 11:20:14 -08:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* This file defines a set of functions that are available to GUI scripts, to allow
|
|
|
|
|
* interaction with the rest of the engine.
|
|
|
|
|
* Functions are exposed to scripts within the global object 'Engine', so
|
|
|
|
|
* scripts should call "Engine.FunctionName(...)" etc.
|
|
|
|
|
*/
|
|
|
|
|
|
2010-07-07 14:47:31 -07:00
|
|
|
extern void restart_mainloop_in_atlas(); // from main.cpp
|
2014-01-04 02:14:53 -08:00
|
|
|
extern void EndGame();
|
|
|
|
|
extern void kill_mainloop();
|
2010-07-07 14:47:31 -07:00
|
|
|
|
2010-01-09 11:20:14 -08:00
|
|
|
namespace {
|
|
|
|
|
|
2016-01-23 07:17:56 -08:00
|
|
|
void OpenURL(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::string& url)
|
2010-07-07 14:47:31 -07:00
|
|
|
{
|
|
|
|
|
sys_open_url(url);
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-04 02:14:53 -08:00
|
|
|
std::wstring GetMatchID(ScriptInterface::CxPrivate* UNUSED(pCxPrivate))
|
2013-11-07 12:07:24 -08:00
|
|
|
{
|
|
|
|
|
return ps_generate_guid().FromUTF8();
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-04 02:14:53 -08:00
|
|
|
void RestartInAtlas(ScriptInterface::CxPrivate* UNUSED(pCxPrivate))
|
2010-07-07 14:47:31 -07:00
|
|
|
{
|
|
|
|
|
restart_mainloop_in_atlas();
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-04 02:14:53 -08:00
|
|
|
bool AtlasIsAvailable(ScriptInterface::CxPrivate* UNUSED(pCxPrivate))
|
2010-07-07 14:47:31 -07:00
|
|
|
{
|
|
|
|
|
return ATLAS_IsAvailable();
|
|
|
|
|
}
|
2010-07-02 14:28:48 -07:00
|
|
|
|
2014-01-04 02:14:53 -08:00
|
|
|
bool IsAtlasRunning(ScriptInterface::CxPrivate* UNUSED(pCxPrivate))
|
2012-04-19 18:41:54 -07:00
|
|
|
{
|
2017-09-11 18:38:46 -07:00
|
|
|
return g_AtlasGameLoop && g_AtlasGameLoop->running;
|
2012-04-19 18:41:54 -07:00
|
|
|
}
|
|
|
|
|
|
2016-01-23 07:17:56 -08:00
|
|
|
JS::Value LoadMapSettings(ScriptInterface::CxPrivate* pCxPrivate, const VfsPath& pathname)
|
2010-08-04 14:15:41 -07:00
|
|
|
{
|
2015-01-24 06:46:52 -08:00
|
|
|
JSContext* cx = pCxPrivate->pScriptInterface->GetContext();
|
|
|
|
|
JSAutoRequest rq(cx);
|
|
|
|
|
|
2010-08-04 14:15:41 -07:00
|
|
|
CMapSummaryReader reader;
|
2010-10-29 21:02:42 -07:00
|
|
|
|
Implements skirmish maps, based on patch by sanderd17, fixes #1198. Skirmish maps are like scenarios, except the player can choose their civ during match setup. To create a skirmish map: place some skirmish entities for each player in Atlas (see templates/skirmish/* for examples), uncheck the player's civ in Atlas' player panel if desired, and save in the maps/skirmishes directory. The map will appear in match setup under the "Skirmish" match type.
Implements custom, VFS-based map load/save dialogs for Atlas (replaces
broken native file dialogs), fixes #631, #889.
Fixes map loading/saving to handle arbitrary subdirectories for better
organization.
Adds default settings to Atlas player panel, fixes #1872. Each setting
now has a checkbox to choose whether it should be saved with the map
(avoids writing lots of useless default data for each map).
Adds map preview setting to Atlas, refs #1745.
Cleans up and simplifies some duplicate code.
Fixes optional serialization performance test.
This was SVN commit r13938.
2013-10-03 19:29:16 -07:00
|
|
|
if (reader.LoadMap(pathname) != PSRETURN_OK)
|
2015-01-24 06:46:52 -08:00
|
|
|
return JS::UndefinedValue();
|
2010-08-04 14:15:41 -07:00
|
|
|
|
2015-01-24 06:46:52 -08:00
|
|
|
JS::RootedValue settings(cx);
|
|
|
|
|
reader.GetMapSettings(*(pCxPrivate->pScriptInterface), &settings);
|
|
|
|
|
return settings;
|
2010-08-04 14:15:41 -07:00
|
|
|
}
|
|
|
|
|
|
2016-01-23 07:17:56 -08:00
|
|
|
bool HotkeyIsPressed_(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::string& hotkeyName)
|
2010-11-07 12:42:52 -08:00
|
|
|
{
|
|
|
|
|
return HotkeyIsPressed(hotkeyName);
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-04 02:14:53 -08:00
|
|
|
void Script_EndGame(ScriptInterface::CxPrivate* UNUSED(pCxPrivate))
|
|
|
|
|
{
|
|
|
|
|
EndGame();
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-15 10:13:35 -07:00
|
|
|
CStrW GetSystemUsername(ScriptInterface::CxPrivate* UNUSED(pCxPrivate))
|
|
|
|
|
{
|
|
|
|
|
return sys_get_user_name();
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-04 02:14:53 -08:00
|
|
|
void ExitProgram(ScriptInterface::CxPrivate* UNUSED(pCxPrivate))
|
|
|
|
|
{
|
|
|
|
|
kill_mainloop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// - This value is recalculated once a frame. We take special care to
|
|
|
|
|
// filter it, so it is both accurate and free of jitter.
|
|
|
|
|
int GetFps(ScriptInterface::CxPrivate* UNUSED(pCxPrivate))
|
|
|
|
|
{
|
|
|
|
|
int freq = 0;
|
|
|
|
|
if (g_frequencyFilter)
|
|
|
|
|
freq = g_frequencyFilter->StableFrequency();
|
|
|
|
|
return freq;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-04 09:14:46 -08:00
|
|
|
int GetTextWidth(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const CStr& fontName, const CStrW& text)
|
|
|
|
|
{
|
|
|
|
|
int width = 0;
|
|
|
|
|
int height = 0;
|
|
|
|
|
CStrIntern _fontName(fontName);
|
|
|
|
|
CFontMetrics fontMetrics(_fontName);
|
|
|
|
|
fontMetrics.CalculateStringSize(text.c_str(), width, height);
|
|
|
|
|
return width;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-17 15:10:09 -07:00
|
|
|
} // namespace
|
2014-01-04 02:14:53 -08:00
|
|
|
|
2010-01-09 11:20:14 -08:00
|
|
|
void GuiScriptingInit(ScriptInterface& scriptInterface)
|
|
|
|
|
{
|
2014-01-04 02:14:53 -08:00
|
|
|
JSI_IGUIObject::init(scriptInterface);
|
|
|
|
|
JSI_GUITypes::init(scriptInterface);
|
2014-04-20 13:03:57 -07:00
|
|
|
|
2013-09-12 05:40:05 -07:00
|
|
|
JSI_GameView::RegisterScriptFunctions(scriptInterface);
|
2013-09-21 01:24:45 -07:00
|
|
|
JSI_Renderer::RegisterScriptFunctions(scriptInterface);
|
2013-09-21 07:48:19 -07:00
|
|
|
JSI_Console::RegisterScriptFunctions(scriptInterface);
|
2013-09-29 11:56:50 -07:00
|
|
|
JSI_ConfigDB::RegisterScriptFunctions(scriptInterface);
|
2017-09-11 19:25:28 -07:00
|
|
|
JSI_Debug::RegisterScriptFunctions(scriptInterface);
|
2017-09-11 18:38:46 -07:00
|
|
|
JSI_Game::RegisterScriptFunctions(scriptInterface);
|
2017-09-11 17:54:49 -07:00
|
|
|
JSI_GUIManager::RegisterScriptFunctions(scriptInterface);
|
2014-08-25 09:02:54 -07:00
|
|
|
JSI_Mod::RegisterScriptFunctions(scriptInterface);
|
2017-09-11 17:29:10 -07:00
|
|
|
JSI_Network::RegisterScriptFunctions(scriptInterface);
|
2017-09-11 16:22:28 -07:00
|
|
|
JSI_SavedGame::RegisterScriptFunctions(scriptInterface);
|
2014-01-04 02:14:53 -08:00
|
|
|
JSI_Sound::RegisterScriptFunctions(scriptInterface);
|
2017-09-11 18:53:46 -07:00
|
|
|
JSI_Simulation::RegisterScriptFunctions(scriptInterface);
|
2014-04-20 13:03:57 -07:00
|
|
|
JSI_L10n::RegisterScriptFunctions(scriptInterface);
|
2014-11-15 18:10:24 -08:00
|
|
|
JSI_Lobby::RegisterScriptFunctions(scriptInterface);
|
2017-09-07 21:01:26 -07:00
|
|
|
JSI_VFS::RegisterScriptFunctions(scriptInterface);
|
2015-09-21 10:00:21 -07:00
|
|
|
JSI_VisualReplay::RegisterScriptFunctions(scriptInterface);
|
2015-08-21 10:08:41 -07:00
|
|
|
|
2014-01-04 02:14:53 -08:00
|
|
|
scriptInterface.RegisterFunction<void, &Script_EndGame>("EndGame");
|
2010-07-07 14:47:31 -07:00
|
|
|
scriptInterface.RegisterFunction<void, std::string, &OpenURL>("OpenURL");
|
2013-11-07 12:07:24 -08:00
|
|
|
scriptInterface.RegisterFunction<std::wstring, &GetMatchID>("GetMatchID");
|
2010-07-07 14:47:31 -07:00
|
|
|
scriptInterface.RegisterFunction<void, &RestartInAtlas>("RestartInAtlas");
|
|
|
|
|
scriptInterface.RegisterFunction<bool, &AtlasIsAvailable>("AtlasIsAvailable");
|
2012-04-19 18:41:54 -07:00
|
|
|
scriptInterface.RegisterFunction<bool, &IsAtlasRunning>("IsAtlasRunning");
|
2015-01-24 06:46:52 -08:00
|
|
|
scriptInterface.RegisterFunction<JS::Value, VfsPath, &LoadMapSettings>("LoadMapSettings");
|
2010-11-07 12:42:52 -08:00
|
|
|
scriptInterface.RegisterFunction<bool, std::string, &HotkeyIsPressed_>("HotkeyIsPressed");
|
2014-01-04 02:14:53 -08:00
|
|
|
scriptInterface.RegisterFunction<void, &ExitProgram>("Exit");
|
|
|
|
|
scriptInterface.RegisterFunction<int, &GetFps>("GetFPS");
|
2016-02-04 09:14:46 -08:00
|
|
|
scriptInterface.RegisterFunction<int, CStr, CStrW, &GetTextWidth>("GetTextWidth");
|
2016-05-15 10:13:35 -07:00
|
|
|
scriptInterface.RegisterFunction<CStrW, &GetSystemUsername>("GetSystemUsername");
|
2010-01-09 11:20:14 -08:00
|
|
|
}
|