void Application::startScripting() { //this should be defined in some kind of text file, which should be different depending on what game you're playing (like deeds) try { //load the bootstrap script which will load all other scripts mServices->getScriptingService().loadScript("lua/Bootstrap.lua"); } catch (const std::exception& e) { S_LOG_FAILURE("Error when loading bootstrap script." << e); } //load any user defined scripts const std::string userScriptDirectoryPath = mServices->getConfigService().getHomeDirectory() + "/scripts"; oslink::directory scriptDir(userScriptDirectoryPath); if (scriptDir.isExisting()) { static const std::string luaSuffix = ".lua"; std::list<std::string> luaFiles; while (scriptDir) { std::string fileName = scriptDir.next(); if (fileName != "." && fileName != "..") { std::string lowerCaseFileName = fileName; std::transform(lowerCaseFileName.begin(), lowerCaseFileName.end(), lowerCaseFileName.begin(), ::tolower); if (lowerCaseFileName.compare(lowerCaseFileName.length() - luaSuffix.length(), luaSuffix.length(), luaSuffix) == 0) { luaFiles.push_back(fileName); } } } //Sorting, because we want to load the scripts in a deterministic order. luaFiles.sort(); for (auto& fileName : luaFiles) { std::ifstream stream(userScriptDirectoryPath + "/" + fileName, std::ios::in); if (stream) { std::stringstream ss; ss << stream.rdbuf(); stream.close(); //It's important that we inform the user that we're loading a script (in case it provides any confusing behaviour). ConsoleBackend::getSingleton().pushMessage("Loading user Lua script from '" + fileName + "'.", "info"); mServices->getScriptingService().executeCode(ss.str(), "LuaScriptingProvider"); } } } else { try { //Create the script user script directory oslink::directory::mkdir(userScriptDirectoryPath.c_str()); std::ofstream readme(userScriptDirectoryPath + "/README", std::ios::out); readme << "Any script files placed here will be executed as long as they have a supported file suffix.\nScripts are executed in alphabetical order.\nEmber currently supports lua scripts (ending with '.lua')."; readme.close(); S_LOG_INFO("Created user user scripting directory (" + userScriptDirectoryPath + ")."); } catch (const std::exception& e) { S_LOG_INFO("Could not create user scripting directory."); } } }
void ModelView::setXml(QDomElement node) { ui->categoryCombo->setCurrentIndex(ui->categoryCombo->findText(scriptDir().contains("transmission") ? "transmission" : "vector" )); ui->userIdText->setEnabled(false); ui->userIdText->setText(scriptDir().remove(0, scriptDir().lastIndexOf('/') + 1).replace("_scripts", "")); ui->list->clear(); ui->mainFile->clear(); ui->titleText->setText(node.firstChildElement("title").text()); ui->nameText->setText(node.firstChildElement("name").text()); ui->typeCombo->setCurrentIndex(ui->typeCombo->findText(node.firstChildElement("type").text())); ui->descriptionText->setText(node.firstChildElement("description").text()); QDomElement authors = node.firstChildElement("authors"); ui->authorsList->clear(); for (QDomElement author = authors.firstChildElement("author"); !author.isNull(); author = author.nextSiblingElement("author")) { QListWidgetItem* item = new QListWidgetItem(author.text()); item->setFlags(item->flags () | Qt::ItemIsEditable); ui->authorsList->addItem(item); } QDomElement source = node.firstChildElement("sourcecode"); QDomElement files = source.firstChildElement("files"); ui->mainFile->setCurrentIndex(ui->mainFile->findText(source.firstChildElement("main").text())); ui->interpreter->setCurrentIndex(ui->interpreter->findText(source.attribute("interpreter"))); ui->versionCombo->setCurrentIndex(ui->versionCombo->findText(source.attribute("version"))); for (QDomElement file = files.firstChildElement("file"); !file.isNull(); file = file.nextSiblingElement("file")) { addItem(file.text()); } ui->userIdText->setEnabled(false); ui->categoryCombo->setEnabled(false); ui->modelInfo->setVisible(false); ui->modelType->setVisible(false); ui->modelInfo->setText(node.firstChildElement("category").text()); }
void Scripting::executeFromUserDirectory() { auto value = getSettingsValue(PYTHON_AUTOSTART_FOLDER); if (value.isNull()) { return; } auto autoStartFolder = value.toString(); if (autoStartFolder.isEmpty()) { return; } QDir scriptDir(autoStartFolder); QStringList endings; endings << "*.py"; scriptDir.setNameFilters(endings); QFileInfoList entries = scriptDir.entryInfoList(); foreach(const QFileInfo &script, entries) { QFile file(script.canonicalFilePath()); if(!file.open(QIODevice::Text | QIODevice::ReadOnly)) { continue; } _ctx.evalFile(script.canonicalFilePath()); }
bool ModelPackager::zipModel() { QTemporaryDir dir; dir.setAutoRemove(true); QDir tempDir(dir.path()); QByteArray nameField = _mapping.value(NAME_FIELD).toByteArray(); tempDir.mkpath(nameField + "/textures"); tempDir.mkpath(nameField + "/scripts"); QDir fbxDir(tempDir.path() + "/" + nameField); QDir texDir(fbxDir.path() + "/textures"); QDir scriptDir(fbxDir.path() + "/scripts"); // Copy textures listTextures(); if (!_textures.empty()) { QByteArray texdirField = _mapping.value(TEXDIR_FIELD).toByteArray(); _texDir = _modelFile.path() + "/" + texdirField; copyTextures(_texDir, texDir); } // Copy scripts QByteArray scriptField = _mapping.value(SCRIPT_FIELD).toByteArray(); _mapping.remove(SCRIPT_FIELD); if (scriptField.size() > 1) { tempDir.mkpath(nameField + "/scripts"); _scriptDir = _modelFile.path() + "/" + scriptField; QDir wdir = QDir(_scriptDir); _mapping.remove(SCRIPT_FIELD); wdir.setSorting(QDir::Name | QDir::Reversed); auto list = wdir.entryList(QDir::NoDotAndDotDot | QDir::AllEntries); for (auto script : list) { auto sc = tempDir.relativeFilePath(scriptDir.path()) + "/" + QUrl(script).fileName(); _mapping.insertMulti(SCRIPT_FIELD, sc); } copyDirectoryContent(wdir, scriptDir); } // Copy LODs QVariantHash lodField = _mapping.value(LOD_FIELD).toHash(); if (!lodField.empty()) { for (auto it = lodField.constBegin(); it != lodField.constEnd(); ++it) { QString oldPath = _modelFile.path() + "/" + it.key(); QFile lod(oldPath); QString newPath = fbxDir.path() + "/" + QFileInfo(lod).fileName(); if (lod.exists()) { lod.copy(newPath); } } } // Copy FBX QFile fbx(_fbxInfo.filePath()); QByteArray filenameField = _mapping.value(FILENAME_FIELD).toByteArray(); QString newPath = fbxDir.path() + "/" + QFileInfo(filenameField).fileName(); fbx.copy(newPath); // Correct FST _mapping[FILENAME_FIELD] = tempDir.relativeFilePath(newPath); _mapping[TEXDIR_FIELD] = tempDir.relativeFilePath(texDir.path()); for (auto multi : _mapping.values(SCRIPT_FIELD)) { multi.fromValue(tempDir.relativeFilePath(scriptDir.path()) + multi.toString()); } // Copy FST QFile fst(tempDir.path() + "/" + nameField + ".fst"); if (fst.open(QIODevice::WriteOnly)) { fst.write(FSTReader::writeMapping(_mapping)); fst.close(); } else { qCDebug(interfaceapp) << "Couldn't write FST file" << fst.fileName(); return false; } QString saveDirPath = QFileDialog::getExistingDirectory(nullptr, "Save Model", "", QFileDialog::ShowDirsOnly); if (saveDirPath.isEmpty()) { qCDebug(interfaceapp) << "Invalid directory" << saveDirPath; return false; } QDir saveDir(saveDirPath); copyDirectoryContent(tempDir, saveDir); return true; }
QDomDocument ModelView::getXml() { QDomDocument doc; QDomElement root = doc.createElement("model"); QDomElement id = doc.createElement("id"); id.appendChild(doc.createTextNode(ui->userIdText->text())); QDomElement userid = doc.createElement("userid"); userid.appendChild(doc.createTextNode(ui->userIdText->text())); QDomElement name = doc.createElement("name"); name.appendChild(doc.createTextNode(ui->nameText->text())); QDomElement category = doc.createElement("category"); category.appendChild(doc.createTextNode(scriptDir().contains("transmission") ? "transmission" : "vector" )); QDomElement title = doc.createElement("title"); title.appendChild(doc.createTextNode(ui->titleText->text())); QDomElement type = doc.createElement("type"); type.appendChild(doc.createTextNode(ui->typeCombo->currentText())); QDomElement authorsRoot = doc.createElement("authors"); for (int i = 0; i < ui->authorsList->count(); ++i) { QDomElement author = doc.createElement("author"); author.appendChild(doc.createTextNode(ui->authorsList->item(i)->text())); authorsRoot.appendChild(author); } QDomElement description = doc.createElement("description"); description.appendChild(doc.createTextNode(ui->descriptionText->toPlainText())); root.appendChild(id); root.appendChild(userid); root.appendChild(name); root.appendChild(category); root.appendChild(type); root.appendChild(title); root.appendChild(authorsRoot); root.appendChild(description); QDomElement root2 = doc.createElement("sourcecode"); root2.setAttribute("lang", ui->interpreter->currentText() == "TerraME" ? "TerraML" : "R"); root2.setAttribute("interpreter", ui->interpreter->currentText()); root2.setAttribute("version", ui->versionCombo->currentText()); QDomElement modelVersion = doc.createElement("version"); modelVersion.appendChild(doc.createTextNode("1.0")); root2.appendChild(modelVersion); QDomElement fileRoot = doc.createElement("files"); for (int i = 0; i < ui->list->count(); ++i) { FileInfo* fi = dynamic_cast<FileInfo*>(ui->list->itemWidget(ui->list->item(i))); if (fi == NULL) continue; QDomElement file = doc.createElement("file"); file.appendChild(doc.createTextNode(fi->path->text())); fileRoot.appendChild(file); } root2.appendChild(fileRoot); QDomElement main = doc.createElement("main"); main.appendChild(doc.createTextNode(ui->mainFile->currentText())); root2.appendChild(main); QDomElement dependencies = doc.createElement("dependencies"); dependencies.appendChild(doc.createTextNode("dependencias")); root2.appendChild(dependencies); ui->userIdText->setEnabled(false); ui->categoryCombo->setEnabled(false); root.appendChild(root2); doc.appendChild(root); return doc; }