diff --git a/Cargo.lock b/Cargo.lock index f9fe27d3..6719a57c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1510,7 +1510,7 @@ dependencies = [ [[package]] name = "lune" -version = "0.8.6" +version = "0.8.6-1" dependencies = [ "anyhow", "clap", @@ -1522,6 +1522,7 @@ dependencies = [ "lune-roblox", "lune-std", "lune-utils", + "lz4-compression", "mlua", "mlua-luau-scheduler", "once_cell", @@ -1730,6 +1731,12 @@ dependencies = [ "lz4-sys", ] +[[package]] +name = "lz4-compression" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "761104bf97f13a3caf47d822498a0760a10d00d220148bac2669f63fc3bb8270" + [[package]] name = "lz4-sys" version = "1.9.4" diff --git a/crates/lune/Cargo.toml b/crates/lune/Cargo.toml index 71e293c7..11482b94 100644 --- a/crates/lune/Cargo.toml +++ b/crates/lune/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "lune" -version = "0.8.6" +version = "0.8.6-1" edition = "2021" license = "MPL-2.0" repository = "https://github.com/lune-org/lune" @@ -54,6 +54,7 @@ mlua = { version = "0.9.7", features = ["luau"] } mlua-luau-scheduler = { version = "0.0.2", path = "../mlua-luau-scheduler" } postcard = { version = "1.0.8", default-features = false } +lz4-compression = { version = "0.7.0" } anyhow = "1.0" console = "0.15" dialoguer = "0.11" diff --git a/crates/lune/src/standalone/metadata.rs b/crates/lune/src/standalone/metadata.rs index 9c6227b3..94f4c936 100644 --- a/crates/lune/src/standalone/metadata.rs +++ b/crates/lune/src/standalone/metadata.rs @@ -63,25 +63,19 @@ impl Metadata { scripts: Vec, ) -> Result> { let mut patched_bin = fs::read(base_exe_path).await?; + let decompresesd_bin = postcard::to_extend(&Self { scripts }, Vec::new()).unwrap(); - // Append metadata to the end - // NOTE: if the 512000 limit ever becomes an issue, use the heapless crate - let mut buffer = [0u8; 512000]; + // Append compressed binary + let compressed_bin = lz4_compression::compress::compress(&decompresesd_bin); + let compressed_len = &compressed_bin.len(); + patched_bin.extend(compressed_bin); - let bytes = postcard::to_slice(&Self { scripts }, &mut buffer).unwrap(); - patched_bin.extend_from_slice(bytes); - - // Append the length of metadata to the end - let mut buffer = [0u8; 2]; - let length_as_bytes = postcard::to_slice(&bytes.len(), &mut buffer).unwrap(); - patched_bin.extend_from_slice(length_as_bytes); + // Append length of compressed binary + let mut patched_bin = postcard::to_extend(compressed_len, patched_bin).unwrap(); // Append the magic word to the end patched_bin.extend_from_slice(MAGIC); - // println!("{length_as_bytes:?}"); - // println!("{}", bytes.len()); - Ok(patched_bin) } @@ -90,17 +84,15 @@ impl Metadata { */ pub fn from_bytes(bytes: impl AsRef<[u8]>) -> Result { let bytes = bytes.as_ref(); - // println!("{:?}", &bytes[bytes.len() - 8..bytes.len()]); let Ok(length) = postcard::from_bytes::(&bytes[bytes.len() - 2..bytes.len()]) else { bail!("Failed to get binary length") }; - // println!("{length}"); - let bytes = &bytes[0..bytes.len() - 2]; - let bytes = &bytes[bytes.len() - length..bytes.len()]; - let metadata = postcard::from_bytes::(bytes); + let compressed_bin = &bytes[bytes.len() - length..bytes.len()]; + let decompressed_bin = lz4_compression::decompress::decompress(compressed_bin).unwrap(); + let metadata = postcard::from_bytes::(&decompressed_bin); if metadata.is_err() { bail!("Metadata is not attached: {}", metadata.err().unwrap())