compress platform module

This commit is contained in:
2021-11-21 23:38:01 +01:00
parent f6d0bdfa8f
commit 88bc4fe364
10 changed files with 67 additions and 24 deletions

View File

@@ -30,13 +30,13 @@ fn ftoi(v: f32) -> i32 {
#[no_mangle] #[no_mangle]
pub fn tic(time: i32) { pub fn tic(time: i32) {
for i in 0..320 * 256 { for i in 0..320 * 240 {
let t = time as f32 / 10 as f32; let t = time as f32 / 10 as f32;
let x = (i % 320 - 160) as f32; let x = (i % 320 - 160) as f32;
let y = (i / 320 - 128) as f32; let y = (i / 320 - 120) as f32;
let d = 20000 as f32 / sqrt(x * x + y * y + 1 as f32); let d = 40000 as f32 / sqrt(x * x + y * y + 1 as f32);
let u = atan2(x, y) * 512f32 / 3.141; let u = atan2(x, y) * 512f32 / 3.141;
let c = (ftoi(d + t) ^ ftoi(u + t)) as u8; let c = (ftoi(d + t * 2 as f32) ^ ftoi(u + t)) as u8;
unsafe { unsafe {
*((120 + i) as *mut u8) = c; *((120 + i) as *mut u8) = c;
} }

2
platform/Cargo.lock generated
View File

@@ -94,7 +94,7 @@ checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7"
[[package]] [[package]]
name = "curlywas" name = "curlywas"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/exoticorn/curlywas.git?rev=788a565#788a5657b535448f0532b1481fbfe9a77ee3b5e1" source = "git+https://github.com/exoticorn/curlywas.git?rev=c0140d0#c0140d0671e81b57e8fd142c66c675a659fd50f8"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"ariadne", "ariadne",

View File

@@ -6,6 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
curlywas = { git="https://github.com/exoticorn/curlywas.git", rev="788a565" } curlywas = { git="https://github.com/exoticorn/curlywas.git", rev="c0140d0" }
uw8-tool = { path="../uw8-tool" } uw8-tool = { path="../uw8-tool" }
anyhow = "1" anyhow = "1"

Binary file not shown.

Binary file not shown.

View File

@@ -10,6 +10,8 @@ export fn load_uw8(module_size: i32) -> i32 {
let module_end = 0x1e000 + module_size; let module_end = 0x1e000 + module_size;
if version & 1 { if version & 1 {
module_size?0 = 0;
module_size?1 = 0;
module_end = uncompress(1, 0x1e001); module_end = uncompress(1, 0x1e001);
} else { } else {
copy(0x1e000, 0, module_size); copy(0x1e000, 0, module_size);

View File

@@ -1,16 +1,25 @@
use std::{fs::File, path::Path};
use std::io::prelude::*; use std::io::prelude::*;
use std::{fs::File, path::Path};
use anyhow::Result; use anyhow::Result;
fn main() -> Result<()> { fn main() -> Result<()> {
println!("Generating compressed base module");
uw8_tool::BaseModule::create_binary(&Path::new("target/base.upk"))?; uw8_tool::BaseModule::create_binary(&Path::new("target/base.upk"))?;
println!("Compiling loader module");
let loader = curlywas::compile_file("src/loader.cwa")?; let loader = curlywas::compile_file("src/loader.cwa")?;
File::create("bin/loader.wasm")?.write_all(&loader)?; File::create("bin/loader.wasm")?.write_all(&loader)?;
println!("Compiling platform module");
let platform = curlywas::compile_file("src/platform.cwa")?; let platform = curlywas::compile_file("src/platform.cwa")?;
println!("Compressing platform module");
let platform = uw8_tool::pack(
&platform,
uw8_tool::PackConfig::default().with_compression(),
)?;
File::create("bin/platform.uw8")?.write_all(&platform)?; File::create("bin/platform.uw8")?.write_all(&platform)?;
println!("All done!");
Ok(()) Ok(())
} }

View File

@@ -1,5 +1,5 @@
mod base_module; mod base_module;
pub mod pack; mod pack;
pub use base_module::BaseModule; pub use base_module::BaseModule;
pub use pack::{pack_file, unpack, unpack_file}; pub use pack::{pack, pack_file, unpack, unpack_file, PackConfig};

View File

@@ -14,10 +14,13 @@ fn main() -> Result<()> {
BaseModule::create_binary(&path)?; BaseModule::create_binary(&path)?;
} }
"pack" => { "pack" => {
let version: u8 = args.opt_value_from_str(["-v", "--version"])?.unwrap_or(1); let mut config = uw8_tool::PackConfig::default();
if args.contains(["-c", "--compress"]) {
config = config.with_compression();
}
let source: PathBuf = args.free_from_str()?; let source: PathBuf = args.free_from_str()?;
let dest: PathBuf = args.free_from_str()?; let dest: PathBuf = args.free_from_str()?;
uw8_tool::pack_file(&source, &dest, version)?; uw8_tool::pack_file(&source, &dest, config)?;
} }
"unpack" => { "unpack" => {
let source: PathBuf = args.free_from_str()?; let source: PathBuf = args.free_from_str()?;

View File

@@ -13,22 +13,50 @@ use wasmparser::{
ImportSectionEntryType, ImportSectionReader, TypeSectionReader, ImportSectionEntryType, ImportSectionReader, TypeSectionReader,
}; };
pub fn pack_file(source: &Path, dest: &Path, version: u8) -> Result<()> { pub struct PackConfig {
let base = BaseModule::for_format_version(version)?; compression: bool,
}
impl PackConfig {
pub fn with_compression(mut self) -> Self {
self.compression = true;
self
}
}
impl Default for PackConfig {
fn default() -> PackConfig {
PackConfig { compression: false }
}
}
pub fn pack_file(source: &Path, dest: &Path, config: PackConfig) -> Result<()> {
let mut source_data = vec![]; let mut source_data = vec![];
File::open(source)?.read_to_end(&mut source_data)?; File::open(source)?.read_to_end(&mut source_data)?;
let parsed_module = ParsedModule::parse(&source_data)?; let dest_data = pack(&source_data, config)?;
let result = parsed_module.pack(&base)?;
let mut dest_data = vec![version as u8];
dest_data.extend_from_slice(&result[8..]);
File::create(dest)?.write_all(&dest_data)?; File::create(dest)?.write_all(&dest_data)?;
Ok(()) Ok(())
} }
pub fn pack(data: &[u8], config: PackConfig) -> Result<Vec<u8>> {
let base = BaseModule::for_format_version(1)?;
let parsed_module = ParsedModule::parse(data)?;
let result = parsed_module.pack(&base)?;
if config.compression {
let mut uw8 = vec![2];
uw8.extend_from_slice(&upkr::pack(&result[8..]));
Ok(uw8)
} else {
let mut uw8 = vec![1];
uw8.extend_from_slice(&result[8..]);
Ok(uw8)
}
}
pub fn unpack_file(source: &Path, dest: &Path) -> Result<()> { pub fn unpack_file(source: &Path, dest: &Path) -> Result<()> {
let mut source_data = vec![]; let mut source_data = vec![];
File::open(source)?.read_to_end(&mut source_data)?; File::open(source)?.read_to_end(&mut source_data)?;
@@ -38,14 +66,16 @@ pub fn unpack_file(source: &Path, dest: &Path) -> Result<()> {
} }
pub fn unpack(data: Vec<u8>) -> Result<Vec<u8>> { pub fn unpack(data: Vec<u8>) -> Result<Vec<u8>> {
let version = data[0]; let (version, data) = match data[0] {
if version == 0 { 0 => return Ok(data),
return Ok(data); 1 => (1, data[1..].to_vec()),
} 2 => (1, upkr::unpack(&data[1..])),
other => bail!("Uknown format version {}", other),
};
let mut data = data.as_slice();
let base_data = BaseModule::for_format_version(version)?.to_wasm(); let base_data = BaseModule::for_format_version(version)?.to_wasm();
let mut data = &data[1..];
let mut base_data = base_data.as_slice(); let mut base_data = base_data.as_slice();
let mut dest = base_data[..8].to_vec(); let mut dest = base_data[..8].to_vec();
@@ -56,7 +86,6 @@ pub fn unpack(data: Vec<u8>) -> Result<Vec<u8>> {
let inner_len = reader.read_var_u32()? as usize; let inner_len = reader.read_var_u32()? as usize;
let header_len = reader.original_position(); let header_len = reader.original_position();
let len = header_len + inner_len; let len = header_len + inner_len;
dbg!(len);
if len > data.len() { if len > data.len() {
bail!("Section length greater than size of the rest of the file"); bail!("Section length greater than size of the rest of the file");
} }