int main(int argc, char* argv[]) { try { log_init(); INFO("create zim file from db"); zim::writer::DbSource dbSource(argc, argv); return create(argc, argv, dbSource); } catch (const std::exception& e) { log_fatal(e.what()); std::cerr << e.what() << std::endl; } }
void MainWindow::on_SaveGame_New_clicked() { //load the information NewGame nameGame(this); if(!nameGame.haveSkin()) { QMessageBox::critical(this,tr("Error"),QString("Sorry but no skin found into: %1").arg(QFileInfo(datapackPath+DATAPACK_BASE_PATH_SKIN).absoluteFilePath())); return; } nameGame.exec(); if(!nameGame.haveTheInformation()) return; int index=0; //locate the new folder and create it while(QDir().exists(savegamePath+QString::number(index))) index++; QString savegamesPath=savegamePath+QString::number(index)+"/"; if(!QDir().mkpath(savegamesPath)) { QMessageBox::critical(this,tr("Error"),QString("Unable to write savegame into: %1").arg(savegamesPath)); return; } //initialize the db QFile dbSource(":/catchchallenger.db.sqlite"); if(!dbSource.open(QIODevice::ReadOnly)) { QMessageBox::critical(this,tr("Error"),QString("Unable to open the db model: %1").arg(savegamesPath)); rmpath(savegamesPath); return; } QByteArray dbData=dbSource.readAll(); if(dbData.isEmpty()) { QMessageBox::critical(this,tr("Error"),QString("Unable to read the db model: %1").arg(savegamesPath)); rmpath(savegamesPath); return; } dbSource.close(); QFile dbDestination(savegamesPath+"catchchallenger.db.sqlite"); if(!dbDestination.open(QIODevice::WriteOnly)) { QMessageBox::critical(this,tr("Error"),QString("Unable to write savegame into: %1").arg(savegamesPath)); rmpath(savegamesPath); return; } if(!dbDestination.write(dbData)) { dbDestination.close(); QMessageBox::critical(this,tr("Error"),QString("Unable to write savegame into: %1").arg(savegamesPath)); rmpath(savegamesPath); return; } dbDestination.close(); //initialise the pass QString pass=CatchChallenger::FacilityLib::randomPassword("abcdefghijklmnopqurstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890",16); QCryptographicHash hash(QCryptographicHash::Sha1); hash.addData(pass.toUtf8()); QByteArray passHash=hash.result(); //initialise the meta data bool settingOk=false; { QSettings metaData(savegamesPath+"metadata.conf",QSettings::IniFormat); if(metaData.isWritable()) { if(metaData.status()==QSettings::NoError) { metaData.setValue("title",nameGame.gameName()); metaData.setValue("location","Starting city"); metaData.setValue("time_played",0); metaData.setValue("pass",pass); settingOk=true; } else qDebug() << "Settings error: " << metaData.status(); } } //check if meta data is ok, and db can be open if(!settingOk) { QMessageBox::critical(this,tr("Error"),QString("Unable to write savegame into: %1").arg(savegamesPath)); rmpath(savegamesPath); return; } QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName(savegamesPath+"catchchallenger.db.sqlite"); if(!db.open()) { QMessageBox::critical(this,tr("Error"),QString("Unable to initialize the savegame\nError: open database: %1").arg(db.lastError().text())); rmpath(savegamesPath); return; } //empty the player db and put the new player into it QSqlQuery sqlQuery; if(!sqlQuery.exec(QString("DELETE FROM \"player\""))) { db.close(); QMessageBox::critical(this,tr("Error"),QString("Unable to initialize the savegame (error: clean the table: %1)").arg(sqlQuery.lastError().text())); rmpath(savegamesPath); return; } if(!sqlQuery.exec(QString("INSERT INTO \"player\"(\"id\",\"login\",\"password\",\"pseudo\",\"skin\",\"position_x\",\"position_y\",\"orientation\",\"map_name\",\"type\",\"clan\",\"cash\") VALUES(1,'admin','%1','%2','%3',1,1,'bottom','world/0.0.tmx','normal',NULL,0);").arg(QString(passHash.toHex())).arg(nameGame.pseudo()).arg(nameGame.skin()))) { db.close(); QMessageBox::critical(this,tr("Error"),QString("Unable to initialize the savegame (error: initialize the entry: %1)").arg(sqlQuery.lastError().text())); rmpath(savegamesPath); return; } db.close(); updateSavegameList(); play(savegamesPath); }