made updates to fix building

This commit is contained in:
utkarshgautamnyu 2017-09-28 11:12:26 -07:00
parent 290e3d307c
commit 463afd6be5
7 changed files with 77 additions and 80 deletions

View file

@ -50,9 +50,9 @@ static const int INTERFACE_RUNNING_CHECK_FREQUENCY_MS = 1000;
const QString ASSET_SERVER_LOGGING_TARGET_NAME = "asset-server"; 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 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_MODEL_SIMPLE_NAME = "asset.fbx";
static const QString BAKED_TEXTURE_SIMPLE_NAME = "texture.ktx"; static const QString BAKED_TEXTURE_SIMPLE_NAME = "texture.ktx";
static const QString BAKED_SCRIPT_SIMPLE_NAME = "asset.js"; static const QString BAKED_SCRIPT_SIMPLE_NAME = "asset.js";
@ -125,7 +125,7 @@ std::pair<BakingStatus, QString> AssetServer::getAssetStatus(const AssetPath& pa
return { Error, meta.lastBakeErrors }; return { Error, meta.lastBakeErrors };
} }
} }
return { Pending, "" }; return { Pending, "" };
} }
@ -147,8 +147,8 @@ void AssetServer::maybeBake(const AssetPath& path, const AssetHash& hash) {
void AssetServer::createEmptyMetaFile(const AssetHash& hash) { void AssetServer::createEmptyMetaFile(const AssetHash& hash) {
QString metaFilePath = "atp:/" + hash + "/meta.json"; QString metaFilePath = "atp:/" + hash + "/meta.json";
QFile metaFile { metaFilePath }; QFile metaFile{ metaFilePath };
if (!metaFile.exists()) { if (!metaFile.exists()) {
qDebug() << "Creating metafile for " << hash; qDebug() << "Creating metafile for " << hash;
if (metaFile.open(QFile::WriteOnly)) { if (metaFile.open(QFile::WriteOnly)) {
@ -205,7 +205,7 @@ bool interfaceRunning() {
bool result = false; bool result = false;
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
QSharedMemory sharedMemory { getInterfaceSharedMemoryName() }; QSharedMemory sharedMemory{ getInterfaceSharedMemoryName() };
result = sharedMemory.attach(QSharedMemory::ReadOnly); result = sharedMemory.attach(QSharedMemory::ReadOnly);
if (result) { if (result) {
sharedMemory.detach(); sharedMemory.detach();
@ -226,7 +226,7 @@ void updateConsumedCores() {
auto coreCount = std::thread::hardware_concurrency(); auto coreCount = std::thread::hardware_concurrency();
if (isInterfaceRunning) { if (isInterfaceRunning) {
coreCount = coreCount > MIN_CORES_FOR_MULTICORE ? CPU_AFFINITY_COUNT_HIGH : CPU_AFFINITY_COUNT_LOW; coreCount = coreCount > MIN_CORES_FOR_MULTICORE ? CPU_AFFINITY_COUNT_HIGH : CPU_AFFINITY_COUNT_LOW;
} }
qCDebug(asset_server) << "Setting max consumed cores to " << coreCount; qCDebug(asset_server) << "Setting max consumed cores to " << coreCount;
setMaxCores(coreCount); setMaxCores(coreCount);
} }
@ -235,8 +235,7 @@ void updateConsumedCores() {
AssetServer::AssetServer(ReceivedMessage& message) : AssetServer::AssetServer(ReceivedMessage& message) :
ThreadedAssignment(message), ThreadedAssignment(message),
_transferTaskPool(this), _transferTaskPool(this),
_bakingTaskPool(this) _bakingTaskPool(this) {
{
// store the current state of image compression so we can reset it when this assignment is complete // store the current state of image compression so we can reset it when this assignment is complete
_wasColorTextureCompressionEnabled = image::isColorTexturesCompressionEnabled(); _wasColorTextureCompressionEnabled = image::isColorTexturesCompressionEnabled();
_wasGrayscaleTextureCompressionEnabled = image::isGrayscaleTexturesCompressionEnabled(); _wasGrayscaleTextureCompressionEnabled = image::isGrayscaleTexturesCompressionEnabled();
@ -263,7 +262,7 @@ AssetServer::AssetServer(ReceivedMessage& message) :
packetReceiver.registerListener(PacketType::AssetGetInfo, this, "handleAssetGetInfo"); packetReceiver.registerListener(PacketType::AssetGetInfo, this, "handleAssetGetInfo");
packetReceiver.registerListener(PacketType::AssetUpload, this, "handleAssetUpload"); packetReceiver.registerListener(PacketType::AssetUpload, this, "handleAssetUpload");
packetReceiver.registerListener(PacketType::AssetMappingOperation, this, "handleAssetMappingOperation"); packetReceiver.registerListener(PacketType::AssetMappingOperation, this, "handleAssetMappingOperation");
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
updateConsumedCores(); updateConsumedCores();
QTimer* timer = new QTimer(this); 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 // abort each of our still running bake tasks, remove pending bakes that were never put on the thread pool
auto it = _pendingBakes.begin(); auto it = _pendingBakes.begin();
while (it != _pendingBakes.end()) { while (it != _pendingBakes.end()) {
auto pendingRunnable = _bakingTaskPool.tryTake(it->get()); auto pendingRunnable = _bakingTaskPool.tryTake(it->get());
if (pendingRunnable) { if (pendingRunnable) {
it = _pendingBakes.erase(it); it = _pendingBakes.erase(it);
@ -348,7 +347,7 @@ void AssetServer::completeSetup() {
int maxBandwidth = maxBandwidthFloat * BITS_PER_MEGABITS; int maxBandwidth = maxBandwidthFloat * BITS_PER_MEGABITS;
nodeList->setConnectionMaxBandwidth(maxBandwidth); nodeList->setConnectionMaxBandwidth(maxBandwidth);
qCInfo(asset_server) << "Set maximum bandwith per connection to" << maxBandwidthFloat << "Mb/s." 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 // get the path to the asset folder from the domain server settings
@ -362,7 +361,7 @@ void AssetServer::completeSetup() {
} }
auto assetsPathString = assetsJSONValue.toString(); auto assetsPathString = assetsJSONValue.toString();
QDir assetsPath { assetsPathString }; QDir assetsPath{ assetsPathString };
QString absoluteFilePath = assetsPath.absolutePath(); QString absoluteFilePath = assetsPath.absolutePath();
if (assetsPath.isRelative()) { if (assetsPath.isRelative()) {
@ -390,7 +389,7 @@ void AssetServer::completeSetup() {
// Check the asset directory to output some information about what we have // Check the asset directory to output some information about what we have
auto files = _filesDirectory.entryList(QDir::Files); auto files = _filesDirectory.entryList(QDir::Files);
QRegExp hashFileRegex { ASSET_HASH_REGEX_STRING }; QRegExp hashFileRegex{ ASSET_HASH_REGEX_STRING };
auto hashedFiles = files.filter(hashFileRegex); auto hashedFiles = files.filter(hashFileRegex);
qCInfo(asset_server) << "There are" << hashedFiles.size() << "asset files in the asset directory."; qCInfo(asset_server) << "There are" << hashedFiles.size() << "asset files in the asset directory.";
@ -409,7 +408,7 @@ void AssetServer::completeSetup() {
} }
void AssetServer::cleanupUnmappedFiles() { 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); auto files = _filesDirectory.entryInfoList(QDir::Files);
@ -418,7 +417,7 @@ void AssetServer::cleanupUnmappedFiles() {
for (const auto& fileInfo : files) { for (const auto& fileInfo : files) {
auto filename = fileInfo.fileName(); auto filename = fileInfo.fileName();
if (hashFileRegex.exactMatch(filename)) { if (hashFileRegex.exactMatch(filename)) {
bool matched { false }; bool matched{ false };
for (auto& pair : _fileMappings) { for (auto& pair : _fileMappings) {
if (pair.second == filename) { if (pair.second == filename) {
matched = true; matched = true;
@ -427,7 +426,7 @@ void AssetServer::cleanupUnmappedFiles() {
} }
if (!matched) { if (!matched) {
// remove the unmapped file // remove the unmapped file
QFile removeableFile { fileInfo.absoluteFilePath() }; QFile removeableFile{ fileInfo.absoluteFilePath() };
if (removeableFile.remove()) { if (removeableFile.remove()) {
qCDebug(asset_server) << "\tDeleted" << filename << "from asset files directory since it is unmapped."; qCDebug(asset_server) << "\tDeleted" << filename << "from asset files directory since it is unmapped.";
@ -479,7 +478,7 @@ void AssetServer::handleAssetMappingOperation(QSharedPointer<ReceivedMessage> me
void AssetServer::handleGetMappingOperation(ReceivedMessage& message, SharedNodePointer senderNode, NLPacketList& replyPacket) { void AssetServer::handleGetMappingOperation(ReceivedMessage& message, SharedNodePointer senderNode, NLPacketList& replyPacket) {
QString assetPath = message.readString(); QString assetPath = message.readString();
QUrl url { assetPath }; QUrl url{ assetPath };
assetPath = url.path(); assetPath = url.path();
auto it = _fileMappings.find(assetPath); auto it = _fileMappings.find(assetPath);
@ -498,7 +497,7 @@ void AssetServer::handleGetMappingOperation(ReceivedMessage& message, SharedNode
} else if (BAKEABLE_SCRIPT_EXTENSIONS.contains(assetPathExtension)) { } else if (BAKEABLE_SCRIPT_EXTENSIONS.contains(assetPathExtension)) {
bakedRootFile = BAKED_SCRIPT_SIMPLE_NAME; bakedRootFile = BAKED_SCRIPT_SIMPLE_NAME;
} }
auto originalAssetHash = it->second; auto originalAssetHash = it->second;
QString redirectedAssetHash; QString redirectedAssetHash;
QString bakedAssetPath; QString bakedAssetPath;
@ -562,7 +561,7 @@ void AssetServer::handleGetAllMappingOperation(ReceivedMessage& message, SharedN
replyPacket.writePrimitive(count); 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 mapping = it->first;
auto hash = it->second; auto hash = it->second;
replyPacket.writeString(mapping); replyPacket.writeString(mapping);
@ -603,7 +602,7 @@ void AssetServer::handleSetMappingOperation(ReceivedMessage& message, SharedNode
void AssetServer::handleDeleteMappingsOperation(ReceivedMessage& message, SharedNodePointer senderNode, NLPacketList& replyPacket) { void AssetServer::handleDeleteMappingsOperation(ReceivedMessage& message, SharedNodePointer senderNode, NLPacketList& replyPacket) {
if (senderNode->getCanWriteToAssetServer()) { if (senderNode->getCanWriteToAssetServer()) {
int numberOfDeletedMappings { 0 }; int numberOfDeletedMappings{ 0 };
message.readPrimitive(&numberOfDeletedMappings); message.readPrimitive(&numberOfDeletedMappings);
QStringList mappingsToDelete; QStringList mappingsToDelete;
@ -653,7 +652,7 @@ void AssetServer::handleRenameMappingOperation(ReceivedMessage& message, SharedN
void AssetServer::handleSetBakingEnabledOperation(ReceivedMessage& message, SharedNodePointer senderNode, NLPacketList& replyPacket) { void AssetServer::handleSetBakingEnabledOperation(ReceivedMessage& message, SharedNodePointer senderNode, NLPacketList& replyPacket) {
if (senderNode->getCanWriteToAssetServer()) { if (senderNode->getCanWriteToAssetServer()) {
bool enabled { true }; bool enabled{ true };
message.readPrimitive(&enabled); message.readPrimitive(&enabled);
int numberOfMappings{ 0 }; int numberOfMappings{ 0 };
@ -696,7 +695,7 @@ void AssetServer::handleAssetGetInfo(QSharedPointer<ReceivedMessage> message, Sh
replyPacket->write(assetHash); replyPacket->write(assetHash);
QString fileName = QString(hexHash); QString fileName = QString(hexHash);
QFileInfo fileInfo { _filesDirectory.filePath(fileName) }; QFileInfo fileInfo{ _filesDirectory.filePath(fileName) };
if (fileInfo.exists() && fileInfo.isReadable()) { if (fileInfo.exists() && fileInfo.isReadable()) {
qCDebug(asset_server) << "Opening file: " << fileInfo.filePath(); qCDebug(asset_server) << "Opening file: " << fileInfo.filePath();
@ -827,7 +826,7 @@ bool AssetServer::loadMappingsFromFile() {
auto mapFilePath = _resourcesDirectory.absoluteFilePath(MAP_FILE_NAME); auto mapFilePath = _resourcesDirectory.absoluteFilePath(MAP_FILE_NAME);
QFile mapFile { mapFilePath }; QFile mapFile{ mapFilePath };
if (mapFile.exists()) { if (mapFile.exists()) {
if (mapFile.open(QIODevice::ReadOnly)) { if (mapFile.open(QIODevice::ReadOnly)) {
QJsonParseError error; QJsonParseError error;
@ -883,7 +882,7 @@ bool AssetServer::loadMappingsFromFile() {
bool AssetServer::writeMappingsToFile() { bool AssetServer::writeMappingsToFile() {
auto mapFilePath = _resourcesDirectory.absoluteFilePath(MAP_FILE_NAME); auto mapFilePath = _resourcesDirectory.absoluteFilePath(MAP_FILE_NAME);
QFile mapFile { mapFilePath }; QFile mapFile{ mapFilePath };
if (mapFile.open(QIODevice::WriteOnly)) { if (mapFile.open(QIODevice::WriteOnly)) {
QJsonObject root; QJsonObject root;
@ -891,7 +890,7 @@ bool AssetServer::writeMappingsToFile() {
root[it.first] = it.second; root[it.first] = it.second;
} }
QJsonDocument jsonDocument { root }; QJsonDocument jsonDocument{ root };
if (mapFile.write(jsonDocument.toJson()) != -1) { if (mapFile.write(jsonDocument.toJson()) != -1) {
qCDebug(asset_server) << "Wrote JSON mappings to file at" << mapFilePath; 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 // 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 // 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"; 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; hashesToCheckForDeletion << it->second;
qCDebug(asset_server) << "Deleted a mapping:" << path << "=>" << it->second; qCDebug(asset_server) << "Deleted a mapping:" << path << "=>" << it->second;
_fileMappings.erase(it); _fileMappings.erase(it);
} else { } else {
qCDebug(asset_server) << "Unable to delete a mapping that was not found:" << path; 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 // we now have a set of hashes that are unmapped - we will delete those asset files
for (auto& hash : hashesToCheckForDeletion) { for (auto& hash : hashesToCheckForDeletion) {
// remove the unmapped file // remove the unmapped file
QFile removeableFile { _filesDirectory.absoluteFilePath(hash) }; QFile removeableFile{ _filesDirectory.absoluteFilePath(hash) };
if (removeableFile.remove()) { if (removeableFile.remove()) {
qCDebug(asset_server) << "\tDeleted" << hash << "from asset files directory since it is now unmapped."; 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<QString> bakedFilePaths) { void AssetServer::handleCompletedBake(QString originalAssetHash, QString originalAssetPath, QVector<QString> bakedFilePaths) {
bool errorCompletingBake { false }; bool errorCompletingBake{ false };
QString errorReason; QString errorReason;
qDebug() << "Completing bake for " << originalAssetHash; qDebug() << "Completing bake for " << originalAssetHash;
@ -1272,7 +1271,7 @@ std::pair<bool, AssetMeta> AssetServer::readMetaFile(AssetHash hash) {
auto it = _fileMappings.find(metaFilePath); auto it = _fileMappings.find(metaFilePath);
if (it == _fileMappings.end()) { if (it == _fileMappings.end()) {
return { false, {} }; return { false,{} };
} }
auto metaFileHash = it->second; auto metaFileHash = it->second;
@ -1309,7 +1308,7 @@ std::pair<bool, AssetMeta> AssetServer::readMetaFile(AssetHash hash) {
} }
} }
return { false, {} }; return { false,{} };
} }
bool AssetServer::writeMetaFile(AssetHash originalAssetHash, const AssetMeta& meta) { 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); auto extension = path.mid(dotIndex + 1);
QString bakedFilename; QString bakedFilename;
if (BAKEABLE_MODEL_EXTENSIONS.contains(extension)) { 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)) { } else if (BAKEABLE_TEXTURE_EXTENSIONS.contains(extension.toLocal8Bit()) && hasMetaFile(hash)) {

View file

@ -20,8 +20,7 @@
BakeAssetTask::BakeAssetTask(const AssetHash& assetHash, const AssetPath& assetPath, const QString& filePath) : BakeAssetTask::BakeAssetTask(const AssetHash& assetHash, const AssetPath& assetPath, const QString& filePath) :
_assetHash(assetHash), _assetHash(assetHash),
_assetPath(assetPath), _assetPath(assetPath),
_filePath(filePath) _filePath(filePath) {
{
} }
@ -32,7 +31,7 @@ void BakeAssetTask::run() {
TextureBakerThreadGetter fn = []() -> QThread* { return QThread::currentThread(); }; TextureBakerThreadGetter fn = []() -> QThread* { return QThread::currentThread(); };
if (_assetPath.endsWith(".fbx")) { if (_assetPath.endsWith(".fbx")) {
_baker = std::unique_ptr<FBXBaker> { _baker = std::unique_ptr<FBXBaker>{
new FBXBaker(QUrl("file:///" + _filePath), fn, PathUtils::generateTemporaryDir()) new FBXBaker(QUrl("file:///" + _filePath), fn, PathUtils::generateTemporaryDir())
}; };
} else if (_assetPath.endsWith(".js", Qt::CaseInsensitive)) { } else if (_assetPath.endsWith(".js", Qt::CaseInsensitive)) {
@ -40,9 +39,9 @@ void BakeAssetTask::run() {
new JSBaker(QUrl("file:///" + _filePath), PathUtils::generateTemporaryDir()) new JSBaker(QUrl("file:///" + _filePath), PathUtils::generateTemporaryDir())
}; };
} else { } else {
_baker = std::unique_ptr<TextureBaker> { _baker = std::unique_ptr<TextureBaker>{
new TextureBaker(QUrl("file:///" + _filePath), image::TextureUsage::CUBE_TEXTURE, new TextureBaker(QUrl("file:///" + _filePath), image::TextureUsage::CUBE_TEXTURE,
PathUtils::generateTemporaryDir()) PathUtils::generateTemporaryDir())
}; };
} }

View file

@ -18,25 +18,24 @@ const int ASCII_CHARACTERS_UPPER_LIMIT = 126;
JSBaker::JSBaker(const QUrl& jsURL, const QString& bakedOutputDir) : JSBaker::JSBaker(const QUrl& jsURL, const QString& bakedOutputDir) :
_jsURL(jsURL), _jsURL(jsURL),
_bakedOutputDir(bakedOutputDir) _bakedOutputDir(bakedOutputDir) {
{
}; };
void JSBaker::bake() { void JSBaker::bake() {
qCDebug(js_baking) << "JS Baker " << _jsURL << "bake starting"; qCDebug(js_baking) << "JS Baker " << _jsURL << "bake starting";
// Import file to start baking // Import file to start baking
QFile jsFile(_jsURL.toLocalFile()); QFile jsFile(_jsURL.toLocalFile());
if (!jsFile.open(QIODevice::ReadOnly | QIODevice::Text)) { if (!jsFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
handleError("Error opening " + _jsURL.fileName() + " for reading"); handleError("Error opening " + _jsURL.fileName() + " for reading");
return; return;
} }
// Read file into an array // Read file into an array
QByteArray inputJS = jsFile.readAll(); QByteArray inputJS = jsFile.readAll();
QByteArray outputJS; QByteArray outputJS;
// Call baking on inputJS and store result in outputJS // Call baking on inputJS and store result in outputJS
bool success = bakeJS(&inputJS, &outputJS); bool success = bakeJS(&inputJS, &outputJS);
if (!success) { if (!success) {
@ -44,23 +43,23 @@ void JSBaker::bake() {
handleError("Error unterminated multi line comment"); handleError("Error unterminated multi line comment");
return; return;
} }
// Bake Successful. Export the file // Bake Successful. Export the file
auto fileName = _jsURL.fileName(); auto fileName = _jsURL.fileName();
auto baseName = fileName.left(fileName.lastIndexOf('.')); auto baseName = fileName.left(fileName.lastIndexOf('.'));
auto bakedFilename = baseName + BAKED_JS_EXTENSION; auto bakedFilename = baseName + BAKED_JS_EXTENSION;
_bakedJSFilePath = _bakedOutputDir + "/" + bakedFilename; _bakedJSFilePath = _bakedOutputDir + "/" + bakedFilename;
QFile bakedFile; QFile bakedFile;
bakedFile.setFileName(_bakedJSFilePath); bakedFile.setFileName(_bakedJSFilePath);
if (!bakedFile.open(QIODevice::WriteOnly)) { if (!bakedFile.open(QIODevice::WriteOnly)) {
handleError("Error opening " + _bakedJSFilePath + " for writing"); handleError("Error opening " + _bakedJSFilePath + " for writing");
return; return;
} }
bakedFile.write(outputJS); bakedFile.write(outputJS);
// Export successful // Export successful
_outputFiles.push_back(_bakedJSFilePath); _outputFiles.push_back(_bakedJSFilePath);
qCDebug(js_baking) << "Exported" << _jsURL << "with re-written paths to" << _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) { bool JSBaker::bakeJS(const QByteArray* inputFile, QByteArray* outputFile) {
// Read from inputFile and write to outputFile per character // Read from inputFile and write to outputFile per character
QTextStream in(*inputFile,QIODevice::ReadOnly); QTextStream in(*inputFile, QIODevice::ReadOnly);
QTextStream out(outputFile, QIODevice::WriteOnly); QTextStream out(outputFile, QIODevice::WriteOnly);
// Algorithm requires the knowledge of previous and next character for each character read // 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; QChar nextCharacter;
// Initialize previousCharacter with new line // Initialize previousCharacter with new line
QChar previousCharacter = '\n'; QChar previousCharacter = '\n';
in >> currentCharacter; in >> currentCharacter;
while (!in.atEnd()) { while (!in.atEnd()) {
in >> nextCharacter; in >> nextCharacter;
if (currentCharacter == '\r') { if (currentCharacter == '\r') {
out << '\n'; out << '\n';
} else if (currentCharacter == '/') { } else if (currentCharacter == '/') {
// Check if single line comment i.e. // // Check if single line comment i.e. //
if (nextCharacter == '/') { if (nextCharacter == '/') {
handleSingleLineComments(&in); handleSingleLineComments(&in);
//Start fresh after handling comments //Start fresh after handling comments
previousCharacter = '\n'; previousCharacter = '\n';
in >> currentCharacter; in >> currentCharacter;
@ -111,7 +110,7 @@ bool JSBaker::bakeJS(const QByteArray* inputFile, QByteArray* outputFile) {
// If '/' is not followed by '/' or '*' print '/' // If '/' is not followed by '/' or '*' print '/'
out << currentCharacter; out << currentCharacter;
} }
} else if (isSpaceOrTab(currentCharacter)) { } else if (isSpaceOrTab(currentCharacter)) {
// Check if white space or tab // Check if white space or tab
// Skip multiple spaces or tabs // Skip multiple spaces or tabs
@ -126,20 +125,20 @@ bool JSBaker::bakeJS(const QByteArray* inputFile, QByteArray* outputFile) {
if (!canOmitSpace(previousCharacter, nextCharacter)) { if (!canOmitSpace(previousCharacter, nextCharacter)) {
out << ' '; out << ' ';
} }
} else if (currentCharacter == '\n') { } else if (currentCharacter == '\n') {
// Check if new line // Check if new line
//Skip multiple new lines //Skip multiple new lines
//Skip new line followed by space or tab //Skip new line followed by space or tab
while (nextCharacter == '\n' || isSpaceOrTab(nextCharacter)) { while (nextCharacter == '\n' || isSpaceOrTab(nextCharacter)) {
in >> nextCharacter; in >> nextCharacter;
} }
// Check if new line can be omitted // Check if new line can be omitted
if (!canOmitNewLine(previousCharacter, nextCharacter)) { if (!canOmitNewLine(previousCharacter, nextCharacter)) {
out << '\n'; out << '\n';
} }
} else if (isQuote(currentCharacter)) { } else if (isQuote(currentCharacter)) {
// Print the current quote and nextCharacter as is // Print the current quote and nextCharacter as is
out << currentCharacter; out << currentCharacter;
out << nextCharacter; out << nextCharacter;
@ -160,17 +159,17 @@ bool JSBaker::bakeJS(const QByteArray* inputFile, QByteArray* outputFile) {
} else { } else {
// In all other cases write the currentCharacter to outputFile // In all other cases write the currentCharacter to outputFile
out << currentCharacter; out << currentCharacter;
} }
previousCharacter = currentCharacter; previousCharacter = currentCharacter;
currentCharacter = nextCharacter; currentCharacter = nextCharacter;
} }
//write currentCharacter to output file when nextCharacter reaches EOF //write currentCharacter to output file when nextCharacter reaches EOF
if (currentCharacter != '\n') { if (currentCharacter != '\n') {
out << currentCharacter; out << currentCharacter;
} }
// Successful bake. Return true // Successful bake. Return true
return true; return true;
} }
@ -200,19 +199,19 @@ bool JSBaker::handleMultiLineComments(QTextStream* in) {
bool JSBaker::canOmitSpace(QChar previousCharacter, QChar nextCharacter) { bool JSBaker::canOmitSpace(QChar previousCharacter, QChar nextCharacter) {
return(!((isAlphanum(previousCharacter) || isNonAscii(previousCharacter) || isSpecialCharacter(previousCharacter)) && 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) { bool JSBaker::canOmitNewLine(QChar previousCharacter, QChar nextCharacter) {
return (!((isAlphanum(previousCharacter) || isNonAscii(previousCharacter) || isSpecialCharacterPrevious(previousCharacter)) && 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 //Check if character is alphabet, number or one of the following: '_', '$', '\\' or a non-ASCII character
bool JSBaker::isAlphanum(QChar c) { 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); || c == '_' || c == '$' || c == '\\' || c > ASCII_CHARACTERS_UPPER_LIMIT);
} }

View file

@ -22,13 +22,13 @@ class JSBaker : public Baker {
public: public:
JSBaker(const QUrl& jsURL, const QString& bakedOutputDir); JSBaker(const QUrl& jsURL, const QString& bakedOutputDir);
public slots: public slots:
virtual void bake() override; virtual void bake() override;
public: public:
static bool bakeJS(const QByteArray* inputFile, QByteArray* outputFile); static bool bakeJS(const QByteArray* inputFile, QByteArray* outputFile);
private : private:
QUrl _jsURL; QUrl _jsURL;
QString _bakedOutputDir; QString _bakedOutputDir;
QString _bakedJSFilePath; QString _bakedJSFilePath;

View file

@ -14,7 +14,7 @@ QTEST_MAIN(JSBakerTest)
void JSBakerTest::setTestCases() { void JSBakerTest::setTestCases() {
// Test cases contain a std::pair(input, desiredOutput) // Test cases contain a std::pair(input, desiredOutput)
_testCases.emplace_back("var a=1;", "var a=1;"); _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("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\rb", "a\nb");
@ -22,8 +22,8 @@ void JSBakerTest::setTestCases() {
_testCases.emplace_back("a/b", "a/b"); _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 spaces omitted
_testCases.emplace_back("var a= 1;", "var a=1;"); // Multiple tabs 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("var x", "var x");
_testCases.emplace_back("a '", "a '"); _testCases.emplace_back("a '", "a '");
_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 whitespace
_testCases.emplace_back("a\n\n b", "a\nb"); // Skip multiple new lines followed by tab _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\nb", "a\nb");
_testCases.emplace_back("a\n9", "a\n9"); _testCases.emplace_back("a\n9", "a\n9");
_testCases.emplace_back("9\na", "9\na"); _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"); _testCases.emplace_back("+\na", "+\na");
_testCases.emplace_back("-\na", "-\na"); _testCases.emplace_back("-\na", "-\na");
// Cases to check quoted strings are not modified // 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$%^&[](){}\"\na"); _testCases.emplace_back("\"abcd1234$%^&[](){}\"\na", "\"abcd1234$%^&[](){}\"\na");
_testCases.emplace_back("`abcd1234$%^&[](){}`\na", "`abcd1234$%^&[](){}`a"); _testCases.emplace_back("`abcd1234$%^&[](){}`\na", "`abcd1234$%^&[](){}`a");
// Edge Cases // Edge Cases
//No semicolon to terminate an expression, instead a new line used for termination //No semicolon to terminate an expression, instead a new line used for termination
@ -77,12 +77,12 @@ void JSBakerTest::setTestCases() {
} }
void JSBakerTest::testJSBaking() { void JSBakerTest::testJSBaking() {
for (int i = 0;i < _testCases.size();i++) { for (int i = 0;i < _testCases.size();i++) {
QByteArray output; QByteArray output;
auto input = _testCases.at(i).first; auto input = _testCases.at(i).first;
JSBaker::bakeJS(&input, &output); JSBaker::bakeJS(&input, &output);
auto desiredOutput = _testCases.at(i).second; auto desiredOutput = _testCases.at(i).second;
QCOMPARE(output, desiredOutput); QCOMPARE(output, desiredOutput);
} }

View file

@ -15,12 +15,12 @@
#include <QtTest/QtTest> #include <QtTest/QtTest>
#include "../../libraries/baking/src/JSBaker.h" #include "../../libraries/baking/src/JSBaker.h"
class JSBakerTest: public QObject { class JSBakerTest : public QObject {
Q_OBJECT Q_OBJECT
private slots: private slots:
void setTestCases(); void setTestCases();
void testJSBaking(); void testJSBaking();
private: private:
std::vector<std::pair<QByteArray, QByteArray>> _testCases; std::vector<std::pair<QByteArray, QByteArray>> _testCases;