From cda5e94b334dddf139540da1e5dbdbaab370fea7 Mon Sep 17 00:00:00 2001 From: utkarshgautamnyu Date: Fri, 22 Sep 2017 20:57:18 -0700 Subject: [PATCH 01/36] Added JavaScript Baking --- assignment-client/src/assets/AssetServer.cpp | 34 +- .../src/assets/BakeAssetTask.cpp | 5 + libraries/baking/src/JSBaker.cpp | 349 ++++++++++++++++++ libraries/baking/src/JSBaker.h | 57 +++ 4 files changed, 438 insertions(+), 7 deletions(-) create mode 100644 libraries/baking/src/JSBaker.cpp create mode 100644 libraries/baking/src/JSBaker.h diff --git a/assignment-client/src/assets/AssetServer.cpp b/assignment-client/src/assets/AssetServer.cpp index 9df606c227..78ea1ed7be 100644 --- a/assignment-client/src/assets/AssetServer.cpp +++ b/assignment-client/src/assets/AssetServer.cpp @@ -30,6 +30,7 @@ #include #include +#include #include #include #include @@ -49,10 +50,11 @@ static const int INTERFACE_RUNNING_CHECK_FREQUENCY_MS = 1000; const QString ASSET_SERVER_LOGGING_TARGET_NAME = "asset-server"; -static const QStringList BAKEABLE_MODEL_EXTENSIONS = { "fbx" }; +static const QStringList BAKEABLE_MODEL_EXTENSIONS = { "fbx" , "js" }; static QStringList BAKEABLE_TEXTURE_EXTENSIONS; static const QString BAKED_MODEL_SIMPLE_NAME = "asset.fbx"; static const QString BAKED_TEXTURE_SIMPLE_NAME = "texture.ktx"; +static const QString BAKED_SCRIPT_SIMPLE_NAME = "script.js"; void AssetServer::bakeAsset(const AssetHash& assetHash, const AssetPath& assetPath, const QString& filePath) { qDebug() << "Starting bake for: " << assetPath << assetHash; @@ -96,7 +98,11 @@ std::pair AssetServer::getAssetStatus(const AssetPath& pa QString bakedFilename; if (BAKEABLE_MODEL_EXTENSIONS.contains(extension)) { - bakedFilename = BAKED_MODEL_SIMPLE_NAME; + if (extension == "js") { + bakedFilename = BAKED_SCRIPT_SIMPLE_NAME; + } else { + bakedFilename = BAKED_MODEL_SIMPLE_NAME; + } } else if (BAKEABLE_TEXTURE_EXTENSIONS.contains(extension.toLocal8Bit()) && hasMetaFile(hash)) { bakedFilename = BAKED_TEXTURE_SIMPLE_NAME; } else { @@ -183,7 +189,11 @@ bool AssetServer::needsToBeBaked(const AssetPath& path, const AssetHash& assetHa } if (BAKEABLE_MODEL_EXTENSIONS.contains(extension)) { - bakedFilename = BAKED_MODEL_SIMPLE_NAME; + if (extension == "js") { + bakedFilename = BAKED_SCRIPT_SIMPLE_NAME; + } else { + bakedFilename = BAKED_MODEL_SIMPLE_NAME; + } } else if (loaded && BAKEABLE_TEXTURE_EXTENSIONS.contains(extension.toLocal8Bit())) { bakedFilename = BAKED_TEXTURE_SIMPLE_NAME; } else { @@ -485,7 +495,11 @@ void AssetServer::handleGetMappingOperation(ReceivedMessage& message, SharedNode QString bakedRootFile; if (BAKEABLE_MODEL_EXTENSIONS.contains(assetPathExtension)) { - bakedRootFile = BAKED_MODEL_SIMPLE_NAME; + if (assetPathExtension == "js") { + bakedRootFile = BAKED_SCRIPT_SIMPLE_NAME; + } else { + bakedRootFile = BAKED_MODEL_SIMPLE_NAME; + } } else if (BAKEABLE_TEXTURE_EXTENSIONS.contains(assetPathExtension.toLocal8Bit())) { bakedRootFile = BAKED_TEXTURE_SIMPLE_NAME; } @@ -1141,6 +1155,7 @@ bool AssetServer::renameMapping(AssetPath oldPath, AssetPath newPath) { static const QString BAKED_ASSET_SIMPLE_FBX_NAME = "asset.fbx"; static const QString BAKED_ASSET_SIMPLE_TEXTURE_NAME = "texture.ktx"; +static const QString BAKED_ASSET_SIMPLE_JS_NAME = "script.js"; QString getBakeMapping(const AssetHash& hash, const QString& relativeFilePath) { return HIDDEN_BAKED_CONTENT_FOLDER + hash + "/" + relativeFilePath; @@ -1203,8 +1218,9 @@ void AssetServer::handleCompletedBake(QString originalAssetHash, QString origina // setup the mapping for this bake file auto relativeFilePath = QUrl(filePath).fileName(); qDebug() << "Relative file path is: " << relativeFilePath; - - if (relativeFilePath.endsWith(".fbx", Qt::CaseInsensitive)) { + if (relativeFilePath.endsWith(".js", Qt::CaseInsensitive)) { + relativeFilePath = BAKED_ASSET_SIMPLE_JS_NAME; + } else 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; @@ -1350,7 +1366,11 @@ bool AssetServer::setBakingEnabled(const AssetPathList& paths, bool enabled) { QString bakedFilename; if (BAKEABLE_MODEL_EXTENSIONS.contains(extension)) { - bakedFilename = BAKED_MODEL_SIMPLE_NAME; + if (extension == "js") { + bakedFilename = BAKED_SCRIPT_SIMPLE_NAME; + } else { + bakedFilename = BAKED_MODEL_SIMPLE_NAME; + } } else if (BAKEABLE_TEXTURE_EXTENSIONS.contains(extension.toLocal8Bit()) && hasMetaFile(hash)) { bakedFilename = BAKED_TEXTURE_SIMPLE_NAME; } else { diff --git a/assignment-client/src/assets/BakeAssetTask.cpp b/assignment-client/src/assets/BakeAssetTask.cpp index 9073510f79..daff3a3834 100644 --- a/assignment-client/src/assets/BakeAssetTask.cpp +++ b/assignment-client/src/assets/BakeAssetTask.cpp @@ -15,6 +15,7 @@ #include #include +#include BakeAssetTask::BakeAssetTask(const AssetHash& assetHash, const AssetPath& assetPath, const QString& filePath) : _assetHash(assetHash), @@ -34,6 +35,10 @@ void BakeAssetTask::run() { _baker = std::unique_ptr { new FBXBaker(QUrl("file:///" + _filePath), fn, PathUtils::generateTemporaryDir()) }; + } else if (_assetPath.endsWith(".js", Qt::CaseInsensitive)) { + _baker = std::unique_ptr{ + new JSBaker(QUrl("file:///" + _filePath), PathUtils::generateTemporaryDir()) + }; } else { _baker = std::unique_ptr { new TextureBaker(QUrl("file:///" + _filePath), image::TextureUsage::CUBE_TEXTURE, diff --git a/libraries/baking/src/JSBaker.cpp b/libraries/baking/src/JSBaker.cpp new file mode 100644 index 0000000000..2c42b342b0 --- /dev/null +++ b/libraries/baking/src/JSBaker.cpp @@ -0,0 +1,349 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "JSBaker.h" +#include "Baker.h" + +JSBaker::JSBaker(const QUrl& jsURL, const QString& bakedOutputDir) : + _jsURL(jsURL), + _bakedOutputDir(bakedOutputDir) +{ + +}; + +void JSBaker::bake() { + + auto tempDir = PathUtils::generateTemporaryDir(); + + if (tempDir.isEmpty()) { + handleError("Failed to create a temporary directory."); + return; + } + + _tempDir = tempDir; + + _originalJSFilePath = _tempDir.filePath(_jsURL.fileName()); + qDebug() << "Made temporary dir " << _tempDir; + qDebug() << "Origin file path: " << _originalJSFilePath; + + connect(this, &JSBaker::sourceCopyReadyToLoad, this, &JSBaker::bakeSourceCopy); + + loadSourceJS(); +} + +void JSBaker::loadSourceJS() { + + // load the local file + QFile localJS{ _jsURL.toLocalFile() }; + + if (!localJS.exists()) { + + handleError("Could not find " + _jsURL.toString()); + return; + } + + // copy to original file + qDebug() << "Local file url: " << _jsURL << _jsURL.toString() << _jsURL.toLocalFile() << ", copying to: " << _originalJSFilePath; + localJS.copy(_originalJSFilePath); + + // emit signal to indicate script is ready to import + emit sourceCopyReadyToLoad(); +} + +void JSBaker::bakeSourceCopy() { + + importScript(); + + if (hasErrors()) { + return; + } + + /*bakeScript(); + if (hasErrors()) { + return; + }*/ + + /*exportScript(); + + if (hasErrors()) { + return; + }*/ +} + + + +void JSBaker::importScript() { + + //qDebug() << "file path: " << _originalJSFilePath.toLocal8Bit().data() << QDir(_originalJSFilePath).exists(); + + // Import the file to be processed + QFile jsFile(_originalJSFilePath); + if (!jsFile.open(QIODevice::ReadOnly | QIODevice::Text)) { + handleError("Error opening " + _originalJSFilePath + " for reading"); + return; + } + + // Call Bake Script with the opened file + bakeScript(&jsFile); +} + +void JSBaker::bakeScript(QFile* inFile) { + + QFile outFile; + outFile.open(QIODevice::WriteOnly | QIODevice::Text); + QTextStream in(inFile); + QTextStream out(&outFile); + + QChar currentChar; + QChar prevChar; + QChar nextChar; + + // Initialize prevChar with new line + prevChar = '\n'; + in >> currentChar; + + while (!in.atEnd()) { + + // Reading per character + in >> nextChar; + + if (currentChar == '\r') { + out << '\n'; + //currentChar = '\n'; + + } else if (currentChar == '/') { + + if (nextChar == '/') { + + handleSingleLineComments(&in); + //out << '\n'; + + //Start fresh after handling comments + prevChar = '\n'; + in >> currentChar; + continue; + + } else if (nextChar == '*') { + + handleMultiLineComments(&in); + //out << ' '; + + //Start fresh after handling comments + prevChar = '\n'; + in >> currentChar; + //currentChar = ' '; + continue; + } else { + out << currentChar; + } + } else if (currentChar == ' ' || (int) currentChar.toLatin1() == 9) { + + // Handle multiple spaces + if (nextChar == ' ' || (int)currentChar.toLatin1() == 9) { + while (nextChar == ' ' || (int)nextChar.toLatin1() == 9 ) { + + in >> nextChar; + if (nextChar == '\n') + break; + } + } + if (!omitSpace(prevChar,nextChar)) { + + out << ' '; + } + + } else if (currentChar == '\n') { + qDebug() << "prevChar2" << prevChar; + qDebug() << "currentChar2" << currentChar; + qDebug() << "nextChar2" << nextChar; + //Handle multiple new lines + //Hnadle new line followed by space or tab + if (nextChar == '\n' || nextChar == ' ' || (int)nextChar.toLatin1() == 9) { + while (nextChar == '\n' || nextChar == ' ' || (int)nextChar.toLatin1() == 9) { + in >> nextChar; + } + } + + if (!omitNewLine(prevChar, nextChar)) { + out << '\n'; + } + + } else if (currentChar == '"' ) { + //Don't modify quoted strings + out << currentChar; + out << nextChar; + while (nextChar != '"') { + in >> nextChar; + out << nextChar; + } + + + //Start fresh after handling quoted strings + //prevChar = '"'; + prevChar = nextChar; + //currentChar = nextChar; + in >> currentChar; + continue; + } else if (currentChar == "'") { + //Don't modify quoted strings + out << currentChar; + out << nextChar; + while (nextChar != "'") { + in >> nextChar; + out << nextChar; + } + + qDebug() << "prevChar" << prevChar; + qDebug() << "currentChar" << currentChar; + qDebug() << "nextChar" << nextChar; + + //out << nextChar; + //Start fresh after handling quoted strings + //prevChar = '\''; + prevChar = nextChar; + //currentChar = nextChar; + in >> currentChar; + qDebug() << "prevChar1" << prevChar; + qDebug() << "currentChar1" << currentChar; + qDebug() << "nextChar1" << nextChar; + continue; + } else { + out << currentChar; + + } + + prevChar = currentChar; + currentChar = nextChar; + } + + //Output current character when next character reaches EOF + if (currentChar != '\n') { + + out << currentChar; + } + + + inFile->close(); + exportScript(&outFile); +} + +void JSBaker::handleSingleLineComments(QTextStream * in) { + QChar ch; + + while (!in->atEnd()) { + *in >> ch; + if (ch <= '\n') + break; + } + + //*out << '\n'; +} + +void JSBaker::handleMultiLineComments(QTextStream * in) { + QChar ch; + + while (!in->atEnd()) { + *in >> ch; + if (ch == '*') { + if (in->read(1) == '/') { + return; + } + } + } + + handleError("Eror unterminated multi line comment"); + +} + +bool JSBaker::isAlphanum(QChar c) { + return ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || + (c >= 'A' && c <= 'Z') || c == '_' || c == '$' || c == '\\' || + c > 126); +} + +bool JSBaker::omitSpace(QChar prevChar, QChar nextChar) { + + if ((isAlphanum(prevChar) || isNonAscii(prevChar) || isSpecialChar(prevChar)) && + (isAlphanum(nextChar) || isNonAscii(nextChar) || isSpecialChar(nextChar)) + ) { + + return false; + } + + return true; + +} + +bool JSBaker::isNonAscii(QChar c) { + return ((int)c.toLatin1() > 127); +} + +bool JSBaker::isSpecialChar(QChar c) { + + if (c == '\'' || c == '$' || c == '_') + return true; + + return false; +} + +bool JSBaker::isSpecialCharPre(QChar c) { + + if (c == '\'' || c == '$' || c == '_' || c == '}' || c == ']' || c == ')' || c == '+' || c == '-' + || c == '"' || c == "'") + return true; + + return false; +} + +bool JSBaker::isSpecialCharPost(QChar c) { + + if (c == '\'' || c == '$' || c == '_' || c == '{' || c == '[' || c == '(' || c == '+' || c == '-' || c == '/') + return true; + + return false; +} + +bool JSBaker::omitNewLine(QChar prevChar, QChar nextChar) { + + if ((isAlphanum(prevChar) || isNonAscii(prevChar) || isSpecialCharPre(prevChar)) && + (isAlphanum(nextChar) || isNonAscii(nextChar) || isSpecialCharPost(nextChar)) + ) { + + return false; + } + + return true; + +} + +void JSBaker::exportScript(QFile* bakedFile) { + // save the relative path to this FBX inside our passed output folder + auto fileName = _jsURL.fileName(); + auto baseName = fileName.left(fileName.lastIndexOf('.')); + auto bakedFilename = baseName + BAKED_JS_EXTENSION; + + _bakedJSFilePath = _bakedOutputDir + "/" + bakedFilename; + + bakedFile->setFileName(_bakedJSFilePath); + + //QFile bakedFile(_bakedJSFilePath); + + if (!bakedFile->open(QIODevice::WriteOnly)) { + handleError("Error opening " + _bakedJSFilePath + " for writing"); + return; + } + + /*QByteArray x(5, 'a'); + bakedFile.copy();*/ + + _outputFiles.push_back(_bakedJSFilePath); + + qCDebug(model_baking) << "Exported" << _jsURL << "with re-written paths to" << _bakedJSFilePath; + bakedFile->close(); + emit finished(); +} \ No newline at end of file diff --git a/libraries/baking/src/JSBaker.h b/libraries/baking/src/JSBaker.h new file mode 100644 index 0000000000..0fb593bd92 --- /dev/null +++ b/libraries/baking/src/JSBaker.h @@ -0,0 +1,57 @@ +#ifndef hifi_JSBaker_h +#define hifi_JSBaker_h + +#include +#include +#include + +#include "Baker.h" +#include "TextureBaker.h" +#include "ModelBakingLoggingCategory.h" + +static const QString BAKED_JS_EXTENSION = ".baked.js"; + +class JSBaker : public Baker { + Q_OBJECT + +public: + JSBaker(const QUrl& jsURL, const QString& bakedOutputDir); + +public slots: + virtual void bake() override; + +signals: + void sourceCopyReadyToLoad(); + +private slots: + void bakeSourceCopy(); + + +private : + + QUrl _jsURL; + QString _bakedOutputDir; + QDir _tempDir; + QString _originalJSFilePath; + QString _bakedJSFilePath; + QByteArray _buffer; + + void loadSourceJS(); + void importScript(); + + void exportScript(QFile*); + void bakeScript(QFile*); + void handleSingleLineComments(QTextStream*); + void handleMultiLineComments(QTextStream*); + + bool isAlphanum(QChar); + bool omitSpace(QChar, QChar); + bool isNonAscii(QChar c); + bool isSpecialChar(QChar c); + bool isSpecialCharPre(QChar c); + bool isSpecialCharPost(QChar c); + bool omitNewLine(QChar, QChar); + +}; + +#endif // !hifi_JSBaker_h \ No newline at end of file From 6c40d39f816832782557d5d6eaac2654399c9d00 Mon Sep 17 00:00:00 2001 From: utkarshgautamnyu Date: Sun, 24 Sep 2017 20:53:09 -0700 Subject: [PATCH 02/36] refactored jsbaker.cpp --- libraries/baking/src/JSBaker.cpp | 446 ++++++++---------- libraries/baking/src/JSBaker.h | 43 +- .../baking/src/JSBakingLoggingCategory.cpp | 14 + .../baking/src/JSBakingLoggingCategory.h | 19 + 4 files changed, 256 insertions(+), 266 deletions(-) create mode 100644 libraries/baking/src/JSBakingLoggingCategory.cpp create mode 100644 libraries/baking/src/JSBakingLoggingCategory.h diff --git a/libraries/baking/src/JSBaker.cpp b/libraries/baking/src/JSBaker.cpp index 2c42b342b0..12b6d2f438 100644 --- a/libraries/baking/src/JSBaker.cpp +++ b/libraries/baking/src/JSBaker.cpp @@ -1,10 +1,15 @@ -#include -#include -#include -#include -#include -#include -#include +// +// JSBaker.cpp +// libraries/baking/src +// +// Created by Utkarsh Gautam on 9/18/17. +// Copyright 2017 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include // To generate Temporary directory #include "JSBaker.h" #include "Baker.h" @@ -17,7 +22,6 @@ JSBaker::JSBaker(const QUrl& jsURL, const QString& bakedOutputDir) : }; void JSBaker::bake() { - auto tempDir = PathUtils::generateTemporaryDir(); if (tempDir.isEmpty()) { @@ -31,13 +35,14 @@ void JSBaker::bake() { qDebug() << "Made temporary dir " << _tempDir; qDebug() << "Origin file path: " << _originalJSFilePath; - connect(this, &JSBaker::sourceCopyReadyToLoad, this, &JSBaker::bakeSourceCopy); + // When source JS is loaded, trigger the importJS method + connect(this, &JSBaker::sourceJSLoaded, this, &JSBaker::importJS); + // Make a local copy of the JS File loadSourceJS(); } void JSBaker::loadSourceJS() { - // load the local file QFile localJS{ _jsURL.toLocalFile() }; @@ -47,282 +52,147 @@ void JSBaker::loadSourceJS() { return; } - // copy to original file + // make a copy of local file at _originalJSFilePath qDebug() << "Local file url: " << _jsURL << _jsURL.toString() << _jsURL.toLocalFile() << ", copying to: " << _originalJSFilePath; localJS.copy(_originalJSFilePath); // emit signal to indicate script is ready to import - emit sourceCopyReadyToLoad(); + emit sourceJSLoaded(); } -void JSBaker::bakeSourceCopy() { - - importScript(); - - if (hasErrors()) { - return; - } - - /*bakeScript(); - if (hasErrors()) { - return; - }*/ - - /*exportScript(); - - if (hasErrors()) { - return; - }*/ -} - - - -void JSBaker::importScript() { - +void JSBaker::importJS() { //qDebug() << "file path: " << _originalJSFilePath.toLocal8Bit().data() << QDir(_originalJSFilePath).exists(); - // Import the file to be processed + // Import the file to be processed from _originalJSFilePath QFile jsFile(_originalJSFilePath); if (!jsFile.open(QIODevice::ReadOnly | QIODevice::Text)) { handleError("Error opening " + _originalJSFilePath + " for reading"); - return; } - // Call Bake Script with the opened file - bakeScript(&jsFile); + if (hasErrors()) { + // Return if cannot open file + return; + } else { + // Import successful, Call the baking function with the imported file + bakeJS(&jsFile); + } } -void JSBaker::bakeScript(QFile* inFile) { - - QFile outFile; - outFile.open(QIODevice::WriteOnly | QIODevice::Text); - QTextStream in(inFile); - QTextStream out(&outFile); - - QChar currentChar; - QChar prevChar; - QChar nextChar; +void JSBaker::bakeJS(QFile* inputFile) { + // Create an output file which will be exported as the bakedFile + QFile outputFile; + outputFile.open(QIODevice::WriteOnly | QIODevice::Text); - // Initialize prevChar with new line - prevChar = '\n'; - in >> currentChar; - - while (!in.atEnd()) { - - // Reading per character - in >> nextChar; + // Read from inputFile and write to outputFile per character + QTextStream readCharacter(inputFile); + QTextStream writeCharacter(&outputFile); - if (currentChar == '\r') { - out << '\n'; - //currentChar = '\n'; - - } else if (currentChar == '/') { - - if (nextChar == '/') { - - handleSingleLineComments(&in); - //out << '\n'; - + // Algorithm requires the knowledge of previous and next character for each character read + QChar currentCharacter; + QChar nextCharacter; + // Initialize previousCharacter with new line + QChar previousCharacter = '\n'; + + // Input currentCharacter + readCharacter >> currentCharacter; + + while (!readCharacter.atEnd()) { + // input nextCharacter + readCharacter >> nextCharacter; + if (currentCharacter == '\r') { + writeCharacter << '\n'; + } else if (currentCharacter == '/') { + // Check if single line comment i.e. // + if (nextCharacter == '/') { + currentCharacter = handleSingleLineComments(&readCharacter); + //writeCharacter << '\n'; //Start fresh after handling comments - prevChar = '\n'; - in >> currentChar; + //previousCharacter = '\n'; + //readCharacter >> currentCharacter; continue; - - } else if (nextChar == '*') { - - handleMultiLineComments(&in); - //out << ' '; - + } else if (nextCharacter == '*') { + // Check if multi line comment i.e. /* + handleMultiLineComments(&readCharacter); + //writeCharacter << ' '; //Start fresh after handling comments - prevChar = '\n'; - in >> currentChar; - //currentChar = ' '; + previousCharacter = '\n'; + readCharacter >> currentCharacter; continue; } else { - out << currentChar; + // If '/' is not followed by '/' or '*' print '/' + writeCharacter << currentCharacter; } - } else if (currentChar == ' ' || (int) currentChar.toLatin1() == 9) { + } else if (isControlCharacter(currentCharacter)) { // Check if white space or tab - // Handle multiple spaces - if (nextChar == ' ' || (int)currentChar.toLatin1() == 9) { - while (nextChar == ' ' || (int)nextChar.toLatin1() == 9 ) { - - in >> nextChar; - if (nextChar == '\n') + // Skip multiple spaces or tabs + if (isControlCharacter(nextCharacter)) { + while (isControlCharacter(nextCharacter)) { + readCharacter >> nextCharacter; + if (nextCharacter == '\n') break; } } - if (!omitSpace(prevChar,nextChar)) { - - out << ' '; + // check if space can be omitted + if (!canOmitSpace(previousCharacter,nextCharacter)) { + writeCharacter << ' '; } - - } else if (currentChar == '\n') { - qDebug() << "prevChar2" << prevChar; - qDebug() << "currentChar2" << currentChar; - qDebug() << "nextChar2" << nextChar; - //Handle multiple new lines - //Hnadle new line followed by space or tab - if (nextChar == '\n' || nextChar == ' ' || (int)nextChar.toLatin1() == 9) { - while (nextChar == '\n' || nextChar == ' ' || (int)nextChar.toLatin1() == 9) { - in >> nextChar; + } else if (currentCharacter == '\n') { // Check if new line + + //Skip multiple new lines + //Skip new line followed by space or tab + if (nextCharacter == '\n' || isControlCharacter(nextCharacter)) { + while (nextCharacter == '\n' || isControlCharacter(nextCharacter)) { + readCharacter >> nextCharacter; } } - - if (!omitNewLine(prevChar, nextChar)) { - out << '\n'; + // Check if new line can be omitted + if (!canOmitNewLine(previousCharacter, nextCharacter)) { + writeCharacter << '\n'; } + } else if (isQuote(currentCharacter)) { // If the currentCharacter is " or ' or ` + // Print the current quote and nextCharacter as is + writeCharacter << currentCharacter; + writeCharacter << nextCharacter; - } else if (currentChar == '"' ) { - //Don't modify quoted strings - out << currentChar; - out << nextChar; - while (nextChar != '"') { - in >> nextChar; - out << nextChar; + // Store the type of quote we are processing + QChar quote = currentCharacter; + + // Don't modify the quoted strings + while (nextCharacter != quote) { + readCharacter >> nextCharacter; + writeCharacter << nextCharacter; } - //Start fresh after handling quoted strings - //prevChar = '"'; - prevChar = nextChar; - //currentChar = nextChar; - in >> currentChar; - continue; - } else if (currentChar == "'") { - //Don't modify quoted strings - out << currentChar; - out << nextChar; - while (nextChar != "'") { - in >> nextChar; - out << nextChar; - } - - qDebug() << "prevChar" << prevChar; - qDebug() << "currentChar" << currentChar; - qDebug() << "nextChar" << nextChar; - - //out << nextChar; - //Start fresh after handling quoted strings - //prevChar = '\''; - prevChar = nextChar; - //currentChar = nextChar; - in >> currentChar; - qDebug() << "prevChar1" << prevChar; - qDebug() << "currentChar1" << currentChar; - qDebug() << "nextChar1" << nextChar; + previousCharacter = nextCharacter; + readCharacter >> currentCharacter; continue; } else { - out << currentChar; - + // In all other cases write the currentCharacter to outputFile + writeCharacter << currentCharacter; } - prevChar = currentChar; - currentChar = nextChar; + previousCharacter = currentCharacter; + currentCharacter = nextCharacter; } - //Output current character when next character reaches EOF - if (currentChar != '\n') { - - out << currentChar; + //write currentCharacter to output file when nextCharacter reaches EOF + if (currentCharacter != '\n') { + writeCharacter << currentCharacter; } - - inFile->close(); - exportScript(&outFile); -} + inputFile->close(); -void JSBaker::handleSingleLineComments(QTextStream * in) { - QChar ch; - - while (!in->atEnd()) { - *in >> ch; - if (ch <= '\n') - break; + if (hasErrors()) { + return; + } else { + // Bake successful, Export the compressed outputFile + exportJS(&outputFile); } - - //*out << '\n'; } -void JSBaker::handleMultiLineComments(QTextStream * in) { - QChar ch; - - while (!in->atEnd()) { - *in >> ch; - if (ch == '*') { - if (in->read(1) == '/') { - return; - } - } - } - - handleError("Eror unterminated multi line comment"); - -} - -bool JSBaker::isAlphanum(QChar c) { - return ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || - (c >= 'A' && c <= 'Z') || c == '_' || c == '$' || c == '\\' || - c > 126); -} - -bool JSBaker::omitSpace(QChar prevChar, QChar nextChar) { - - if ((isAlphanum(prevChar) || isNonAscii(prevChar) || isSpecialChar(prevChar)) && - (isAlphanum(nextChar) || isNonAscii(nextChar) || isSpecialChar(nextChar)) - ) { - - return false; - } - - return true; - -} - -bool JSBaker::isNonAscii(QChar c) { - return ((int)c.toLatin1() > 127); -} - -bool JSBaker::isSpecialChar(QChar c) { - - if (c == '\'' || c == '$' || c == '_') - return true; - - return false; -} - -bool JSBaker::isSpecialCharPre(QChar c) { - - if (c == '\'' || c == '$' || c == '_' || c == '}' || c == ']' || c == ')' || c == '+' || c == '-' - || c == '"' || c == "'") - return true; - - return false; -} - -bool JSBaker::isSpecialCharPost(QChar c) { - - if (c == '\'' || c == '$' || c == '_' || c == '{' || c == '[' || c == '(' || c == '+' || c == '-' || c == '/') - return true; - - return false; -} - -bool JSBaker::omitNewLine(QChar prevChar, QChar nextChar) { - - if ((isAlphanum(prevChar) || isNonAscii(prevChar) || isSpecialCharPre(prevChar)) && - (isAlphanum(nextChar) || isNonAscii(nextChar) || isSpecialCharPost(nextChar)) - ) { - - return false; - } - - return true; - -} - -void JSBaker::exportScript(QFile* bakedFile) { - // save the relative path to this FBX inside our passed output folder +void JSBaker::exportJS(QFile* bakedFile) { + // save the relative path to this JS inside the output folder auto fileName = _jsURL.fileName(); auto baseName = fileName.left(fileName.lastIndexOf('.')); auto bakedFilename = baseName + BAKED_JS_EXTENSION; @@ -331,19 +201,101 @@ void JSBaker::exportScript(QFile* bakedFile) { bakedFile->setFileName(_bakedJSFilePath); - //QFile bakedFile(_bakedJSFilePath); - if (!bakedFile->open(QIODevice::WriteOnly)) { handleError("Error opening " + _bakedJSFilePath + " for writing"); - return; } - /*QByteArray x(5, 'a'); - bakedFile.copy();*/ + if (hasErrors()) { + // cannot open bakedFile return + return; + } else { + _outputFiles.push_back(_bakedJSFilePath); + qCDebug(js_baking) << "Exported" << _jsURL << "with re-written paths to" << _bakedJSFilePath; + } - _outputFiles.push_back(_bakedJSFilePath); - - qCDebug(model_baking) << "Exported" << _jsURL << "with re-written paths to" << _bakedJSFilePath; + //close the outputFile bakedFile->close(); + // emit signal to indicate the JS baking is finished emit finished(); +} + +QChar JSBaker::handleSingleLineComments(QTextStream * readCharacter) { + QChar character; + while (!readCharacter->atEnd()) { + *readCharacter >> character; + if (character <= '\n') + break; + } + return character; +} + +void JSBaker::handleMultiLineComments(QTextStream * readCharacter) { + QChar character; + while (!readCharacter->atEnd()) { + *readCharacter >> character; + if (character == '*') { + if (readCharacter->read(1) == '/') { + return; + } + } + } + handleError("Eror unterminated multi line comment"); +} + +bool JSBaker::canOmitSpace(QChar previousCharacter, QChar nextCharacter) { + + if ((isAlphanum(previousCharacter) || isNonAscii(previousCharacter) || isSpecialChar(previousCharacter)) && + (isAlphanum(nextCharacter) || isNonAscii(nextCharacter) || isSpecialChar(nextCharacter)) + ) { + return false; + } + return true; +} + +bool JSBaker::canOmitNewLine(QChar previousCharacter, QChar nextCharacter) { + if ((isAlphanum(previousCharacter) || isNonAscii(previousCharacter) || isSpecialCharPre(previousCharacter)) && + (isAlphanum(nextCharacter) || isNonAscii(nextCharacter) || isSpecialCharPost(nextCharacter)) + ) { + return false; + } + return true; +} + +bool JSBaker::isAlphanum(QChar c) { + return ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') + || c == '_' || c == '$' || c == '\\' || c > 126); +} + +bool JSBaker::isNonAscii(QChar c) { + return ((int)c.toLatin1() > 127); +} + +bool JSBaker::isSpecialChar(QChar c) { + if (c == '\'' || c == '$' || c == '_' || '/') { + return true; + } + return false; +} + +bool JSBaker::isSpecialCharPre(QChar c) { + if (c == '\'' || c == '$' || c == '_' || c == '}' || c == ']' || c == ')' || c == '+' || c == '-' + || c == '"' || c == "'") { + return true; + } + return false; +} + +bool JSBaker::isSpecialCharPost(QChar c) { + if (c == '\'' || c == '$' || c == '_' || c == '{' || c == '[' || c == '(' || c == '+' || c == '-' || c == '/') { + return true; + } + return false; +} + +bool JSBaker::isControlCharacter(QChar c) { + return (c == ' ' || (int)c.toLatin1() == 9); +} + +bool JSBaker::isQuote(QChar c) { + return (c == '"' || c == "'" || c == '`'); } \ No newline at end of file diff --git a/libraries/baking/src/JSBaker.h b/libraries/baking/src/JSBaker.h index 0fb593bd92..2a428294d0 100644 --- a/libraries/baking/src/JSBaker.h +++ b/libraries/baking/src/JSBaker.h @@ -1,19 +1,24 @@ +// +// JSBaker.h +// libraries/baking/src +// +// Created by Utkarsh Gautam on 9/18/17. +// Copyright 2017 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + #ifndef hifi_JSBaker_h #define hifi_JSBaker_h -#include -#include -#include - #include "Baker.h" -#include "TextureBaker.h" -#include "ModelBakingLoggingCategory.h" +#include "JSBakingLoggingCategory.h" static const QString BAKED_JS_EXTENSION = ".baked.js"; class JSBaker : public Baker { - Q_OBJECT - + //Q_OBJECT public: JSBaker(const QUrl& jsURL, const QString& bakedOutputDir); @@ -21,11 +26,10 @@ public slots: virtual void bake() override; signals: - void sourceCopyReadyToLoad(); + void sourceJSLoaded(); private slots: - void bakeSourceCopy(); - + void importJS(); private : @@ -37,21 +41,22 @@ private : QByteArray _buffer; void loadSourceJS(); - void importScript(); - - void exportScript(QFile*); - void bakeScript(QFile*); - void handleSingleLineComments(QTextStream*); + void bakeJS(QFile*); + void exportJS(QFile*); + + QChar handleSingleLineComments(QTextStream*); void handleMultiLineComments(QTextStream*); + bool canOmitSpace(QChar, QChar); + bool canOmitNewLine(QChar, QChar); + bool isAlphanum(QChar); - bool omitSpace(QChar, QChar); bool isNonAscii(QChar c); bool isSpecialChar(QChar c); bool isSpecialCharPre(QChar c); bool isSpecialCharPost(QChar c); - bool omitNewLine(QChar, QChar); - + bool isControlCharacter(QChar); + bool isQuote(QChar); }; #endif // !hifi_JSBaker_h \ No newline at end of file diff --git a/libraries/baking/src/JSBakingLoggingCategory.cpp b/libraries/baking/src/JSBakingLoggingCategory.cpp new file mode 100644 index 0000000000..77c667922f --- /dev/null +++ b/libraries/baking/src/JSBakingLoggingCategory.cpp @@ -0,0 +1,14 @@ +// +// JSBakingLoggingCategory.cpp +// libraries/baking/src +// +// Created by Utkarsh Gautam on 9/18/17. +// Copyright 2017 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include "JSBakingLoggingCategory.h" + +Q_LOGGING_CATEGORY(js_baking, "hifi.JS-baking"); diff --git a/libraries/baking/src/JSBakingLoggingCategory.h b/libraries/baking/src/JSBakingLoggingCategory.h new file mode 100644 index 0000000000..2c3ff535af --- /dev/null +++ b/libraries/baking/src/JSBakingLoggingCategory.h @@ -0,0 +1,19 @@ +// +// JSBakingLoggingCategory.h +// libraries/baking/src +// +// Created by Utkarsh Gautam on 9/18/17. +// Copyright 2017 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_JSBakingLoggingCategory_h +#define hifi_JSBakingLoggingCategory_h + +#include + +Q_DECLARE_LOGGING_CATEGORY(js_baking) + +#endif // hifi_ModelBakingLoggingCategory_h From 0a57874cc2cec0740efa475b6e984ab9812e3816 Mon Sep 17 00:00:00 2001 From: utkarshgautamnyu Date: Mon, 25 Sep 2017 14:46:38 -0700 Subject: [PATCH 03/36] Made updates to JSBaker --- libraries/baking/src/JSBaker.cpp | 72 +++++++++++++++++--------------- libraries/baking/src/JSBaker.h | 13 +++--- 2 files changed, 45 insertions(+), 40 deletions(-) diff --git a/libraries/baking/src/JSBaker.cpp b/libraries/baking/src/JSBaker.cpp index 12b6d2f438..53b7994b70 100644 --- a/libraries/baking/src/JSBaker.cpp +++ b/libraries/baking/src/JSBaker.cpp @@ -32,8 +32,8 @@ void JSBaker::bake() { _tempDir = tempDir; _originalJSFilePath = _tempDir.filePath(_jsURL.fileName()); - qDebug() << "Made temporary dir " << _tempDir; - qDebug() << "Origin file path: " << _originalJSFilePath; + qCDebug(js_baking) << "Made temporary dir " << _tempDir; + qCDebug(js_baking) << "Origin file path: " << _originalJSFilePath; // When source JS is loaded, trigger the importJS method connect(this, &JSBaker::sourceJSLoaded, this, &JSBaker::importJS); @@ -53,16 +53,14 @@ void JSBaker::loadSourceJS() { } // make a copy of local file at _originalJSFilePath - qDebug() << "Local file url: " << _jsURL << _jsURL.toString() << _jsURL.toLocalFile() << ", copying to: " << _originalJSFilePath; + qCDebug(js_baking) << "Local file url: " << _jsURL << _jsURL.toString() << _jsURL.toLocalFile() << ", copying to: " << _originalJSFilePath; localJS.copy(_originalJSFilePath); // emit signal to indicate script is ready to import emit sourceJSLoaded(); } -void JSBaker::importJS() { - //qDebug() << "file path: " << _originalJSFilePath.toLocal8Bit().data() << QDir(_originalJSFilePath).exists(); - +void JSBaker::importJS() { // Import the file to be processed from _originalJSFilePath QFile jsFile(_originalJSFilePath); if (!jsFile.open(QIODevice::ReadOnly | QIODevice::Text)) { @@ -99,21 +97,22 @@ void JSBaker::bakeJS(QFile* inputFile) { while (!readCharacter.atEnd()) { // input nextCharacter readCharacter >> nextCharacter; + if (currentCharacter == '\r') { writeCharacter << '\n'; } else if (currentCharacter == '/') { // Check if single line comment i.e. // if (nextCharacter == '/') { - currentCharacter = handleSingleLineComments(&readCharacter); - //writeCharacter << '\n'; + handleSingleLineComments(&readCharacter); + //Start fresh after handling comments - //previousCharacter = '\n'; - //readCharacter >> currentCharacter; + previousCharacter = '\n'; + readCharacter >> currentCharacter; continue; } else if (nextCharacter == '*') { // Check if multi line comment i.e. /* handleMultiLineComments(&readCharacter); - //writeCharacter << ' '; + //Start fresh after handling comments previousCharacter = '\n'; readCharacter >> currentCharacter; @@ -122,21 +121,25 @@ void JSBaker::bakeJS(QFile* inputFile) { // If '/' is not followed by '/' or '*' print '/' writeCharacter << currentCharacter; } - } else if (isControlCharacter(currentCharacter)) { // Check if white space or tab - + } else if (isControlCharacter(currentCharacter)) { + // Check if white space or tab + // Skip multiple spaces or tabs if (isControlCharacter(nextCharacter)) { while (isControlCharacter(nextCharacter)) { readCharacter >> nextCharacter; - if (nextCharacter == '\n') + if (nextCharacter == '\n') { break; + } } } + // check if space can be omitted - if (!canOmitSpace(previousCharacter,nextCharacter)) { + if (!canOmitSpace(previousCharacter, nextCharacter)) { writeCharacter << ' '; } - } else if (currentCharacter == '\n') { // Check if new line + } else if (currentCharacter == '\n') { + // Check if new line //Skip multiple new lines //Skip new line followed by space or tab @@ -149,7 +152,7 @@ void JSBaker::bakeJS(QFile* inputFile) { if (!canOmitNewLine(previousCharacter, nextCharacter)) { writeCharacter << '\n'; } - } else if (isQuote(currentCharacter)) { // If the currentCharacter is " or ' or ` + } else if (isQuote(currentCharacter)) { // Print the current quote and nextCharacter as is writeCharacter << currentCharacter; writeCharacter << nextCharacter; @@ -181,6 +184,7 @@ void JSBaker::bakeJS(QFile* inputFile) { writeCharacter << currentCharacter; } + // Reading done. Closing the inputFile inputFile->close(); if (hasErrors()) { @@ -213,20 +217,20 @@ void JSBaker::exportJS(QFile* bakedFile) { qCDebug(js_baking) << "Exported" << _jsURL << "with re-written paths to" << _bakedJSFilePath; } - //close the outputFile + // Exporting done. Closing the outputFile bakedFile->close(); // emit signal to indicate the JS baking is finished emit finished(); } -QChar JSBaker::handleSingleLineComments(QTextStream * readCharacter) { +void JSBaker::handleSingleLineComments(QTextStream * readCharacter) { QChar character; while (!readCharacter->atEnd()) { *readCharacter >> character; if (character <= '\n') break; } - return character; + return; } void JSBaker::handleMultiLineComments(QTextStream * readCharacter) { @@ -243,24 +247,24 @@ void JSBaker::handleMultiLineComments(QTextStream * readCharacter) { } bool JSBaker::canOmitSpace(QChar previousCharacter, QChar nextCharacter) { - - if ((isAlphanum(previousCharacter) || isNonAscii(previousCharacter) || isSpecialChar(previousCharacter)) && - (isAlphanum(nextCharacter) || isNonAscii(nextCharacter) || isSpecialChar(nextCharacter)) - ) { + if ((isAlphanum(previousCharacter) || isNonAscii(previousCharacter) || isSpecialCharacter(previousCharacter)) && + (isAlphanum(nextCharacter) || isNonAscii(nextCharacter) || isSpecialCharacter(nextCharacter)) + ) { return false; } return true; } bool JSBaker::canOmitNewLine(QChar previousCharacter, QChar nextCharacter) { - if ((isAlphanum(previousCharacter) || isNonAscii(previousCharacter) || isSpecialCharPre(previousCharacter)) && - (isAlphanum(nextCharacter) || isNonAscii(nextCharacter) || isSpecialCharPost(nextCharacter)) - ) { + if ((isAlphanum(previousCharacter) || isNonAscii(previousCharacter) || isSpecialCharacterPrevious(previousCharacter)) && + (isAlphanum(nextCharacter) || isNonAscii(nextCharacter) || isSpecialCharacterNext(nextCharacter)) + ) { return false; } return true; } +//Check if character is alphabet, number or one of the following: '_', '$', '\\' bool JSBaker::isAlphanum(QChar c) { return ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || c == '_' || c == '$' || c == '\\' || c > 126); @@ -270,14 +274,14 @@ bool JSBaker::isNonAscii(QChar c) { return ((int)c.toLatin1() > 127); } -bool JSBaker::isSpecialChar(QChar c) { - if (c == '\'' || c == '$' || c == '_' || '/') { +bool JSBaker::isSpecialCharacter(QChar c) { + if (c == '\'' || c == '$' || c == '_' || c == '/') { return true; } return false; } -bool JSBaker::isSpecialCharPre(QChar c) { +bool JSBaker::isSpecialCharacterPrevious(QChar c) { if (c == '\'' || c == '$' || c == '_' || c == '}' || c == ']' || c == ')' || c == '+' || c == '-' || c == '"' || c == "'") { return true; @@ -285,17 +289,19 @@ bool JSBaker::isSpecialCharPre(QChar c) { return false; } -bool JSBaker::isSpecialCharPost(QChar c) { - if (c == '\'' || c == '$' || c == '_' || c == '{' || c == '[' || c == '(' || c == '+' || c == '-' || c == '/') { +bool JSBaker::isSpecialCharacterNext(QChar c) { + if (c == '\'' || c == '$' || c == '_' || c == '{' || c == '[' || c == '(' || c == '+' || c == '-') { return true; } return false; } +// Check if white space or tab bool JSBaker::isControlCharacter(QChar c) { return (c == ' ' || (int)c.toLatin1() == 9); } +// Check If the currentCharacter is " or ' or ` bool JSBaker::isQuote(QChar c) { return (c == '"' || c == "'" || c == '`'); -} \ No newline at end of file +} diff --git a/libraries/baking/src/JSBaker.h b/libraries/baking/src/JSBaker.h index 2a428294d0..4612fc5072 100644 --- a/libraries/baking/src/JSBaker.h +++ b/libraries/baking/src/JSBaker.h @@ -18,7 +18,7 @@ static const QString BAKED_JS_EXTENSION = ".baked.js"; class JSBaker : public Baker { - //Q_OBJECT + Q_OBJECT public: JSBaker(const QUrl& jsURL, const QString& bakedOutputDir); @@ -32,7 +32,6 @@ private slots: void importJS(); private : - QUrl _jsURL; QString _bakedOutputDir; QDir _tempDir; @@ -44,7 +43,7 @@ private : void bakeJS(QFile*); void exportJS(QFile*); - QChar handleSingleLineComments(QTextStream*); + void handleSingleLineComments(QTextStream*); void handleMultiLineComments(QTextStream*); bool canOmitSpace(QChar, QChar); @@ -52,11 +51,11 @@ private : bool isAlphanum(QChar); bool isNonAscii(QChar c); - bool isSpecialChar(QChar c); - bool isSpecialCharPre(QChar c); - bool isSpecialCharPost(QChar c); + bool isSpecialCharacter(QChar c); + bool isSpecialCharacterPrevious(QChar c); + bool isSpecialCharacterNext(QChar c); bool isControlCharacter(QChar); bool isQuote(QChar); }; -#endif // !hifi_JSBaker_h \ No newline at end of file +#endif // !hifi_JSBaker_h From 91dad385a6b3608d3bf62fe065301c15faec252c Mon Sep 17 00:00:00 2001 From: utkarshgautamnyu Date: Mon, 25 Sep 2017 15:12:04 -0700 Subject: [PATCH 04/36] Refactored AssetServer.cpp --- assignment-client/src/assets/AssetServer.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/assignment-client/src/assets/AssetServer.cpp b/assignment-client/src/assets/AssetServer.cpp index 78ea1ed7be..4292f5663c 100644 --- a/assignment-client/src/assets/AssetServer.cpp +++ b/assignment-client/src/assets/AssetServer.cpp @@ -50,7 +50,7 @@ static const int INTERFACE_RUNNING_CHECK_FREQUENCY_MS = 1000; const QString ASSET_SERVER_LOGGING_TARGET_NAME = "asset-server"; -static const QStringList BAKEABLE_MODEL_EXTENSIONS = { "fbx" , "js" }; +static const QStringList BAKEABLE_MODEL_EXTENSIONS = { "fbx", "js" }; static QStringList BAKEABLE_TEXTURE_EXTENSIONS; static const QString BAKED_MODEL_SIMPLE_NAME = "asset.fbx"; static const QString BAKED_TEXTURE_SIMPLE_NAME = "texture.ktx"; @@ -98,10 +98,10 @@ std::pair AssetServer::getAssetStatus(const AssetPath& pa QString bakedFilename; if (BAKEABLE_MODEL_EXTENSIONS.contains(extension)) { - if (extension == "js") { + if (extension == "fbx") { + bakedFilename = BAKED_MODEL_SIMPLE_NAME; + } else if (extension == "js") { bakedFilename = BAKED_SCRIPT_SIMPLE_NAME; - } else { - bakedFilename = BAKED_MODEL_SIMPLE_NAME; } } else if (BAKEABLE_TEXTURE_EXTENSIONS.contains(extension.toLocal8Bit()) && hasMetaFile(hash)) { bakedFilename = BAKED_TEXTURE_SIMPLE_NAME; @@ -189,10 +189,10 @@ bool AssetServer::needsToBeBaked(const AssetPath& path, const AssetHash& assetHa } if (BAKEABLE_MODEL_EXTENSIONS.contains(extension)) { - if (extension == "js") { - bakedFilename = BAKED_SCRIPT_SIMPLE_NAME; - } else { + if (extension == "fbx") { bakedFilename = BAKED_MODEL_SIMPLE_NAME; + } else if (extension == "js") { + bakedFilename = BAKED_SCRIPT_SIMPLE_NAME; } } else if (loaded && BAKEABLE_TEXTURE_EXTENSIONS.contains(extension.toLocal8Bit())) { bakedFilename = BAKED_TEXTURE_SIMPLE_NAME; @@ -495,10 +495,10 @@ void AssetServer::handleGetMappingOperation(ReceivedMessage& message, SharedNode QString bakedRootFile; if (BAKEABLE_MODEL_EXTENSIONS.contains(assetPathExtension)) { - if (assetPathExtension == "js") { - bakedRootFile = BAKED_SCRIPT_SIMPLE_NAME; - } else { + if (assetPathExtension == "fbx") { bakedRootFile = BAKED_MODEL_SIMPLE_NAME; + } else if (assetPathExtension == "js") { + bakedRootFile = BAKED_SCRIPT_SIMPLE_NAME; } } else if (BAKEABLE_TEXTURE_EXTENSIONS.contains(assetPathExtension.toLocal8Bit())) { bakedRootFile = BAKED_TEXTURE_SIMPLE_NAME; From 209201e592277b94d5f475e7a2d61198b1cc3864 Mon Sep 17 00:00:00 2001 From: utkarshgautamnyu Date: Mon, 25 Sep 2017 15:55:12 -0700 Subject: [PATCH 05/36] updates to handle white space within a single line comment --- libraries/baking/src/JSBaker.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/baking/src/JSBaker.cpp b/libraries/baking/src/JSBaker.cpp index 53b7994b70..71e99bc590 100644 --- a/libraries/baking/src/JSBaker.cpp +++ b/libraries/baking/src/JSBaker.cpp @@ -227,7 +227,7 @@ void JSBaker::handleSingleLineComments(QTextStream * readCharacter) { QChar character; while (!readCharacter->atEnd()) { *readCharacter >> character; - if (character <= '\n') + if (character == '\n') break; } return; From 9e91ab7630dd3f56bca559dc0772af4422a54ac1 Mon Sep 17 00:00:00 2001 From: utkarshgautamnyu Date: Mon, 25 Sep 2017 20:19:53 -0700 Subject: [PATCH 06/36] Update AssetServer.cpp --- assignment-client/src/assets/AssetServer.cpp | 40 +++++++++----------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/assignment-client/src/assets/AssetServer.cpp b/assignment-client/src/assets/AssetServer.cpp index 4292f5663c..a2158bc289 100644 --- a/assignment-client/src/assets/AssetServer.cpp +++ b/assignment-client/src/assets/AssetServer.cpp @@ -50,11 +50,12 @@ static const int INTERFACE_RUNNING_CHECK_FREQUENCY_MS = 1000; const QString ASSET_SERVER_LOGGING_TARGET_NAME = "asset-server"; -static const QStringList BAKEABLE_MODEL_EXTENSIONS = { "fbx", "js" }; +static const QStringList BAKEABLE_MODEL_EXTENSIONS = { "fbx"}; static QStringList BAKEABLE_TEXTURE_EXTENSIONS; +static const QStringList BAKEABLE_SCRIPT_EXTENSIONS = {"js"}; static const QString BAKED_MODEL_SIMPLE_NAME = "asset.fbx"; static const QString BAKED_TEXTURE_SIMPLE_NAME = "texture.ktx"; -static const QString BAKED_SCRIPT_SIMPLE_NAME = "script.js"; +static const QString BAKED_SCRIPT_SIMPLE_NAME = "asset.js"; void AssetServer::bakeAsset(const AssetHash& assetHash, const AssetPath& assetPath, const QString& filePath) { qDebug() << "Starting bake for: " << assetPath << assetHash; @@ -98,14 +99,13 @@ std::pair AssetServer::getAssetStatus(const AssetPath& pa QString bakedFilename; if (BAKEABLE_MODEL_EXTENSIONS.contains(extension)) { - if (extension == "fbx") { bakedFilename = BAKED_MODEL_SIMPLE_NAME; - } else if (extension == "js") { - bakedFilename = BAKED_SCRIPT_SIMPLE_NAME; - } } else if (BAKEABLE_TEXTURE_EXTENSIONS.contains(extension.toLocal8Bit()) && hasMetaFile(hash)) { bakedFilename = BAKED_TEXTURE_SIMPLE_NAME; - } else { + } else if (BAKEABLE_SCRIPT_EXTENSIONS.contains(extension)) { + bakedFilename = BAKED_SCRIPT_SIMPLE_NAME; + } + else { return { Irrelevant, "" }; } @@ -189,13 +189,11 @@ bool AssetServer::needsToBeBaked(const AssetPath& path, const AssetHash& assetHa } if (BAKEABLE_MODEL_EXTENSIONS.contains(extension)) { - if (extension == "fbx") { - bakedFilename = BAKED_MODEL_SIMPLE_NAME; - } else if (extension == "js") { - bakedFilename = BAKED_SCRIPT_SIMPLE_NAME; - } + bakedFilename = BAKED_MODEL_SIMPLE_NAME; } else if (loaded && BAKEABLE_TEXTURE_EXTENSIONS.contains(extension.toLocal8Bit())) { bakedFilename = BAKED_TEXTURE_SIMPLE_NAME; + } else if (BAKEABLE_SCRIPT_EXTENSIONS.contains(extension)) { + bakedFilename = BAKED_SCRIPT_SIMPLE_NAME; } else { return false; } @@ -495,13 +493,11 @@ void AssetServer::handleGetMappingOperation(ReceivedMessage& message, SharedNode QString bakedRootFile; if (BAKEABLE_MODEL_EXTENSIONS.contains(assetPathExtension)) { - if (assetPathExtension == "fbx") { - bakedRootFile = BAKED_MODEL_SIMPLE_NAME; - } else if (assetPathExtension == "js") { - bakedRootFile = BAKED_SCRIPT_SIMPLE_NAME; - } + bakedRootFile = BAKED_MODEL_SIMPLE_NAME; } else if (BAKEABLE_TEXTURE_EXTENSIONS.contains(assetPathExtension.toLocal8Bit())) { bakedRootFile = BAKED_TEXTURE_SIMPLE_NAME; + } else if (BAKEABLE_SCRIPT_EXTENSIONS.contains(assetPathExtension)) { + bakedRootFile = BAKED_SCRIPT_SIMPLE_NAME; } auto originalAssetHash = it->second; @@ -1155,7 +1151,7 @@ bool AssetServer::renameMapping(AssetPath oldPath, AssetPath newPath) { static const QString BAKED_ASSET_SIMPLE_FBX_NAME = "asset.fbx"; static const QString BAKED_ASSET_SIMPLE_TEXTURE_NAME = "texture.ktx"; -static const QString BAKED_ASSET_SIMPLE_JS_NAME = "script.js"; +static const QString BAKED_ASSET_SIMPLE_JS_NAME = "asset.js"; QString getBakeMapping(const AssetHash& hash, const QString& relativeFilePath) { return HIDDEN_BAKED_CONTENT_FOLDER + hash + "/" + relativeFilePath; @@ -1366,13 +1362,11 @@ bool AssetServer::setBakingEnabled(const AssetPathList& paths, bool enabled) { QString bakedFilename; if (BAKEABLE_MODEL_EXTENSIONS.contains(extension)) { - if (extension == "js") { - bakedFilename = BAKED_SCRIPT_SIMPLE_NAME; - } else { - bakedFilename = BAKED_MODEL_SIMPLE_NAME; - } + bakedFilename = BAKED_MODEL_SIMPLE_NAME; } else if (BAKEABLE_TEXTURE_EXTENSIONS.contains(extension.toLocal8Bit()) && hasMetaFile(hash)) { bakedFilename = BAKED_TEXTURE_SIMPLE_NAME; + } else if (BAKEABLE_SCRIPT_EXTENSIONS.contains(extension)) { + bakedFilename = BAKED_SCRIPT_SIMPLE_NAME; } else { continue; } From b86dd9dea4068c4c1a5f1ca84ad1da89a836c84a Mon Sep 17 00:00:00 2001 From: utkarshgautamnyu Date: Mon, 25 Sep 2017 20:20:30 -0700 Subject: [PATCH 07/36] Update JSBaker.cpp --- libraries/baking/src/JSBaker.cpp | 189 +++++++++++-------------------- 1 file changed, 69 insertions(+), 120 deletions(-) diff --git a/libraries/baking/src/JSBaker.cpp b/libraries/baking/src/JSBaker.cpp index 71e99bc590..1286a5592e 100644 --- a/libraries/baking/src/JSBaker.cpp +++ b/libraries/baking/src/JSBaker.cpp @@ -9,11 +9,13 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -#include // To generate Temporary directory +#include #include "JSBaker.h" #include "Baker.h" +const int ASCII_CHARACTERS_UPPER_LIMIT = 126; + JSBaker::JSBaker(const QUrl& jsURL, const QString& bakedOutputDir) : _jsURL(jsURL), _bakedOutputDir(bakedOutputDir) @@ -22,58 +24,22 @@ JSBaker::JSBaker(const QUrl& jsURL, const QString& bakedOutputDir) : }; void JSBaker::bake() { - auto tempDir = PathUtils::generateTemporaryDir(); + qCDebug(js_baking) << "JS Baker " << _jsURL << "bake starting"; - if (tempDir.isEmpty()) { - handleError("Failed to create a temporary directory."); - return; - } - - _tempDir = tempDir; - - _originalJSFilePath = _tempDir.filePath(_jsURL.fileName()); - qCDebug(js_baking) << "Made temporary dir " << _tempDir; - qCDebug(js_baking) << "Origin file path: " << _originalJSFilePath; - - // When source JS is loaded, trigger the importJS method - connect(this, &JSBaker::sourceJSLoaded, this, &JSBaker::importJS); - - // Make a local copy of the JS File - loadSourceJS(); -} - -void JSBaker::loadSourceJS() { - // load the local file - QFile localJS{ _jsURL.toLocalFile() }; - - if (!localJS.exists()) { - - handleError("Could not find " + _jsURL.toString()); - return; - } - - // make a copy of local file at _originalJSFilePath - qCDebug(js_baking) << "Local file url: " << _jsURL << _jsURL.toString() << _jsURL.toLocalFile() << ", copying to: " << _originalJSFilePath; - localJS.copy(_originalJSFilePath); - - // emit signal to indicate script is ready to import - emit sourceJSLoaded(); + // Import the script to start baking + importJS(); } void JSBaker::importJS() { - // Import the file to be processed from _originalJSFilePath - QFile jsFile(_originalJSFilePath); + // Import the file to be processed from _jsURL + QFile jsFile(_jsURL.toLocalFile()); if (!jsFile.open(QIODevice::ReadOnly | QIODevice::Text)) { handleError("Error opening " + _originalJSFilePath + " for reading"); - } - - if (hasErrors()) { - // Return if cannot open file return; - } else { - // Import successful, Call the baking function with the imported file - bakeJS(&jsFile); } + + // Import successful, Call the baking function with the imported file + bakeJS(&jsFile); } void JSBaker::bakeJS(QFile* inputFile) { @@ -82,8 +48,8 @@ void JSBaker::bakeJS(QFile* inputFile) { outputFile.open(QIODevice::WriteOnly | QIODevice::Text); // Read from inputFile and write to outputFile per character - QTextStream readCharacter(inputFile); - QTextStream writeCharacter(&outputFile); + QTextStream in(inputFile); + QTextStream out(&outputFile); // Algorithm requires the knowledge of previous and next character for each character read QChar currentCharacter; @@ -91,35 +57,33 @@ void JSBaker::bakeJS(QFile* inputFile) { // Initialize previousCharacter with new line QChar previousCharacter = '\n'; - // Input currentCharacter - readCharacter >> currentCharacter; + in >> currentCharacter; - while (!readCharacter.atEnd()) { - // input nextCharacter - readCharacter >> nextCharacter; + while (!in.atEnd()) { + in >> nextCharacter; if (currentCharacter == '\r') { - writeCharacter << '\n'; + out << '\n'; } else if (currentCharacter == '/') { // Check if single line comment i.e. // if (nextCharacter == '/') { - handleSingleLineComments(&readCharacter); + handleSingleLineComments(&in); //Start fresh after handling comments previousCharacter = '\n'; - readCharacter >> currentCharacter; + in >> currentCharacter; continue; } else if (nextCharacter == '*') { // Check if multi line comment i.e. /* - handleMultiLineComments(&readCharacter); + handleMultiLineComments(&in); //Start fresh after handling comments previousCharacter = '\n'; - readCharacter >> currentCharacter; + in >> currentCharacter; continue; } else { // If '/' is not followed by '/' or '*' print '/' - writeCharacter << currentCharacter; + out << currentCharacter; } } else if (isControlCharacter(currentCharacter)) { // Check if white space or tab @@ -127,7 +91,7 @@ void JSBaker::bakeJS(QFile* inputFile) { // Skip multiple spaces or tabs if (isControlCharacter(nextCharacter)) { while (isControlCharacter(nextCharacter)) { - readCharacter >> nextCharacter; + in >> nextCharacter; if (nextCharacter == '\n') { break; } @@ -136,43 +100,42 @@ void JSBaker::bakeJS(QFile* inputFile) { // check if space can be omitted if (!canOmitSpace(previousCharacter, nextCharacter)) { - writeCharacter << ' '; + out << ' '; } } else if (currentCharacter == '\n') { // Check if new line //Skip multiple new lines //Skip new line followed by space or tab - if (nextCharacter == '\n' || isControlCharacter(nextCharacter)) { - while (nextCharacter == '\n' || isControlCharacter(nextCharacter)) { - readCharacter >> nextCharacter; - } - } + while (nextCharacter == '\n' || isControlCharacter(nextCharacter)) { + in >> nextCharacter; + } + // Check if new line can be omitted if (!canOmitNewLine(previousCharacter, nextCharacter)) { - writeCharacter << '\n'; + out << '\n'; } } else if (isQuote(currentCharacter)) { // Print the current quote and nextCharacter as is - writeCharacter << currentCharacter; - writeCharacter << nextCharacter; + out << currentCharacter; + out << nextCharacter; // Store the type of quote we are processing QChar quote = currentCharacter; // Don't modify the quoted strings while (nextCharacter != quote) { - readCharacter >> nextCharacter; - writeCharacter << nextCharacter; + in >> nextCharacter; + out << nextCharacter; } //Start fresh after handling quoted strings previousCharacter = nextCharacter; - readCharacter >> currentCharacter; + in >> currentCharacter; continue; } else { // In all other cases write the currentCharacter to outputFile - writeCharacter << currentCharacter; + out << currentCharacter; } previousCharacter = currentCharacter; @@ -181,7 +144,7 @@ void JSBaker::bakeJS(QFile* inputFile) { //write currentCharacter to output file when nextCharacter reaches EOF if (currentCharacter != '\n') { - writeCharacter << currentCharacter; + out << currentCharacter; } // Reading done. Closing the inputFile @@ -207,38 +170,36 @@ void JSBaker::exportJS(QFile* bakedFile) { if (!bakedFile->open(QIODevice::WriteOnly)) { handleError("Error opening " + _bakedJSFilePath + " for writing"); - } - - if (hasErrors()) { - // cannot open bakedFile return return; - } else { - _outputFiles.push_back(_bakedJSFilePath); - qCDebug(js_baking) << "Exported" << _jsURL << "with re-written paths to" << _bakedJSFilePath; } - + + // Export successful + _outputFiles.push_back(_bakedJSFilePath); + qCDebug(js_baking) << "Exported" << _jsURL << "with re-written paths to" << _bakedJSFilePath; + // Exporting done. Closing the outputFile bakedFile->close(); + // emit signal to indicate the JS baking is finished emit finished(); } -void JSBaker::handleSingleLineComments(QTextStream * readCharacter) { +void JSBaker::handleSingleLineComments(QTextStream * in) { QChar character; - while (!readCharacter->atEnd()) { - *readCharacter >> character; - if (character == '\n') - break; + while (!in->atEnd()) { + *in >> character; + if (character == '\n') { + break; + } } - return; } -void JSBaker::handleMultiLineComments(QTextStream * readCharacter) { +void JSBaker::handleMultiLineComments(QTextStream * in) { QChar character; - while (!readCharacter->atEnd()) { - *readCharacter >> character; + while (!in->atEnd()) { + *in >> character; if (character == '*') { - if (readCharacter->read(1) == '/') { + if (in->read(1) == '/') { return; } } @@ -247,58 +208,46 @@ void JSBaker::handleMultiLineComments(QTextStream * readCharacter) { } bool JSBaker::canOmitSpace(QChar previousCharacter, QChar nextCharacter) { - if ((isAlphanum(previousCharacter) || isNonAscii(previousCharacter) || isSpecialCharacter(previousCharacter)) && - (isAlphanum(nextCharacter) || isNonAscii(nextCharacter) || isSpecialCharacter(nextCharacter)) - ) { - return false; - } - return true; + return(!((isAlphanum(previousCharacter) || isNonAscii(previousCharacter) || isSpecialCharacter(previousCharacter)) && + (isAlphanum(nextCharacter) || isNonAscii(nextCharacter) || isSpecialCharacter(nextCharacter))) + ); } bool JSBaker::canOmitNewLine(QChar previousCharacter, QChar nextCharacter) { - if ((isAlphanum(previousCharacter) || isNonAscii(previousCharacter) || isSpecialCharacterPrevious(previousCharacter)) && - (isAlphanum(nextCharacter) || isNonAscii(nextCharacter) || isSpecialCharacterNext(nextCharacter)) - ) { - return false; - } - return true; + return (!((isAlphanum(previousCharacter) || isNonAscii(previousCharacter) || isSpecialCharacterPrevious(previousCharacter)) && + (isAlphanum(nextCharacter) || isNonAscii(nextCharacter) || isSpecialCharacterNext(nextCharacter))) + ); } -//Check if character is alphabet, number or one of the following: '_', '$', '\\' +//Check if character is alphabet, number or one of the following: '_', '$', '\\' or a non-ASCII character bool JSBaker::isAlphanum(QChar c) { return ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') - || c == '_' || c == '$' || c == '\\' || c > 126); + || c == '_' || c == '$' || c == '\\' || c > ASCII_CHARACTERS_UPPER_LIMIT); } bool JSBaker::isNonAscii(QChar c) { - return ((int)c.toLatin1() > 127); + return ((int)c.toLatin1() > ASCII_CHARACTERS_UPPER_LIMIT); } +// If previous and next characters are special characters, don't omit space bool JSBaker::isSpecialCharacter(QChar c) { - if (c == '\'' || c == '$' || c == '_' || c == '/') { - return true; - } - return false; + return (c == '\'' || c == '$' || c == '_' || c == '/'); } +// If previous character is a special character, maybe don't omit new line (depends on next character as well) bool JSBaker::isSpecialCharacterPrevious(QChar c) { - if (c == '\'' || c == '$' || c == '_' || c == '}' || c == ']' || c == ')' || c == '+' || c == '-' - || c == '"' || c == "'") { - return true; - } - return false; + return (c == '\'' || c == '$' || c == '_' || c == '}' || c == ']' || c == ')' || c == '+' || c == '-' + || c == '"' || c == "'"); } +// If next character is a special character, maybe don't omit new line (depends on previous character as well) bool JSBaker::isSpecialCharacterNext(QChar c) { - if (c == '\'' || c == '$' || c == '_' || c == '{' || c == '[' || c == '(' || c == '+' || c == '-') { - return true; - } - return false; + return (c == '\'' || c == '$' || c == '_' || c == '{' || c == '[' || c == '(' || c == '+' || c == '-'); } // Check if white space or tab bool JSBaker::isControlCharacter(QChar c) { - return (c == ' ' || (int)c.toLatin1() == 9); + return (c == ' ' || c == '\t'); } // Check If the currentCharacter is " or ' or ` From 1ba7aefd03f436238afd301accb1b85295e8e74b Mon Sep 17 00:00:00 2001 From: utkarshgautamnyu Date: Tue, 26 Sep 2017 09:57:02 -0700 Subject: [PATCH 08/36] Update JSBaker.h --- libraries/baking/src/JSBaker.h | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/libraries/baking/src/JSBaker.h b/libraries/baking/src/JSBaker.h index 4612fc5072..360dc18a98 100644 --- a/libraries/baking/src/JSBaker.h +++ b/libraries/baking/src/JSBaker.h @@ -25,21 +25,12 @@ public: public slots: virtual void bake() override; -signals: - void sourceJSLoaded(); - -private slots: - void importJS(); - private : QUrl _jsURL; QString _bakedOutputDir; - QDir _tempDir; - QString _originalJSFilePath; QString _bakedJSFilePath; - QByteArray _buffer; - void loadSourceJS(); + void importJS(); void bakeJS(QFile*); void exportJS(QFile*); From a9aab67a06fd58d289e6f975dfe4eff53960046b Mon Sep 17 00:00:00 2001 From: utkarshgautamnyu Date: Tue, 26 Sep 2017 11:01:26 -0700 Subject: [PATCH 09/36] Update JSBaker.h --- libraries/baking/src/JSBaker.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/baking/src/JSBaker.h b/libraries/baking/src/JSBaker.h index 360dc18a98..f64047fe8b 100644 --- a/libraries/baking/src/JSBaker.h +++ b/libraries/baking/src/JSBaker.h @@ -45,7 +45,7 @@ private : bool isSpecialCharacter(QChar c); bool isSpecialCharacterPrevious(QChar c); bool isSpecialCharacterNext(QChar c); - bool isControlCharacter(QChar); + bool isSpaceOrTab(QChar); bool isQuote(QChar); }; From 0b387f5b38c3d81e4fb2140bc360403138b08af5 Mon Sep 17 00:00:00 2001 From: utkarshgautamnyu Date: Tue, 26 Sep 2017 11:01:56 -0700 Subject: [PATCH 10/36] Update JSBaker.cpp --- libraries/baking/src/JSBaker.cpp | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/libraries/baking/src/JSBaker.cpp b/libraries/baking/src/JSBaker.cpp index 1286a5592e..630b7ae78e 100644 --- a/libraries/baking/src/JSBaker.cpp +++ b/libraries/baking/src/JSBaker.cpp @@ -34,7 +34,7 @@ void JSBaker::importJS() { // Import the file to be processed from _jsURL QFile jsFile(_jsURL.toLocalFile()); if (!jsFile.open(QIODevice::ReadOnly | QIODevice::Text)) { - handleError("Error opening " + _originalJSFilePath + " for reading"); + handleError("Error opening " + _jsURL.fileName() + " for reading"); return; } @@ -85,19 +85,17 @@ void JSBaker::bakeJS(QFile* inputFile) { // If '/' is not followed by '/' or '*' print '/' out << currentCharacter; } - } else if (isControlCharacter(currentCharacter)) { + } else if (isSpaceOrTab(currentCharacter)) { // Check if white space or tab // Skip multiple spaces or tabs - if (isControlCharacter(nextCharacter)) { - while (isControlCharacter(nextCharacter)) { - in >> nextCharacter; - if (nextCharacter == '\n') { - break; - } + while (isSpaceOrTab(nextCharacter)) { + in >> nextCharacter; + if (nextCharacter == '\n') { + break; } } - + // check if space can be omitted if (!canOmitSpace(previousCharacter, nextCharacter)) { out << ' '; @@ -107,7 +105,7 @@ void JSBaker::bakeJS(QFile* inputFile) { //Skip multiple new lines //Skip new line followed by space or tab - while (nextCharacter == '\n' || isControlCharacter(nextCharacter)) { + while (nextCharacter == '\n' || isSpaceOrTab(nextCharacter)) { in >> nextCharacter; } @@ -246,7 +244,7 @@ bool JSBaker::isSpecialCharacterNext(QChar c) { } // Check if white space or tab -bool JSBaker::isControlCharacter(QChar c) { +bool JSBaker::isSpaceOrTab(QChar c) { return (c == ' ' || c == '\t'); } From 0260749b6b6b1f70ab41bb50d31e6f1c03896ccb Mon Sep 17 00:00:00 2001 From: utkarshgautamnyu Date: Tue, 26 Sep 2017 11:02:22 -0700 Subject: [PATCH 11/36] Update AssetServer.cpp --- assignment-client/src/assets/AssetServer.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/assignment-client/src/assets/AssetServer.cpp b/assignment-client/src/assets/AssetServer.cpp index a2158bc289..63acffa187 100644 --- a/assignment-client/src/assets/AssetServer.cpp +++ b/assignment-client/src/assets/AssetServer.cpp @@ -99,7 +99,7 @@ std::pair AssetServer::getAssetStatus(const AssetPath& pa QString bakedFilename; if (BAKEABLE_MODEL_EXTENSIONS.contains(extension)) { - bakedFilename = BAKED_MODEL_SIMPLE_NAME; + bakedFilename = BAKED_MODEL_SIMPLE_NAME; } else if (BAKEABLE_TEXTURE_EXTENSIONS.contains(extension.toLocal8Bit()) && hasMetaFile(hash)) { bakedFilename = BAKED_TEXTURE_SIMPLE_NAME; } else if (BAKEABLE_SCRIPT_EXTENSIONS.contains(extension)) { @@ -1214,15 +1214,14 @@ void AssetServer::handleCompletedBake(QString originalAssetHash, QString origina // setup the mapping for this bake file auto relativeFilePath = QUrl(filePath).fileName(); qDebug() << "Relative file path is: " << relativeFilePath; - if (relativeFilePath.endsWith(".js", Qt::CaseInsensitive)) { - relativeFilePath = BAKED_ASSET_SIMPLE_JS_NAME; - } else if (relativeFilePath.endsWith(".fbx", Qt::CaseInsensitive)) { + 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); From e81501a4d13c2b186af523af3a68ed28ad40236a Mon Sep 17 00:00:00 2001 From: utkarshgautamnyu Date: Tue, 26 Sep 2017 11:08:48 -0700 Subject: [PATCH 12/36] Update AssetServer.cpp --- assignment-client/src/assets/AssetServer.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/assignment-client/src/assets/AssetServer.cpp b/assignment-client/src/assets/AssetServer.cpp index 63acffa187..ae81976668 100644 --- a/assignment-client/src/assets/AssetServer.cpp +++ b/assignment-client/src/assets/AssetServer.cpp @@ -50,7 +50,7 @@ static const int INTERFACE_RUNNING_CHECK_FREQUENCY_MS = 1000; const QString ASSET_SERVER_LOGGING_TARGET_NAME = "asset-server"; -static const QStringList BAKEABLE_MODEL_EXTENSIONS = { "fbx"}; +static const QStringList BAKEABLE_MODEL_EXTENSIONS = {"fbx"}; static QStringList BAKEABLE_TEXTURE_EXTENSIONS; static const QStringList BAKEABLE_SCRIPT_EXTENSIONS = {"js"}; static const QString BAKED_MODEL_SIMPLE_NAME = "asset.fbx"; @@ -99,7 +99,7 @@ std::pair AssetServer::getAssetStatus(const AssetPath& pa QString bakedFilename; if (BAKEABLE_MODEL_EXTENSIONS.contains(extension)) { - bakedFilename = BAKED_MODEL_SIMPLE_NAME; + bakedFilename = BAKED_MODEL_SIMPLE_NAME; } else if (BAKEABLE_TEXTURE_EXTENSIONS.contains(extension.toLocal8Bit()) && hasMetaFile(hash)) { bakedFilename = BAKED_TEXTURE_SIMPLE_NAME; } else if (BAKEABLE_SCRIPT_EXTENSIONS.contains(extension)) { From 258c32f9aa88a6fb98778da9d126fee26853436f Mon Sep 17 00:00:00 2001 From: utkarshgautamnyu Date: Tue, 26 Sep 2017 11:52:25 -0700 Subject: [PATCH 13/36] Update AssetServer.cpp --- assignment-client/src/assets/AssetServer.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/assignment-client/src/assets/AssetServer.cpp b/assignment-client/src/assets/AssetServer.cpp index ae81976668..9e3732762d 100644 --- a/assignment-client/src/assets/AssetServer.cpp +++ b/assignment-client/src/assets/AssetServer.cpp @@ -104,8 +104,7 @@ std::pair AssetServer::getAssetStatus(const AssetPath& pa bakedFilename = BAKED_TEXTURE_SIMPLE_NAME; } else if (BAKEABLE_SCRIPT_EXTENSIONS.contains(extension)) { bakedFilename = BAKED_SCRIPT_SIMPLE_NAME; - } - else { + } else { return { Irrelevant, "" }; } From 4fd045269400ee628a97122253d2bc32b678688e Mon Sep 17 00:00:00 2001 From: utkarshgautamnyu Date: Tue, 26 Sep 2017 12:04:03 -0700 Subject: [PATCH 14/36] Update JSBaker.cpp --- libraries/baking/src/JSBaker.cpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/libraries/baking/src/JSBaker.cpp b/libraries/baking/src/JSBaker.cpp index 630b7ae78e..17c12c4d5e 100644 --- a/libraries/baking/src/JSBaker.cpp +++ b/libraries/baking/src/JSBaker.cpp @@ -76,7 +76,9 @@ void JSBaker::bakeJS(QFile* inputFile) { } else if (nextCharacter == '*') { // Check if multi line comment i.e. /* handleMultiLineComments(&in); - + if (hasErrors()) { + return; + } //Start fresh after handling comments previousCharacter = '\n'; in >> currentCharacter; @@ -147,13 +149,9 @@ void JSBaker::bakeJS(QFile* inputFile) { // Reading done. Closing the inputFile inputFile->close(); - - if (hasErrors()) { - return; - } else { - // Bake successful, Export the compressed outputFile - exportJS(&outputFile); - } + + // Bake successful, Export the compressed outputFile + exportJS(&outputFile); } void JSBaker::exportJS(QFile* bakedFile) { From 167dda9f80a5d0f3d31ad7db72ce9796da1d0da8 Mon Sep 17 00:00:00 2001 From: utkarshgautamnyu Date: Tue, 26 Sep 2017 19:35:54 -0700 Subject: [PATCH 15/36] made bakeJS a static function --- libraries/baking/src/JSBaker.cpp | 103 +++++++++++++++---------------- 1 file changed, 49 insertions(+), 54 deletions(-) diff --git a/libraries/baking/src/JSBaker.cpp b/libraries/baking/src/JSBaker.cpp index 17c12c4d5e..478a460cfa 100644 --- a/libraries/baking/src/JSBaker.cpp +++ b/libraries/baking/src/JSBaker.cpp @@ -26,30 +26,53 @@ JSBaker::JSBaker(const QUrl& jsURL, const QString& bakedOutputDir) : void JSBaker::bake() { qCDebug(js_baking) << "JS Baker " << _jsURL << "bake starting"; - // Import the script to start baking - importJS(); -} - -void JSBaker::importJS() { - // Import the file to be processed from _jsURL + // Import file to start baking QFile jsFile(_jsURL.toLocalFile()); if (!jsFile.open(QIODevice::ReadOnly | QIODevice::Text)) { handleError("Error opening " + _jsURL.fileName() + " for reading"); return; } - // Import successful, Call the baking function with the imported file - bakeJS(&jsFile); + // Read file into an array + QByteArray inputJS = jsFile.readAll(); + QByteArray outputJS; + + // Call baking on inputJS and store result in outputJS + bool success = bakeJS(&inputJS, &outputJS); + if (!success) { + qCDebug(js_baking) << "Bake Failed"; + handleError("Eror unterminated multi line comment"); + return; + } + + // Bake Successful. Export the file + auto fileName = _jsURL.fileName(); + auto baseName = fileName.left(fileName.lastIndexOf('.')); + auto bakedFilename = baseName + BAKED_JS_EXTENSION; + + _bakedJSFilePath = _bakedOutputDir + "/" + bakedFilename; + + QFile bakedFile; + bakedFile.setFileName(_bakedJSFilePath); + if (!bakedFile.open(QIODevice::WriteOnly)) { + handleError("Error opening " + _bakedJSFilePath + " for writing"); + return; + } + + bakedFile.write(outputJS); + + // Export successful + _outputFiles.push_back(_bakedJSFilePath); + qCDebug(js_baking) << "Exported" << _jsURL << "with re-written paths to" << _bakedJSFilePath; + + // emit signal to indicate the JS baking is finished + emit finished(); } -void JSBaker::bakeJS(QFile* inputFile) { - // Create an output file which will be exported as the bakedFile - QFile outputFile; - outputFile.open(QIODevice::WriteOnly | QIODevice::Text); - +bool JSBaker::bakeJS(QByteArray* inputFile, QByteArray* outputFile) { // Read from inputFile and write to outputFile per character - QTextStream in(inputFile); - QTextStream out(&outputFile); + QTextStream in(inputFile,QIODevice::ReadOnly); + QTextStream out(outputFile, QIODevice::WriteOnly); // Algorithm requires the knowledge of previous and next character for each character read QChar currentCharacter; @@ -75,9 +98,10 @@ void JSBaker::bakeJS(QFile* inputFile) { continue; } else if (nextCharacter == '*') { // Check if multi line comment i.e. /* - handleMultiLineComments(&in); - if (hasErrors()) { - return; + bool success = handleMultiLineComments(&in); + if (!success) { + // Errors present return false + return false; } //Start fresh after handling comments previousCharacter = '\n'; @@ -119,7 +143,7 @@ void JSBaker::bakeJS(QFile* inputFile) { // Print the current quote and nextCharacter as is out << currentCharacter; out << nextCharacter; - + // Store the type of quote we are processing QChar quote = currentCharacter; @@ -135,7 +159,7 @@ void JSBaker::bakeJS(QFile* inputFile) { continue; } else { // In all other cases write the currentCharacter to outputFile - out << currentCharacter; + out << currentCharacter; } previousCharacter = currentCharacter; @@ -147,37 +171,8 @@ void JSBaker::bakeJS(QFile* inputFile) { out << currentCharacter; } - // Reading done. Closing the inputFile - inputFile->close(); - - // Bake successful, Export the compressed outputFile - exportJS(&outputFile); -} - -void JSBaker::exportJS(QFile* bakedFile) { - // save the relative path to this JS inside the output folder - auto fileName = _jsURL.fileName(); - auto baseName = fileName.left(fileName.lastIndexOf('.')); - auto bakedFilename = baseName + BAKED_JS_EXTENSION; - - _bakedJSFilePath = _bakedOutputDir + "/" + bakedFilename; - - bakedFile->setFileName(_bakedJSFilePath); - - if (!bakedFile->open(QIODevice::WriteOnly)) { - handleError("Error opening " + _bakedJSFilePath + " for writing"); - return; - } - - // Export successful - _outputFiles.push_back(_bakedJSFilePath); - qCDebug(js_baking) << "Exported" << _jsURL << "with re-written paths to" << _bakedJSFilePath; - - // Exporting done. Closing the outputFile - bakedFile->close(); - - // emit signal to indicate the JS baking is finished - emit finished(); + // Successful bake. Return true + return true; } void JSBaker::handleSingleLineComments(QTextStream * in) { @@ -190,17 +185,17 @@ void JSBaker::handleSingleLineComments(QTextStream * in) { } } -void JSBaker::handleMultiLineComments(QTextStream * in) { +bool JSBaker::handleMultiLineComments(QTextStream * in) { QChar character; while (!in->atEnd()) { *in >> character; if (character == '*') { if (in->read(1) == '/') { - return; + return true; } } } - handleError("Eror unterminated multi line comment"); + return false; } bool JSBaker::canOmitSpace(QChar previousCharacter, QChar nextCharacter) { From 70a39f03f7f79e9abab5f8bbf635ad9cc57b34e7 Mon Sep 17 00:00:00 2001 From: utkarshgautamnyu Date: Tue, 26 Sep 2017 19:36:41 -0700 Subject: [PATCH 16/36] Update JSBaker.h --- libraries/baking/src/JSBaker.h | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/libraries/baking/src/JSBaker.h b/libraries/baking/src/JSBaker.h index f64047fe8b..06bbd75e43 100644 --- a/libraries/baking/src/JSBaker.h +++ b/libraries/baking/src/JSBaker.h @@ -30,23 +30,21 @@ private : QString _bakedOutputDir; QString _bakedJSFilePath; - void importJS(); - void bakeJS(QFile*); - void exportJS(QFile*); + static bool bakeJS(QByteArray*, QByteArray*); - void handleSingleLineComments(QTextStream*); - void handleMultiLineComments(QTextStream*); + static void handleSingleLineComments(QTextStream*); + static bool handleMultiLineComments(QTextStream*); - bool canOmitSpace(QChar, QChar); - bool canOmitNewLine(QChar, QChar); + static bool canOmitSpace(QChar, QChar); + static bool canOmitNewLine(QChar, QChar); - bool isAlphanum(QChar); - bool isNonAscii(QChar c); - bool isSpecialCharacter(QChar c); - bool isSpecialCharacterPrevious(QChar c); - bool isSpecialCharacterNext(QChar c); - bool isSpaceOrTab(QChar); - bool isQuote(QChar); + static bool isAlphanum(QChar); + static bool isNonAscii(QChar c); + static bool isSpecialCharacter(QChar c); + static bool isSpecialCharacterPrevious(QChar c); + static bool isSpecialCharacterNext(QChar c); + static bool isSpaceOrTab(QChar); + static bool isQuote(QChar); }; #endif // !hifi_JSBaker_h From d4b8559cb2addc90acb9e11083306dcd7b4255aa Mon Sep 17 00:00:00 2001 From: utkarshgautamnyu Date: Wed, 27 Sep 2017 10:49:10 -0700 Subject: [PATCH 17/36] Update JSBaker.cpp --- libraries/baking/src/JSBaker.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/baking/src/JSBaker.cpp b/libraries/baking/src/JSBaker.cpp index 478a460cfa..34820acccf 100644 --- a/libraries/baking/src/JSBaker.cpp +++ b/libraries/baking/src/JSBaker.cpp @@ -41,7 +41,7 @@ void JSBaker::bake() { bool success = bakeJS(&inputJS, &outputJS); if (!success) { qCDebug(js_baking) << "Bake Failed"; - handleError("Eror unterminated multi line comment"); + handleError("Error unterminated multi line comment"); return; } @@ -69,9 +69,9 @@ void JSBaker::bake() { emit finished(); } -bool JSBaker::bakeJS(QByteArray* inputFile, QByteArray* outputFile) { +bool JSBaker::bakeJS(const QByteArray* inputFile, QByteArray* outputFile) { // Read from inputFile and write to outputFile per character - QTextStream in(inputFile,QIODevice::ReadOnly); + QTextStream in(*inputFile,QIODevice::ReadOnly); QTextStream out(outputFile, QIODevice::WriteOnly); // Algorithm requires the knowledge of previous and next character for each character read From 5ad1efb9234b3b752f2368354dd70383fce820e7 Mon Sep 17 00:00:00 2001 From: utkarshgautamnyu Date: Wed, 27 Sep 2017 10:49:48 -0700 Subject: [PATCH 18/36] Update JSBaker.h --- libraries/baking/src/JSBaker.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/baking/src/JSBaker.h b/libraries/baking/src/JSBaker.h index 06bbd75e43..b5c48476aa 100644 --- a/libraries/baking/src/JSBaker.h +++ b/libraries/baking/src/JSBaker.h @@ -30,7 +30,7 @@ private : QString _bakedOutputDir; QString _bakedJSFilePath; - static bool bakeJS(QByteArray*, QByteArray*); + static bool bakeJS(const QByteArray*, QByteArray*); static void handleSingleLineComments(QTextStream*); static bool handleMultiLineComments(QTextStream*); From 4486212e0513d31efa2fbaac19db930e6c85219b Mon Sep 17 00:00:00 2001 From: utkarshgautamnyu Date: Wed, 27 Sep 2017 15:20:30 -0700 Subject: [PATCH 19/36] Update JSBaker.cpp --- libraries/baking/src/JSBaker.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/baking/src/JSBaker.cpp b/libraries/baking/src/JSBaker.cpp index 34820acccf..079650d6d1 100644 --- a/libraries/baking/src/JSBaker.cpp +++ b/libraries/baking/src/JSBaker.cpp @@ -175,7 +175,7 @@ bool JSBaker::bakeJS(const QByteArray* inputFile, QByteArray* outputFile) { return true; } -void JSBaker::handleSingleLineComments(QTextStream * in) { +void JSBaker::handleSingleLineComments(QTextStream* in) { QChar character; while (!in->atEnd()) { *in >> character; @@ -185,7 +185,7 @@ void JSBaker::handleSingleLineComments(QTextStream * in) { } } -bool JSBaker::handleMultiLineComments(QTextStream * in) { +bool JSBaker::handleMultiLineComments(QTextStream* in) { QChar character; while (!in->atEnd()) { *in >> character; From fbe3f2fbe3a6094280bfc2bb10fb233ba0f3c86d Mon Sep 17 00:00:00 2001 From: utkarshgautamnyu Date: Wed, 27 Sep 2017 15:26:17 -0700 Subject: [PATCH 20/36] Update JSBaker.h --- libraries/baking/src/JSBaker.h | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/libraries/baking/src/JSBaker.h b/libraries/baking/src/JSBaker.h index b5c48476aa..695785a730 100644 --- a/libraries/baking/src/JSBaker.h +++ b/libraries/baking/src/JSBaker.h @@ -23,28 +23,29 @@ public: JSBaker(const QUrl& jsURL, const QString& bakedOutputDir); public slots: - virtual void bake() override; + virtual void bake() override; + +public: + static bool bakeJS(const QByteArray* inputFile, QByteArray* outputFile); private : QUrl _jsURL; QString _bakedOutputDir; QString _bakedJSFilePath; - static bool bakeJS(const QByteArray*, QByteArray*); + static void handleSingleLineComments(QTextStream* in); + static bool handleMultiLineComments(QTextStream* in); - static void handleSingleLineComments(QTextStream*); - static bool handleMultiLineComments(QTextStream*); + static bool canOmitSpace(QChar previousCharacter, QChar nextCharacter); + static bool canOmitNewLine(QChar previousCharacter, QChar nextCharacter); - static bool canOmitSpace(QChar, QChar); - static bool canOmitNewLine(QChar, QChar); - - static bool isAlphanum(QChar); + static bool isAlphanum(QChar c); static bool isNonAscii(QChar c); static bool isSpecialCharacter(QChar c); static bool isSpecialCharacterPrevious(QChar c); static bool isSpecialCharacterNext(QChar c); - static bool isSpaceOrTab(QChar); - static bool isQuote(QChar); + static bool isSpaceOrTab(QChar c); + static bool isQuote(QChar c); }; #endif // !hifi_JSBaker_h From e4c25c0aa1569e5bdcbdc4640ab3138beb4771b1 Mon Sep 17 00:00:00 2001 From: utkarshgautamnyu Date: Wed, 27 Sep 2017 18:04:08 -0700 Subject: [PATCH 21/36] Create CMakeLists --- tests/baking/CMakeLists | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 tests/baking/CMakeLists diff --git a/tests/baking/CMakeLists b/tests/baking/CMakeLists new file mode 100644 index 0000000000..40217eded4 --- /dev/null +++ b/tests/baking/CMakeLists @@ -0,0 +1,10 @@ + +# Declare dependencies +macro (setup_testcase_dependencies) + # link in the shared libraries + link_hifi_libraries(shared baking) + + package_libraries_for_deployment() +endmacro () + +setup_hifi_testcase() From 354fa12d365521c37e3a3d34ef7f2cc98d8c027d Mon Sep 17 00:00:00 2001 From: utkarshgautamnyu Date: Wed, 27 Sep 2017 18:05:28 -0700 Subject: [PATCH 22/36] Create JSBakerTest.h --- tests/baking/src/JSBakerTest.h | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 tests/baking/src/JSBakerTest.h diff --git a/tests/baking/src/JSBakerTest.h b/tests/baking/src/JSBakerTest.h new file mode 100644 index 0000000000..a1d1f73e9f --- /dev/null +++ b/tests/baking/src/JSBakerTest.h @@ -0,0 +1,29 @@ +// +// JSBakerTest.h +// tests/networking/src +// +// Created by Utkarsh Gautam on 9/26/17. +// Copyright 2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_JSBakerTest_h +#define hifi_JSBakerTest_h + +#include +#include "../../libraries/baking/src/JSBaker.h" + +class JSBakerTest: public QObject { + Q_OBJECT + +private slots: + void setTestCases(); + void testJSBaking(); + +private: + std::vector> _testCases; +}; + +#endif From 290e3d307c36a6e057ee6241b916c95f15115849 Mon Sep 17 00:00:00 2001 From: utkarshgautamnyu Date: Wed, 27 Sep 2017 18:06:35 -0700 Subject: [PATCH 23/36] Create JSBakerTest.cpp --- tests/baking/src/JSBakerTest.cpp | 89 ++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 tests/baking/src/JSBakerTest.cpp diff --git a/tests/baking/src/JSBakerTest.cpp b/tests/baking/src/JSBakerTest.cpp new file mode 100644 index 0000000000..c3398a8d67 --- /dev/null +++ b/tests/baking/src/JSBakerTest.cpp @@ -0,0 +1,89 @@ +// +// JSBakerTest.cpp +// tests/networking/src +// +// Created by Utkarsh Gautam on 09/26/17. +// Copyright 2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include "JSBakerTest.h" +QTEST_MAIN(JSBakerTest) + +void JSBakerTest::setTestCases() { + // Test cases contain a std::pair(input, desiredOutput) + + _testCases.emplace_back("var a=1;", "var a=1;"); + _testCases.emplace_back("var a=1;//single line comment\nvar b=2;", "var a=1;var b=2;"); + _testCases.emplace_back("a\rb", "a\nb"); + _testCases.emplace_back("a/*multi\n line \n comment*/ b", "ab"); + _testCases.emplace_back("a/b", "a/b"); + _testCases.emplace_back("var a = 1;", "var a=1;"); // Multiple spaces omitted + _testCases.emplace_back("var a= 1;", "var a=1;"); // Multiple tabs omitted + + // Cases for space not omitted + _testCases.emplace_back("var x", "var x"); + _testCases.emplace_back("a '", "a '"); + _testCases.emplace_back("a $", "a $"); + _testCases.emplace_back("a _", "a _"); + _testCases.emplace_back("a /", "a /"); + _testCases.emplace_back("a 1", "a 1"); + _testCases.emplace_back("1 a", "1 a"); + _testCases.emplace_back("$ a", "$ a"); + _testCases.emplace_back("_ a", "_ a"); + _testCases.emplace_back("/ a", "/ a"); + _testCases.emplace_back("$ $", "$ $"); + _testCases.emplace_back("_ _", "_ _"); + _testCases.emplace_back("/ /", "/ /"); + + _testCases.emplace_back("a\n\n\n\nb", "a\nb"); // Skip multiple new lines + _testCases.emplace_back("a\n\n b", "a\nb"); // Skip multiple new lines followed by whitespace + _testCases.emplace_back("a\n\n b", "a\nb"); // Skip multiple new lines followed by tab + + //Cases for new line not omitted + _testCases.emplace_back("a\nb", "a\nb"); + _testCases.emplace_back("a\n9", "a\n9"); + _testCases.emplace_back("9\na", "9\na"); + _testCases.emplace_back("a\n$", "a\n$"); + _testCases.emplace_back("a\n[", "a\n["); + _testCases.emplace_back("a\n{", "a\n{"); + _testCases.emplace_back("a\n(", "a\n("); + _testCases.emplace_back("a\n+", "a\n+"); + _testCases.emplace_back("a\n'", "a\n'"); + _testCases.emplace_back("a\n-", "a\n-"); + _testCases.emplace_back("$\na", "$\na"); + _testCases.emplace_back("$\na", "$\na"); + _testCases.emplace_back("_\na", "_\na"); + _testCases.emplace_back("]\na", "]\na"); + _testCases.emplace_back("}\na", "}\na"); + _testCases.emplace_back(")\na", ")\na"); + _testCases.emplace_back("+\na", "+\na"); + _testCases.emplace_back("-\na", "-\na"); + + // Cases to check quoted strings are not modified + _testCases.emplace_back("'abcd1234$%^&[](){}'\na", "'abcd1234$%^&[](){}'\na"); + _testCases.emplace_back("\"abcd1234$%^&[](){}\"\na", "\"abcd1234$%^&[](){}\"\na"); + _testCases.emplace_back("`abcd1234$%^&[](){}`\na", "`abcd1234$%^&[](){}`a"); + + // Edge Cases + + //No semicolon to terminate an expression, instead a new line used for termination + _testCases.emplace_back("var x=5\nvar y=6;", "var x=5\nvar y=6;"); + + //a + ++b is minified as a+++b, interpreted as a++ + b. Ensure original script has correct parantheses + _testCases.emplace_back("a + ++b", "a+++b"); +} + +void JSBakerTest::testJSBaking() { + + for (int i = 0;i < _testCases.size();i++) { + QByteArray output; + auto input = _testCases.at(i).first; + JSBaker::bakeJS(&input, &output); + + auto desiredOutput = _testCases.at(i).second; + QCOMPARE(output, desiredOutput); + } +} From 463afd6be570e37397b10d4cd9089fd9e336d30a Mon Sep 17 00:00:00 2001 From: utkarshgautamnyu Date: Thu, 28 Sep 2017 11:12:26 -0700 Subject: [PATCH 24/36] made updates to fix building --- assignment-client/src/assets/AssetServer.cpp | 65 +++++++++---------- .../src/assets/BakeAssetTask.cpp | 9 ++- libraries/baking/src/JSBaker.cpp | 55 ++++++++-------- libraries/baking/src/JSBaker.h | 6 +- tests/baking/{CMakeLists => CMakeLists.txt} | 0 tests/baking/src/JSBakerTest.cpp | 16 ++--- tests/baking/src/JSBakerTest.h | 6 +- 7 files changed, 77 insertions(+), 80 deletions(-) rename tests/baking/{CMakeLists => CMakeLists.txt} (100%) diff --git a/assignment-client/src/assets/AssetServer.cpp b/assignment-client/src/assets/AssetServer.cpp index 9e3732762d..919cec6e2d 100644 --- a/assignment-client/src/assets/AssetServer.cpp +++ b/assignment-client/src/assets/AssetServer.cpp @@ -50,9 +50,9 @@ static const int INTERFACE_RUNNING_CHECK_FREQUENCY_MS = 1000; const QString ASSET_SERVER_LOGGING_TARGET_NAME = "asset-server"; -static const QStringList BAKEABLE_MODEL_EXTENSIONS = {"fbx"}; +static const QStringList BAKEABLE_MODEL_EXTENSIONS = { "fbx" }; static QStringList BAKEABLE_TEXTURE_EXTENSIONS; -static const QStringList BAKEABLE_SCRIPT_EXTENSIONS = {"js"}; +static const QStringList BAKEABLE_SCRIPT_EXTENSIONS = { "js" }; static const QString BAKED_MODEL_SIMPLE_NAME = "asset.fbx"; static const QString BAKED_TEXTURE_SIMPLE_NAME = "texture.ktx"; static const QString BAKED_SCRIPT_SIMPLE_NAME = "asset.js"; @@ -125,7 +125,7 @@ std::pair AssetServer::getAssetStatus(const AssetPath& pa return { Error, meta.lastBakeErrors }; } } - + return { Pending, "" }; } @@ -147,8 +147,8 @@ void AssetServer::maybeBake(const AssetPath& path, const AssetHash& hash) { void AssetServer::createEmptyMetaFile(const AssetHash& hash) { QString metaFilePath = "atp:/" + hash + "/meta.json"; - QFile metaFile { metaFilePath }; - + QFile metaFile{ metaFilePath }; + if (!metaFile.exists()) { qDebug() << "Creating metafile for " << hash; if (metaFile.open(QFile::WriteOnly)) { @@ -205,7 +205,7 @@ bool interfaceRunning() { bool result = false; #ifdef Q_OS_WIN - QSharedMemory sharedMemory { getInterfaceSharedMemoryName() }; + QSharedMemory sharedMemory{ getInterfaceSharedMemoryName() }; result = sharedMemory.attach(QSharedMemory::ReadOnly); if (result) { sharedMemory.detach(); @@ -226,7 +226,7 @@ void updateConsumedCores() { auto coreCount = std::thread::hardware_concurrency(); if (isInterfaceRunning) { coreCount = coreCount > MIN_CORES_FOR_MULTICORE ? CPU_AFFINITY_COUNT_HIGH : CPU_AFFINITY_COUNT_LOW; - } + } qCDebug(asset_server) << "Setting max consumed cores to " << coreCount; setMaxCores(coreCount); } @@ -235,8 +235,7 @@ void updateConsumedCores() { AssetServer::AssetServer(ReceivedMessage& message) : ThreadedAssignment(message), _transferTaskPool(this), - _bakingTaskPool(this) -{ + _bakingTaskPool(this) { // store the current state of image compression so we can reset it when this assignment is complete _wasColorTextureCompressionEnabled = image::isColorTexturesCompressionEnabled(); _wasGrayscaleTextureCompressionEnabled = image::isGrayscaleTexturesCompressionEnabled(); @@ -263,7 +262,7 @@ AssetServer::AssetServer(ReceivedMessage& message) : packetReceiver.registerListener(PacketType::AssetGetInfo, this, "handleAssetGetInfo"); packetReceiver.registerListener(PacketType::AssetUpload, this, "handleAssetUpload"); packetReceiver.registerListener(PacketType::AssetMappingOperation, this, "handleAssetMappingOperation"); - + #ifdef Q_OS_WIN updateConsumedCores(); QTimer* timer = new QTimer(this); @@ -287,7 +286,7 @@ void AssetServer::aboutToFinish() { // abort each of our still running bake tasks, remove pending bakes that were never put on the thread pool auto it = _pendingBakes.begin(); while (it != _pendingBakes.end()) { - auto pendingRunnable = _bakingTaskPool.tryTake(it->get()); + auto pendingRunnable = _bakingTaskPool.tryTake(it->get()); if (pendingRunnable) { it = _pendingBakes.erase(it); @@ -348,7 +347,7 @@ void AssetServer::completeSetup() { int maxBandwidth = maxBandwidthFloat * BITS_PER_MEGABITS; nodeList->setConnectionMaxBandwidth(maxBandwidth); qCInfo(asset_server) << "Set maximum bandwith per connection to" << maxBandwidthFloat << "Mb/s." - " (" << maxBandwidth << "bits/s)"; + " (" << maxBandwidth << "bits/s)"; } // get the path to the asset folder from the domain server settings @@ -362,7 +361,7 @@ void AssetServer::completeSetup() { } auto assetsPathString = assetsJSONValue.toString(); - QDir assetsPath { assetsPathString }; + QDir assetsPath{ assetsPathString }; QString absoluteFilePath = assetsPath.absolutePath(); if (assetsPath.isRelative()) { @@ -390,7 +389,7 @@ void AssetServer::completeSetup() { // Check the asset directory to output some information about what we have auto files = _filesDirectory.entryList(QDir::Files); - QRegExp hashFileRegex { ASSET_HASH_REGEX_STRING }; + QRegExp hashFileRegex{ ASSET_HASH_REGEX_STRING }; auto hashedFiles = files.filter(hashFileRegex); qCInfo(asset_server) << "There are" << hashedFiles.size() << "asset files in the asset directory."; @@ -409,7 +408,7 @@ void AssetServer::completeSetup() { } void AssetServer::cleanupUnmappedFiles() { - QRegExp hashFileRegex { "^[a-f0-9]{" + QString::number(SHA256_HASH_HEX_LENGTH) + "}" }; + QRegExp hashFileRegex{ "^[a-f0-9]{" + QString::number(SHA256_HASH_HEX_LENGTH) + "}" }; auto files = _filesDirectory.entryInfoList(QDir::Files); @@ -418,7 +417,7 @@ void AssetServer::cleanupUnmappedFiles() { for (const auto& fileInfo : files) { auto filename = fileInfo.fileName(); if (hashFileRegex.exactMatch(filename)) { - bool matched { false }; + bool matched{ false }; for (auto& pair : _fileMappings) { if (pair.second == filename) { matched = true; @@ -427,7 +426,7 @@ void AssetServer::cleanupUnmappedFiles() { } if (!matched) { // remove the unmapped file - QFile removeableFile { fileInfo.absoluteFilePath() }; + QFile removeableFile{ fileInfo.absoluteFilePath() }; if (removeableFile.remove()) { qCDebug(asset_server) << "\tDeleted" << filename << "from asset files directory since it is unmapped."; @@ -479,7 +478,7 @@ void AssetServer::handleAssetMappingOperation(QSharedPointer me void AssetServer::handleGetMappingOperation(ReceivedMessage& message, SharedNodePointer senderNode, NLPacketList& replyPacket) { QString assetPath = message.readString(); - QUrl url { assetPath }; + QUrl url{ assetPath }; assetPath = url.path(); auto it = _fileMappings.find(assetPath); @@ -498,7 +497,7 @@ void AssetServer::handleGetMappingOperation(ReceivedMessage& message, SharedNode } else if (BAKEABLE_SCRIPT_EXTENSIONS.contains(assetPathExtension)) { bakedRootFile = BAKED_SCRIPT_SIMPLE_NAME; } - + auto originalAssetHash = it->second; QString redirectedAssetHash; QString bakedAssetPath; @@ -562,7 +561,7 @@ void AssetServer::handleGetAllMappingOperation(ReceivedMessage& message, SharedN replyPacket.writePrimitive(count); - for (auto it = _fileMappings.cbegin(); it != _fileMappings.cend(); ++ it) { + for (auto it = _fileMappings.cbegin(); it != _fileMappings.cend(); ++it) { auto mapping = it->first; auto hash = it->second; replyPacket.writeString(mapping); @@ -603,7 +602,7 @@ void AssetServer::handleSetMappingOperation(ReceivedMessage& message, SharedNode void AssetServer::handleDeleteMappingsOperation(ReceivedMessage& message, SharedNodePointer senderNode, NLPacketList& replyPacket) { if (senderNode->getCanWriteToAssetServer()) { - int numberOfDeletedMappings { 0 }; + int numberOfDeletedMappings{ 0 }; message.readPrimitive(&numberOfDeletedMappings); QStringList mappingsToDelete; @@ -653,7 +652,7 @@ void AssetServer::handleRenameMappingOperation(ReceivedMessage& message, SharedN void AssetServer::handleSetBakingEnabledOperation(ReceivedMessage& message, SharedNodePointer senderNode, NLPacketList& replyPacket) { if (senderNode->getCanWriteToAssetServer()) { - bool enabled { true }; + bool enabled{ true }; message.readPrimitive(&enabled); int numberOfMappings{ 0 }; @@ -696,7 +695,7 @@ void AssetServer::handleAssetGetInfo(QSharedPointer message, Sh replyPacket->write(assetHash); QString fileName = QString(hexHash); - QFileInfo fileInfo { _filesDirectory.filePath(fileName) }; + QFileInfo fileInfo{ _filesDirectory.filePath(fileName) }; if (fileInfo.exists() && fileInfo.isReadable()) { qCDebug(asset_server) << "Opening file: " << fileInfo.filePath(); @@ -827,7 +826,7 @@ bool AssetServer::loadMappingsFromFile() { auto mapFilePath = _resourcesDirectory.absoluteFilePath(MAP_FILE_NAME); - QFile mapFile { mapFilePath }; + QFile mapFile{ mapFilePath }; if (mapFile.exists()) { if (mapFile.open(QIODevice::ReadOnly)) { QJsonParseError error; @@ -883,7 +882,7 @@ bool AssetServer::loadMappingsFromFile() { bool AssetServer::writeMappingsToFile() { auto mapFilePath = _resourcesDirectory.absoluteFilePath(MAP_FILE_NAME); - QFile mapFile { mapFilePath }; + QFile mapFile{ mapFilePath }; if (mapFile.open(QIODevice::WriteOnly)) { QJsonObject root; @@ -891,7 +890,7 @@ bool AssetServer::writeMappingsToFile() { root[it.first] = it.second; } - QJsonDocument jsonDocument { root }; + QJsonDocument jsonDocument{ root }; if (mapFile.write(jsonDocument.toJson()) != -1) { qCDebug(asset_server) << "Wrote JSON mappings to file at" << mapFilePath; @@ -955,7 +954,7 @@ void AssetServer::removeBakedPathsForDeletedAsset(AssetHash hash) { // check if we had baked content for that file that should also now be removed // by calling deleteMappings for the hidden baked content folder for this hash - AssetPathList hiddenBakedFolder { HIDDEN_BAKED_CONTENT_FOLDER + hash + "/" }; + AssetPathList hiddenBakedFolder{ HIDDEN_BAKED_CONTENT_FOLDER + hash + "/" }; qCDebug(asset_server) << "Deleting baked content below" << hiddenBakedFolder << "since" << hash << "was deleted"; @@ -1003,7 +1002,7 @@ bool AssetServer::deleteMappings(const AssetPathList& paths) { hashesToCheckForDeletion << it->second; qCDebug(asset_server) << "Deleted a mapping:" << path << "=>" << it->second; - + _fileMappings.erase(it); } else { qCDebug(asset_server) << "Unable to delete a mapping that was not found:" << path; @@ -1026,7 +1025,7 @@ bool AssetServer::deleteMappings(const AssetPathList& paths) { // we now have a set of hashes that are unmapped - we will delete those asset files for (auto& hash : hashesToCheckForDeletion) { // remove the unmapped file - QFile removeableFile { _filesDirectory.absoluteFilePath(hash) }; + QFile removeableFile{ _filesDirectory.absoluteFilePath(hash) }; if (removeableFile.remove()) { qCDebug(asset_server) << "\tDeleted" << hash << "from asset files directory since it is now unmapped."; @@ -1173,7 +1172,7 @@ void AssetServer::handleFailedBake(QString originalAssetHash, QString assetPath, } void AssetServer::handleCompletedBake(QString originalAssetHash, QString originalAssetPath, QVector bakedFilePaths) { - bool errorCompletingBake { false }; + bool errorCompletingBake{ false }; QString errorReason; qDebug() << "Completing bake for " << originalAssetHash; @@ -1272,7 +1271,7 @@ std::pair AssetServer::readMetaFile(AssetHash hash) { auto it = _fileMappings.find(metaFilePath); if (it == _fileMappings.end()) { - return { false, {} }; + return { false,{} }; } auto metaFileHash = it->second; @@ -1309,7 +1308,7 @@ std::pair AssetServer::readMetaFile(AssetHash hash) { } } - return { false, {} }; + return { false,{} }; } bool AssetServer::writeMetaFile(AssetHash originalAssetHash, const AssetMeta& meta) { @@ -1358,7 +1357,7 @@ bool AssetServer::setBakingEnabled(const AssetPathList& paths, bool enabled) { auto extension = path.mid(dotIndex + 1); QString bakedFilename; - + if (BAKEABLE_MODEL_EXTENSIONS.contains(extension)) { bakedFilename = BAKED_MODEL_SIMPLE_NAME; } else if (BAKEABLE_TEXTURE_EXTENSIONS.contains(extension.toLocal8Bit()) && hasMetaFile(hash)) { diff --git a/assignment-client/src/assets/BakeAssetTask.cpp b/assignment-client/src/assets/BakeAssetTask.cpp index daff3a3834..65b6efb53a 100644 --- a/assignment-client/src/assets/BakeAssetTask.cpp +++ b/assignment-client/src/assets/BakeAssetTask.cpp @@ -20,8 +20,7 @@ BakeAssetTask::BakeAssetTask(const AssetHash& assetHash, const AssetPath& assetPath, const QString& filePath) : _assetHash(assetHash), _assetPath(assetPath), - _filePath(filePath) -{ + _filePath(filePath) { } @@ -32,7 +31,7 @@ void BakeAssetTask::run() { TextureBakerThreadGetter fn = []() -> QThread* { return QThread::currentThread(); }; if (_assetPath.endsWith(".fbx")) { - _baker = std::unique_ptr { + _baker = std::unique_ptr{ new FBXBaker(QUrl("file:///" + _filePath), fn, PathUtils::generateTemporaryDir()) }; } else if (_assetPath.endsWith(".js", Qt::CaseInsensitive)) { @@ -40,9 +39,9 @@ void BakeAssetTask::run() { new JSBaker(QUrl("file:///" + _filePath), PathUtils::generateTemporaryDir()) }; } else { - _baker = std::unique_ptr { + _baker = std::unique_ptr{ new TextureBaker(QUrl("file:///" + _filePath), image::TextureUsage::CUBE_TEXTURE, - PathUtils::generateTemporaryDir()) + PathUtils::generateTemporaryDir()) }; } diff --git a/libraries/baking/src/JSBaker.cpp b/libraries/baking/src/JSBaker.cpp index 079650d6d1..ffbc4f01dc 100644 --- a/libraries/baking/src/JSBaker.cpp +++ b/libraries/baking/src/JSBaker.cpp @@ -18,25 +18,24 @@ const int ASCII_CHARACTERS_UPPER_LIMIT = 126; JSBaker::JSBaker(const QUrl& jsURL, const QString& bakedOutputDir) : _jsURL(jsURL), - _bakedOutputDir(bakedOutputDir) -{ - + _bakedOutputDir(bakedOutputDir) { + }; void JSBaker::bake() { qCDebug(js_baking) << "JS Baker " << _jsURL << "bake starting"; - + // Import file to start baking QFile jsFile(_jsURL.toLocalFile()); if (!jsFile.open(QIODevice::ReadOnly | QIODevice::Text)) { handleError("Error opening " + _jsURL.fileName() + " for reading"); return; } - + // Read file into an array QByteArray inputJS = jsFile.readAll(); QByteArray outputJS; - + // Call baking on inputJS and store result in outputJS bool success = bakeJS(&inputJS, &outputJS); if (!success) { @@ -44,23 +43,23 @@ void JSBaker::bake() { handleError("Error unterminated multi line comment"); return; } - + // Bake Successful. Export the file auto fileName = _jsURL.fileName(); auto baseName = fileName.left(fileName.lastIndexOf('.')); auto bakedFilename = baseName + BAKED_JS_EXTENSION; _bakedJSFilePath = _bakedOutputDir + "/" + bakedFilename; - + QFile bakedFile; bakedFile.setFileName(_bakedJSFilePath); if (!bakedFile.open(QIODevice::WriteOnly)) { handleError("Error opening " + _bakedJSFilePath + " for writing"); return; } - + bakedFile.write(outputJS); - + // Export successful _outputFiles.push_back(_bakedJSFilePath); qCDebug(js_baking) << "Exported" << _jsURL << "with re-written paths to" << _bakedJSFilePath; @@ -71,7 +70,7 @@ void JSBaker::bake() { bool JSBaker::bakeJS(const QByteArray* inputFile, QByteArray* outputFile) { // Read from inputFile and write to outputFile per character - QTextStream in(*inputFile,QIODevice::ReadOnly); + QTextStream in(*inputFile, QIODevice::ReadOnly); QTextStream out(outputFile, QIODevice::WriteOnly); // Algorithm requires the knowledge of previous and next character for each character read @@ -79,19 +78,19 @@ bool JSBaker::bakeJS(const QByteArray* inputFile, QByteArray* outputFile) { QChar nextCharacter; // Initialize previousCharacter with new line QChar previousCharacter = '\n'; - + in >> currentCharacter; - + while (!in.atEnd()) { in >> nextCharacter; - + if (currentCharacter == '\r') { out << '\n'; } else if (currentCharacter == '/') { // Check if single line comment i.e. // if (nextCharacter == '/') { handleSingleLineComments(&in); - + //Start fresh after handling comments previousCharacter = '\n'; in >> currentCharacter; @@ -111,7 +110,7 @@ bool JSBaker::bakeJS(const QByteArray* inputFile, QByteArray* outputFile) { // If '/' is not followed by '/' or '*' print '/' out << currentCharacter; } - } else if (isSpaceOrTab(currentCharacter)) { + } else if (isSpaceOrTab(currentCharacter)) { // Check if white space or tab // Skip multiple spaces or tabs @@ -126,20 +125,20 @@ bool JSBaker::bakeJS(const QByteArray* inputFile, QByteArray* outputFile) { if (!canOmitSpace(previousCharacter, nextCharacter)) { out << ' '; } - } else if (currentCharacter == '\n') { + } else if (currentCharacter == '\n') { // Check if new line - + //Skip multiple new lines //Skip new line followed by space or tab while (nextCharacter == '\n' || isSpaceOrTab(nextCharacter)) { in >> nextCharacter; } - + // Check if new line can be omitted if (!canOmitNewLine(previousCharacter, nextCharacter)) { out << '\n'; } - } else if (isQuote(currentCharacter)) { + } else if (isQuote(currentCharacter)) { // Print the current quote and nextCharacter as is out << currentCharacter; out << nextCharacter; @@ -160,17 +159,17 @@ bool JSBaker::bakeJS(const QByteArray* inputFile, QByteArray* outputFile) { } else { // In all other cases write the currentCharacter to outputFile out << currentCharacter; - } + } previousCharacter = currentCharacter; currentCharacter = nextCharacter; } - + //write currentCharacter to output file when nextCharacter reaches EOF if (currentCharacter != '\n') { out << currentCharacter; } - + // Successful bake. Return true return true; } @@ -200,19 +199,19 @@ bool JSBaker::handleMultiLineComments(QTextStream* in) { bool JSBaker::canOmitSpace(QChar previousCharacter, QChar nextCharacter) { return(!((isAlphanum(previousCharacter) || isNonAscii(previousCharacter) || isSpecialCharacter(previousCharacter)) && - (isAlphanum(nextCharacter) || isNonAscii(nextCharacter) || isSpecialCharacter(nextCharacter))) - ); + (isAlphanum(nextCharacter) || isNonAscii(nextCharacter) || isSpecialCharacter(nextCharacter))) + ); } bool JSBaker::canOmitNewLine(QChar previousCharacter, QChar nextCharacter) { return (!((isAlphanum(previousCharacter) || isNonAscii(previousCharacter) || isSpecialCharacterPrevious(previousCharacter)) && - (isAlphanum(nextCharacter) || isNonAscii(nextCharacter) || isSpecialCharacterNext(nextCharacter))) - ); + (isAlphanum(nextCharacter) || isNonAscii(nextCharacter) || isSpecialCharacterNext(nextCharacter))) + ); } //Check if character is alphabet, number or one of the following: '_', '$', '\\' or a non-ASCII character bool JSBaker::isAlphanum(QChar c) { - return ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') + return ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || c == '_' || c == '$' || c == '\\' || c > ASCII_CHARACTERS_UPPER_LIMIT); } diff --git a/libraries/baking/src/JSBaker.h b/libraries/baking/src/JSBaker.h index 695785a730..88cbbc8eb0 100644 --- a/libraries/baking/src/JSBaker.h +++ b/libraries/baking/src/JSBaker.h @@ -22,13 +22,13 @@ class JSBaker : public Baker { public: JSBaker(const QUrl& jsURL, const QString& bakedOutputDir); -public slots: - virtual void bake() override; + public slots: + virtual void bake() override; public: static bool bakeJS(const QByteArray* inputFile, QByteArray* outputFile); -private : +private: QUrl _jsURL; QString _bakedOutputDir; QString _bakedJSFilePath; diff --git a/tests/baking/CMakeLists b/tests/baking/CMakeLists.txt similarity index 100% rename from tests/baking/CMakeLists rename to tests/baking/CMakeLists.txt diff --git a/tests/baking/src/JSBakerTest.cpp b/tests/baking/src/JSBakerTest.cpp index c3398a8d67..b1cd991969 100644 --- a/tests/baking/src/JSBakerTest.cpp +++ b/tests/baking/src/JSBakerTest.cpp @@ -14,7 +14,7 @@ QTEST_MAIN(JSBakerTest) void JSBakerTest::setTestCases() { // Test cases contain a std::pair(input, desiredOutput) - + _testCases.emplace_back("var a=1;", "var a=1;"); _testCases.emplace_back("var a=1;//single line comment\nvar b=2;", "var a=1;var b=2;"); _testCases.emplace_back("a\rb", "a\nb"); @@ -22,8 +22,8 @@ void JSBakerTest::setTestCases() { _testCases.emplace_back("a/b", "a/b"); _testCases.emplace_back("var a = 1;", "var a=1;"); // Multiple spaces omitted _testCases.emplace_back("var a= 1;", "var a=1;"); // Multiple tabs omitted - - // Cases for space not omitted + + // Cases for space not omitted _testCases.emplace_back("var x", "var x"); _testCases.emplace_back("a '", "a '"); _testCases.emplace_back("a $", "a $"); @@ -42,7 +42,7 @@ void JSBakerTest::setTestCases() { _testCases.emplace_back("a\n\n b", "a\nb"); // Skip multiple new lines followed by whitespace _testCases.emplace_back("a\n\n b", "a\nb"); // Skip multiple new lines followed by tab - //Cases for new line not omitted + //Cases for new line not omitted _testCases.emplace_back("a\nb", "a\nb"); _testCases.emplace_back("a\n9", "a\n9"); _testCases.emplace_back("9\na", "9\na"); @@ -61,12 +61,12 @@ void JSBakerTest::setTestCases() { _testCases.emplace_back(")\na", ")\na"); _testCases.emplace_back("+\na", "+\na"); _testCases.emplace_back("-\na", "-\na"); - + // Cases to check quoted strings are not modified _testCases.emplace_back("'abcd1234$%^&[](){}'\na", "'abcd1234$%^&[](){}'\na"); _testCases.emplace_back("\"abcd1234$%^&[](){}\"\na", "\"abcd1234$%^&[](){}\"\na"); _testCases.emplace_back("`abcd1234$%^&[](){}`\na", "`abcd1234$%^&[](){}`a"); - + // Edge Cases //No semicolon to terminate an expression, instead a new line used for termination @@ -77,12 +77,12 @@ void JSBakerTest::setTestCases() { } void JSBakerTest::testJSBaking() { - + for (int i = 0;i < _testCases.size();i++) { QByteArray output; auto input = _testCases.at(i).first; JSBaker::bakeJS(&input, &output); - + auto desiredOutput = _testCases.at(i).second; QCOMPARE(output, desiredOutput); } diff --git a/tests/baking/src/JSBakerTest.h b/tests/baking/src/JSBakerTest.h index a1d1f73e9f..84d39deb64 100644 --- a/tests/baking/src/JSBakerTest.h +++ b/tests/baking/src/JSBakerTest.h @@ -15,12 +15,12 @@ #include #include "../../libraries/baking/src/JSBaker.h" -class JSBakerTest: public QObject { +class JSBakerTest : public QObject { Q_OBJECT -private slots: + private slots: void setTestCases(); - void testJSBaking(); + void testJSBaking(); private: std::vector> _testCases; From 32e319fedc541ea8cb069f2d2307263ab31d2752 Mon Sep 17 00:00:00 2001 From: utkarshgautamnyu Date: Thu, 28 Sep 2017 11:23:20 -0700 Subject: [PATCH 25/36] Update AssetServer.cpp --- assignment-client/src/assets/AssetServer.cpp | 65 ++++++++++---------- 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/assignment-client/src/assets/AssetServer.cpp b/assignment-client/src/assets/AssetServer.cpp index 919cec6e2d..9e3732762d 100644 --- a/assignment-client/src/assets/AssetServer.cpp +++ b/assignment-client/src/assets/AssetServer.cpp @@ -50,9 +50,9 @@ static const int INTERFACE_RUNNING_CHECK_FREQUENCY_MS = 1000; const QString ASSET_SERVER_LOGGING_TARGET_NAME = "asset-server"; -static const QStringList BAKEABLE_MODEL_EXTENSIONS = { "fbx" }; +static const QStringList BAKEABLE_MODEL_EXTENSIONS = {"fbx"}; static QStringList BAKEABLE_TEXTURE_EXTENSIONS; -static const QStringList BAKEABLE_SCRIPT_EXTENSIONS = { "js" }; +static const QStringList BAKEABLE_SCRIPT_EXTENSIONS = {"js"}; static const QString BAKED_MODEL_SIMPLE_NAME = "asset.fbx"; static const QString BAKED_TEXTURE_SIMPLE_NAME = "texture.ktx"; static const QString BAKED_SCRIPT_SIMPLE_NAME = "asset.js"; @@ -125,7 +125,7 @@ std::pair AssetServer::getAssetStatus(const AssetPath& pa return { Error, meta.lastBakeErrors }; } } - + return { Pending, "" }; } @@ -147,8 +147,8 @@ void AssetServer::maybeBake(const AssetPath& path, const AssetHash& hash) { void AssetServer::createEmptyMetaFile(const AssetHash& hash) { QString metaFilePath = "atp:/" + hash + "/meta.json"; - QFile metaFile{ metaFilePath }; - + QFile metaFile { metaFilePath }; + if (!metaFile.exists()) { qDebug() << "Creating metafile for " << hash; if (metaFile.open(QFile::WriteOnly)) { @@ -205,7 +205,7 @@ bool interfaceRunning() { bool result = false; #ifdef Q_OS_WIN - QSharedMemory sharedMemory{ getInterfaceSharedMemoryName() }; + QSharedMemory sharedMemory { getInterfaceSharedMemoryName() }; result = sharedMemory.attach(QSharedMemory::ReadOnly); if (result) { sharedMemory.detach(); @@ -226,7 +226,7 @@ void updateConsumedCores() { auto coreCount = std::thread::hardware_concurrency(); if (isInterfaceRunning) { coreCount = coreCount > MIN_CORES_FOR_MULTICORE ? CPU_AFFINITY_COUNT_HIGH : CPU_AFFINITY_COUNT_LOW; - } + } qCDebug(asset_server) << "Setting max consumed cores to " << coreCount; setMaxCores(coreCount); } @@ -235,7 +235,8 @@ void updateConsumedCores() { AssetServer::AssetServer(ReceivedMessage& message) : ThreadedAssignment(message), _transferTaskPool(this), - _bakingTaskPool(this) { + _bakingTaskPool(this) +{ // store the current state of image compression so we can reset it when this assignment is complete _wasColorTextureCompressionEnabled = image::isColorTexturesCompressionEnabled(); _wasGrayscaleTextureCompressionEnabled = image::isGrayscaleTexturesCompressionEnabled(); @@ -262,7 +263,7 @@ AssetServer::AssetServer(ReceivedMessage& message) : packetReceiver.registerListener(PacketType::AssetGetInfo, this, "handleAssetGetInfo"); packetReceiver.registerListener(PacketType::AssetUpload, this, "handleAssetUpload"); packetReceiver.registerListener(PacketType::AssetMappingOperation, this, "handleAssetMappingOperation"); - + #ifdef Q_OS_WIN updateConsumedCores(); QTimer* timer = new QTimer(this); @@ -286,7 +287,7 @@ void AssetServer::aboutToFinish() { // abort each of our still running bake tasks, remove pending bakes that were never put on the thread pool auto it = _pendingBakes.begin(); while (it != _pendingBakes.end()) { - auto pendingRunnable = _bakingTaskPool.tryTake(it->get()); + auto pendingRunnable = _bakingTaskPool.tryTake(it->get()); if (pendingRunnable) { it = _pendingBakes.erase(it); @@ -347,7 +348,7 @@ void AssetServer::completeSetup() { int maxBandwidth = maxBandwidthFloat * BITS_PER_MEGABITS; nodeList->setConnectionMaxBandwidth(maxBandwidth); qCInfo(asset_server) << "Set maximum bandwith per connection to" << maxBandwidthFloat << "Mb/s." - " (" << maxBandwidth << "bits/s)"; + " (" << maxBandwidth << "bits/s)"; } // get the path to the asset folder from the domain server settings @@ -361,7 +362,7 @@ void AssetServer::completeSetup() { } auto assetsPathString = assetsJSONValue.toString(); - QDir assetsPath{ assetsPathString }; + QDir assetsPath { assetsPathString }; QString absoluteFilePath = assetsPath.absolutePath(); if (assetsPath.isRelative()) { @@ -389,7 +390,7 @@ void AssetServer::completeSetup() { // Check the asset directory to output some information about what we have auto files = _filesDirectory.entryList(QDir::Files); - QRegExp hashFileRegex{ ASSET_HASH_REGEX_STRING }; + QRegExp hashFileRegex { ASSET_HASH_REGEX_STRING }; auto hashedFiles = files.filter(hashFileRegex); qCInfo(asset_server) << "There are" << hashedFiles.size() << "asset files in the asset directory."; @@ -408,7 +409,7 @@ void AssetServer::completeSetup() { } void AssetServer::cleanupUnmappedFiles() { - QRegExp hashFileRegex{ "^[a-f0-9]{" + QString::number(SHA256_HASH_HEX_LENGTH) + "}" }; + QRegExp hashFileRegex { "^[a-f0-9]{" + QString::number(SHA256_HASH_HEX_LENGTH) + "}" }; auto files = _filesDirectory.entryInfoList(QDir::Files); @@ -417,7 +418,7 @@ void AssetServer::cleanupUnmappedFiles() { for (const auto& fileInfo : files) { auto filename = fileInfo.fileName(); if (hashFileRegex.exactMatch(filename)) { - bool matched{ false }; + bool matched { false }; for (auto& pair : _fileMappings) { if (pair.second == filename) { matched = true; @@ -426,7 +427,7 @@ void AssetServer::cleanupUnmappedFiles() { } if (!matched) { // remove the unmapped file - QFile removeableFile{ fileInfo.absoluteFilePath() }; + QFile removeableFile { fileInfo.absoluteFilePath() }; if (removeableFile.remove()) { qCDebug(asset_server) << "\tDeleted" << filename << "from asset files directory since it is unmapped."; @@ -478,7 +479,7 @@ void AssetServer::handleAssetMappingOperation(QSharedPointer me void AssetServer::handleGetMappingOperation(ReceivedMessage& message, SharedNodePointer senderNode, NLPacketList& replyPacket) { QString assetPath = message.readString(); - QUrl url{ assetPath }; + QUrl url { assetPath }; assetPath = url.path(); auto it = _fileMappings.find(assetPath); @@ -497,7 +498,7 @@ void AssetServer::handleGetMappingOperation(ReceivedMessage& message, SharedNode } else if (BAKEABLE_SCRIPT_EXTENSIONS.contains(assetPathExtension)) { bakedRootFile = BAKED_SCRIPT_SIMPLE_NAME; } - + auto originalAssetHash = it->second; QString redirectedAssetHash; QString bakedAssetPath; @@ -561,7 +562,7 @@ void AssetServer::handleGetAllMappingOperation(ReceivedMessage& message, SharedN replyPacket.writePrimitive(count); - for (auto it = _fileMappings.cbegin(); it != _fileMappings.cend(); ++it) { + for (auto it = _fileMappings.cbegin(); it != _fileMappings.cend(); ++ it) { auto mapping = it->first; auto hash = it->second; replyPacket.writeString(mapping); @@ -602,7 +603,7 @@ void AssetServer::handleSetMappingOperation(ReceivedMessage& message, SharedNode void AssetServer::handleDeleteMappingsOperation(ReceivedMessage& message, SharedNodePointer senderNode, NLPacketList& replyPacket) { if (senderNode->getCanWriteToAssetServer()) { - int numberOfDeletedMappings{ 0 }; + int numberOfDeletedMappings { 0 }; message.readPrimitive(&numberOfDeletedMappings); QStringList mappingsToDelete; @@ -652,7 +653,7 @@ void AssetServer::handleRenameMappingOperation(ReceivedMessage& message, SharedN void AssetServer::handleSetBakingEnabledOperation(ReceivedMessage& message, SharedNodePointer senderNode, NLPacketList& replyPacket) { if (senderNode->getCanWriteToAssetServer()) { - bool enabled{ true }; + bool enabled { true }; message.readPrimitive(&enabled); int numberOfMappings{ 0 }; @@ -695,7 +696,7 @@ void AssetServer::handleAssetGetInfo(QSharedPointer message, Sh replyPacket->write(assetHash); QString fileName = QString(hexHash); - QFileInfo fileInfo{ _filesDirectory.filePath(fileName) }; + QFileInfo fileInfo { _filesDirectory.filePath(fileName) }; if (fileInfo.exists() && fileInfo.isReadable()) { qCDebug(asset_server) << "Opening file: " << fileInfo.filePath(); @@ -826,7 +827,7 @@ bool AssetServer::loadMappingsFromFile() { auto mapFilePath = _resourcesDirectory.absoluteFilePath(MAP_FILE_NAME); - QFile mapFile{ mapFilePath }; + QFile mapFile { mapFilePath }; if (mapFile.exists()) { if (mapFile.open(QIODevice::ReadOnly)) { QJsonParseError error; @@ -882,7 +883,7 @@ bool AssetServer::loadMappingsFromFile() { bool AssetServer::writeMappingsToFile() { auto mapFilePath = _resourcesDirectory.absoluteFilePath(MAP_FILE_NAME); - QFile mapFile{ mapFilePath }; + QFile mapFile { mapFilePath }; if (mapFile.open(QIODevice::WriteOnly)) { QJsonObject root; @@ -890,7 +891,7 @@ bool AssetServer::writeMappingsToFile() { root[it.first] = it.second; } - QJsonDocument jsonDocument{ root }; + QJsonDocument jsonDocument { root }; if (mapFile.write(jsonDocument.toJson()) != -1) { qCDebug(asset_server) << "Wrote JSON mappings to file at" << mapFilePath; @@ -954,7 +955,7 @@ void AssetServer::removeBakedPathsForDeletedAsset(AssetHash hash) { // check if we had baked content for that file that should also now be removed // by calling deleteMappings for the hidden baked content folder for this hash - AssetPathList hiddenBakedFolder{ HIDDEN_BAKED_CONTENT_FOLDER + hash + "/" }; + AssetPathList hiddenBakedFolder { HIDDEN_BAKED_CONTENT_FOLDER + hash + "/" }; qCDebug(asset_server) << "Deleting baked content below" << hiddenBakedFolder << "since" << hash << "was deleted"; @@ -1002,7 +1003,7 @@ bool AssetServer::deleteMappings(const AssetPathList& paths) { hashesToCheckForDeletion << it->second; qCDebug(asset_server) << "Deleted a mapping:" << path << "=>" << it->second; - + _fileMappings.erase(it); } else { qCDebug(asset_server) << "Unable to delete a mapping that was not found:" << path; @@ -1025,7 +1026,7 @@ bool AssetServer::deleteMappings(const AssetPathList& paths) { // we now have a set of hashes that are unmapped - we will delete those asset files for (auto& hash : hashesToCheckForDeletion) { // remove the unmapped file - QFile removeableFile{ _filesDirectory.absoluteFilePath(hash) }; + QFile removeableFile { _filesDirectory.absoluteFilePath(hash) }; if (removeableFile.remove()) { qCDebug(asset_server) << "\tDeleted" << hash << "from asset files directory since it is now unmapped."; @@ -1172,7 +1173,7 @@ void AssetServer::handleFailedBake(QString originalAssetHash, QString assetPath, } void AssetServer::handleCompletedBake(QString originalAssetHash, QString originalAssetPath, QVector bakedFilePaths) { - bool errorCompletingBake{ false }; + bool errorCompletingBake { false }; QString errorReason; qDebug() << "Completing bake for " << originalAssetHash; @@ -1271,7 +1272,7 @@ std::pair AssetServer::readMetaFile(AssetHash hash) { auto it = _fileMappings.find(metaFilePath); if (it == _fileMappings.end()) { - return { false,{} }; + return { false, {} }; } auto metaFileHash = it->second; @@ -1308,7 +1309,7 @@ std::pair AssetServer::readMetaFile(AssetHash hash) { } } - return { false,{} }; + return { false, {} }; } bool AssetServer::writeMetaFile(AssetHash originalAssetHash, const AssetMeta& meta) { @@ -1357,7 +1358,7 @@ bool AssetServer::setBakingEnabled(const AssetPathList& paths, bool enabled) { auto extension = path.mid(dotIndex + 1); QString bakedFilename; - + if (BAKEABLE_MODEL_EXTENSIONS.contains(extension)) { bakedFilename = BAKED_MODEL_SIMPLE_NAME; } else if (BAKEABLE_TEXTURE_EXTENSIONS.contains(extension.toLocal8Bit()) && hasMetaFile(hash)) { From 0bfb199fa02ed0362002367623ebfe77324b005d Mon Sep 17 00:00:00 2001 From: utkarshgautamnyu Date: Thu, 28 Sep 2017 11:24:45 -0700 Subject: [PATCH 26/36] Update BakeAssetTask.cpp --- assignment-client/src/assets/BakeAssetTask.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/assignment-client/src/assets/BakeAssetTask.cpp b/assignment-client/src/assets/BakeAssetTask.cpp index 65b6efb53a..daff3a3834 100644 --- a/assignment-client/src/assets/BakeAssetTask.cpp +++ b/assignment-client/src/assets/BakeAssetTask.cpp @@ -20,7 +20,8 @@ BakeAssetTask::BakeAssetTask(const AssetHash& assetHash, const AssetPath& assetPath, const QString& filePath) : _assetHash(assetHash), _assetPath(assetPath), - _filePath(filePath) { + _filePath(filePath) +{ } @@ -31,7 +32,7 @@ void BakeAssetTask::run() { TextureBakerThreadGetter fn = []() -> QThread* { return QThread::currentThread(); }; if (_assetPath.endsWith(".fbx")) { - _baker = std::unique_ptr{ + _baker = std::unique_ptr { new FBXBaker(QUrl("file:///" + _filePath), fn, PathUtils::generateTemporaryDir()) }; } else if (_assetPath.endsWith(".js", Qt::CaseInsensitive)) { @@ -39,9 +40,9 @@ void BakeAssetTask::run() { new JSBaker(QUrl("file:///" + _filePath), PathUtils::generateTemporaryDir()) }; } else { - _baker = std::unique_ptr{ + _baker = std::unique_ptr { new TextureBaker(QUrl("file:///" + _filePath), image::TextureUsage::CUBE_TEXTURE, - PathUtils::generateTemporaryDir()) + PathUtils::generateTemporaryDir()) }; } From 305719da243272681ec2a2d4a1106c778fd8401f Mon Sep 17 00:00:00 2001 From: utkarshgautamnyu Date: Thu, 28 Sep 2017 11:47:39 -0700 Subject: [PATCH 27/36] Update JSBakerTest.h --- tests/baking/src/JSBakerTest.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/baking/src/JSBakerTest.h b/tests/baking/src/JSBakerTest.h index 84d39deb64..ea2cdc6696 100644 --- a/tests/baking/src/JSBakerTest.h +++ b/tests/baking/src/JSBakerTest.h @@ -13,12 +13,12 @@ #define hifi_JSBakerTest_h #include -#include "../../libraries/baking/src/JSBaker.h" +#include class JSBakerTest : public QObject { Q_OBJECT - private slots: +private slots: void setTestCases(); void testJSBaking(); From 16a75d20dc744c1be65f9d4575f00948e5a082ed Mon Sep 17 00:00:00 2001 From: utkarshgautamnyu Date: Thu, 28 Sep 2017 11:58:07 -0700 Subject: [PATCH 28/36] Update JSBakerTest.cpp --- tests/baking/src/JSBakerTest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/baking/src/JSBakerTest.cpp b/tests/baking/src/JSBakerTest.cpp index b1cd991969..0e6f0cb516 100644 --- a/tests/baking/src/JSBakerTest.cpp +++ b/tests/baking/src/JSBakerTest.cpp @@ -21,7 +21,7 @@ void JSBakerTest::setTestCases() { _testCases.emplace_back("a/*multi\n line \n comment*/ b", "ab"); _testCases.emplace_back("a/b", "a/b"); _testCases.emplace_back("var a = 1;", "var a=1;"); // Multiple spaces omitted - _testCases.emplace_back("var a= 1;", "var a=1;"); // Multiple tabs omitted + _testCases.emplace_back("var a=\t\t\t1;", "var a=1;"); // Multiple tabs omitted // Cases for space not omitted _testCases.emplace_back("var x", "var x"); From 5c14c7c3b9a1cc72cf9c19ac779a7b559c4ae967 Mon Sep 17 00:00:00 2001 From: utkarshgautamnyu Date: Thu, 28 Sep 2017 13:24:03 -0700 Subject: [PATCH 29/36] Update JSBaker.cpp --- libraries/baking/src/JSBaker.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/baking/src/JSBaker.cpp b/libraries/baking/src/JSBaker.cpp index ffbc4f01dc..d1fd502e10 100644 --- a/libraries/baking/src/JSBaker.cpp +++ b/libraries/baking/src/JSBaker.cpp @@ -221,7 +221,7 @@ bool JSBaker::isNonAscii(QChar c) { // If previous and next characters are special characters, don't omit space bool JSBaker::isSpecialCharacter(QChar c) { - return (c == '\'' || c == '$' || c == '_' || c == '/'); + return (c == '\'' || c == '$' || c == '_' || c == '/' || c== '+' || c == '-'); } // If previous character is a special character, maybe don't omit new line (depends on next character as well) From cae15607086ba8da792df75efa2d9212ab385d0c Mon Sep 17 00:00:00 2001 From: utkarshgautamnyu Date: Thu, 28 Sep 2017 13:25:08 -0700 Subject: [PATCH 30/36] Update JSBakerTest.cpp --- tests/baking/src/JSBakerTest.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/baking/src/JSBakerTest.cpp b/tests/baking/src/JSBakerTest.cpp index 0e6f0cb516..412d91a3e6 100644 --- a/tests/baking/src/JSBakerTest.cpp +++ b/tests/baking/src/JSBakerTest.cpp @@ -71,9 +71,6 @@ void JSBakerTest::setTestCases() { //No semicolon to terminate an expression, instead a new line used for termination _testCases.emplace_back("var x=5\nvar y=6;", "var x=5\nvar y=6;"); - - //a + ++b is minified as a+++b, interpreted as a++ + b. Ensure original script has correct parantheses - _testCases.emplace_back("a + ++b", "a+++b"); } void JSBakerTest::testJSBaking() { From da8b2d21372eec8ba5a3c38c9be32291768520d6 Mon Sep 17 00:00:00 2001 From: utkarshgautamnyu Date: Fri, 29 Sep 2017 13:33:07 -0700 Subject: [PATCH 31/36] Update JSBakerTest.cpp --- tests/baking/src/JSBakerTest.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/baking/src/JSBakerTest.cpp b/tests/baking/src/JSBakerTest.cpp index 412d91a3e6..244eab4dbc 100644 --- a/tests/baking/src/JSBakerTest.cpp +++ b/tests/baking/src/JSBakerTest.cpp @@ -71,6 +71,12 @@ void JSBakerTest::setTestCases() { //No semicolon to terminate an expression, instead a new line used for termination _testCases.emplace_back("var x=5\nvar y=6;", "var x=5\nvar y=6;"); + + //a + ++b is minified as a+ ++b. + _testCases.emplace_back("a + ++b", "a + ++b"); + + //a - --b is minified as a- --b. + _testCases.emplace_back("a - --b", "a - --b"); } void JSBakerTest::testJSBaking() { From 1d373c2988c2982ed7b308e933eac9e3d808eda3 Mon Sep 17 00:00:00 2001 From: utkarshgautamnyu Date: Wed, 11 Oct 2017 10:38:48 -0700 Subject: [PATCH 32/36] Update JSBaker.cpp --- libraries/baking/src/JSBaker.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/libraries/baking/src/JSBaker.cpp b/libraries/baking/src/JSBaker.cpp index d1fd502e10..cd58a715fb 100644 --- a/libraries/baking/src/JSBaker.cpp +++ b/libraries/baking/src/JSBaker.cpp @@ -18,9 +18,10 @@ const int ASCII_CHARACTERS_UPPER_LIMIT = 126; JSBaker::JSBaker(const QUrl& jsURL, const QString& bakedOutputDir) : _jsURL(jsURL), - _bakedOutputDir(bakedOutputDir) { + _bakedOutputDir(bakedOutputDir) +{ -}; +} void JSBaker::bake() { qCDebug(js_baking) << "JS Baker " << _jsURL << "bake starting"; @@ -40,7 +41,7 @@ void JSBaker::bake() { bool success = bakeJS(&inputJS, &outputJS); if (!success) { qCDebug(js_baking) << "Bake Failed"; - handleError("Error unterminated multi line comment"); + handleError("Unterminated multi-line comment"); return; } @@ -62,7 +63,7 @@ void JSBaker::bake() { // Export successful _outputFiles.push_back(_bakedJSFilePath); - qCDebug(js_baking) << "Exported" << _jsURL << "with re-written paths to" << _bakedJSFilePath; + qCDebug(js_baking) << "Exported" << _jsURL << "minified to" << _bakedJSFilePath; // emit signal to indicate the JS baking is finished emit finished(); From 6e4a5da69d866c7f296790476590dc5cfcde7532 Mon Sep 17 00:00:00 2001 From: utkarshgautamnyu Date: Wed, 11 Oct 2017 10:44:19 -0700 Subject: [PATCH 33/36] Update JSBaker.h --- libraries/baking/src/JSBaker.h | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/libraries/baking/src/JSBaker.h b/libraries/baking/src/JSBaker.h index 88cbbc8eb0..9eb7300e8a 100644 --- a/libraries/baking/src/JSBaker.h +++ b/libraries/baking/src/JSBaker.h @@ -21,13 +21,11 @@ class JSBaker : public Baker { Q_OBJECT public: JSBaker(const QUrl& jsURL, const QString& bakedOutputDir); - - public slots: - virtual void bake() override; - -public: static bool bakeJS(const QByteArray* inputFile, QByteArray* outputFile); +public slots: + virtual void bake() override; + private: QUrl _jsURL; QString _bakedOutputDir; From f3983b3edc51ac788c63a3fda489019c012c1271 Mon Sep 17 00:00:00 2001 From: utkarshgautamnyu Date: Thu, 12 Oct 2017 11:34:28 -0700 Subject: [PATCH 34/36] Update JSBaker.cpp --- libraries/baking/src/JSBaker.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/libraries/baking/src/JSBaker.cpp b/libraries/baking/src/JSBaker.cpp index cd58a715fb..75811bea49 100644 --- a/libraries/baking/src/JSBaker.cpp +++ b/libraries/baking/src/JSBaker.cpp @@ -38,7 +38,7 @@ void JSBaker::bake() { QByteArray outputJS; // Call baking on inputJS and store result in outputJS - bool success = bakeJS(&inputJS, &outputJS); + bool success = bakeJS(inputJS, outputJS); if (!success) { qCDebug(js_baking) << "Bake Failed"; handleError("Unterminated multi-line comment"); @@ -69,9 +69,9 @@ void JSBaker::bake() { emit finished(); } -bool JSBaker::bakeJS(const QByteArray* inputFile, QByteArray* outputFile) { +bool JSBaker::bakeJS(const QByteArray& inputFile, QByteArray& outputFile) { // Read from inputFile and write to outputFile per character - QTextStream in(*inputFile, QIODevice::ReadOnly); + QTextStream in(inputFile, QIODevice::ReadOnly); QTextStream out(outputFile, QIODevice::WriteOnly); // Algorithm requires the knowledge of previous and next character for each character read @@ -90,7 +90,7 @@ bool JSBaker::bakeJS(const QByteArray* inputFile, QByteArray* outputFile) { } else if (currentCharacter == '/') { // Check if single line comment i.e. // if (nextCharacter == '/') { - handleSingleLineComments(&in); + handleSingleLineComments(in); //Start fresh after handling comments previousCharacter = '\n'; @@ -98,7 +98,7 @@ bool JSBaker::bakeJS(const QByteArray* inputFile, QByteArray* outputFile) { continue; } else if (nextCharacter == '*') { // Check if multi line comment i.e. /* - bool success = handleMultiLineComments(&in); + bool success = handleMultiLineComments(in); if (!success) { // Errors present return false return false; @@ -175,22 +175,22 @@ bool JSBaker::bakeJS(const QByteArray* inputFile, QByteArray* outputFile) { return true; } -void JSBaker::handleSingleLineComments(QTextStream* in) { +void JSBaker::handleSingleLineComments(QTextStream& in) { QChar character; - while (!in->atEnd()) { - *in >> character; + while (!in.atEnd()) { + in >> character; if (character == '\n') { break; } } } -bool JSBaker::handleMultiLineComments(QTextStream* in) { +bool JSBaker::handleMultiLineComments(QTextStream& in) { QChar character; - while (!in->atEnd()) { - *in >> character; + while (!in.atEnd()) { + in >> character; if (character == '*') { - if (in->read(1) == '/') { + if (in.read(1) == '/') { return true; } } From 49f601329fa99f70b642b9e9bb6104cc849c9d53 Mon Sep 17 00:00:00 2001 From: utkarshgautamnyu Date: Thu, 12 Oct 2017 11:35:38 -0700 Subject: [PATCH 35/36] Update JSBaker.h --- libraries/baking/src/JSBaker.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/baking/src/JSBaker.h b/libraries/baking/src/JSBaker.h index 9eb7300e8a..b5889440cb 100644 --- a/libraries/baking/src/JSBaker.h +++ b/libraries/baking/src/JSBaker.h @@ -21,7 +21,7 @@ class JSBaker : public Baker { Q_OBJECT public: JSBaker(const QUrl& jsURL, const QString& bakedOutputDir); - static bool bakeJS(const QByteArray* inputFile, QByteArray* outputFile); + static bool bakeJS(const QByteArray& inputFile, QByteArray& outputFile); public slots: virtual void bake() override; @@ -31,8 +31,8 @@ private: QString _bakedOutputDir; QString _bakedJSFilePath; - static void handleSingleLineComments(QTextStream* in); - static bool handleMultiLineComments(QTextStream* in); + static void handleSingleLineComments(QTextStream& in); + static bool handleMultiLineComments(QTextStream& in); static bool canOmitSpace(QChar previousCharacter, QChar nextCharacter); static bool canOmitNewLine(QChar previousCharacter, QChar nextCharacter); From 75aa3a73addf48eb82b52f46fba3a466e401ac45 Mon Sep 17 00:00:00 2001 From: utkarshgautamnyu Date: Thu, 12 Oct 2017 12:08:44 -0700 Subject: [PATCH 36/36] changed pointer args to references in JSBaker:bakeJS() --- tests/baking/src/JSBakerTest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/baking/src/JSBakerTest.cpp b/tests/baking/src/JSBakerTest.cpp index 244eab4dbc..082ffb047f 100644 --- a/tests/baking/src/JSBakerTest.cpp +++ b/tests/baking/src/JSBakerTest.cpp @@ -84,7 +84,7 @@ void JSBakerTest::testJSBaking() { for (int i = 0;i < _testCases.size();i++) { QByteArray output; auto input = _testCases.at(i).first; - JSBaker::bakeJS(&input, &output); + JSBaker::bakeJS(input, output); auto desiredOutput = _testCases.at(i).second; QCOMPARE(output, desiredOutput);