jsval CMessage::ToJSValCached(ScriptInterface& scriptInterface) const { if (m_Cached.uninitialised()) m_Cached = CScriptValRooted(scriptInterface.GetContext(), ToJSVal(scriptInterface)); return m_Cached.get(); }
/** * Send a request to register a game to the server. * * @param data A JS array of game attributes */ void XmppClient::SendIqRegisterGame(ScriptInterface& scriptInterface, CScriptVal data) { glooxwrapper::JID xpartamuppJid(m_xpartamuppId); JS::RootedValue dataval(scriptInterface.GetContext(), data.get()); // Setup some base stanza attributes GameListQuery* g = new GameListQuery(); g->m_Command = "register"; glooxwrapper::Tag* game = glooxwrapper::Tag::allocate("game"); // Add a fake ip which will be overwritten by the ip stamp XMPP module on the server. game->addAttribute("ip", "fake"); // Iterate through all the properties reported and add them to the stanza. std::vector<std::string> properties; scriptInterface.EnumeratePropertyNamesWithPrefix(dataval, "", properties); for (std::vector<int>::size_type i = 0; i != properties.size(); i++) { std::wstring value; scriptInterface.GetProperty(dataval, properties[i].c_str(), value); game->addAttribute(properties[i], utf8_from_wstring(value)); } // Push the stanza onto the IQ g->m_GameList.push_back(game); // Send IQ glooxwrapper::IQ iq(gloox::IQ::Set, xpartamuppJid); iq.addExtension(g); DbgXMPP("SendIqRegisterGame [" << tag_xml(iq) << "]"); m_client->send(iq); }
/** * Send game report containing numerous game properties to the server. * * @param data A JS array of game statistics */ void XmppClient::SendIqGameReport(ScriptInterface& scriptInterface, CScriptVal data) { glooxwrapper::JID xpartamuppJid(m_xpartamuppId); JS::RootedValue dataval(scriptInterface.GetContext(), data.get()); // Setup some base stanza attributes GameReport* game = new GameReport(); glooxwrapper::Tag* report = glooxwrapper::Tag::allocate("game"); // Iterate through all the properties reported and add them to the stanza. std::vector<std::string> properties; scriptInterface.EnumeratePropertyNamesWithPrefix(dataval, "", properties); for (std::vector<int>::size_type i = 0; i != properties.size(); i++) { std::wstring value; scriptInterface.GetProperty(dataval, properties[i].c_str(), value); report->addAttribute(properties[i], utf8_from_wstring(value)); } // Add stanza to IQ game->m_GameReport.push_back(report); // Send IQ glooxwrapper::IQ iq(gloox::IQ::Set, xpartamuppJid); iq.addExtension(game); DbgXMPP("SendGameReport [" << tag_xml(iq) << "]"); m_client->send(iq); };
static std::vector<SimulationCommand> CloneCommandsFromOtherContext(ScriptInterface& oldScript, ScriptInterface& newScript, const std::vector<SimulationCommand>& commands) { JSContext* cxOld = oldScript.GetContext(); JSAutoRequest rqOld(cxOld); std::vector<SimulationCommand> newCommands; newCommands.reserve(commands.size()); for (const SimulationCommand& command : commands) { JSContext* cxNew = newScript.GetContext(); JSAutoRequest rqNew(cxNew); JS::RootedValue tmpCommand(cxNew, newScript.CloneValueFromOtherContext(oldScript, command.data)); SimulationCommand cmd(command.player, cxNew, tmpCommand); newCommands.emplace_back(std::move(cmd)); } return newCommands; }
static std::vector<SimulationCommand> CloneCommandsFromOtherContext(ScriptInterface& oldScript, ScriptInterface& newScript, const std::vector<SimulationCommand>& commands) { std::vector<SimulationCommand> newCommands = commands; for (size_t i = 0; i < newCommands.size(); ++i) { newCommands[i].data = CScriptValRooted(newScript.GetContext(), newScript.CloneValueFromOtherContext(oldScript, newCommands[i].data.get())); } return newCommands; }
JS::Value CProfileViewer::SaveToJS(ScriptInterface& scriptInterface) { JSContext* cx = scriptInterface.GetContext(); JSAutoRequest rq(cx); JS::RootedValue root(cx); scriptInterface.Eval("({})", &root); std::vector<AbstractProfileTable*> tables = m->rootTables; sort(tables.begin(), tables.end(), SortByName); for_each(tables.begin(), tables.end(), DumpTable(scriptInterface, root)); return root; }
void ConvertTLBs(ScriptInterface& scriptInterface, JS::MutableHandleValue ret) { JSContext* cx = scriptInterface.GetContext(); JSAutoRequest rq(cx); scriptInterface.Eval("[]", ret); for(size_t i = 0; ; i++) { const x86_x64::Cache* ptlb = x86_x64::Caches(x86_x64::TLB+i); if (!ptlb) break; JS::RootedValue tlb(cx); scriptInterface.Eval("({})", &tlb); scriptInterface.SetProperty(tlb, "type", (u32)ptlb->type); scriptInterface.SetProperty(tlb, "level", (u32)ptlb->level); scriptInterface.SetProperty(tlb, "associativity", (u32)ptlb->associativity); scriptInterface.SetProperty(tlb, "pagesize", (u32)ptlb->entrySize); scriptInterface.SetProperty(tlb, "entries", (u32)ptlb->numEntries); scriptInterface.SetPropertyInt(ret, i, tlb); } }
void ConvertCaches(ScriptInterface& scriptInterface, x86_x64::IdxCache idxCache, JS::MutableHandleValue ret) { JSContext* cx = scriptInterface.GetContext(); JSAutoRequest rq(cx); scriptInterface.Eval("[]", ret); for (size_t idxLevel = 0; idxLevel < x86_x64::Cache::maxLevels; ++idxLevel) { const x86_x64::Cache* pcache = x86_x64::Caches(idxCache+idxLevel); if (pcache->type == x86_x64::Cache::kNull || pcache->numEntries == 0) continue; JS::RootedValue cache(cx); scriptInterface.Eval("({})", &cache); scriptInterface.SetProperty(cache, "type", (u32)pcache->type); scriptInterface.SetProperty(cache, "level", (u32)pcache->level); scriptInterface.SetProperty(cache, "associativity", (u32)pcache->associativity); scriptInterface.SetProperty(cache, "linesize", (u32)pcache->entrySize); scriptInterface.SetProperty(cache, "sharedby", (u32)pcache->sharedBy); scriptInterface.SetProperty(cache, "totalsize", (u32)pcache->TotalSize()); scriptInterface.SetPropertyInt(ret, idxLevel, cache); } }
ValueCloner(ScriptInterface& from, ScriptInterface& to) : scriptInterfaceFrom(from), cxFrom(from.GetContext()), cxTo(to.GetContext()), m_RooterFrom(from), m_RooterTo(to) { }
/** * Returns a JS object containing a listing of available mods that * have a modname.json file in their modname folder. The returned * object looks like { modname1: json1, modname2: json2, ... } where * jsonN is the content of the modnameN/modnameN.json file as a JS * object. * * @return JS object with available mods as the keys of the modname.json * properties. */ JS::Value JSI_Mod::GetAvailableMods(ScriptInterface::CxPrivate* pCxPrivate) { ScriptInterface* scriptInterface = pCxPrivate->pScriptInterface; JSContext* cx = scriptInterface->GetContext(); JSAutoRequest rq(cx); JS::RootedObject obj(cx, JS_NewObject(cx, nullptr, JS::NullPtr(), JS::NullPtr())); const Paths paths(g_args); // loop over all possible paths OsPath modPath = paths.RData()/"mods"; OsPath modUserPath = paths.UserData()/"mods"; DirectoryNames modDirs; DirectoryNames modDirsUser; GetDirectoryEntries(modPath, NULL, &modDirs); // Sort modDirs so that we can do a fast lookup below std::sort(modDirs.begin(), modDirs.end()); PIVFS vfs = CreateVfs(1); // No cache needed; TODO but 0 crashes for (DirectoryNames::iterator iter = modDirs.begin(); iter != modDirs.end(); ++iter) { vfs->Clear(); if (vfs->Mount(L"", modPath / *iter, VFS_MOUNT_MUST_EXIST) < 0) continue; CVFSFile modinfo; if (modinfo.Load(vfs, L"mod.json", false) != PSRETURN_OK) continue; JS::RootedValue json(cx); if (!scriptInterface->ParseJSON(modinfo.GetAsString(), &json)) continue; // Valid mod, add it to our structure JS_SetProperty(cx, obj, utf8_from_wstring(iter->string()).c_str(), json); } GetDirectoryEntries(modUserPath, NULL, &modDirsUser); bool dev = InDevelopmentCopy(); for (DirectoryNames::iterator iter = modDirsUser.begin(); iter != modDirsUser.end(); ++iter) { // If we are in a dev copy we do not mount mods in the user mod folder that // are already present in the mod folder, thus we skip those here. if (dev && std::binary_search(modDirs.begin(), modDirs.end(), *iter)) continue; vfs->Clear(); if (vfs->Mount(L"", modUserPath / *iter, VFS_MOUNT_MUST_EXIST) < 0) continue; CVFSFile modinfo; if (modinfo.Load(vfs, L"mod.json", false) != PSRETURN_OK) continue; JS::RootedValue json(cx); if (!scriptInterface->ParseJSON(modinfo.GetAsString(), &json)) continue; // Valid mod, add it to our structure JS_SetProperty(cx, obj, utf8_from_wstring(iter->string()).c_str(), json); } return JS::ObjectValue(*obj); }
CGUIManager::CGUIManager(ScriptInterface& scriptInterface) : m_ScriptInterface(scriptInterface) { ENSURE(ScriptInterface::GetCallbackData(scriptInterface.GetContext()) == NULL); scriptInterface.SetCallbackData(this); }
std::vector<std::string> CTemplateLoader::FindPlaceableTemplates(const std::string& path, bool includeSubdirectories, ETemplatesType templatesType, ScriptInterface& scriptInterface) { JSContext* cx = scriptInterface.GetContext(); JSAutoRequest rq(cx); std::vector<std::string> templates; Status ok; VfsPath templatePath; if (templatesType == SIMULATION_TEMPLATES || templatesType == ALL_TEMPLATES) { JS::RootedValue placeablesFilter(cx); scriptInterface.ReadJSONFile("simulation/data/placeablesFilter.json", &placeablesFilter); JS::RootedObject folders(cx); if (scriptInterface.GetProperty(placeablesFilter, "templates", &folders)) { if (!(JS_IsArrayObject(cx, folders))) { LOGERROR("FindPlaceableTemplates: Argument must be an array!"); return templates; } u32 length; if (!JS_GetArrayLength(cx, folders, &length)) { LOGERROR("FindPlaceableTemplates: Failed to get array length!"); return templates; } templatePath = VfsPath(TEMPLATE_ROOT) / path; //I have every object inside, just run for each for (u32 i=0; i<length; ++i) { JS::RootedValue val(cx); if (!JS_GetElement(cx, folders, i, &val)) { LOGERROR("FindPlaceableTemplates: Failed to read array element!"); return templates; } std::string directoryPath; std::wstring fileFilter; scriptInterface.GetProperty(val, "directory", directoryPath); scriptInterface.GetProperty(val, "file", fileFilter); VfsPaths filenames; if (vfs::GetPathnames(g_VFS, templatePath / (directoryPath + "/"), fileFilter.c_str(), filenames) != INFO::OK) continue; for (const VfsPath& filename : filenames) { // Strip the .xml extension VfsPath pathstem = filename.ChangeExtension(L""); // Strip the root from the path std::wstring name = pathstem.string().substr(ARRAY_SIZE(TEMPLATE_ROOT) - 1); templates.emplace_back(name.begin(), name.end()); } } } } if (templatesType == ACTOR_TEMPLATES || templatesType == ALL_TEMPLATES) { templatePath = VfsPath(ACTOR_ROOT) / path; if (includeSubdirectories) ok = vfs::ForEachFile(g_VFS, templatePath, AddActorToTemplates, (uintptr_t)&templates, L"*.xml", vfs::DIR_RECURSIVE); else ok = vfs::ForEachFile(g_VFS, templatePath, AddActorToTemplates, (uintptr_t)&templates, L"*.xml"); WARN_IF_ERR(ok); } if (templatesType != SIMULATION_TEMPLATES && templatesType != ACTOR_TEMPLATES && templatesType != ALL_TEMPLATES) LOGERROR("Undefined template type (valid: all, simulation, actor)"); return templates; }