luaengineapp/src/luaenginec/main.cpp

134 lines
4.2 KiB
C++
Raw Permalink Normal View History

2019-10-14 21:19:14 +02:00
/*****************************************************************************
* luaEngine Lua Engine for Qt
* Copyright (C) 2018-2019 Syping
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/
#include "LuaEngine.h"
#include <QCommandLineParser>
#include <QCommandLineOption>
#include <QCoreApplication>
#include <QTextStream>
#include <QSaveFile>
#include <QFile>
2019-10-17 14:25:49 +02:00
#include <QDir>
2019-10-14 21:19:14 +02:00
2021-05-01 15:32:32 +02:00
#if QT_VERSION >= 0x050F00
#define le_endl Qt::endl
#else
#define le_endl endl
#endif
2019-10-14 21:19:14 +02:00
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
2019-10-16 22:07:14 +02:00
a.setApplicationName("LuaEngine Compiler");
2019-10-14 21:19:14 +02:00
a.setApplicationVersion("0.1");
QCommandLineParser parser;
2019-10-17 14:25:49 +02:00
QCommandLineOption pLuaEngineOption(QStringList() << "le" << "luaengine", "Location of portable LuaEngine executor.", "luaengine");
2019-10-14 21:19:14 +02:00
parser.addOption(pLuaEngineOption);
parser.addPositionalArgument("source", "Source file to build.");
parser.addPositionalArgument("output", "Output file.");
parser.addHelpOption();
parser.addVersionOption();
parser.process(a);
2019-10-17 14:25:49 +02:00
QString inputFile;
QString outputFile;
2019-10-14 21:19:14 +02:00
const QStringList args = parser.positionalArguments();
2019-10-17 14:25:49 +02:00
if (args.length() < 1) {
2021-05-01 15:32:32 +02:00
QTextStream(stderr) << "Error: No input files." << le_endl;
2019-10-17 14:25:49 +02:00
return 1;
}
else {
inputFile = args.at(0);
}
if (args.length() < 2) {
outputFile = QDir::current().absolutePath() + "/luaenginec.out";
}
else {
outputFile = args.at(1);
}
2019-10-14 21:19:14 +02:00
2019-10-17 14:25:49 +02:00
QFile luaScript(inputFile);
if (!luaScript.open(QIODevice::ReadOnly)) {
2021-05-01 15:32:32 +02:00
QTextStream(stderr) << "Error: Failed to open \"" << inputFile << "\"." << le_endl;
2019-10-17 14:25:49 +02:00
return 1;
}
if (QString::fromUtf8(luaScript.read(2)) == "#!") {
luaScript.readLine();
}
else {
luaScript.reset();
}
2019-10-14 21:19:14 +02:00
LuaEngine luaEngine;
2019-10-17 14:25:49 +02:00
if (!luaEngine.loadLuaScript(luaScript.readAll())) {
2021-05-01 15:32:32 +02:00
QTextStream(stderr) << "Error: Failed to load \"" << inputFile << "\"." << le_endl;
2019-10-14 21:19:14 +02:00
return 1;
}
2019-10-17 14:25:49 +02:00
QSaveFile outputSaveFile(outputFile);
if (!outputSaveFile.open(QIODevice::WriteOnly)) {
2021-05-01 15:32:32 +02:00
QTextStream(stderr) << "Error: Failed to open \"" << outputFile << "\"." << le_endl;
2019-10-14 21:19:14 +02:00
return 1;
}
QString pLuaEngine = parser.value(pLuaEngineOption);
if (!pLuaEngine.isEmpty()) {
QFile portableLuaEngine(pLuaEngine);
if (!portableLuaEngine.open(QIODevice::ReadOnly)) {
2019-10-17 14:25:49 +02:00
outputSaveFile.cancelWriting();
outputSaveFile.commit();
2021-05-01 15:32:32 +02:00
QTextStream(stderr) << "Error: Failed to open \"" << pLuaEngine << "\"." << le_endl;
2019-10-14 21:19:14 +02:00
return 1;
}
2019-10-17 14:25:49 +02:00
outputSaveFile.write(portableLuaEngine.readAll());
2019-10-14 21:19:14 +02:00
}
const QByteArray luaDump = luaEngine.dumpLuaScript();
int dumpSize = luaDump.size();
2019-10-17 14:25:49 +02:00
outputSaveFile.write(luaDump);
2019-10-14 21:19:14 +02:00
if (!pLuaEngine.isEmpty()) {
QByteArray lengthArray = QByteArray::number(dumpSize, 16);
if (lengthArray.size() > 8) {
2019-10-17 14:25:49 +02:00
outputSaveFile.cancelWriting();
outputSaveFile.commit();
2021-05-01 15:32:32 +02:00
QTextStream(stderr) << "Error: Lua Engine script is too large." << le_endl;
2019-10-14 21:19:14 +02:00
return 1;
}
else {
while (lengthArray.size() != 8) {
lengthArray.insert(0, "0");
}
}
2019-10-17 14:25:49 +02:00
outputSaveFile.write(QByteArray::fromHex(lengthArray));
outputSaveFile.write("\xb4\x00", 2);
2019-10-14 21:19:14 +02:00
}
2019-10-17 14:25:49 +02:00
if (!outputSaveFile.commit()) {
2021-05-01 15:32:32 +02:00
QTextStream(stderr) << "Error: Failed to write \"" << args.at(1) << "\"." << le_endl;
2019-10-14 21:19:14 +02:00
return 1;
}
return 0;
}