Pkg.LuaDecompiler — API for decompiling Lua compiled bytecode

Overview

The Pkg.LuaDecompiler module contains the API for decompiling Lua compiled bytecode (LUAC) back into readable Lua source code. It supports all major Lua versions from 5.0 through 5.4 and operates on a parsed Pkg.LUAC.LUACObject instance. The decompiler performs symbolic register interpretation, control flow reconstruction (if/while/for/repeat), table and string concatenation recovery, and nested function prototype rendering.

Decompiling a LUAC File

The following code example demonstrates how to decompile a Lua bytecode file:

from Pro.Core import *
from Pkg.LUAC import *
from Pkg.LuaDecompiler import LuaDecompiler

def decompileLUAC(fname):
    c = createContainerFromFile(fname)
    if c.isNull():
        return
    luac = LUACObject()
    if not luac.Load(c) or not luac.Initialize():
        return
    dc = LuaDecompiler(luac)
    out = proTextStream()
    if not dc.decompile(out):
        return
    print(out.buffer)

Decompiling a Single Function

The following code example demonstrates how to decompile a single function prototype from the function tree:

from Pro.Core import *
from Pkg.LUAC import *
from Pkg.LuaDecompiler import LuaDecompiler

def decompileInnerFunction(fname, index):
    c = createContainerFromFile(fname)
    if c.isNull():
        return
    luac = LUACObject()
    if not luac.Load(c) or not luac.Initialize():
        return
    main = luac.GetFunction()
    if index >= len(main.protos):
        return
    dc = LuaDecompiler(luac)
    out = proTextStream()
    if not dc.decompileFunction(out, main.protos[index]):
        return
    print(out.buffer)

Decompiling with a Wait Object

Decompilation of large bytecode files can be long-running. A wait object can be passed to the constructor to make the operation interruptible:

from Pro.Core import *
from Pkg.LUAC import *
from Pkg.LuaDecompiler import LuaDecompiler

def decompileLUACWithWait(fname, wo):
    c = createContainerFromFile(fname)
    if c.isNull():
        return
    luac = LUACObject()
    if not luac.Load(c) or not luac.Initialize():
        return
    dc = LuaDecompiler(luac, wo=wo)
    out = proTextStream()
    if not dc.decompile(out):
        return
    print(out.buffer)

Module API

Pkg.LuaDecompiler module API.

Classes:

LuaDecompiler(luac, *, wo)

Decompiler for Lua compiled bytecode.

class LuaDecompiler(luac: CFFObject, *, wo: Optional[NTIWait] = None)

Decompiler for Lua compiled bytecode. Supports Lua 5.0 through 5.4.

This class takes a parsed LUACObject and reconstructs Lua source code from its bytecode.

Usage:

from Pkg.LUAC import LUACObject
from Pkg.LuaDecompiler import LuaDecompiler

luac = LUACObject()
luac.Load(stream)
luac.Initialize()

dc = LuaDecompiler(luac)
out = proTextStream()
dc.decompile(out)
source = out.buffer

Initializes the decompiler with a parsed LUAC object.

Parameters
  • luac (LUACObject) – A parsed LUAC object.

  • wo (Optional[NTIWait]) – Optional wait object for long-running operations.

Methods:

decompile(out)

Decompiles the entire LUAC file and writes the reconstructed Lua source code to the output stream.

decompileFunction(out, func)

Decompiles a single function prototype and writes the reconstructed Lua source code to the output stream.

decompile(out: NTTextStream)bool

Decompiles the entire LUAC file and writes the reconstructed Lua source code to the output stream.

Parameters

out (NTTextStream) – The output text stream.

Returns

Returns True if successful; otherwise returns False.

Return type

bool

See also decompileFunction().

decompileFunction(out: NTTextStream, func: object)bool

Decompiles a single function prototype and writes the reconstructed Lua source code to the output stream.

Parameters
  • out (NTTextStream) – The output text stream.

  • func (object) – The function prototype to decompile.

Returns

Returns True if successful; otherwise returns False.

Return type

bool

See also decompile().