void ExportCommon::progressSteps(int steps) { progressMax += steps; exportInfo("$:$%d\n", progressMax); }
void ExportCommon::exportAssets(ExportContext *ctx, bool compileLua) { QStringList allluafiles; QStringList allluafiles_abs; if ((ctx->fileQueue.size() == 0) || (ctx->player)) //No assets -> Player return; exportInfo("Exporting assets\n"); for (std::size_t i = 0; i < ctx->folderList.size(); ++i) ctx->outputDir.mkdir(ctx->folderList[i]); ctx->allfiles.clear(); ctx->allfiles_abs.clear(); ctx->luafiles.clear(); ctx->luafiles_abs.clear(); QDir path(QFileInfo(ctx->projectFileName_).path()); ExportCommon::progressSteps(ctx->fileQueue.size()); for (std::size_t i = 0; i < ctx->fileQueue.size(); ++i) { const QString& s1 = ctx->fileQueue[i].first; const QString& s2 = ctx->fileQueue[i].second; exportInfo("Exporting %s\n", s1.toUtf8().constData()); ExportCommon::progressStep(s1.toUtf8().constData()); QString src = QDir::cleanPath(path.absoluteFilePath(s2)); QString dst = QDir::cleanPath(ctx->outputDir.absoluteFilePath(s1)); QString rdst = QDir::cleanPath(ctx->outputDir.relativeFilePath(s1)); QString suffix = QFileInfo(dst).suffix().toLower(); bool isJet = false; if ((!ctx->jetset.isEmpty()) && (!ctx->jetset.contains(suffix))) { dst += ".jet"; isJet = true; } ctx->allfiles.push_back(s1); ctx->allfiles_abs.push_back(dst); QFile::remove(dst); bool copied = false; if (QFileInfo(src).suffix().toLower() == "lua") { allluafiles.push_back(s1); allluafiles_abs.push_back(dst); if (std::find(ctx->topologicalSort.begin(), ctx->topologicalSort.end(), std::make_pair(s2, true)) == ctx->topologicalSort.end()) { ctx->luafiles.push_back(s1); ctx->luafiles_abs.push_back(dst); } // compile lua files (with luac) if (compileLua) { QDir toolsDir = QDir(QCoreApplication::applicationDirPath()); #if defined(Q_OS_WIN) QString luac = toolsDir.filePath("luac.exe"); #else QString luac = toolsDir.filePath("luac"); #endif QDir old = QDir::current(); QDir::setCurrent(ctx->outputDir.path()); QString dfile = "\"" + dst + "\""; QString sfile = "\"" + rdst + "\""; QFile::copy(src, rdst); QProcess::execute(quote(luac) + " -o " + dfile + " " + sfile); if (isJet) QFile::remove(rdst); copied = true; QDir::setCurrent(old.path()); } } if (!copied) QFile::copy(src, dst); } #if 0 // compile lua files if (false) { compileThread_ = new CompileThread(ctx->luafiles_abs, false, "", QString(), this); compileThread_->start(); compileThread_->wait(); delete compileThread_; } #endif // encrypt lua, png, jpg, jpeg and wav files if (true) { exportInfo("Encrypting assets\n"); ExportCommon::progressSteps(ctx->allfiles_abs.size()); for (int i = 0; i < ctx->allfiles_abs.size(); ++i) { ExportCommon::progressStep( ctx->allfiles_abs[i].toUtf8().constData()); QString filename = ctx->allfiles_abs[i]; QString ext = QFileInfo(ctx->allfiles[i]).suffix().toLower(); exportInfo("Encrypting %s [%s]\n", filename.toUtf8().constData(), ext.toUtf8().constData()); if (ext != "lua" && ext != "png" && ext != "jpeg" && ext != "jpg" && ext != "wav") continue; QByteArray encryptionKey = (ext == "lua") ? ctx->codeKey : ctx->assetsKey; QFile fis(filename); if (!fis.open(QIODevice::ReadOnly)) { exportError("Failed to open %s\n", filename.toUtf8().constData()); continue; } QByteArray data = fis.readAll(); fis.close(); int ks = encryptionKey.size(); for (int j = 32; j < data.size(); ++j) data[j] = data[j] ^ encryptionKey[((j * 13) + ((j / ks) * 31)) % ks]; QFile fos(filename); if (!fos.open(QIODevice::WriteOnly)) { exportError("Failed to save %s\n", filename.toUtf8().constData()); continue; } fos.write(data); fos.close(); } } // compress lua files if (false) { for (int i = 0; i < ctx->luafiles_abs.size(); ++i) { QString file = "\"" + ctx->luafiles_abs[i] + "\""; QProcess::execute( "Tools/lua Tools/LuaSrcDiet.lua --quiet " + file + " -o " + file); } } ctx->assetfiles = ctx->allfiles; ctx->assetfiles_abs = ctx->allfiles_abs; }
bool ExportCommon::unzip(ExportContext *ctx, QString file, QString dest) { QDir toPath = QFileInfo( QDir::cleanPath(ctx->outputDir.absoluteFilePath(dest))).dir(); QFile zfile(file); if (!zfile.open(QIODevice::ReadOnly)) { exportError("Can't open file %s\n", file.toStdString().c_str()); return false; } while (true) { struct _ZipHdr { quint32 Signature;// local file header signature 4 bytes (0x04034b50) #define ZIPHDR_SIG 0x04034b50 quint16 Version; // version needed to extract 2 bytes quint16 Flags; // general purpose bit flag 2 bytes quint16 Compression; // compression method 2 bytes quint16 ModTime; // last mod file time 2 bytes quint16 ModDate; // last mod file date 2 bytes quint32 Crc32; // crc-32 4 bytes quint32 CompSize; // compressed size 4 bytes quint32 OrigSize; // uncompressed size 4 bytes quint16 NameLen; // file name length 2 bytes quint16 ExtraLen;// extra field length 2 bytes }PACKED Hdr; if (zfile.read((char *) &Hdr, sizeof(Hdr)) != sizeof(Hdr)) break; if (_letohl(Hdr.Signature) != ZIPHDR_SIG) break; if (_letohs(Hdr.Version) > 20) { exportError("Unsupported ZIP version for %s [%d]\n", file.toStdString().c_str(), _letohs(Hdr.Version)); return false; } if (Hdr.Flags != 0) { exportError("Unsupported flags for %s [%04x]\n", file.toStdString().c_str(), Hdr.Flags); return false; } if ((Hdr.Compression > 0) && (_letohs(Hdr.Compression) != 8)) { exportError("Unsupported compression method for %s [%d]\n", file.toStdString().c_str(), _letohs(Hdr.Compression)); return false; } QByteArray fname = zfile.read(_letohs(Hdr.NameLen)); QString lname = QString(fname); zfile.read(_letohs(Hdr.ExtraLen)); exportInfo("Extracting %s\n",lname.toStdString().c_str()); QByteArray fcont = zfile.read( Hdr.Compression ? _letohl(Hdr.CompSize) : _letohl(Hdr.OrigSize)); if (Hdr.Compression) { QByteArray decomp; if (!gzInflate(fcont,decomp)) { exportError("Failed to uncompress %s\n", lname.toStdString().c_str()); break; } fcont = decomp; } if (lname.endsWith("/")) ctx->outputDir.mkpath(toPath.absoluteFilePath(lname)); else { QFile ofile(toPath.absoluteFilePath(lname)); if (ofile.open(QIODevice::WriteOnly)) { ofile.write(fcont); ofile.close(); } else { exportError("Can't open file %s\n", lname.toStdString().c_str()); break; } } } zfile.close(); return true; }
void GolangPresentEdit::extFinish(bool error, int code, QString /*msg*/) { int exportType = m_process->userData(0).toInt(); if (exportType == EXPORT_TYPE_VERIFY) { if (!error && code == 0) { m_editor->setNavigateHead(LiteApi::EditorNavigateNormal,tr("Present verify success")); } else { m_editor->setNavigateHead(LiteApi::EditorNavigateError,tr("Present verify false")+"\n"+m_errorMsg.join("\n")); } } else if (error || code != 0) { m_liteApp->appendLog("GolangPresent",m_errorMsg.join(";"),true); } if (!error && code == 0) { if (exportType == EXPORT_TYPE_HTML) { QFileInfo info(m_editor->filePath()); ExportDialog dlg(m_liteApp->mainWindow()); dlg.setWindowTitle(tr("Export HTML")); dlg.setFileName(info.completeBaseName()+".html"); if (!m_exportName.isEmpty()) { dlg.setFileName(m_exportName); } if (dlg.exec() == QDialog::Rejected) { return; } QString exportFile = dlg.fileName(); if (exportFile.isEmpty()) { return; } m_exportName = exportFile; QFileInfo exportInfo(exportFile); QString ext = exportInfo.suffix().toLower(); if (ext != "html" && ext != "htm") { exportInfo = QFileInfo(exportFile += ".html"); } if (!exportInfo.isAbsolute()) { exportInfo = QFileInfo(info.absolutePath(),exportInfo.fileName()); } QFile file(exportInfo.filePath()); if (!file.open(QFile::WriteOnly)) { return; } file.write(m_exportData); QDir dir(exportInfo.absolutePath()); dir.mkdir("static"); dir.mkdir("js"); FileUtil::CopyDirectory(m_liteApp->resourcePath()+"/gopresent/static",dir.path()+"/static"); FileUtil::CopyDirectory(m_liteApp->resourcePath()+"/gopresent/js",dir.path()+"/js"); m_liteApp->appendLog("GoPresent","export "+exportInfo.filePath(),false); if (dlg.isExportAndView()) { QDesktopServices::openUrl(QUrl::fromLocalFile(exportInfo.filePath())); } } else if (exportType == EXPORT_TYPE_PDF) { QString init = QFileInfo(m_editor->filePath()).absolutePath()+"/"+QFileInfo(m_editor->filePath()).completeBaseName()+".pdf"; m_pdfFileName = QFileDialog::getSaveFileName(m_liteApp->mainWindow(),tr("Export PDF"),init,"*.pdf"); if (m_pdfFileName.isEmpty()) { return; } if (!m_htmldoc) { m_htmldoc = m_liteApp->htmlWidgetManager()->createDocument(this); connect(m_htmldoc,SIGNAL(loadFinished(bool)),this,SLOT(loadHtmlFinished(bool))); } QUrl url = QUrl::fromLocalFile(m_liteApp->resourcePath()+"/gopresent/export.html"); m_htmldoc->setHtml(QString::fromUtf8(m_exportData),url); }