Skip to content

Commit

Permalink
feat: actually got it working
Browse files Browse the repository at this point in the history
  • Loading branch information
markisha64 committed Sep 1, 2024
1 parent d116738 commit 1d1705e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 19 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ pub struct MapObject {

pub struct ModelObject {
packed: dmw3_pack::Packed,
og_len: usize,
file_name: String,
header: Header,
}
Expand Down Expand Up @@ -606,6 +607,12 @@ fn write_model_objects(path: &PathBuf, objects: &Objects) {
let rom_name = path.file_name().unwrap().to_str().unwrap();

for model in &objects.model_objects {
if model.packed.buffer.len()
> ((model.og_len / 2048) + (model.og_len % 2048 != 0) as usize) * 2048
{
continue;
}

let mut new_model = File::create(format!(
"extract/{}/AAA/DAT/FIGHT/MODEL/{}",
rom_name, model.file_name,
Expand Down Expand Up @@ -669,6 +676,7 @@ fn read_model_objects(path: &PathBuf) -> Vec<ModelObject> {
let header = Header::read(&mut Cursor::new(&packed.get_file(header_buf).unwrap())).unwrap();

r.push(ModelObject {
og_len: packed.buffer.len(),
packed,
file_name,
header,
Expand Down
44 changes: 31 additions & 13 deletions src/rand/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use tim::Tim;
use crate::{json::Randomizer, rand::Objects};

pub fn patch(preset: &Randomizer, objects: &mut Objects, rng: &mut Xoshiro256StarStar) {
let mut s = false;
for model in &mut objects.model_objects {
let texture_packed = dmw3_pack::Packed::try_from(
model
Expand All @@ -19,15 +20,24 @@ pub fn patch(preset: &Randomizer, objects: &mut Objects, rng: &mut Xoshiro256Sta
Err(_) => texture_packed.get_file(0).unwrap().into(),
};

if (!s) {
std::fs::write("old.tim", &texture_raw).unwrap();
}

let mut texture_tim = Tim::from(texture_raw);

let hue_shift = (rng.next_u32() as f64) / (rng.next_u32() as f64);
let hue_shift = (rng.next_u32() as f64).rem_euclid(360.0);

if (!s) {
dbg!(hue_shift);
}

for i in 0..64 {
for j in 0..4 {
let l = (i + (224 + j * 8) * 64) * 2;
let l1 = (i + (224 + j * 8) * 64) * 2;
let l2 = (i + (225 + j * 8) * 64) * 2;

let color = &texture_tim.image.bytes[l..l + 2];
let color = &texture_tim.image.bytes[l1..l1 + 2];

let raw = u16::from_le_bytes([color[0], color[1]]);

Expand Down Expand Up @@ -95,14 +105,22 @@ pub fn patch(preset: &Randomizer, objects: &mut Objects, rng: &mut Xoshiro256Sta

let new_c_bytes = new_c.to_le_bytes();

texture_tim.image.bytes[l..l + 2]
.copy_from_slice(&[new_c_bytes[0], new_c_bytes[1]]);
texture_tim.image.bytes[l1..l1 + 2].copy_from_slice(&new_c_bytes);
}
}

let new_tim: Vec<u8> = texture_tim.into();

let recoded = rlen_encode(&new_tim);
if (!s) {
std::fs::write("new.tim", &new_tim).unwrap();
s = true;
}

let mut recoded = rlen_encode(&new_tim);

let padding_needed = 4 - (recoded.len() % 4);

recoded.extend(vec![0; padding_needed]);

let offset = model
.packed
Expand All @@ -119,19 +137,19 @@ pub fn patch(preset: &Randomizer, objects: &mut Objects, rng: &mut Xoshiro256Sta

model.packed.buffer.resize(new_size, 0);

model.packed.buffer[(offset + 8)..(offset + recoded.len() + 8)]
.copy_from_slice(&recoded[..]);
model.packed.buffer[(offset + 8)..(offset + recoded_length)].copy_from_slice(&recoded[..]);

model.packed.buffer[(offset + recoded.len() + 8)..].copy_from_slice(&ending[..]);
model.packed.buffer[(offset + recoded_length)..].copy_from_slice(&ending[..]);

for idx in model.packed.iter() {
let mut offset = model.packed.get_offset(idx).unwrap() as usize;
let mut n_offset = model.packed.get_offset(idx).unwrap() as usize;

if offset >= offset + 8 + assumed_length {
offset += recoded_length - assumed_length;
if n_offset > offset {
n_offset += recoded_length;
n_offset -= assumed_length;

model.packed.buffer[idx * 4..(idx + 1) * 4]
.copy_from_slice(&(offset as u32).to_le_bytes());
.copy_from_slice(&(n_offset as u32).to_le_bytes());
}
}
}
Expand Down

0 comments on commit 1d1705e

Please sign in to comment.