Lua compilation API: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Created)
 
(Add Lua example.)
Line 50: Line 50:
===Lua example===
===Lua example===
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
--TODO
local compile = 1
local debug = 0
local obfuscate = 3
local scriptsToCompile = {"server.lua"}
local errorCodes = {["ERROR Nothing to do - Please select compile and/or obfuscate"] = true, ["ERROR Could not compile file"] = true, ["ERROR Could not read file"] = true}
 
local function loadScript(pPath)
    local scriptExist = fileExists(pPath)
 
    if scriptExist then
        local scriptFile = fileOpen(pPath)
 
        if scriptFile then
            local scriptSize = fileGetSize(scriptFile)
            local scriptCode = fileRead(scriptFile, scriptSize)
 
            fileClose(scriptFile)
 
            return scriptCode
        else
            outputDebugString("[Script compile]: Failed to open "..pPath..".")
        end
    else
        outputDebugString("[Script compile]: "..pPath.." doesn't exist.")
    end
end
 
local function compileCallback(pCompiledCode, pErrors, pScript)
    if pErrors == 0 then
        local haveError = errorCodes[pCompiledCode]
 
        if haveError then
            outputDebugString("[Script compile]: Failed with error: "..pCompiledCode)
            return
        end
 
        local compiledScript = fileCreate("compiled/"..pScript)
 
        if compiledScript then
            fileWrite(compiledScript, pCompiledCode)
            fileClose(compiledScript)
 
            outputDebugString("[Script compile]: "..pScript.." compiled successfully.")
        else
            outputDebugString("[Script compile]: Failed to create "..pScript..".")
        end
    else
        outputDebugString("[Script compile]: Couldn't compile "..pScript..".")
    end
end
 
local function compileScripts()
    local scriptName = false
    local fetchURL = string.format("https://luac.mtasa.com/?compile=%d&debug=%d&obfuscate=%d", compile, debug, obfuscate)
 
    for scriptID = 1, #scriptsToCompile do
        scriptName = scriptsToCompile[scriptID]
 
        fetchRemote(fetchURL, compileCallback, loadScript(scriptName), true, scriptName)
    end
end
addEventHandler("onResourceStart", resourceRoot, compileScripts)
</syntaxhighlight>
</syntaxhighlight>



Revision as of 15:38, 16 May 2021

This page is a guide for Lua compilation API

Syntax

POST "http://luac.mtasa.com/index.php"

Parameters

  • luasource: type file the file content
  • compile: type value set to 1 to enable compilation
  • debug: type value set to 1 to enable debug information
  • luasource: type value
    • Set to 1 to enable some obfuscation
    • Set to 2 to enable more obfuscation (From 1.5.2-9.07903)
    • Set to 3 to enable even more obfuscation (From 1.5.6-9.18728)

Examples

Linux example using curl

FROM="example.lua"
TO="compiled.lua"
curl -s -X POST -F compile=1 -F debug=0 -F obfuscate=3 -F luasource=@$FROM http://luac.mtasa.com/ > $TO

Linux example using luac replacement

luac_mta -e3 -o compiled.lua example.lua
if [ $? -ne 0 ]; then
   echo "Error"
fi

Need luac_mta (R13 2019-07-12) for Linux 32 bit or Linux 64 bit

Windows batch file example using curl

set FROM="example.lua"
set TO="compiled.lua"
curl.exe -s -X POST -F compile=1 -F debug=0 -F obfuscate=3 -F luasource=@%FROM% http://luac.mtasa.com/ > %TO%

Get curl.exe
(Original from http://curl.haxx.se/download.html)

Windows example using luac.exe replacement

luac_mta.exe -e3 -o compiled.lua example.lua
IF NOT ERRORLEVEL 1 goto lp1
   echo "Error"
:lp1

Get luac_mta.exe (R12 2019-07-12) only x86

Lua example

local compile = 1
local debug = 0
local obfuscate = 3
local scriptsToCompile = {"server.lua"}
local errorCodes = {["ERROR Nothing to do - Please select compile and/or obfuscate"] = true, ["ERROR Could not compile file"] = true, ["ERROR Could not read file"] = true}

local function loadScript(pPath)
    local scriptExist = fileExists(pPath)

    if scriptExist then
        local scriptFile = fileOpen(pPath)

        if scriptFile then
            local scriptSize = fileGetSize(scriptFile)
            local scriptCode = fileRead(scriptFile, scriptSize)

            fileClose(scriptFile)

            return scriptCode
        else
            outputDebugString("[Script compile]: Failed to open "..pPath..".")
        end
    else
        outputDebugString("[Script compile]: "..pPath.." doesn't exist.")
    end
end

local function compileCallback(pCompiledCode, pErrors, pScript)
    if pErrors == 0 then
        local haveError = errorCodes[pCompiledCode]

        if haveError then
            outputDebugString("[Script compile]: Failed with error: "..pCompiledCode)
            return
        end

        local compiledScript = fileCreate("compiled/"..pScript)

        if compiledScript then
            fileWrite(compiledScript, pCompiledCode)
            fileClose(compiledScript)

            outputDebugString("[Script compile]: "..pScript.." compiled successfully.")
        else
            outputDebugString("[Script compile]: Failed to create "..pScript..".")
        end
    else
        outputDebugString("[Script compile]: Couldn't compile "..pScript..".")
    end
end

local function compileScripts()
    local scriptName = false
    local fetchURL = string.format("https://luac.mtasa.com/?compile=%d&debug=%d&obfuscate=%d", compile, debug, obfuscate)

    for scriptID = 1, #scriptsToCompile do
        scriptName = scriptsToCompile[scriptID]

        fetchRemote(fetchURL, compileCallback, loadScript(scriptName), true, scriptName)
    end
end
addEventHandler("onResourceStart", resourceRoot, compileScripts)

Changelog

Version Description
2014-08-10 encrypt has been renamed obfuscate.
blockdecompile has been removed.