Exemplo n.º 1
0
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);
  }
}
Exemplo n.º 2
0
EntityDefinitionProto* EntityLoader::loadDefinition(const TiXmlElement& entity)
{
   EntityDefinitionProto* pentitydef = new EntityDefinitionProto();

   const char* pname = entity.Attribute("name");
   pentitydef->mName = String::fromUtf8(pname);

   const char* pclasstype = entity.Attribute("class");
   pentitydef->mClassName = (pclasstype != NULL ? String::fromUtf8(pclasstype) : UTEXT("engine.game.Entity"));

   for ( const TiXmlElement* pelement = entity.FirstChildElement(); pelement != NULL; pelement = pelement->NextSiblingElement() )
   {
      String name = String::fromUtf8(pelement->Value());

      if ( name == sChild )
      {
         loadChildDefinition(*pentitydef, *pelement);
      }
      else if ( name == sLink )
      {
         loadLinkDefinition(*pentitydef, *pelement);
      }
      else if ( name == sEntity )
      {
         EntityDefinitionProto* pentity = loadDefinition(*pelement);
         pentitydef->mEntities.push_back(pentity);
      }
      else
      {
         Loaders::iterator it = mLoaders.find(name);
         if ( it != mLoaders.end() )
         {
            ComponentLoader* ploader = it->second;
            ASSERT_PTR(ploader);

            ComponentDefinitionProto* pdefinition = ploader->load(*pelement);
            if ( pdefinition == NULL )
            {
               // throw and error!
            }

            pentitydef->mComponents.push_back(pdefinition);
         }
      }
   }

   return pentitydef;
}
Exemplo n.º 3
0
EntityDefinitionProto* EntityLoader::load(const String& filename)
{
   std::string name = filename.toUtf8();
   TiXmlDocument doc(name.c_str());
   if ( !doc.LoadFile() )
   {
      return NULL;
   }

   TiXmlElement* pbase = doc.FirstChildElement("entity");
   if ( pbase == NULL )
   {
      return NULL;
   }
   
   return loadDefinition(*pbase);
}
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);
	}
}
Exemplo n.º 5
0
DefinitionManager::DefinitionManager(QWidget *parent) :
    QWidget(parent),
    isUpdating(false),
    dimensionManager(DimensionIdentifier::Instance()),
    blockManager(BlockIdentifier::Instance()),
    biomeManager(BiomeIdentifier::Instance()),
    entityManager(EntityIdentifier::Instance()),
    flatteningConverter(FlatteningConverter::Instance())
{
  setWindowFlags(Qt::Window);
  setWindowTitle(tr("Definitions"));

  QVBoxLayout *layout = new QVBoxLayout;
  QStringList labels;
  labels << tr("Name") << tr("Version") << tr("Type");
  table = new QTableWidget(0, 3);
  table->setHorizontalHeaderLabels(labels);
  table->horizontalHeader()->setSectionResizeMode(
      QHeaderView::ResizeToContents);
  table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
  table->horizontalHeader()->setHighlightSections(false);
  table->verticalHeader()->hide();
  table->setShowGrid(false);
  table->setSelectionBehavior(QAbstractItemView::SelectRows);
  layout->addWidget(table, 1);

  QWidget *buttonBar = new QWidget;
  QHBoxLayout *buttons = new QHBoxLayout;
  QPushButton *add = new QPushButton(tr("Add Pack..."));
  connect(add, SIGNAL(clicked()),
          this, SLOT(addPack()));
  buttons->addWidget(add);
  QPushButton *remove = new QPushButton(tr("Remove Pack"));
  connect(remove, SIGNAL(clicked()),
          this, SLOT(removePack()));
  connect(this, SIGNAL(packSelected(bool)),
          remove, SLOT(setEnabled(bool)));
  buttons->addWidget(remove);
  QPushButton *save = new QPushButton(tr("Export Pack..."));
  connect(save, SIGNAL(clicked()),
          this, SLOT(exportPack()));
  connect(this, SIGNAL(packSelected(bool)),
          save, SLOT(setEnabled(bool)));
  buttons->addWidget(save);
  buttonBar->setLayout(buttons);
  layout->addWidget(buttonBar, 0);

  emit packSelected(false);
  setLayout(layout);

  QSettings settings;
  sorted = settings.value("packs").toList();

  // clean old hashed files without extra seed
  QString destdir = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
  const QStringList old_hashed_list { "1050220429", "1241760321", "1443276275", "1798448990", "2422344665" };
  for ( const auto& old_hashed_file : old_hashed_list  ) {
    QString old_path = destdir + "/" + old_hashed_file + ".json";
    QFile::remove(old_path);
    sorted.removeOne(old_path);
  }

  // copy over built-in definitions if necessary
  QDir dir;
  dir.mkpath(destdir);
  QDirIterator it(":/definitions", QDir::Files | QDir::Readable);
  while (it.hasNext()) {
    it.next();
    installJson(it.filePath(), false, false);
  }
  settings.setValue("packs", sorted);
  // we load the definitions backwards for priority.
  for (int i = sorted.length() - 1; i >= 0; i--)
    loadDefinition(sorted[i].toString());

  // hook up table selection signal
  connect(table,
          SIGNAL(currentItemChanged(QTableWidgetItem*, QTableWidgetItem*)),
          this, SLOT(selectedPack(QTableWidgetItem*, QTableWidgetItem*)));
  // fill out table
  refresh();
}
Exemplo n.º 6
0
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);
  }
}
DefinitionManager::DefinitionManager(QWidget *parent) : QWidget(parent)
{
	setWindowFlags(Qt::Window);
	setWindowTitle(tr("Definitions"));

	QVBoxLayout *layout=new QVBoxLayout;
	QStringList labels;
	labels<<tr("Name")<<tr("Version")<<tr("Type"); //<<tr("Active");
	table=new QTableWidget(0,3);
	table->setHorizontalHeaderLabels(labels);
	table->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
	table->horizontalHeader()->setSectionResizeMode(0,QHeaderView::Stretch);
	table->horizontalHeader()->setHighlightSections(false);
	table->verticalHeader()->hide();
	table->setShowGrid(false);
	table->setSelectionBehavior(QAbstractItemView::SelectRows);
	layout->addWidget(table,1);

	QWidget *buttonBar=new QWidget;
	QHBoxLayout *buttons=new QHBoxLayout;
	QPushButton *add=new QPushButton(tr("Add Pack..."));
	connect(add,SIGNAL(clicked()),
			this,SLOT(addPack()));
	buttons->addWidget(add);
	QPushButton *remove=new QPushButton(tr("Remove Pack"));
	connect(remove,SIGNAL(clicked()),
			this,SLOT(removePack()));
	connect(this,SIGNAL(packSelected(bool)),
			remove,SLOT(setEnabled(bool)));
	buttons->addWidget(remove);
	QPushButton *save=new QPushButton(tr("Export Pack..."));
	connect(save,SIGNAL(clicked()),
			this,SLOT(exportPack()));
	connect(this,SIGNAL(packSelected(bool)),
			save,SLOT(setEnabled(bool)));
	buttons->addWidget(save);
	buttonBar->setLayout(buttons);
	layout->addWidget(buttonBar,0);

	emit packSelected(false);
	setLayout(layout);

	dimensionList = new Dimensions;
	blocks=new BlockIdentifier;
	biomes=new BiomeIdentifier;

	QSettings settings;
	sorted=settings.value("packs").toList();
	lastUpdated=settings.value("packupdates").toHash();

	//copy over built-in definitions if necessary
	QString defdir=QStandardPaths::writableLocation(QStandardPaths::DataLocation);
	QDir dir;
	dir.mkpath(defdir);
	QDirIterator it(":/definitions",QDir::Files|QDir::Readable);
	while (it.hasNext())
	{
		it.next();
		installJson(it.filePath(),false,false);
	}
	settings.setValue("packs",sorted);
	// we load the definitions backwards for priority.
	for (int i=sorted.length()-1;i>=0;i--)
		loadDefinition(sorted[i].toString());

	//hook up table selection signal
	connect(table,SIGNAL(currentItemChanged(QTableWidgetItem*,QTableWidgetItem*)),
			this, SLOT(selectedPack(QTableWidgetItem*,QTableWidgetItem*)));
	//fill out table
	refresh();
}