Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/simulation2/system/ComponentManager.cpp
Show All 24 Lines | |||||
#include "SimContext.h" | #include "SimContext.h" | ||||
#include "simulation2/MessageTypes.h" | #include "simulation2/MessageTypes.h" | ||||
#include "simulation2/components/ICmpTemplateManager.h" | #include "simulation2/components/ICmpTemplateManager.h" | ||||
#include "lib/utf8.h" | #include "lib/utf8.h" | ||||
#include "ps/CLogger.h" | #include "ps/CLogger.h" | ||||
#include "ps/Filesystem.h" | #include "ps/Filesystem.h" | ||||
#include "ps/scripting/JSInterface_VFS.h" | |||||
/** | /** | ||||
* Used for script-only message types. | * Used for script-only message types. | ||||
*/ | */ | ||||
class CMessageScripted : public CMessage | class CMessageScripted : public CMessage | ||||
{ | { | ||||
public: | public: | ||||
virtual int GetType() const { return mtid; } | virtual int GetType() const { return mtid; } | ||||
Show All 22 Lines | CComponentManager::CComponentManager(CSimContext& context, shared_ptr<ScriptRuntime> rt, bool skipScriptFunctions) : | ||||
m_ScriptInterface.SetCallbackData(static_cast<void*> (this)); | m_ScriptInterface.SetCallbackData(static_cast<void*> (this)); | ||||
m_ScriptInterface.ReplaceNondeterministicRNG(m_RNG); | m_ScriptInterface.ReplaceNondeterministicRNG(m_RNG); | ||||
m_ScriptInterface.LoadGlobalScripts(); | m_ScriptInterface.LoadGlobalScripts(); | ||||
// For component script tests, the test system sets up its own scripted implementation of | // For component script tests, the test system sets up its own scripted implementation of | ||||
// these functions, so we skip registering them here in those cases | // these functions, so we skip registering them here in those cases | ||||
if (!skipScriptFunctions) | if (!skipScriptFunctions) | ||||
{ | { | ||||
JSI_VFS::RegisterScriptFunctions_Simulation(m_ScriptInterface); | |||||
m_ScriptInterface.RegisterFunction<void, int, std::string, JS::HandleValue, CComponentManager::Script_RegisterComponentType> ("RegisterComponentType"); | m_ScriptInterface.RegisterFunction<void, int, std::string, JS::HandleValue, CComponentManager::Script_RegisterComponentType> ("RegisterComponentType"); | ||||
m_ScriptInterface.RegisterFunction<void, int, std::string, JS::HandleValue, CComponentManager::Script_RegisterSystemComponentType> ("RegisterSystemComponentType"); | m_ScriptInterface.RegisterFunction<void, int, std::string, JS::HandleValue, CComponentManager::Script_RegisterSystemComponentType> ("RegisterSystemComponentType"); | ||||
m_ScriptInterface.RegisterFunction<void, int, std::string, JS::HandleValue, CComponentManager::Script_ReRegisterComponentType> ("ReRegisterComponentType"); | m_ScriptInterface.RegisterFunction<void, int, std::string, JS::HandleValue, CComponentManager::Script_ReRegisterComponentType> ("ReRegisterComponentType"); | ||||
m_ScriptInterface.RegisterFunction<void, std::string, CComponentManager::Script_RegisterInterface> ("RegisterInterface"); | m_ScriptInterface.RegisterFunction<void, std::string, CComponentManager::Script_RegisterInterface> ("RegisterInterface"); | ||||
m_ScriptInterface.RegisterFunction<void, std::string, CComponentManager::Script_RegisterMessageType> ("RegisterMessageType"); | m_ScriptInterface.RegisterFunction<void, std::string, CComponentManager::Script_RegisterMessageType> ("RegisterMessageType"); | ||||
m_ScriptInterface.RegisterFunction<void, std::string, JS::HandleValue, CComponentManager::Script_RegisterGlobal> ("RegisterGlobal"); | m_ScriptInterface.RegisterFunction<void, std::string, JS::HandleValue, CComponentManager::Script_RegisterGlobal> ("RegisterGlobal"); | ||||
m_ScriptInterface.RegisterFunction<IComponent*, int, int, CComponentManager::Script_QueryInterface> ("QueryInterface"); | m_ScriptInterface.RegisterFunction<IComponent*, int, int, CComponentManager::Script_QueryInterface> ("QueryInterface"); | ||||
m_ScriptInterface.RegisterFunction<std::vector<int>, int, CComponentManager::Script_GetEntitiesWithInterface> ("GetEntitiesWithInterface"); | m_ScriptInterface.RegisterFunction<std::vector<int>, int, CComponentManager::Script_GetEntitiesWithInterface> ("GetEntitiesWithInterface"); | ||||
m_ScriptInterface.RegisterFunction<std::vector<IComponent*>, int, CComponentManager::Script_GetComponentsWithInterface> ("GetComponentsWithInterface"); | m_ScriptInterface.RegisterFunction<std::vector<IComponent*>, int, CComponentManager::Script_GetComponentsWithInterface> ("GetComponentsWithInterface"); | ||||
m_ScriptInterface.RegisterFunction<void, int, int, JS::HandleValue, CComponentManager::Script_PostMessage> ("PostMessage"); | m_ScriptInterface.RegisterFunction<void, int, int, JS::HandleValue, CComponentManager::Script_PostMessage> ("PostMessage"); | ||||
m_ScriptInterface.RegisterFunction<void, int, JS::HandleValue, CComponentManager::Script_BroadcastMessage> ("BroadcastMessage"); | m_ScriptInterface.RegisterFunction<void, int, JS::HandleValue, CComponentManager::Script_BroadcastMessage> ("BroadcastMessage"); | ||||
m_ScriptInterface.RegisterFunction<int, std::string, CComponentManager::Script_AddEntity> ("AddEntity"); | m_ScriptInterface.RegisterFunction<int, std::string, CComponentManager::Script_AddEntity> ("AddEntity"); | ||||
m_ScriptInterface.RegisterFunction<int, std::string, CComponentManager::Script_AddLocalEntity> ("AddLocalEntity"); | m_ScriptInterface.RegisterFunction<int, std::string, CComponentManager::Script_AddLocalEntity> ("AddLocalEntity"); | ||||
m_ScriptInterface.RegisterFunction<void, int, CComponentManager::Script_DestroyEntity> ("DestroyEntity"); | m_ScriptInterface.RegisterFunction<void, int, CComponentManager::Script_DestroyEntity> ("DestroyEntity"); | ||||
m_ScriptInterface.RegisterFunction<void, CComponentManager::Script_FlushDestroyedEntities> ("FlushDestroyedEntities"); | m_ScriptInterface.RegisterFunction<void, CComponentManager::Script_FlushDestroyedEntities> ("FlushDestroyedEntities"); | ||||
m_ScriptInterface.RegisterFunction<bool, std::wstring, CComponentManager::Script_DataFileExists> ("DataFileExists"); | m_ScriptInterface.RegisterFunction<bool, std::wstring, CComponentManager::Script_DataFileExists> ("DataFileExists"); | ||||
m_ScriptInterface.RegisterFunction<JS::Value, std::wstring, CComponentManager::Script_ReadJSONFile> ("ReadJSONFile"); | |||||
m_ScriptInterface.RegisterFunction<JS::Value, std::wstring, CComponentManager::Script_ReadCivJSONFile> ("ReadCivJSONFile"); | |||||
m_ScriptInterface.RegisterFunction<std::vector<std::string>, std::wstring, bool, CComponentManager::Script_FindJSONFiles> ("FindJSONFiles"); | m_ScriptInterface.RegisterFunction<std::vector<std::string>, std::wstring, bool, CComponentManager::Script_FindJSONFiles> ("FindJSONFiles"); | ||||
} | } | ||||
// Define MT_*, IID_* as script globals, and store their names | // Define MT_*, IID_* as script globals, and store their names | ||||
#define MESSAGE(name) m_ScriptInterface.SetGlobal("MT_" #name, (int)MT_##name); | #define MESSAGE(name) m_ScriptInterface.SetGlobal("MT_" #name, (int)MT_##name); | ||||
#define INTERFACE(name) \ | #define INTERFACE(name) \ | ||||
m_ScriptInterface.SetGlobal("IID_" #name, (int)IID_##name); \ | m_ScriptInterface.SetGlobal("IID_" #name, (int)IID_##name); \ | ||||
m_InterfaceIdsByName[#name] = IID_##name; | m_InterfaceIdsByName[#name] = IID_##name; | ||||
▲ Show 20 Lines • Show All 1,090 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
bool CComponentManager::Script_DataFileExists(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& fileName) | bool CComponentManager::Script_DataFileExists(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& fileName) | ||||
{ | { | ||||
VfsPath path = VfsPath(L"simulation/data") / fileName; | VfsPath path = VfsPath(L"simulation/data") / fileName; | ||||
return VfsFileExists(path); | return VfsFileExists(path); | ||||
} | } | ||||
JS::Value CComponentManager::Script_ReadJSONFile(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& fileName) | |||||
{ | |||||
return ReadJSONFile(pCxPrivate, L"simulation/data", fileName); | |||||
} | |||||
JS::Value CComponentManager::Script_ReadCivJSONFile(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& fileName) | |||||
{ | |||||
return ReadJSONFile(pCxPrivate, L"simulation/data/civs", fileName); | |||||
} | |||||
JS::Value CComponentManager::ReadJSONFile(ScriptInterface::CxPrivate* pCxPrivate, const std::wstring& filePath, const std::wstring& fileName) | |||||
{ | |||||
CComponentManager* componentManager = static_cast<CComponentManager*> (pCxPrivate->pCBData); | |||||
JSContext* cx = pCxPrivate->pScriptInterface->GetContext(); | |||||
JSAutoRequest rq(cx); | |||||
VfsPath path = VfsPath(filePath) / fileName; | |||||
JS::RootedValue out(cx); | |||||
componentManager->GetScriptInterface().ReadJSONFile(path, &out); | |||||
return out; | |||||
} | |||||
Status CComponentManager::FindJSONFilesCallback(const VfsPath& pathname, const CFileInfo& UNUSED(fileInfo), const uintptr_t cbData) | Status CComponentManager::FindJSONFilesCallback(const VfsPath& pathname, const CFileInfo& UNUSED(fileInfo), const uintptr_t cbData) | ||||
{ | { | ||||
FindJSONFilesCallbackData* data = (FindJSONFilesCallbackData*)cbData; | FindJSONFilesCallbackData* data = (FindJSONFilesCallbackData*)cbData; | ||||
VfsPath pathstem = pathname.ChangeExtension(L""); | VfsPath pathstem = pathname.ChangeExtension(L""); | ||||
// Strip the root from the path | // Strip the root from the path | ||||
std::wstring name = pathstem.string().substr(data->path.string().length()); | std::wstring name = pathstem.string().substr(data->path.string().length()); | ||||
Show All 26 Lines |
Wildfire Games · Phabricator