fix localstorage error

This commit is contained in:
Simon Gardling 2025-12-03 11:40:08 -05:00
parent 0889f81664
commit 5a92020dae
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D
4 changed files with 23 additions and 18 deletions

7
Cargo.lock generated
View File

@ -169,6 +169,12 @@ dependencies = [
"windows-link", "windows-link",
] ]
[[package]]
name = "base64"
version = "0.21.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567"
[[package]] [[package]]
name = "benchmarks" name = "benchmarks"
version = "0.1.0" version = "0.1.0"
@ -3776,6 +3782,7 @@ dependencies = [
name = "ytbn_graphing_software" name = "ytbn_graphing_software"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"base64",
"benchmarks", "benchmarks",
"bincode", "bincode",
"cfg-if", "cfg-if",

View File

@ -56,6 +56,7 @@ itertools = "0.10"
static_assertions = "1.1" static_assertions = "1.1"
bincode = "1.3" bincode = "1.3"
serde = "1" serde = "1"
base64 = "0.21"
[dev-dependencies] [dev-dependencies]
benchmarks = { path = "./benchmarks" } benchmarks = { path = "./benchmarks" }

View File

@ -150,10 +150,9 @@ impl MathApp {
let data = get_localstorage().get_item(DATA_NAME).ok()??; let data = get_localstorage().get_item(DATA_NAME).ok()??;
let (commit, cached_data) = crate::misc::hashed_storage_read(&data)?; let (commit, cached_data) = crate::misc::hashed_storage_read(&data)?;
if commit == unsafe { std::mem::transmute::<&str, crate::misc::HashBytes>(build::SHORT_COMMIT) } { if commit == unsafe { std::mem::transmute::<&str, crate::misc::HashBytes>(build::SHORT_COMMIT) } {
tracing::info!("Reading decompression cache. Bytes: {}", cached_data.len()); tracing::info!("Reading decompression cache. Bytes: {}", cached_data.len());
return Some(cached_data.to_vec()); return Some(cached_data);
} else { } else {
None None
} }
@ -161,14 +160,8 @@ impl MathApp {
fn load_functions() -> Option<FunctionManager> { fn load_functions() -> Option<FunctionManager> {
let data = get_localstorage().get_item(FUNC_NAME).ok()??; let data = get_localstorage().get_item(FUNC_NAME).ok()??;
if crate::misc::HASH_LENGTH >= data.len() {
return None;
}
// TODO: stabilize FunctionManager serialize so it can persist across builds
let (commit, func_data) = crate::misc::hashed_storage_read(&data)?; let (commit, func_data) = crate::misc::hashed_storage_read(&data)?;
if commit == unsafe { std::mem::transmute::<&str, &[u8]>(build::SHORT_COMMIT) } { if commit == unsafe { std::mem::transmute::<&str, &[u8]>(build::SHORT_COMMIT) } {
tracing::info!("Reading previous function data"); tracing::info!("Reading previous function data");
let function_manager: FunctionManager = bincode::deserialize(&func_data).ok()?; let function_manager: FunctionManager = bincode::deserialize(&func_data).ok()?;

View File

@ -1,3 +1,4 @@
use base64::{engine::general_purpose, Engine as _};
use egui_plot::{Line, PlotPoint, PlotPoints, Points}; use egui_plot::{Line, PlotPoint, PlotPoints, Points};
use emath::Pos2; use emath::Pos2;
use getrandom::getrandom; use getrandom::getrandom;
@ -165,24 +166,27 @@ pub type HashBytes = [u8; HASH_LENGTH];
#[allow(dead_code)] #[allow(dead_code)]
pub fn hashed_storage_create(hashbytes: HashBytes, data: &[u8]) -> String { pub fn hashed_storage_create(hashbytes: HashBytes, data: &[u8]) -> String {
unsafe { std::mem::transmute::<Vec<u8>, String>([hashbytes.to_vec(), data.to_vec()].concat()) } let combined_data = [hashbytes.to_vec(), data.to_vec()].concat();
general_purpose::STANDARD.encode(combined_data)
} }
#[allow(dead_code)] #[allow(dead_code)]
pub fn hashed_storage_read(data: &str) -> Option<(HashBytes, &[u8])> { pub fn hashed_storage_read(data: &str) -> Option<(HashBytes, Vec<u8>)> {
// Decode base64 data
let decoded_bytes = general_purpose::STANDARD.decode(data).ok()?;
// Make sure data is long enough to decode // Make sure data is long enough to decode
if HASH_LENGTH >= data.len() { if HASH_LENGTH > decoded_bytes.len() {
return None; return None;
} }
// Transmute data into slice // Split hash and data
let decoded_1: &[u8] = unsafe { std::mem::transmute::<&str, &[u8]>(data) }; let (hash_bytes, data_bytes) = decoded_bytes.split_at(HASH_LENGTH);
// Return hash and decoded data // Convert hash bytes to HashBytes
Some(( let hash: HashBytes = hash_bytes.try_into().ok()?;
unsafe { *(decoded_1[..HASH_LENGTH].as_ptr() as *const HashBytes) },
&decoded_1[HASH_LENGTH..], Some((hash, data_bytes.to_vec()))
))
} }
/// Creates and returns random u64 /// Creates and returns random u64