Skip to content
This repository has been archived by the owner on Aug 1, 2024. It is now read-only.

Commit

Permalink
compress metadata attached to binary
Browse files Browse the repository at this point in the history
  • Loading branch information
FlowGnarly committed Jul 21, 2024
1 parent 94dc83f commit 6ca92f1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
9 changes: 8 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion crates/lune/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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"
Expand Down
28 changes: 10 additions & 18 deletions crates/lune/src/standalone/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,19 @@ impl Metadata {
scripts: Vec<LuauScript>,
) -> Result<Vec<u8>> {
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)
}

Expand All @@ -90,17 +84,15 @@ impl Metadata {
*/
pub fn from_bytes(bytes: impl AsRef<[u8]>) -> Result<Self> {
let bytes = bytes.as_ref();
// println!("{:?}", &bytes[bytes.len() - 8..bytes.len()]);

let Ok(length) = postcard::from_bytes::<usize>(&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::<Metadata>(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::<Metadata>(&decompressed_bin);

if metadata.is_err() {
bail!("Metadata is not attached: {}", metadata.err().unwrap())
Expand Down

0 comments on commit 6ca92f1

Please sign in to comment.