luaengineapp/scripts/luaenginec.lea

131 lines
4.8 KiB
Text

#!/usr/bin/env luaengine
--[[
******************************************************************************
* luaenginec LuaEngine Compiler
* Copyright (C) 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.
******************************************************************************
--]]
local sstubDir
local mainWindow
local currentSstub
local compilerPath
local compileButton
local currentFilter
local scriptLineEdit
local outputLineEdit
local sstubsTable = {}
local radioButtonFile = {}
local radioButtonFilter = {}
function main()
local initOk = init()
if (initOk == false) then
return 1
end
mainWindow = createMainWindow("LuaEngine Compiler")
local mainLayout = createLayout(VerticalLayout, mainWindow)
createLabel("<strong>LuaEngine Compiler</strong>", mainLayout)
local scriptLayout = createLayout(HorizontalLayout, mainLayout)
createLabel("Script:", scriptLayout)
scriptLineEdit = createLineEdit("", scriptLayout)
setWidgetReadOnly(scriptLineEdit)
local scriptToolButton = createToolButton("...", scriptLayout)
connect(scriptToolButton, "clicked()", "scriptButtonPressed")
local radioButton = createRadioButton("LuaEngine Bytecode", mainLayout)
currentFilter = "LuaEngine Bytecode (*.luac *.leac)"
radioButtonFile[radioButton] = nil
radioButtonFilter[radioButton] = currentFilter
connect(radioButton, "clicked()", "radioButtonClicked")
setWidgetChecked(radioButton, true)
for index, subtable in ipairs(sstubsTable) do
radioButton = createRadioButton(subtable["name"], mainLayout)
radioButtonFile[radioButton] = subtable["file"]
radioButtonFilter[radioButton] = subtable["filter"]
connect(radioButton, "clicked()", "radioButtonClicked")
end
local buttonLayout = createLayout(HorizontalLayout, mainLayout)
createSpacerItem(SizePolicyExpanding, SizePolicyMinimum, buttonLayout)
compileButton = createPushButton("&Compile", buttonLayout)
setWidgetEnabled(compileButton, false)
connect(compileButton, "clicked()", "compileScript")
setWidgetSize(mainWindow, 300, 0)
setWidgetFixed(mainWindow, true)
showWidget(mainWindow, ShowDefault)
return GuiExecuted
end
function init()
local binDir = getDirectoryPath(_LuaEngineRT)
if (luaEnginePlatform() == "Windows") then
compilerPath = binDir.."/luaenginec.exe"
if (checkFileExists(compilerPath)) then
sstubDir = binDir.."/sstubs"
for index, file in ipairs(getDirectoryContent(sstubDir, "*.json", Files)) do
local handle = io.open(file, "r")
if (handle ~= nil) then
local content = handle:read("*a")
table.insert(sstubsTable, jsonToTable(content))
handle:close()
end
end
else
showMessageBox(WarningMessageBox, "LuaEngine Compiler not found!", "LuaEngine Compiler")
return false
end
else
showMessageBox(InfoMessageBox, "LuaEngine Compiler only supported on Windows now.", "LuaEngine Compiler")
return false
end
return true
end
function scriptButtonPressed()
local filePath = showFileDialog(OpenFileDialog, "Select LuaEngine script...", "LuaEngine scripts (*.lua *.lea)", mainWindow)
if (filePath ~= nil) then
setObjectText(scriptLineEdit, filePath)
setWidgetEnabled(compileButton, true)
end
end
function radioButtonClicked(radioButton)
currentSstub = radioButtonFile[radioButton]
currentFilter = radioButtonFilter[radioButton]
end
function compileScript()
local filePath = showFileDialog(SaveFileDialog, "Select output file...", currentFilter, mainWindow)
if (filePath ~= nil) then
local compileArgs = { getObjectText(scriptLineEdit) }
if (currentSstub ~= nil) then
compileArgs[2] = "--luaengine"
compileArgs[3] = sstubDir.."/"..currentSstub
compileArgs[4] = filePath
else
compileArgs[2] = filePath
end
if (executeProcess(compilerPath, compileArgs, false) == false) then
showMessageBox(WarningMessageBox, "Error occurred while compile!", "LuaEngine Compiler")
end
end
end