mirror of
https://github.com/overte-org/overte.git
synced 2025-06-22 13:41:49 +02:00
Fix oven integration in the asset server
This commit is contained in:
parent
5781ed8afb
commit
7f4292440a
4 changed files with 111 additions and 66 deletions
|
@ -1326,12 +1326,40 @@ void AssetServer::handleFailedBake(QString originalAssetHash, QString assetPath,
|
||||||
}
|
}
|
||||||
|
|
||||||
void AssetServer::handleCompletedBake(QString originalAssetHash, QString originalAssetPath,
|
void AssetServer::handleCompletedBake(QString originalAssetHash, QString originalAssetPath,
|
||||||
QString bakedTempOutputDir, QVector<QString> bakedFilePaths) {
|
QString bakedTempOutputDir) {
|
||||||
bool errorCompletingBake { false };
|
bool errorCompletingBake { false };
|
||||||
QString errorReason;
|
QString errorReason;
|
||||||
|
|
||||||
qDebug() << "Completing bake for " << originalAssetHash;
|
qDebug() << "Completing bake for " << originalAssetHash;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
QDir outputDir(bakedTempOutputDir);
|
||||||
|
auto directories = outputDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
|
||||||
|
assert(directories.size() == 1);
|
||||||
|
QString bakedDirectoryPath;
|
||||||
|
for (const auto& dirName : directories) {
|
||||||
|
outputDir.cd(dirName);
|
||||||
|
if (outputDir.exists("baked") && outputDir.exists("original")) {
|
||||||
|
bakedDirectoryPath = outputDir.filePath("baked");
|
||||||
|
}
|
||||||
|
outputDir.cdUp();
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(!bakedDirectoryPath.isEmpty());
|
||||||
|
|
||||||
|
QDirIterator it(bakedDirectoryPath, QDirIterator::Subdirectories);
|
||||||
|
QVector<QString> bakedFilePaths;
|
||||||
|
while (it.hasNext()) {
|
||||||
|
it.next();
|
||||||
|
if (it.fileInfo().isFile()) {
|
||||||
|
bakedFilePaths.push_back(it.filePath());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
QDir bakedDirectory(bakedDirectoryPath);
|
||||||
|
QString redirectTarget;
|
||||||
|
|
||||||
for (auto& filePath : bakedFilePaths) {
|
for (auto& filePath : bakedFilePaths) {
|
||||||
// figure out the hash for the contents of this file
|
// figure out the hash for the contents of this file
|
||||||
QFile file(filePath);
|
QFile file(filePath);
|
||||||
|
@ -1340,18 +1368,25 @@ void AssetServer::handleCompletedBake(QString originalAssetHash, QString origina
|
||||||
|
|
||||||
AssetUtils::AssetHash bakedFileHash;
|
AssetUtils::AssetHash bakedFileHash;
|
||||||
|
|
||||||
if (file.open(QIODevice::ReadOnly)) {
|
if (!file.open(QIODevice::ReadOnly)) {
|
||||||
|
qDebug() << "Failed to open baked file: " << filePath;
|
||||||
|
// stop handling this bake, we couldn't open one of the files for reading
|
||||||
|
errorCompletingBake = true;
|
||||||
|
errorReason = "Failed to finalize bake";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
QCryptographicHash hasher(QCryptographicHash::Sha256);
|
QCryptographicHash hasher(QCryptographicHash::Sha256);
|
||||||
|
|
||||||
if (hasher.addData(&file)) {
|
if (!hasher.addData(&file)) {
|
||||||
bakedFileHash = hasher.result().toHex();
|
|
||||||
} else {
|
|
||||||
// stop handling this bake, couldn't hash the contents of the file
|
// stop handling this bake, couldn't hash the contents of the file
|
||||||
errorCompletingBake = true;
|
errorCompletingBake = true;
|
||||||
errorReason = "Failed to finalize bake";
|
errorReason = "Failed to finalize bake";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bakedFileHash = hasher.result().toHex();
|
||||||
|
|
||||||
// first check that we don't already have this bake file in our list
|
// first check that we don't already have this bake file in our list
|
||||||
auto bakeFileDestination = _filesDirectory.absoluteFilePath(bakedFileHash);
|
auto bakeFileDestination = _filesDirectory.absoluteFilePath(bakedFileHash);
|
||||||
if (!QFile::exists(bakeFileDestination)) {
|
if (!QFile::exists(bakeFileDestination)) {
|
||||||
|
@ -1365,37 +1400,27 @@ void AssetServer::handleCompletedBake(QString originalAssetHash, QString origina
|
||||||
}
|
}
|
||||||
|
|
||||||
// setup the mapping for this bake file
|
// setup the mapping for this bake file
|
||||||
auto relativeFilePath = QUrl(filePath).fileName();
|
auto relativeFilePath = bakedDirectory.relativeFilePath(filePath);
|
||||||
qDebug() << "Relative file path is: " << relativeFilePath;
|
qDebug() << "Relative file path is: " << relativeFilePath;
|
||||||
if (relativeFilePath.endsWith(".fbx", Qt::CaseInsensitive)) {
|
|
||||||
// for an FBX file, we replace the filename with the simple name
|
|
||||||
// (to handle the case where two mapped assets have the same hash but different names)
|
|
||||||
relativeFilePath = BAKED_ASSET_SIMPLE_FBX_NAME;
|
|
||||||
} else if (relativeFilePath.endsWith(".js", Qt::CaseInsensitive)) {
|
|
||||||
relativeFilePath = BAKED_ASSET_SIMPLE_JS_NAME;
|
|
||||||
} else if (!originalAssetPath.endsWith(".fbx", Qt::CaseInsensitive)) {
|
|
||||||
relativeFilePath = BAKED_ASSET_SIMPLE_TEXTURE_NAME;
|
|
||||||
}
|
|
||||||
|
|
||||||
QString bakeMapping = getBakeMapping(originalAssetHash, relativeFilePath);
|
QString bakeMapping = getBakeMapping(originalAssetHash, relativeFilePath);
|
||||||
|
|
||||||
|
// Check if this is the file we should redirect to when someone asks for the original asset
|
||||||
|
if ((relativeFilePath.endsWith(".baked.fst", Qt::CaseInsensitive) && originalAssetPath.endsWith(".fbx")) ||
|
||||||
|
(relativeFilePath.endsWith(".texmeta.json", Qt::CaseInsensitive) && !originalAssetPath.endsWith(".fbx"))) {
|
||||||
|
redirectTarget = bakeMapping;
|
||||||
|
}
|
||||||
|
|
||||||
// add a mapping (under the hidden baked folder) for this file resulting from the bake
|
// add a mapping (under the hidden baked folder) for this file resulting from the bake
|
||||||
if (setMapping(bakeMapping, bakedFileHash)) {
|
if (!setMapping(bakeMapping, bakedFileHash)) {
|
||||||
qDebug() << "Added" << bakeMapping << "for bake file" << bakedFileHash << "from bake of" << originalAssetHash;
|
|
||||||
} else {
|
|
||||||
qDebug() << "Failed to set mapping";
|
qDebug() << "Failed to set mapping";
|
||||||
// stop handling this bake, couldn't add a mapping for this bake file
|
// stop handling this bake, couldn't add a mapping for this bake file
|
||||||
errorCompletingBake = true;
|
errorCompletingBake = true;
|
||||||
errorReason = "Failed to finalize bake";
|
errorReason = "Failed to finalize bake";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
qDebug() << "Failed to open baked file: " << filePath;
|
qDebug() << "Added" << bakeMapping << "for bake file" << bakedFileHash << "from bake of" << originalAssetHash;
|
||||||
// stop handling this bake, we couldn't open one of the files for reading
|
|
||||||
errorCompletingBake = true;
|
|
||||||
errorReason = "Failed to finalize bake";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto& filePath : bakedFilePaths) {
|
for (auto& filePath : bakedFilePaths) {
|
||||||
|
@ -1411,9 +1436,12 @@ void AssetServer::handleCompletedBake(QString originalAssetHash, QString origina
|
||||||
auto type = assetTypeForFilename(originalAssetPath);
|
auto type = assetTypeForFilename(originalAssetPath);
|
||||||
auto currentTypeVersion = currentBakeVersionForAssetType(type);
|
auto currentTypeVersion = currentBakeVersionForAssetType(type);
|
||||||
|
|
||||||
|
assert(!redirectTarget.isEmpty());
|
||||||
|
|
||||||
AssetMeta meta;
|
AssetMeta meta;
|
||||||
meta.bakeVersion = currentTypeVersion;
|
meta.bakeVersion = currentTypeVersion;
|
||||||
meta.failedLastBake = errorCompletingBake;
|
meta.failedLastBake = errorCompletingBake;
|
||||||
|
meta.redirectTarget = redirectTarget;
|
||||||
|
|
||||||
if (errorCompletingBake) {
|
if (errorCompletingBake) {
|
||||||
qWarning() << "Could not complete bake for" << originalAssetHash;
|
qWarning() << "Could not complete bake for" << originalAssetHash;
|
||||||
|
@ -1435,6 +1463,7 @@ void AssetServer::handleAbortedBake(QString originalAssetHash, QString assetPath
|
||||||
static const QString BAKE_VERSION_KEY = "bake_version";
|
static const QString BAKE_VERSION_KEY = "bake_version";
|
||||||
static const QString FAILED_LAST_BAKE_KEY = "failed_last_bake";
|
static const QString FAILED_LAST_BAKE_KEY = "failed_last_bake";
|
||||||
static const QString LAST_BAKE_ERRORS_KEY = "last_bake_errors";
|
static const QString LAST_BAKE_ERRORS_KEY = "last_bake_errors";
|
||||||
|
static const QString REDIRECT_TARGET_KEY = "redirect_target";
|
||||||
|
|
||||||
std::pair<bool, AssetMeta> AssetServer::readMetaFile(AssetUtils::AssetHash hash) {
|
std::pair<bool, AssetMeta> AssetServer::readMetaFile(AssetUtils::AssetHash hash) {
|
||||||
auto metaFilePath = AssetUtils::HIDDEN_BAKED_CONTENT_FOLDER + hash + "/" + "meta.json";
|
auto metaFilePath = AssetUtils::HIDDEN_BAKED_CONTENT_FOLDER + hash + "/" + "meta.json";
|
||||||
|
@ -1461,6 +1490,7 @@ std::pair<bool, AssetMeta> AssetServer::readMetaFile(AssetUtils::AssetHash hash)
|
||||||
auto bakeVersion = root[BAKE_VERSION_KEY];
|
auto bakeVersion = root[BAKE_VERSION_KEY];
|
||||||
auto failedLastBake = root[FAILED_LAST_BAKE_KEY];
|
auto failedLastBake = root[FAILED_LAST_BAKE_KEY];
|
||||||
auto lastBakeErrors = root[LAST_BAKE_ERRORS_KEY];
|
auto lastBakeErrors = root[LAST_BAKE_ERRORS_KEY];
|
||||||
|
auto redirectTarget = root[REDIRECT_TARGET_KEY];
|
||||||
|
|
||||||
if (bakeVersion.isDouble()
|
if (bakeVersion.isDouble()
|
||||||
&& failedLastBake.isBool()
|
&& failedLastBake.isBool()
|
||||||
|
@ -1470,6 +1500,7 @@ std::pair<bool, AssetMeta> AssetServer::readMetaFile(AssetUtils::AssetHash hash)
|
||||||
meta.bakeVersion = bakeVersion.toInt();
|
meta.bakeVersion = bakeVersion.toInt();
|
||||||
meta.failedLastBake = failedLastBake.toBool();
|
meta.failedLastBake = failedLastBake.toBool();
|
||||||
meta.lastBakeErrors = lastBakeErrors.toString();
|
meta.lastBakeErrors = lastBakeErrors.toString();
|
||||||
|
meta.redirectTarget = redirectTarget.toString();
|
||||||
|
|
||||||
return { true, meta };
|
return { true, meta };
|
||||||
} else {
|
} else {
|
||||||
|
@ -1488,6 +1519,7 @@ bool AssetServer::writeMetaFile(AssetUtils::AssetHash originalAssetHash, const A
|
||||||
metaFileObject[BAKE_VERSION_KEY] = (int)meta.bakeVersion;
|
metaFileObject[BAKE_VERSION_KEY] = (int)meta.bakeVersion;
|
||||||
metaFileObject[FAILED_LAST_BAKE_KEY] = meta.failedLastBake;
|
metaFileObject[FAILED_LAST_BAKE_KEY] = meta.failedLastBake;
|
||||||
metaFileObject[LAST_BAKE_ERRORS_KEY] = meta.lastBakeErrors;
|
metaFileObject[LAST_BAKE_ERRORS_KEY] = meta.lastBakeErrors;
|
||||||
|
metaFileObject[REDIRECT_TARGET_KEY] = meta.redirectTarget;
|
||||||
|
|
||||||
QJsonDocument metaFileDoc;
|
QJsonDocument metaFileDoc;
|
||||||
metaFileDoc.setObject(metaFileObject);
|
metaFileDoc.setObject(metaFileObject);
|
||||||
|
|
|
@ -62,12 +62,10 @@ enum class ScriptBakeVersion : BakeVersion {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct AssetMeta {
|
struct AssetMeta {
|
||||||
AssetMeta() {
|
|
||||||
}
|
|
||||||
|
|
||||||
BakeVersion bakeVersion { INITIAL_BAKE_VERSION };
|
BakeVersion bakeVersion { INITIAL_BAKE_VERSION };
|
||||||
bool failedLastBake { false };
|
bool failedLastBake { false };
|
||||||
QString lastBakeErrors;
|
QString lastBakeErrors;
|
||||||
|
QString redirectTarget;
|
||||||
};
|
};
|
||||||
|
|
||||||
class BakeAssetTask;
|
class BakeAssetTask;
|
||||||
|
@ -139,8 +137,7 @@ private:
|
||||||
void bakeAsset(const AssetUtils::AssetHash& assetHash, const AssetUtils::AssetPath& assetPath, const QString& filePath);
|
void bakeAsset(const AssetUtils::AssetHash& assetHash, const AssetUtils::AssetPath& assetPath, const QString& filePath);
|
||||||
|
|
||||||
/// Move baked content for asset to baked directory and update baked status
|
/// Move baked content for asset to baked directory and update baked status
|
||||||
void handleCompletedBake(QString originalAssetHash, QString assetPath, QString bakedTempOutputDir,
|
void handleCompletedBake(QString originalAssetHash, QString assetPath, QString bakedTempOutputDir);
|
||||||
QVector<QString> bakedFilePaths);
|
|
||||||
void handleFailedBake(QString originalAssetHash, QString assetPath, QString errors);
|
void handleFailedBake(QString originalAssetHash, QString assetPath, QString errors);
|
||||||
void handleAbortedBake(QString originalAssetHash, QString assetPath);
|
void handleAbortedBake(QString originalAssetHash, QString assetPath);
|
||||||
|
|
||||||
|
|
|
@ -57,12 +57,19 @@ void BakeAssetTask::run() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Make a new temporary directory for the Oven to work in
|
||||||
QString tempOutputDir = PathUtils::generateTemporaryDir();
|
QString tempOutputDir = PathUtils::generateTemporaryDir();
|
||||||
|
|
||||||
|
// Copy file to bake the temporary dir and give a name the oven can work with
|
||||||
|
auto assetName = _assetPath.split("/").last();
|
||||||
|
auto tempAssetPath = tempOutputDir + "/" + assetName;
|
||||||
|
QFile::copy(_filePath, tempAssetPath);
|
||||||
|
|
||||||
auto base = QFileInfo(QCoreApplication::applicationFilePath()).absoluteDir();
|
auto base = QFileInfo(QCoreApplication::applicationFilePath()).absoluteDir();
|
||||||
QString path = base.absolutePath() + "/oven";
|
QString path = base.absolutePath() + "/oven";
|
||||||
QString extension = _assetPath.mid(_assetPath.lastIndexOf('.') + 1);
|
QString extension = _assetPath.mid(_assetPath.lastIndexOf('.') + 1);
|
||||||
QStringList args {
|
QStringList args {
|
||||||
"-i", _filePath,
|
"-i", tempAssetPath,
|
||||||
"-o", tempOutputDir,
|
"-o", tempOutputDir,
|
||||||
"-t", extension,
|
"-t", extension,
|
||||||
};
|
};
|
||||||
|
@ -72,7 +79,7 @@ void BakeAssetTask::run() {
|
||||||
QEventLoop loop;
|
QEventLoop loop;
|
||||||
|
|
||||||
connect(_ovenProcess.get(), static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),
|
connect(_ovenProcess.get(), static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),
|
||||||
this, [&loop, this, tempOutputDir](int exitCode, QProcess::ExitStatus exitStatus) {
|
this, [&loop, this, tempOutputDir, tempAssetPath](int exitCode, QProcess::ExitStatus exitStatus) {
|
||||||
qDebug() << "Baking process finished: " << exitCode << exitStatus;
|
qDebug() << "Baking process finished: " << exitCode << exitStatus;
|
||||||
|
|
||||||
if (exitStatus == QProcess::CrashExit) {
|
if (exitStatus == QProcess::CrashExit) {
|
||||||
|
@ -82,18 +89,20 @@ void BakeAssetTask::run() {
|
||||||
QString errors = "Fatal error occurred while baking";
|
QString errors = "Fatal error occurred while baking";
|
||||||
emit bakeFailed(_assetHash, _assetPath, errors);
|
emit bakeFailed(_assetHash, _assetPath, errors);
|
||||||
}
|
}
|
||||||
} else if (exitCode == OVEN_STATUS_CODE_SUCCESS) {
|
if (!QDir(tempOutputDir).rmdir(".")) {
|
||||||
QDir outputDir = tempOutputDir;
|
qWarning() << "Failed to remove temporary directory:" << tempOutputDir;
|
||||||
auto files = outputDir.entryInfoList(QDir::Files);
|
|
||||||
QVector<QString> outputFiles;
|
|
||||||
for (auto& file : files) {
|
|
||||||
outputFiles.push_back(file.absoluteFilePath());
|
|
||||||
}
|
}
|
||||||
|
} else if (exitCode == OVEN_STATUS_CODE_SUCCESS) {
|
||||||
|
// Remove temp copy of the original asset
|
||||||
|
QFile::remove(tempAssetPath);
|
||||||
|
|
||||||
emit bakeComplete(_assetHash, _assetPath, tempOutputDir, outputFiles);
|
emit bakeComplete(_assetHash, _assetPath, tempOutputDir);
|
||||||
} else if (exitStatus == QProcess::NormalExit && exitCode == OVEN_STATUS_CODE_ABORT) {
|
} else if (exitStatus == QProcess::NormalExit && exitCode == OVEN_STATUS_CODE_ABORT) {
|
||||||
_wasAborted.store(true);
|
_wasAborted.store(true);
|
||||||
emit bakeAborted(_assetHash, _assetPath);
|
emit bakeAborted(_assetHash, _assetPath);
|
||||||
|
if (!QDir(tempOutputDir).rmdir(".")) {
|
||||||
|
qWarning() << "Failed to remove temporary directory:" << tempOutputDir;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
QString errors;
|
QString errors;
|
||||||
if (exitCode == OVEN_STATUS_CODE_FAIL) {
|
if (exitCode == OVEN_STATUS_CODE_FAIL) {
|
||||||
|
@ -108,6 +117,9 @@ void BakeAssetTask::run() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
emit bakeFailed(_assetHash, _assetPath, errors);
|
emit bakeFailed(_assetHash, _assetPath, errors);
|
||||||
|
if (!QDir(tempOutputDir).rmdir(".")) {
|
||||||
|
qWarning() << "Failed to remove temporary directory:" << tempOutputDir;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
loop.quit();
|
loop.quit();
|
||||||
|
@ -115,9 +127,13 @@ void BakeAssetTask::run() {
|
||||||
|
|
||||||
qDebug() << "Starting oven for " << _assetPath;
|
qDebug() << "Starting oven for " << _assetPath;
|
||||||
_ovenProcess->start(path, args, QIODevice::ReadOnly);
|
_ovenProcess->start(path, args, QIODevice::ReadOnly);
|
||||||
|
qDebug() << "Running:" << path << args;
|
||||||
if (!_ovenProcess->waitForStarted(-1)) {
|
if (!_ovenProcess->waitForStarted(-1)) {
|
||||||
QString errors = "Oven process failed to start";
|
QString errors = "Oven process failed to start";
|
||||||
emit bakeFailed(_assetHash, _assetPath, errors);
|
emit bakeFailed(_assetHash, _assetPath, errors);
|
||||||
|
if (!QDir(tempOutputDir).rmdir(".")) {
|
||||||
|
qWarning() << "Failed to remove temporary directory:" << tempOutputDir;
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -37,7 +37,7 @@ public slots:
|
||||||
void abort();
|
void abort();
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void bakeComplete(QString assetHash, QString assetPath, QString tempOutputDir, QVector<QString> outputFiles);
|
void bakeComplete(QString assetHash, QString assetPath, QString tempOutputDir);
|
||||||
void bakeFailed(QString assetHash, QString assetPath, QString errors);
|
void bakeFailed(QString assetHash, QString assetPath, QString errors);
|
||||||
void bakeAborted(QString assetHash, QString assetPath);
|
void bakeAborted(QString assetHash, QString assetPath);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue