From cb9f154f1eed7397ee899078cf47ad7ce9504475 Mon Sep 17 00:00:00 2001 From: Lucas Schwiderski Date: Sat, 18 Feb 2023 10:18:53 +0100 Subject: [PATCH] fix(sdk): Fix lua file compilation Aussiemon provided the last missing piece of information about the binary format. --- lib/sdk/src/filetype/lua.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/sdk/src/filetype/lua.rs b/lib/sdk/src/filetype/lua.rs index 3ce8bdd..ea072b6 100644 --- a/lib/sdk/src/filetype/lua.rs +++ b/lib/sdk/src/filetype/lua.rs @@ -1,10 +1,12 @@ use std::ffi::CStr; use std::io::Cursor; +use std::io::Write; use color_eyre::eyre; use color_eyre::Result; use luajit2_sys as lua; +use crate::binary::sync::WriteExt; use crate::bundle::file::{BundleFileVariant, UserFile}; use crate::{BundleFile, BundleFileType}; @@ -83,16 +85,19 @@ where } }; - // TODO: How are these twelve bytes generated? - let mut data = vec![0x0; 12]; - // Set Fatshark's custom LuaJIT magic bytes - data.extend_from_slice(&[0x1b, 0x46, 0x53, 0x82]); - data.extend_from_slice(&bytecode[4..]); + let mut data = Cursor::new(Vec::with_capacity(bytecode.len() + 12)); + data.write_u32(bytecode.len() as u32)?; + // TODO: Figure out what these two values are + data.write_u32(0x2)?; + data.write_u32(0x0)?; + // Use Fatshark's custom magic bytes + data.write_all(&[0x1b, 0x46, 0x53, 0x82])?; + data.write_all(&bytecode[4..])?; let mut file = BundleFile::new(name, BundleFileType::Lua); let mut variant = BundleFileVariant::new(); - variant.set_data(data); + variant.set_data(data.into_inner()); file.add_variant(variant); Ok(file)