Index: ps/trunk/source/gui/CGUISetting.cpp
===================================================================
--- ps/trunk/source/gui/CGUISetting.cpp (revision 22948)
+++ ps/trunk/source/gui/CGUISetting.cpp (revision 22949)
@@ -1,86 +1,87 @@
/* Copyright (C) 2019 Wildfire Games.
* 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 .
*/
#include "precompiled.h"
#include "CGUISetting.h"
#include "gui/CGUI.h"
template
CGUISetting::CGUISetting(IGUIObject& pObject, const CStr& Name)
: m_pSetting(T()), m_Name(Name), m_pObject(pObject)
{
}
template
bool CGUISetting::FromString(const CStrW& Value, const bool SendMessage)
{
T settingValue;
if (!CGUI::ParseString(&m_pObject.GetGUI(), Value, settingValue))
return false;
m_pObject.SetSetting(m_Name, settingValue, SendMessage);
return true;
};
template<>
bool CGUISetting::FromJSVal(JSContext* cx, JS::HandleValue Value, const bool SendMessage)
{
CGUIColor settingValue;
if (Value.isString())
{
CStr name;
if (!ScriptInterface::FromJSVal(cx, Value, name))
return false;
if (!settingValue.ParseString(m_pObject.GetGUI(), name))
{
+ JSAutoRequest rq(cx);
JS_ReportError(cx, "Invalid color '%s'", name.c_str());
return false;
}
}
else if (!ScriptInterface::FromJSVal(cx, Value, settingValue))
return false;
m_pObject.SetSetting(m_Name, settingValue, SendMessage);
return true;
};
template
bool CGUISetting::FromJSVal(JSContext* cx, JS::HandleValue Value, const bool SendMessage)
{
T settingValue;
if (!ScriptInterface::FromJSVal(cx, Value, settingValue))
return false;
m_pObject.SetSetting(m_Name, settingValue, SendMessage);
return true;
};
template
void CGUISetting::ToJSVal(JSContext* cx, JS::MutableHandleValue Value)
{
ScriptInterface::ToJSVal(cx, Value, m_pSetting);
};
#define TYPE(T) \
template class CGUISetting; \
#include "GUItypes.h"
#undef TYPE
Index: ps/trunk/source/gui/scripting/GuiScriptConversions.cpp
===================================================================
--- ps/trunk/source/gui/scripting/GuiScriptConversions.cpp (revision 22948)
+++ ps/trunk/source/gui/scripting/GuiScriptConversions.cpp (revision 22949)
@@ -1,359 +1,369 @@
/* Copyright (C) 2019 Wildfire Games.
* 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 .
*/
#include "precompiled.h"
#include "gui/CGUIColor.h"
#include "gui/CGUIList.h"
#include "gui/CGUISeries.h"
#include "gui/GUIbase.h"
#include "gui/IGUIObject.h"
#include "lib/external_libraries/libsdl.h"
#include "maths/Vector2D.h"
#include "ps/Hotkey.h"
#include "scriptinterface/ScriptConversions.h"
#include
#define SET(obj, name, value) STMT(JS::RootedValue v_(cx); AssignOrToJSVal(cx, &v_, (value)); JS_SetProperty(cx, obj, (name), v_))
// ignore JS_SetProperty return value, because errors should be impossible
// and we can't do anything useful in the case of errors anyway
template<> void ScriptInterface::ToJSVal(JSContext* cx, JS::MutableHandleValue ret, SDL_Event_ const& val)
{
JSAutoRequest rq(cx);
const char* typeName;
switch (val.ev.type)
{
case SDL_WINDOWEVENT: typeName = "windowevent"; break;
case SDL_KEYDOWN: typeName = "keydown"; break;
case SDL_KEYUP: typeName = "keyup"; break;
case SDL_MOUSEMOTION: typeName = "mousemotion"; break;
case SDL_MOUSEBUTTONDOWN: typeName = "mousebuttondown"; break;
case SDL_MOUSEBUTTONUP: typeName = "mousebuttonup"; break;
case SDL_QUIT: typeName = "quit"; break;
case SDL_HOTKEYDOWN: typeName = "hotkeydown"; break;
case SDL_HOTKEYUP: typeName = "hotkeyup"; break;
default: typeName = "(unknown)"; break;
}
JS::RootedObject obj(cx, JS_NewPlainObject(cx));
if (!obj)
{
ret.setUndefined();
return;
}
SET(obj, "type", typeName);
switch (val.ev.type)
{
case SDL_KEYDOWN:
case SDL_KEYUP:
{
// SET(obj, "which", (int)val.ev.key.which); // (not in wsdl.h)
// SET(obj, "state", (int)val.ev.key.state); // (not in wsdl.h)
JS::RootedObject keysym(cx, JS_NewPlainObject(cx));
if (!keysym)
{
ret.setUndefined();
return;
}
JS::RootedValue keysymVal(cx, JS::ObjectValue(*keysym));
JS_SetProperty(cx, obj, "keysym", keysymVal);
// SET(keysym, "scancode", (int)val.ev.key.keysym.scancode); // (not in wsdl.h)
SET(keysym, "sym", (int)val.ev.key.keysym.sym);
// SET(keysym, "mod", (int)val.ev.key.keysym.mod); // (not in wsdl.h)
{
SET(keysym, "unicode", JS::UndefinedHandleValue);
}
// TODO: scripts have no idea what all the key/mod enum values are;
// we should probably expose them as constants if we expect scripts to use them
break;
}
case SDL_MOUSEMOTION:
{
// SET(obj, "which", (int)val.ev.motion.which); // (not in wsdl.h)
// SET(obj, "state", (int)val.ev.motion.state); // (not in wsdl.h)
SET(obj, "x", (int)val.ev.motion.x);
SET(obj, "y", (int)val.ev.motion.y);
// SET(obj, "xrel", (int)val.ev.motion.xrel); // (not in wsdl.h)
// SET(obj, "yrel", (int)val.ev.motion.yrel); // (not in wsdl.h)
break;
}
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
{
// SET(obj, "which", (int)val.ev.button.which); // (not in wsdl.h)
SET(obj, "button", (int)val.ev.button.button);
SET(obj, "state", (int)val.ev.button.state);
SET(obj, "x", (int)val.ev.button.x);
SET(obj, "y", (int)val.ev.button.y);
SET(obj, "clicks", (int)val.ev.button.clicks);
break;
}
case SDL_HOTKEYDOWN:
case SDL_HOTKEYUP:
{
SET(obj, "hotkey", static_cast(val.ev.user.data1));
break;
}
}
ret.setObject(*obj);
}
template<> void ScriptInterface::ToJSVal(JSContext* UNUSED(cx), JS::MutableHandleValue ret, IGUIObject* const& val)
{
if (val == NULL)
ret.setNull();
else
ret.setObject(*val->GetJSObject());
}
template<> void ScriptInterface::ToJSVal(JSContext* cx, JS::MutableHandleValue ret, const CGUIString& val)
{
ScriptInterface::ToJSVal(cx, ret, val.GetOriginalString());
}
template<> bool ScriptInterface::FromJSVal(JSContext* cx, JS::HandleValue v, CGUIString& out)
{
std::wstring val;
if (!FromJSVal(cx, v, val))
return false;
out.SetValue(val);
return true;
}
JSVAL_VECTOR(CVector2D)
JSVAL_VECTOR(std::vector)
JSVAL_VECTOR(CGUIString)
template<> void ScriptInterface::ToJSVal(JSContext* cx, JS::MutableHandleValue ret, const CGUIColor& val)
{
ToJSVal(cx, ret, val);
}
/**
* The color depends on the predefined color database stored in the current GUI page.
*/
template<> bool ScriptInterface::FromJSVal(JSContext* cx, JS::HandleValue v, CGUIColor& out) = delete;
template<> void ScriptInterface::ToJSVal(JSContext* cx, JS::MutableHandleValue ret, const CSize& val)
{
CreateObject(cx, ret, "width", val.cx, "height", val.cy);
}
template<> bool ScriptInterface::FromJSVal(JSContext* cx, JS::HandleValue v, CSize& out)
{
if (!v.isObject())
{
+ JSAutoRequest rq(cx);
JS_ReportError(cx, "CSize value must be an object!");
return false;
}
if (!FromJSProperty(cx, v, "width", out.cx))
{
+ JSAutoRequest rq(cx);
JS_ReportError(cx, "Failed to get CSize.cx property");
return false;
}
if (!FromJSProperty(cx, v, "height", out.cy))
{
+ JSAutoRequest rq(cx);
JS_ReportError(cx, "Failed to get CSize.cy property");
return false;
}
return true;
}
template<> void ScriptInterface::ToJSVal(JSContext* cx, JS::MutableHandleValue ret, const CPos& val)
{
CreateObject(cx, ret, "x", val.x, "y", val.y);
}
template<> bool ScriptInterface::FromJSVal(JSContext* cx, JS::HandleValue v, CPos& out)
{
if (!v.isObject())
{
+ JSAutoRequest rq(cx);
JS_ReportError(cx, "CPos value must be an object!");
return false;
}
if (!FromJSProperty(cx, v, "x", out.x))
{
+ JSAutoRequest rq(cx);
JS_ReportError(cx, "Failed to get CPos.x property");
return false;
}
if (!FromJSProperty(cx, v, "y", out.y))
{
+ JSAutoRequest rq(cx);
JS_ReportError(cx, "Failed to get CPos.y property");
return false;
}
return true;
}
template<> void ScriptInterface::ToJSVal(JSContext* cx, JS::MutableHandleValue ret, const CRect& val)
{
CreateObject(
cx,
ret,
"left", val.left,
"right", val.right,
"top", val.top,
"bottom", val.bottom);
}
template<> void ScriptInterface::ToJSVal(JSContext* cx, JS::MutableHandleValue ret, const CClientArea& val)
{
val.ToJSVal(cx, ret);
}
template<> bool ScriptInterface::FromJSVal(JSContext* cx, JS::HandleValue v, CClientArea& out)
{
return out.FromJSVal(cx, v);
}
template<> void ScriptInterface::ToJSVal(JSContext* cx, JS::MutableHandleValue ret, const CGUIList& val)
{
ToJSVal(cx, ret, val.m_Items);
}
template<> bool ScriptInterface::FromJSVal(JSContext* cx, JS::HandleValue v, CGUIList& out)
{
return FromJSVal(cx, v, out.m_Items);
}
template<> void ScriptInterface::ToJSVal(JSContext* cx, JS::MutableHandleValue ret, const CGUISeries& val)
{
ToJSVal(cx, ret, val.m_Series);
}
template<> bool ScriptInterface::FromJSVal(JSContext* cx, JS::HandleValue v, CGUISeries& out)
{
return FromJSVal(cx, v, out.m_Series);
}
template<> void ScriptInterface::ToJSVal(JSContext* cx, JS::MutableHandleValue ret, const EVAlign& val)
{
std::string word;
switch (val)
{
case EVAlign_Top:
word = "top";
break;
case EVAlign_Bottom:
word = "bottom";
break;
case EVAlign_Center:
word = "center";
break;
default:
word = "error";
+ JSAutoRequest rq(cx);
JS_ReportError(cx, "Invalid EVAlign");
break;
}
ToJSVal(cx, ret, word);
}
template<> bool ScriptInterface::FromJSVal(JSContext* cx, JS::HandleValue v, EVAlign& out)
{
std::string word;
FromJSVal(cx, v, word);
if (word == "top")
out = EVAlign_Top;
else if (word == "bottom")
out = EVAlign_Bottom;
else if (word == "center")
out = EVAlign_Center;
else
{
out = EVAlign_Top;
+ JSAutoRequest rq(cx);
JS_ReportError(cx, "Invalid alignment (should be 'left', 'right' or 'center')");
return false;
}
return true;
}
template<> void ScriptInterface::ToJSVal(JSContext* cx, JS::MutableHandleValue ret, const EAlign& val)
{
std::string word;
switch (val)
{
case EAlign_Left:
word = "left";
break;
case EAlign_Right:
word = "right";
break;
case EAlign_Center:
word = "center";
break;
default:
word = "error";
+ JSAutoRequest rq(cx);
JS_ReportError(cx, "Invalid alignment (should be 'left', 'right' or 'center')");
break;
}
ToJSVal(cx, ret, word);
}
template<> bool ScriptInterface::FromJSVal(JSContext* cx, JS::HandleValue v, EAlign& out)
{
std::string word;
FromJSVal(cx, v, word);
if (word == "left")
out = EAlign_Left;
else if (word == "right")
out = EAlign_Right;
else if (word == "center")
out = EAlign_Center;
else
{
out = EAlign_Left;
+ JSAutoRequest rq(cx);
JS_ReportError(cx, "Invalid alignment (should be 'left', 'right' or 'center')");
return false;
}
return true;
}
template<> void ScriptInterface::ToJSVal(JSContext* cx, JS::MutableHandleValue ret, const CGUISpriteInstance& val)
{
ToJSVal(cx, ret, val.GetName());
}
template<> bool ScriptInterface::FromJSVal(JSContext* cx, JS::HandleValue v, CGUISpriteInstance& out)
{
std::string name;
if (!FromJSVal(cx, v, name))
return false;
out.SetName(name);
return true;
}
#undef SET
Index: ps/trunk/source/ps/scripting/JSInterface_Game.cpp
===================================================================
--- ps/trunk/source/ps/scripting/JSInterface_Game.cpp (revision 22948)
+++ ps/trunk/source/ps/scripting/JSInterface_Game.cpp (revision 22949)
@@ -1,185 +1,189 @@
/* Copyright (C) 2019 Wildfire Games.
* 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 .
*/
#include "precompiled.h"
#include "JSInterface_Game.h"
#include "graphics/Terrain.h"
#include "network/NetClient.h"
#include "network/NetServer.h"
#include "ps/CLogger.h"
#include "ps/Game.h"
#include "ps/Replay.h"
#include "ps/World.h"
#include "scriptinterface/ScriptInterface.h"
#include "simulation2/system/TurnManager.h"
#include "simulation2/Simulation2.h"
#include "soundmanager/SoundManager.h"
extern void EndGame();
bool JSI_Game::IsGameStarted(ScriptInterface::CxPrivate* UNUSED(pCxPrivate))
{
return g_Game;
}
void JSI_Game::StartGame(ScriptInterface::CxPrivate* pCxPrivate, JS::HandleValue attribs, int playerID)
{
ENSURE(!g_NetServer);
ENSURE(!g_NetClient);
ENSURE(!g_Game);
g_Game = new CGame(true);
// Convert from GUI script context to sim script context
CSimulation2* sim = g_Game->GetSimulation2();
JSContext* cxSim = sim->GetScriptInterface().GetContext();
JSAutoRequest rqSim(cxSim);
JS::RootedValue gameAttribs(cxSim,
sim->GetScriptInterface().CloneValueFromOtherContext(*(pCxPrivate->pScriptInterface), attribs));
g_Game->SetPlayerID(playerID);
g_Game->StartGame(&gameAttribs, "");
}
void JSI_Game::Script_EndGame(ScriptInterface::CxPrivate* UNUSED(pCxPrivate))
{
EndGame();
}
int JSI_Game::GetPlayerID(ScriptInterface::CxPrivate* UNUSED(pCxPrivate))
{
if (!g_Game)
return -1;
return g_Game->GetPlayerID();
}
void JSI_Game::SetPlayerID(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), int id)
{
if (!g_Game)
return;
g_Game->SetPlayerID(id);
}
void JSI_Game::SetViewedPlayer(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), int id)
{
if (!g_Game)
return;
g_Game->SetViewedPlayerID(id);
}
float JSI_Game::GetSimRate(ScriptInterface::CxPrivate* UNUSED(pCxPrivate))
{
return g_Game->GetSimRate();
}
void JSI_Game::SetSimRate(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), float rate)
{
g_Game->SetSimRate(rate);
}
bool JSI_Game::IsPaused(ScriptInterface::CxPrivate* pCxPrivate)
{
if (!g_Game)
{
- JS_ReportError(pCxPrivate->pScriptInterface->GetContext(), "Game is not started");
+ JSContext* cx = pCxPrivate->pScriptInterface->GetContext();
+ JSAutoRequest rq(cx);
+ JS_ReportError(cx, "Game is not started");
return false;
}
return g_Game->m_Paused;
}
void JSI_Game::SetPaused(ScriptInterface::CxPrivate* pCxPrivate, bool pause, bool sendMessage)
{
if (!g_Game)
{
- JS_ReportError(pCxPrivate->pScriptInterface->GetContext(), "Game is not started");
+ JSContext* cx = pCxPrivate->pScriptInterface->GetContext();
+ JSAutoRequest rq(cx);
+ JS_ReportError(cx, "Game is not started");
return;
}
g_Game->m_Paused = pause;
#if CONFIG2_AUDIO
if (g_SoundManager)
g_SoundManager->Pause(pause);
#endif
if (g_NetClient && sendMessage)
g_NetClient->SendPausedMessage(pause);
}
bool JSI_Game::IsVisualReplay(ScriptInterface::CxPrivate* UNUSED(pCxPrivate))
{
if (!g_Game)
return false;
return g_Game->IsVisualReplay();
}
std::wstring JSI_Game::GetCurrentReplayDirectory(ScriptInterface::CxPrivate* UNUSED(pCxPrivate))
{
if (!g_Game)
return std::wstring();
if (g_Game->IsVisualReplay())
return g_Game->GetReplayPath().Parent().Filename().string();
return g_Game->GetReplayLogger().GetDirectory().Filename().string();
}
void JSI_Game::EnableTimeWarpRecording(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), unsigned int numTurns)
{
g_Game->GetTurnManager()->EnableTimeWarpRecording(numTurns);
}
void JSI_Game::RewindTimeWarp(ScriptInterface::CxPrivate* UNUSED(pCxPrivate))
{
g_Game->GetTurnManager()->RewindTimeWarp();
}
void JSI_Game::DumpTerrainMipmap(ScriptInterface::CxPrivate* UNUSED(pCxPrivate))
{
VfsPath filename(L"screenshots/terrainmipmap.png");
g_Game->GetWorld()->GetTerrain()->GetHeightMipmap().DumpToDisk(filename);
OsPath realPath;
g_VFS->GetRealPath(filename, realPath);
LOGMESSAGERENDER("Terrain mipmap written to '%s'", realPath.string8());
}
void JSI_Game::RegisterScriptFunctions(const ScriptInterface& scriptInterface)
{
scriptInterface.RegisterFunction("IsGameStarted");
scriptInterface.RegisterFunction("StartGame");
scriptInterface.RegisterFunction("EndGame");
scriptInterface.RegisterFunction("GetPlayerID");
scriptInterface.RegisterFunction("SetPlayerID");
scriptInterface.RegisterFunction("SetViewedPlayer");
scriptInterface.RegisterFunction("GetSimRate");
scriptInterface.RegisterFunction("SetSimRate");
scriptInterface.RegisterFunction("IsPaused");
scriptInterface.RegisterFunction("SetPaused");
scriptInterface.RegisterFunction("IsVisualReplay");
scriptInterface.RegisterFunction("GetCurrentReplayDirectory");
scriptInterface.RegisterFunction("EnableTimeWarpRecording");
scriptInterface.RegisterFunction("RewindTimeWarp");
scriptInterface.RegisterFunction("DumpTerrainMipmap");
}
Index: ps/trunk/source/ps/scripting/JSInterface_VFS.cpp
===================================================================
--- ps/trunk/source/ps/scripting/JSInterface_VFS.cpp (revision 22948)
+++ ps/trunk/source/ps/scripting/JSInterface_VFS.cpp (revision 22949)
@@ -1,282 +1,281 @@
/* Copyright (C) 2019 Wildfire Games.
* 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 .
*/
#include "precompiled.h"
#include "JSInterface_VFS.h"
#include "lib/file/vfs/vfs_util.h"
#include "ps/CLogger.h"
#include "ps/CStr.h"
#include "ps/Filesystem.h"
#include "scriptinterface/ScriptVal.h"
#include "scriptinterface/ScriptInterface.h"
#include
// Only allow engine compartments to read files they may be concerned about.
#define PathRestriction_GUI {L""}
#define PathRestriction_Simulation {L"simulation/"}
#define PathRestriction_Maps {L"simulation/", L"maps/"}
// shared error handling code
#define JS_CHECK_FILE_ERR(err)\
/* this is liable to happen often, so don't complain */\
if (err == ERR::VFS_FILE_NOT_FOUND)\
{\
return 0; \
}\
/* unknown failure. We output an error message. */\
else if (err < 0)\
LOGERROR("Unknown failure in VFS %i", err );
/* else: success */
// state held across multiple BuildDirEntListCB calls; init by BuildDirEntList.
struct BuildDirEntListState
{
JSContext* cx;
JS::PersistentRootedObject filename_array;
int cur_idx;
BuildDirEntListState(JSContext* cx_)
: cx(cx_),
filename_array(cx, JS_NewArrayObject(cx, JS::HandleValueArray::empty())),
cur_idx(0)
{
}
};
// called for each matching directory entry; add its full pathname to array.
static Status BuildDirEntListCB(const VfsPath& pathname, const CFileInfo& UNUSED(fileINfo), uintptr_t cbData)
{
BuildDirEntListState* s = (BuildDirEntListState*)cbData;
JSAutoRequest rq(s->cx);
JS::RootedObject filenameArrayObj(s->cx, s->filename_array);
JS::RootedValue val(s->cx);
ScriptInterface::ToJSVal( s->cx, &val, CStrW(pathname.string()) );
JS_SetElement(s->cx, filenameArrayObj, s->cur_idx++, val);
return INFO::OK;
}
// Return an array of pathname strings, one for each matching entry in the
// specified directory.
// filter_string: default "" matches everything; otherwise, see vfs_next_dirent.
// recurse: should subdirectories be included in the search? default false.
JS::Value JSI_VFS::BuildDirEntList(ScriptInterface::CxPrivate* pCxPrivate, const std::vector& validPaths, const std::wstring& path, const std::wstring& filterStr, bool recurse)
{
if (!PathRestrictionMet(pCxPrivate, validPaths, path))
return JS::NullValue();
// convert to const wchar_t*; if there's no filter, pass 0 for speed
// (interpreted as: "accept all files without comparing").
const wchar_t* filter = 0;
if (!filterStr.empty())
filter = filterStr.c_str();
int flags = recurse ? vfs::DIR_RECURSIVE : 0;
JSContext* cx = pCxPrivate->pScriptInterface->GetContext();
JSAutoRequest rq(cx);
// build array in the callback function
BuildDirEntListState state(cx);
vfs::ForEachFile(g_VFS, path, BuildDirEntListCB, (uintptr_t)&state, filter, flags);
return JS::ObjectValue(*state.filename_array);
}
// Return true iff the file exits
bool JSI_VFS::FileExists(ScriptInterface::CxPrivate* pCxPrivate, const std::vector& validPaths, const CStrW& filename)
{
return PathRestrictionMet(pCxPrivate, validPaths, filename) && g_VFS->GetFileInfo(filename, 0) == INFO::OK;
}
// Return time [seconds since 1970] of the last modification to the specified file.
double JSI_VFS::GetFileMTime(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& filename)
{
CFileInfo fileInfo;
Status err = g_VFS->GetFileInfo(filename, &fileInfo);
JS_CHECK_FILE_ERR(err);
return (double)fileInfo.MTime();
}
// Return current size of file.
unsigned int JSI_VFS::GetFileSize(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& filename)
{
CFileInfo fileInfo;
Status err = g_VFS->GetFileInfo(filename, &fileInfo);
JS_CHECK_FILE_ERR(err);
return (unsigned int)fileInfo.Size();
}
// Return file contents in a string. Assume file is UTF-8 encoded text.
JS::Value JSI_VFS::ReadFile(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filename)
{
JSContext* cx = pCxPrivate->pScriptInterface->GetContext();
JSAutoRequest rq(cx);
CVFSFile file;
if (file.Load(g_VFS, filename) != PSRETURN_OK)
return JS::NullValue();
CStr contents = file.DecodeUTF8(); // assume it's UTF-8
// Fix CRLF line endings. (This function will only ever be used on text files.)
contents.Replace("\r\n", "\n");
// Decode as UTF-8
JS::RootedValue ret(cx);
ScriptInterface::ToJSVal(cx, &ret, contents.FromUTF8());
return ret;
}
// Return file contents as an array of lines. Assume file is UTF-8 encoded text.
JS::Value JSI_VFS::ReadFileLines(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filename)
{
const ScriptInterface& scriptInterface = *pCxPrivate->pScriptInterface;
JSContext* cx = scriptInterface.GetContext();
JSAutoRequest rq(cx);
CVFSFile file;
if (file.Load(g_VFS, filename) != PSRETURN_OK)
return JS::NullValue();
CStr contents = file.DecodeUTF8(); // assume it's UTF-8
// Fix CRLF line endings. (This function will only ever be used on text files.)
contents.Replace("\r\n", "\n");
// split into array of strings (one per line)
std::stringstream ss(contents);
JS::RootedValue line_array(cx);
ScriptInterface::CreateArray(cx, &line_array);
std::string line;
int cur_line = 0;
while (std::getline(ss, line))
{
// Decode each line as UTF-8
JS::RootedValue val(cx);
ScriptInterface::ToJSVal(cx, &val, CStr(line).FromUTF8());
scriptInterface.SetPropertyInt(line_array, cur_line++, val);
}
return line_array;
}
JS::Value JSI_VFS::ReadJSONFile(ScriptInterface::CxPrivate* pCxPrivate, const std::vector& validPaths, const CStrW& filePath)
{
if (!PathRestrictionMet(pCxPrivate, validPaths, filePath))
return JS::NullValue();
JSContext* cx = pCxPrivate->pScriptInterface->GetContext();
JSAutoRequest rq(cx);
JS::RootedValue out(cx);
pCxPrivate->pScriptInterface->ReadJSONFile(filePath, &out);
return out;
}
void JSI_VFS::WriteJSONFile(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filePath, JS::HandleValue val1)
{
JSContext* cx = pCxPrivate->pScriptInterface->GetContext();
JSAutoRequest rq(cx);
// TODO: This is a workaround because we need to pass a MutableHandle to StringifyJSON.
JS::RootedValue val(cx, val1);
std::string str(pCxPrivate->pScriptInterface->StringifyJSON(&val, false));
VfsPath path(filePath);
WriteBuffer buf;
buf.Append(str.c_str(), str.length());
g_VFS->CreateFile(path, buf.Data(), buf.Size());
}
bool JSI_VFS::PathRestrictionMet(ScriptInterface::CxPrivate* pCxPrivate, const std::vector& validPaths, const CStrW& filePath)
{
for (const CStrW& validPath : validPaths)
if (filePath.find(validPath) == 0)
return true;
CStrW allowedPaths;
for (std::size_t i = 0; i < validPaths.size(); ++i)
{
if (i != 0)
allowedPaths += L", ";
allowedPaths += L"\"" + validPaths[i] + L"\"";
}
- JS_ReportError(
- pCxPrivate->pScriptInterface->GetContext(),
- "This part of the engine may only read from %s!",
- utf8_from_wstring(allowedPaths).c_str());
+ JSContext* cx = pCxPrivate->pScriptInterface->GetContext();
+ JSAutoRequest rq(cx);
+ JS_ReportError(cx, "This part of the engine may only read from %s!", utf8_from_wstring(allowedPaths).c_str());
return false;
}
#define VFS_ScriptFunctions(context)\
JS::Value Script_ReadJSONFile_##context(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filePath)\
{\
return JSI_VFS::ReadJSONFile(pCxPrivate, PathRestriction_##context, filePath);\
}\
JS::Value Script_ListDirectoryFiles_##context(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& path, const std::wstring& filterStr, bool recurse)\
{\
return JSI_VFS::BuildDirEntList(pCxPrivate, PathRestriction_##context, path, filterStr, recurse);\
}\
bool Script_FileExists_##context(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filePath)\
{\
return JSI_VFS::FileExists(pCxPrivate, PathRestriction_##context, filePath);\
}\
VFS_ScriptFunctions(GUI);
VFS_ScriptFunctions(Simulation);
VFS_ScriptFunctions(Maps);
#undef VFS_ScriptFunctions
void JSI_VFS::RegisterScriptFunctions_GUI(const ScriptInterface& scriptInterface)
{
scriptInterface.RegisterFunction("ListDirectoryFiles");
scriptInterface.RegisterFunction("FileExists");
scriptInterface.RegisterFunction("GetFileMTime");
scriptInterface.RegisterFunction("GetFileSize");
scriptInterface.RegisterFunction("ReadFile");
scriptInterface.RegisterFunction("ReadFileLines");
scriptInterface.RegisterFunction("ReadJSONFile");
scriptInterface.RegisterFunction("WriteJSONFile");
}
void JSI_VFS::RegisterScriptFunctions_Simulation(const ScriptInterface& scriptInterface)
{
scriptInterface.RegisterFunction("ListDirectoryFiles");
scriptInterface.RegisterFunction("FileExists");
scriptInterface.RegisterFunction("ReadJSONFile");
}
void JSI_VFS::RegisterScriptFunctions_Maps(const ScriptInterface& scriptInterface)
{
scriptInterface.RegisterFunction("ListDirectoryFiles");
scriptInterface.RegisterFunction("FileExists");
scriptInterface.RegisterFunction("ReadJSONFile");
}
Index: ps/trunk/source/scriptinterface/ScriptInterface.cpp
===================================================================
--- ps/trunk/source/scriptinterface/ScriptInterface.cpp (revision 22948)
+++ ps/trunk/source/scriptinterface/ScriptInterface.cpp (revision 22949)
@@ -1,1148 +1,1149 @@
/* Copyright (C) 2019 Wildfire Games.
* 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 .
*/
#include "precompiled.h"
#include "ScriptInterface.h"
#include "ScriptRuntime.h"
#include "ScriptStats.h"
#include "lib/debug.h"
#include "lib/utf8.h"
#include "ps/CLogger.h"
#include "ps/Filesystem.h"
#include "ps/Profile.h"
#include "ps/utf16string.h"
#include
#include