void DefinitionManager::installJson(QString path,bool overwrite,bool install) { QString destdir=QStandardPaths::writableLocation(QStandardPaths::DataLocation); JSONData *def; QFile f(path); f.open(QIODevice::ReadOnly); try { def=JSON::parse(f.readAll()); f.close(); } catch (JSONParseException e) { f.close(); QMessageBox::warning(this, tr("Couldn't install %1").arg(path), e.reason, QMessageBox::Cancel); return; } QString key=def->at("name")->asString()+def->at("type")->asString(); delete def; QString dest=destdir+"/"+QString("%1").arg(qHash(key))+".json"; if (!QFile::exists(dest) || overwrite) { if (QFile::exists(dest) && install) removeDefinition(dest); if (!QFile::copy(path,dest)) { QMessageBox::warning(this, tr("Couldn't install %1").arg(path), tr("Copy error"), QMessageBox::Cancel); return; } // fix permissions since we might be copying a readonly resource. QFile::setPermissions(dest, QFile::ReadOwner|QFile::WriteOwner); sorted.prepend(dest); if (install) loadDefinition(dest); } }
void DefinitionManager::installZip(QString path, bool overwrite, bool install) { QString destdir = QStandardPaths::writableLocation( QStandardPaths::DataLocation); ZipReader zip(path); if (!zip.open()) { QMessageBox::warning(this, tr("Couldn't install %1").arg(path), tr("Corrupt zip"), QMessageBox::Cancel); return; } // fetch the pack info JSONData *info; try { info = JSON::parse(zip.get("pack_info.json")); } catch (JSONParseException e) { QMessageBox::warning(this, tr("Couldn't install %1").arg(path), tr("pack_info.json : %1").arg(e.reason), QMessageBox::Cancel); zip.close(); return; } // let's verify all the jsons in the pack for (int i = 0; i < info->at("data")->length(); i++) { JSONData *def; try { def = JSON::parse(zip.get(info->at("data")->at(i)->asString())); delete def; } catch (JSONParseException e) { QMessageBox::warning(this, tr("Couldn't install %1").arg(path), tr("%1: %2") .arg(info->at("data")->at(i)->asString(), e.reason), QMessageBox::Cancel); delete info; zip.close(); return; } } QString key = info->at("name")->asString() + info->at("type")->asString(); delete info; QString dest = destdir + "/" + QString("%1").arg(qHash(key,42)) + ".zip"; if (!QFile::exists(dest) || overwrite) { if (QFile::exists(dest) && install) removeDefinition(dest); if (!QFile::copy(path, dest)) { QMessageBox::warning(this, tr("Couldn't install %1").arg(path), tr("Copy error"), QMessageBox::Cancel); return; } sorted.prepend(dest); if (install) loadDefinition(dest); } }
String ExportOldCStyle(std::set<XMASComponent *> allComponents, const JSONData& globals, MemoryPool& returnMemoryPool) { MemoryPool mp; std::map<String,int> enumMap; JSONData::Vector network = JSONData::AllocateVector(mp); for (XMASComponent* component : allComponents) { JSONData::Map jsonComponent = JSONData::AllocateMap(mp); jsonComponent["id"] = JSONData(component->getName()); ExportOldCVisitor visitor(jsonComponent, enumMap); component->accept(visitor); network.push_back(std::move(jsonComponent)); } JSONData::Map root = globals.asObject(); //JSONData::AllocateMap(mp); root["NETWORK"] = network; std::ostringstream buffer; buffer << root; return String(buffer.str())(returnMemoryPool); }
void DefinitionManager::loadDefinition(QString path) { // determine if we're loading a single json or a pack if (path.endsWith(".json", Qt::CaseInsensitive)) { JSONData *def; QFile f(path); if (!f.open(QIODevice::ReadOnly)) return; try { def = JSON::parse(f.readAll()); f.close(); } catch (JSONParseException e) { f.close(); return; } Definition d; d.name = def->at("name")->asString(); d.version = def->at("version")->asString(); d.path = path; d.update = def->at("update")->asString(); QString type = def->at("type")->asString(); QString key = d.name + type; d.enabled = true; // should look this up if (type == "block") { d.id = flatteningConverter.addDefinitions( dynamic_cast<JSONArray*>(def->at("data"))); d.type = Definition::Converter; } else if (type == "flatblock") { d.id = blockManager.addDefinitions( dynamic_cast<JSONArray*>(def->at("data"))); d.type = Definition::Block; } else if (type == "biome") { d.id = biomeManager.addDefinitions( dynamic_cast<JSONArray*>(def->at("data"))); d.type = Definition::Biome; } else if (type == "dimension") { d.id = dimensionManager.addDefinitions( dynamic_cast<JSONArray*>(def->at("data"))); d.type = Definition::Dimension; } else if (type == "entity") { d.id = entityManager.addDefinitions( dynamic_cast<JSONArray*>(def->at("data"))); d.type = Definition::Entity; } definitions.insert(path, d); delete def; } else { ZipReader zip(path); if (!zip.open()) return; JSONData *info; try { info = JSON::parse(zip.get("pack_info.json")); } catch (JSONParseException e) { zip.close(); return; } Definition d; d.name = info->at("name")->asString(); d.version = info->at("version")->asString(); d.update = info->at("update")->asString(); d.path = path; d.enabled = true; d.id = 0; d.type = Definition::Pack; d.blockid = -1; d.biomeid = -1; d.dimensionid = -1; d.entityid = -1; QString key = d.name+"pack"; for (int i = 0; i < info->at("data")->length(); i++) { JSONData *def; try { def = JSON::parse(zip.get(info->at("data")->at(i)->asString())); } catch (JSONParseException e) { continue; } QString type = def->at("type")->asString(); if (type == "block") { // d.blockid = flatteningConverter->addDefinitions( // dynamic_cast<JSONArray*>(def->at("data")), d.blockid); } else if (type == "flatblock") { d.blockid = blockManager.addDefinitions( dynamic_cast<JSONArray*>(def->at("data")), d.blockid); } else if (type == "biome") { d.biomeid = biomeManager.addDefinitions( dynamic_cast<JSONArray*>(def->at("data")), d.biomeid); } else if (type == "dimension") { d.dimensionid = dimensionManager.addDefinitions( dynamic_cast<JSONArray*>(def->at("data")), d.dimensionid); } else if (type == "entity") { d.entityid = entityManager.addDefinitions( dynamic_cast<JSONArray*>(def->at("data")), d.entityid); } delete def; } definitions.insert(path, d); delete info; zip.close(); } }
void DefinitionManager::installJson(QString path, bool overwrite, bool install) { QString destdir = QStandardPaths::writableLocation( QStandardPaths::DataLocation); JSONData *def; QFile f(path); f.open(QIODevice::ReadOnly); try { def = JSON::parse(f.readAll()); f.close(); } catch (JSONParseException e) { f.close(); QMessageBox::warning(this, tr("Couldn't install %1").arg(path), e.reason, QMessageBox::Cancel); return; } QString key = def->at("name")->asString() + def->at("type")->asString(); QString exeversion = def->at("version")->asString(); delete def; QString dest = destdir + "/" + QString("%1").arg(qHash(key,42)) + ".json"; // check if build in version is newer than version on disk if (QFile::exists(dest)) { QFile f(dest); f.open(QIODevice::ReadOnly); try { def = JSON::parse(f.readAll()); f.close(); } catch (JSONParseException e) { f.close(); return; } QString fileversion = def->at("version")->asString(); delete def; if (exeversion.compare(fileversion, Qt::CaseInsensitive) > 0) { // force overwriting outdated local copy QFile::remove(dest); QFile::copy(path, dest); QFile::setPermissions(dest, QFile::ReadOwner|QFile::WriteOwner); } } // import new definition (if file is not present) if (!QFile::exists(dest) || overwrite) { if (QFile::exists(dest) && install) removeDefinition(dest); if (!QFile::copy(path, dest)) { QMessageBox::warning(this, tr("Couldn't install %1").arg(path), tr("Copy error"), QMessageBox::Cancel); return; } // fix permissions since we might be copying a readonly resource. QFile::setPermissions(dest, QFile::ReadOwner|QFile::WriteOwner); sorted.prepend(dest); if (install) loadDefinition(dest); } }