Unify two rmgen and GUI JSON file loading script functions, refs #4868.

Revert the introduction of the exact copies of FileExists ReadJSONFile
functions in MapGenerator.cpp introduced by 871ed04521.
Include JSInterface_VFS.cpp from MapGenerator.cpp to remove that code
while gaining the other missing VFS file loading functions in rmgen/.
Split RegisterScriptFunctions of JSInterface_VFS.cpp from eb4e66aab3
into RegisterReadOnlyScriptFunctions and RegisterWriteScriptFunctions to
prevent unintentional write access.

This was SVN commit r20507.
This commit is contained in:
elexis 2017-11-24 17:19:16 +00:00
parent 7a17a3152c
commit 4275a8a33c
4 changed files with 11 additions and 20 deletions

View file

@ -23,7 +23,7 @@
#include "lib/timer.h"
#include "ps/CLogger.h"
#include "ps/Profile.h"
#include "ps/scripting/JSInterface_VFS.h"
// TODO: what's a good default? perhaps based on map size
#define RMS_RUNTIME_SIZE 96 * 1024 * 1024
@ -100,12 +100,11 @@ bool CMapGeneratorWorker::Run()
m_ScriptInterface->LoadGlobalScripts();
// Functions for RMS
JSI_VFS::RegisterReadOnlyScriptFunctions(*m_ScriptInterface);
m_ScriptInterface->RegisterFunction<bool, std::wstring, CMapGeneratorWorker::LoadLibrary>("LoadLibrary");
m_ScriptInterface->RegisterFunction<void, JS::HandleValue, CMapGeneratorWorker::ExportMap>("ExportMap");
m_ScriptInterface->RegisterFunction<void, int, CMapGeneratorWorker::SetProgress>("SetProgress");
m_ScriptInterface->RegisterFunction<void, CMapGeneratorWorker::MaybeGC>("MaybeGC");
m_ScriptInterface->RegisterFunction<bool, std::wstring, CMapGeneratorWorker::FileExists>("FileExists");
m_ScriptInterface->RegisterFunction<JS::Value, std::wstring, CMapGeneratorWorker::ReadJSONFile> ("ReadJSONFile");
m_ScriptInterface->RegisterFunction<std::vector<std::string>, CMapGeneratorWorker::GetCivData>("GetCivData");
m_ScriptInterface->RegisterFunction<CParamNode, std::string, CMapGeneratorWorker::GetTemplate>("GetTemplate");
m_ScriptInterface->RegisterFunction<bool, std::string, CMapGeneratorWorker::TemplateExists>("TemplateExists");
@ -198,20 +197,6 @@ void CMapGeneratorWorker::MaybeGC(ScriptInterface::CxPrivate* pCxPrivate)
self->m_ScriptInterface->MaybeGC();
}
bool CMapGeneratorWorker::FileExists(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& filePath)
{
return g_VFS->GetFileInfo(filePath, 0) == INFO::OK;
}
JS::Value CMapGeneratorWorker::ReadJSONFile(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filePath)
{
JSContext* cx = pCxPrivate->pScriptInterface->GetContext();
JSAutoRequest rq(cx);
JS::RootedValue out(cx);
pCxPrivate->pScriptInterface->ReadJSONFile(filePath, &out);
return out;
}
std::vector<std::string> CMapGeneratorWorker::GetCivData(ScriptInterface::CxPrivate* UNUSED(pCxPrivate))
{
VfsPath path(L"simulation/data/civs/");

View file

@ -65,6 +65,7 @@ void GuiScriptingInit(ScriptInterface& scriptInterface)
JSI_SavedGame::RegisterScriptFunctions(scriptInterface);
JSI_Simulation::RegisterScriptFunctions(scriptInterface);
JSI_Sound::RegisterScriptFunctions(scriptInterface);
JSI_VFS::RegisterScriptFunctions(scriptInterface);
JSI_VFS::RegisterReadOnlyScriptFunctions(scriptInterface);
JSI_VFS::RegisterWriteScriptFunctions(scriptInterface);
JSI_VisualReplay::RegisterScriptFunctions(scriptInterface);
}

View file

@ -203,7 +203,7 @@ void JSI_VFS::WriteJSONFile(ScriptInterface::CxPrivate* pCxPrivate, const std::w
g_VFS->CreateFile(path, buf.Data(), buf.Size());
}
void JSI_VFS::RegisterScriptFunctions(const ScriptInterface& scriptInterface)
void JSI_VFS::RegisterReadOnlyScriptFunctions(const ScriptInterface& scriptInterface)
{
scriptInterface.RegisterFunction<JS::Value, std::wstring, std::wstring, bool, &JSI_VFS::BuildDirEntList>("BuildDirEntList");
scriptInterface.RegisterFunction<bool, CStrW, JSI_VFS::FileExists>("FileExists");
@ -212,5 +212,9 @@ void JSI_VFS::RegisterScriptFunctions(const ScriptInterface& scriptInterface)
scriptInterface.RegisterFunction<JS::Value, std::wstring, &JSI_VFS::ReadFile>("ReadFile");
scriptInterface.RegisterFunction<JS::Value, std::wstring, &JSI_VFS::ReadFileLines>("ReadFileLines");
scriptInterface.RegisterFunction<JS::Value, std::wstring, &ReadJSONFile>("ReadJSONFile");
}
void JSI_VFS::RegisterWriteScriptFunctions(const ScriptInterface& scriptInterface)
{
scriptInterface.RegisterFunction<void, std::wstring, JS::HandleValue, &WriteJSONFile>("WriteJSONFile");
}

View file

@ -61,7 +61,8 @@ namespace JSI_VFS
// Save given JS Object to a JSON file
void WriteJSONFile(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filePath, JS::HandleValue val1);
void RegisterScriptFunctions(const ScriptInterface& scriptInterface);
void RegisterReadOnlyScriptFunctions(const ScriptInterface& scriptInterface);
void RegisterWriteScriptFunctions(const ScriptInterface& scriptInterface);
}
#endif