Factor out single chunk saving code into a function

This commit is contained in:
crumblingstatue 2023-04-14 17:42:22 +02:00
parent 7c4af574cd
commit 80e10e4e91

View file

@ -91,57 +91,61 @@ impl World {
}
pub fn save_chunks(&self) {
for (pos, chk) in self.chunks.iter() {
let reg_file_name = format_reg_file_name(pos.region());
dbg!(&reg_file_name);
let reg_file_exists = Path::new(&reg_file_name).exists();
if !reg_file_exists {
log::warn!("Region file doesn't exist. Going to create one.");
}
let mut f = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(&reg_file_name)
.unwrap();
let mut existence_bitset = if reg_file_exists {
ExistenceBitset::read_from_file(&mut f)
} else {
ExistenceBitset::EMPTY
};
dbg!(existence_bitset);
let _ = dbg!(f.stream_position());
let mut region_tile_data = if reg_file_exists {
zstd::decode_all(&mut f).unwrap()
} else {
vec![0; REGION_BYTES]
};
// Even the zstd decompressed data should be exactly REGION_BYTES size
assert_eq!(region_tile_data.len(), REGION_BYTES);
let (loc_x, loc_y) = pos.local();
dbg!(loc_x, loc_y);
let loc_idx = loc_idx(loc_y, loc_x);
crate::bitmanip::set_nth_bit(&mut existence_bitset.0, loc_idx as usize, true);
let byte_idx = loc_byte_idx(loc_idx);
dbg!(byte_idx);
let end_idx = byte_idx + CHUNK_BYTES;
dbg!(end_idx);
for (i, tile) in chk.tiles.iter().enumerate() {
let off = byte_idx + (i * TILE_BYTES);
region_tile_data[off..off + 2].copy_from_slice(&tile.bg.to_le_bytes());
region_tile_data[off + 2..off + 4].copy_from_slice(&tile.mid.to_le_bytes());
region_tile_data[off + 4..off + 6].copy_from_slice(&tile.fg.to_le_bytes());
}
f.seek(SeekFrom::Start(0)).unwrap();
f.write_all(&u64::to_le_bytes(existence_bitset.0)[..])
.unwrap();
assert_eq!(f.stream_position().unwrap(), 8);
assert_eq!(region_tile_data.len(), REGION_BYTES);
let result = f.write_all(&zstd::encode_all(&region_tile_data[..], COMP_LEVEL).unwrap());
log::info!("{result:?}");
save_chunk(pos, chk);
}
}
}
fn save_chunk(pos: &ChunkPos, chk: &Chunk) {
let reg_file_name = format_reg_file_name(pos.region());
dbg!(&reg_file_name);
let reg_file_exists = Path::new(&reg_file_name).exists();
if !reg_file_exists {
log::warn!("Region file doesn't exist. Going to create one.");
}
let mut f = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(&reg_file_name)
.unwrap();
let mut existence_bitset = if reg_file_exists {
ExistenceBitset::read_from_file(&mut f)
} else {
ExistenceBitset::EMPTY
};
dbg!(existence_bitset);
let _ = dbg!(f.stream_position());
let mut region_tile_data = if reg_file_exists {
zstd::decode_all(&mut f).unwrap()
} else {
vec![0; REGION_BYTES]
};
// Even the zstd decompressed data should be exactly REGION_BYTES size
assert_eq!(region_tile_data.len(), REGION_BYTES);
let (loc_x, loc_y) = pos.local();
dbg!(loc_x, loc_y);
let loc_idx = loc_idx(loc_y, loc_x);
crate::bitmanip::set_nth_bit(&mut existence_bitset.0, loc_idx as usize, true);
let byte_idx = loc_byte_idx(loc_idx);
dbg!(byte_idx);
let end_idx = byte_idx + CHUNK_BYTES;
dbg!(end_idx);
for (i, tile) in chk.tiles.iter().enumerate() {
let off = byte_idx + (i * TILE_BYTES);
region_tile_data[off..off + 2].copy_from_slice(&tile.bg.to_le_bytes());
region_tile_data[off + 2..off + 4].copy_from_slice(&tile.mid.to_le_bytes());
region_tile_data[off + 4..off + 6].copy_from_slice(&tile.fg.to_le_bytes());
}
f.seek(SeekFrom::Start(0)).unwrap();
f.write_all(&u64::to_le_bytes(existence_bitset.0)[..])
.unwrap();
assert_eq!(f.stream_position().unwrap(), 8);
assert_eq!(region_tile_data.len(), REGION_BYTES);
let result = f.write_all(&zstd::encode_all(&region_tile_data[..], COMP_LEVEL).unwrap());
log::info!("{result:?}");
}
fn loc_byte_idx_xy(x: u8, y: u8) -> usize {
loc_byte_idx(loc_idx(y, x))
}