Copy scripts from android assets at startup

This commit is contained in:
Gabriel Calero 2017-12-19 17:03:29 -03:00
parent d31346c047
commit 78f2a8c566
3 changed files with 35 additions and 0 deletions

View file

@ -105,6 +105,11 @@ int main(int argc, const char* argv[]) {
if (allowMultipleInstances) {
instanceMightBeRunning = false;
}
QFileInfo fInfo("assets:/scripts/test.js");
QDir dirInfo(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
QUrl androidPath = QUrl::fromLocalFile(dirInfo.canonicalPath() + "/scripts");
PathUtils::copyDirDeep("assets:/scripts", androidPath.toLocalFile());
// this needs to be done here in main, as the mechanism for setting the
// scripts directory appears not to work. See the bug report
// https://highfidelity.fogbugz.com/f/cases/5759/Issues-changing-scripts-directory-in-ScriptsEngine

View file

@ -17,6 +17,7 @@
#include <QDateTime>
#include <QFileInfo>
#include <QDir>
#include <QDirIterator>
#include <QUrl>
#include <QtCore/QStandardPaths>
#include <QRegularExpression>
@ -203,3 +204,31 @@ bool PathUtils::isDescendantOf(const QUrl& childURL, const QUrl& parentURL) {
QString parent = stripFilename(parentURL);
return child.startsWith(parent, PathUtils::getFSCaseSensitivity());
}
void PathUtils::copyDirDeep(QString src, QString dst) {
QDir dir = QDir::root();
dir.mkpath(dst);
QDirIterator it(src, QStringList() << "*", QDir::Files|QDir::AllDirs, QDirIterator::Subdirectories);
while (it.hasNext()) {
QString f = it.next();
QFileInfo fInfo(f);
QString newDst = dst + (dst.endsWith("/")?"":"/") + fInfo.fileName();
if (fInfo.isFile()) {
QFile dfile(f);
if (dfile.exists(f))
{
if (dfile.copy(newDst)) {
QFile::setPermissions(newDst, QFile::ReadOwner);
} else {
QFile::setPermissions(newDst, QFile::ReadOwner);
// sometimes copy returns false but it worked anyway
//qWarning() << "Could not copy to " << newDst;
}
}
} else if (fInfo.isDir() ) {
copyDirDeep(f, newDst);
}
}
}

View file

@ -54,6 +54,7 @@ public:
// note: this is FS-case-sensitive version of parentURL.isParentOf(childURL)
static bool isDescendantOf(const QUrl& childURL, const QUrl& parentURL);
static QUrl defaultScriptsLocation(const QString& newDefault = "");
static void copyDirDeep(QString src, QString dst);
};