luaengineapp/src/luaenginec/main.cpp

101 lines
3.3 KiB
C++
Raw 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>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
a.setApplicationName("luaenginec");
a.setApplicationVersion("0.1");
QCommandLineParser parser;
QCommandLineOption pLuaEngineOption(QStringList() << "le" << "luaengine", "Location of portable Lua Engine executor.", "luaengine");
parser.addOption(pLuaEngineOption);
parser.addPositionalArgument("source", "Source file to build.");
parser.addPositionalArgument("output", "Output file.");
parser.addHelpOption();
parser.addVersionOption();
parser.process(a);
const QStringList args = parser.positionalArguments();
QFile luaScript(args.at(0));
LuaEngine luaEngine;
if (!luaEngine.loadLuaScript(&luaScript)) {
QTextStream(stderr) << "Failed to load \"" << args.at(0) << "\"." << endl;
return 1;
}
QSaveFile outputFile(args.at(1));
if (!outputFile.open(QIODevice::WriteOnly)) {
QTextStream(stderr) << "Failed to open \"" << args.at(1) << "\"." << endl;
return 1;
}
QString pLuaEngine = parser.value(pLuaEngineOption);
if (!pLuaEngine.isEmpty()) {
QFile portableLuaEngine(pLuaEngine);
if (!portableLuaEngine.open(QIODevice::ReadOnly)) {
outputFile.cancelWriting();
outputFile.commit();
QTextStream(stderr) << "Failed to open \"" << pLuaEngine << "\"." << endl;
return 1;
}
outputFile.write(portableLuaEngine.readAll());
}
const QByteArray luaDump = luaEngine.dumpLuaScript();
int dumpSize = luaDump.size();
outputFile.write(luaDump);
if (!pLuaEngine.isEmpty()) {
QByteArray lengthArray = QByteArray::number(dumpSize, 16);
if (lengthArray.size() > 8) {
outputFile.cancelWriting();
outputFile.commit();
QTextStream(stderr) << "Lua Engine script is too large." << endl;
return 1;
}
else {
while (lengthArray.size() != 8) {
lengthArray.insert(0, "0");
}
}
outputFile.write(QByteArray::fromHex(lengthArray));
outputFile.write("\xb4\x00", 2);
}
if (!outputFile.commit()) {
QTextStream(stderr) << "Failed to write \"" << args.at(1) << "\"." << endl;
return 1;
}
return 0;
}