Skip to content

Commit

Permalink
Support mapblock v29 (MT 5.5.0 and later)
Browse files Browse the repository at this point in the history
  • Loading branch information
random-geek committed Nov 2, 2022
1 parent 5353832 commit d756766
Show file tree
Hide file tree
Showing 23 changed files with 587 additions and 479 deletions.
135 changes: 85 additions & 50 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mapeditr"
version = "1.0.0"
version = "1.1.0"
authors = ["random-geek (github.com/random-geek)"]
edition = "2018"

Expand All @@ -12,3 +12,4 @@ edition = "2018"
memmem = "0.1"
sqlite = "0.26"
thiserror = "1"
zstd = "0.11"
9 changes: 4 additions & 5 deletions src/block_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ pub fn merge_blocks(
) {
assert!(block_parts_valid(&src_area, &dst_area));

let src_nd = src_block.node_data.get_ref();
let dst_nd = dst_block.node_data.get_mut();
let src_nd = &src_block.node_data;
let dst_nd = &mut dst_block.node_data;
let offset = dst_area.min - src_area.min;
// Warning: diff can be negative!
let diff = offset.x + offset.y * 16 + offset.z * 256;
Expand Down Expand Up @@ -104,12 +104,11 @@ pub fn merge_metadata(

/// Culls duplicate and unused IDs from the name-ID map and node data.
pub fn clean_name_id_map(block: &mut MapBlock) {
let nd = block.node_data.get_mut();
let id_count = (block.nimap.get_max_id().unwrap() + 1) as usize;

// Determine which IDs are used.
let mut used = vec![false; id_count];
for id in &nd.nodes {
for id in &block.node_data.nodes {
used[*id as usize] = true;
}

Expand All @@ -136,7 +135,7 @@ pub fn clean_name_id_map(block: &mut MapBlock) {
block.nimap = new_nimap;

// Re-assign node IDs.
for id in &mut nd.nodes {
for id in &mut block.node_data.nodes {
*id = map[*id as usize];
}
}
25 changes: 8 additions & 17 deletions src/commands/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use super::{Command, ArgResult, BLOCK_CACHE_SIZE};
use crate::{unwrap_or, opt_unwrap_or};
use crate::spatial::{Vec3, Area, MAP_LIMIT};
use crate::map_database::MapDatabase;
use crate::map_block::{MapBlock, MapBlockError, is_valid_generated,
NodeMetadataList, NodeMetadataListExt};
use crate::map_block::{MapBlock, MapBlockError, is_valid_generated};
use crate::block_utils::{merge_blocks, merge_metadata, clean_name_id_map};
use crate::instance::{ArgType, InstBundle, InstArgs};
use crate::utils::{CacheMap, query_keys};
Expand Down Expand Up @@ -69,14 +68,11 @@ fn clone(inst: &mut InstBundle) {
for dst_key in dst_keys {
inst.status.inc_done();

let (mut dst_block, mut dst_meta) = unwrap_or!(
let mut dst_block = unwrap_or!(
opt_unwrap_or!(
get_cached(&mut inst.db, &mut block_cache, dst_key),
continue
).and_then(|b| -> Result<_, MapBlockError> {
let m = NodeMetadataList::deserialize(b.metadata.get_ref())?;
Ok((b, m))
}),
),
{ inst.status.inc_failed(); continue; }
);

Expand All @@ -90,14 +86,10 @@ fn clone(inst: &mut InstBundle) {
continue;
}
let src_key = src_pos.to_block_key();
let (src_block, src_meta) = opt_unwrap_or!(
|| -> Option<_> {
let b = get_cached(
&mut inst.db, &mut block_cache, src_key)?.ok()?;
let m = NodeMetadataList::deserialize(b.metadata.get_ref())
.ok()?;
Some((b, m))
}(),
// Continue if a None or Some(Err) value is retrieved.
let src_block = opt_unwrap_or!(
get_cached(&mut inst.db, &mut block_cache, src_key)
.map(|res| res.ok()).flatten(),
continue
);

Expand All @@ -109,12 +101,11 @@ fn clone(inst: &mut InstBundle) {

merge_blocks(&src_block, &mut dst_block,
src_frag_rel, dst_frag_rel);
merge_metadata(&src_meta, &mut dst_meta,
merge_metadata(&src_block.metadata, &mut dst_block.metadata,
src_frag_rel, dst_frag_rel);
}

clean_name_id_map(&mut dst_block);
*dst_block.metadata.get_mut() = dst_meta.serialize(dst_block.version);
inst.db.set_block(dst_key, &dst_block.serialize()).unwrap();
}

Expand Down
Loading

0 comments on commit d756766

Please sign in to comment.