From 9003f88a9d9773e6636e3321c7ffd9cdcf86d40b Mon Sep 17 00:00:00 2001 From: Quaternions Date: Tue, 31 Dec 2024 01:44:28 -0800 Subject: [PATCH] trey floating point format is not standard --- src/v1.rs | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/src/v1.rs b/src/v1.rs index 72b4257..2aea4f6 100644 --- a/src/v1.rs +++ b/src/v1.rs @@ -1,5 +1,55 @@ use binrw::{binrw,BinReaderExt,io::TakeSeekExt}; +// whatever this is +fn trey_float(f:f32)->f32{ + let bits=f.to_bits(); + let s=bits&1!=0; + let e=((bits>>1)&((1<<8)-1)) as i32; + let m=(bits>>(1+8))&((1<<23)-1); + if e==255{ + if m==0{ + if s{ + f32::NEG_INFINITY + }else{ + f32::INFINITY + } + }else if m==1{ + f32::NAN + }else{ + // this is supposed to be QNAN but idk how to say it in rust + f32::NAN + } + }else if e==0{ + if s{-(m as f32)*2.0f32.powi(-149)}else{(m as f32)*2.0f32.powi(-149)} + }else{ + if s{-(m as f32/2.0f32.powi(23)+1.0)*2.0f32.powi(e-127)}else{(m as f32/2.0f32.powi(23)+1.0)*2.0f32.powi(e-127)} + } +} +fn trey_double(f:f64)->f64{ + let bits=f.to_bits(); + let s=bits&1!=0; + let e=((bits>>1)&((1<<11)-1)) as i32; + let m=(bits>>(1+11))&((1<<52)-1); + if e==2047{ + if m==0{ + if s{ + f64::NEG_INFINITY + }else{ + f64::INFINITY + } + }else if m==1{ + f64::NAN + }else{ + // this is supposed to be QNAN but idk how to say it in rust + f64::NAN + } + }else if e==0{ + if s{-(m as f64)*2.0f64.powi(-1074)}else{(m as f64)*2.0f64.powi(-1074)} + }else{ + if s{-(m as f64/2.0f64.powi(52)+1.0)*2.0f64.powi(e-1023)}else{(m as f64/2.0f64.powi(52)+1.0)*2.0f64.powi(e-1023)} + } +} + #[binrw] #[brw(little)] #[derive(Clone,Copy,Debug)] @@ -20,14 +70,19 @@ impl Into for Bool{ #[binrw] #[brw(little)] pub struct Vector2{ + #[br(map=trey_float)] pub x:f32, + #[br(map=trey_float)] pub y:f32, } #[binrw] #[brw(little)] pub struct Vector3{ + #[br(map=trey_float)] pub x:f32, + #[br(map=trey_float)] pub y:f32, + #[br(map=trey_float)] pub z:f32, } @@ -41,6 +96,7 @@ pub struct InputEvent{ #[binrw] #[brw(little)] pub struct TimedInputEvent{ + #[br(map=trey_double)] pub time:f64, pub event:InputEvent, } @@ -58,6 +114,7 @@ pub struct OutputEvent{ #[binrw] #[brw(little)] pub struct TimedOutputEvent{ + #[br(map=trey_double)] pub time:f64, pub event:OutputEvent, } @@ -72,6 +129,7 @@ pub struct SoundEvent{ #[binrw] #[brw(little)] pub struct TimedSoundEvent{ + #[br(map=trey_double)] pub time:f64, pub event:SoundEvent, } @@ -92,6 +150,7 @@ pub struct WorldEventButton{ #[binrw] #[brw(little)] pub struct WorldEventSetTime{ + #[br(map=trey_double)] pub time:f64, #[brw(magic=b"data")] __:(), @@ -118,6 +177,7 @@ pub enum WorldEvent{ #[binrw] #[brw(little)] pub struct TimedWorldEvent{ + #[br(map=trey_double)] pub time:f64, pub event:WorldEvent, } @@ -131,6 +191,7 @@ pub struct GravityEvent{ #[binrw] #[brw(little)] pub struct TimedGravityEvent{ + #[br(map=trey_double)] pub time:f64, pub event:GravityEvent, } @@ -164,6 +225,7 @@ pub struct RunEvent{ #[binrw] #[brw(little)] pub struct TimedRunEvent{ + #[br(map=trey_double)] pub time:f64, pub event:RunEvent, } @@ -178,6 +240,7 @@ pub struct CameraEvent{ #[binrw] #[brw(little)] pub struct TimedCameraEvent{ + #[br(map=trey_double)] pub time:f64, pub event:CameraEvent, } @@ -187,11 +250,13 @@ pub struct TimedCameraEvent{ #[brw(little)] pub struct SettingEvent{ pub setting_id:u32, + #[br(map=trey_double)] pub value:f64, } #[binrw] #[brw(little)] pub struct TimedSettingEvent{ + #[br(map=trey_double)] pub time:f64, pub event:SettingEvent, } @@ -291,6 +356,7 @@ pub struct BlockId(#[br(map=|i:u32|i-1)]u32); #[brw(little)] #[derive(Debug,Clone,Copy)] pub struct TimedBlockId{ + #[br(map=trey_double)] pub time:f64, pub block_id:BlockId, }