1 unstable release
| 0.2.0 | Jun 5, 2025 |
|---|
#56 in #reverse-engineering
465KB
8K
SLoC
luadec
A Lua 5.1 bytecode decompiler library written in Rust, originated from the lbcdec project.
Installation
Add this to your Cargo.toml:
[dependencies]
luadec = "0.2.0"
Usage
As a Library
Basic Decompilation
use luadec::LuaDecompiler;
// Read bytecode from file
let bytecode = std::fs::read("script.luac")?;
// Create decompiler and decompile
let decompiler = LuaDecompiler::new();
let source_code = decompiler.decompile(&bytecode)?;
println!("{}", source_code);
With Custom Options
use luadec::{LuaDecompiler, DecompileOptions};
let options = DecompileOptions {
include_debug_comments: true,
indent_width: 4,
include_prototype_numbers: true,
};
let decompiler = LuaDecompiler::with_options(options);
let result = decompiler.decompile(&bytecode)?;
Batch Processing
use luadec::batch::{BatchOptions, DecompileOptions};
let batch_options = BatchOptions {
parallel: true,
output_extension: "lua".to_string(),
decompile_options: DecompileOptions::default(),
};
// Decompile all .luac files in a directory
let results = luadec::batch::decompile_bytecode_files(
"*.luac",
Some("output/"),
batch_options,
)?;
File Operations
use luadec::LuaDecompiler;
let decompiler = LuaDecompiler::new();
// Decompile file to file
decompiler.decompile_file_to_file("input.luac", "output.lua")?;
// Decompile file to string
let source = decompiler.decompile_file("input.luac")?;
Configuration Options
DecompileOptions
pub struct DecompileOptions {
/// Include debug comments in output
pub include_debug_comments: bool,
/// Number of spaces for indentation
pub indent_width: usize,
/// Include prototype numbers in function definitions
pub include_prototype_numbers: bool,
}
BatchOptions
pub struct BatchOptions {
/// Enable parallel processing
pub parallel: bool,
/// Output file extension
pub output_extension: String,
/// Decompilation options
pub decompile_options: DecompileOptions,
}
Error Handling
The library provides detailed error information:
use luadec::{DecompileError, DecompileResult};
match decompiler.decompile(&bytecode) {
Ok(source) => println!("Success: {}", source),
Err(DecompileError::InvalidBytecode(msg)) => {
eprintln!("Invalid bytecode: {}", msg);
},
Err(DecompileError::DecompileError(msg)) => {
eprintln!("Decompilation failed: {}", msg);
},
Err(DecompileError::IoError(err)) => {
eprintln!("IO error: {}", err);
},
}
Performance
- Memory Efficient: Processes bytecode without loading entire files into memory
- Parallel Processing: Batch operations can utilize multiple CPU cores
- Optimized Parsing: Efficient bytecode parsing with minimal allocations
Limitations
- Lua 5.1 Only: Currently supports Lua 5.1 bytecode format only
- Debug Information: Some debug information may be lost during compilation
- Variable Names: Local variable names may be generic if debug info is stripped
- Comments: Original source comments are not preserved in bytecode
Development
Building from Source
git clone https://github.com/ItsLucas/luadec.git
cd luadec
cargo build --release
Running Tests
cargo test
Generating Documentation
cargo doc --open
Related Projects
- lbcdec: The original project this library is based on
License
This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.
Acknowledgments
- Original
lbcdecproject by Dwayne Slater
Changelog
Version 0.2.0
- Initial Release
Dependencies
~4–11MB
~228K SLoC