Skip to content

Commit

Permalink
refactor: Use Vector to keep the order consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
dulingzhi committed Oct 24, 2023
1 parent 5ffb323 commit 17c21a1
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ use std::fs;

use ceres_mpq as mpq;

type FileList = HashMap<String, String>;
struct File {
name: String,
path: String,
}

type FileList = Vec<File>;

fn main() -> Result<(), Error> {
let matches = App::new("MopaqPack-rs")
Expand Down Expand Up @@ -150,18 +155,21 @@ fn generate_file_list(input: &str) -> Result<FileList, Error> {
.filter_map(Result::ok);
for img in walker {
let p = img.path();
files.insert(
p.strip_prefix(input).unwrap().to_str().unwrap().to_string(),
p.to_str().unwrap().to_string(),
);
files.push(File {
name: p.strip_prefix(input).unwrap().to_str().unwrap().to_string(),
path: p.to_str().unwrap().to_string(),
});
}
} else {
let json = fs::read_to_string(input)?;

let data: Vec<Vec<String>> = serde_json::from_str(json.as_str())?;

for item in data {
files.insert(item[0].to_string(), item[1].to_string());
files.push(File {
name: item[0].to_string(),
path: item[1].to_string(),
});
}
}

Expand All @@ -174,9 +182,9 @@ fn exec(files: &FileList, output: &str, filelist: bool) -> Result<bool, Error> {
}

let ar = mpq::MPQArchive::create(output, files.len(), filelist)?;
for (n, p) in files {
let data = fs::read(p)?;
ar.write_file(n, &*data)?;
for f in files {
let data = fs::read(f.path.as_str())?;
ar.write_file(f.name.as_str(), &*data)?;
}

Ok(true)
Expand All @@ -200,9 +208,9 @@ fn pack(mpq: &str, files: &FileList) -> Result<bool, Error> {
let max = ar.get_max_files();
ar.set_max_files(files.len() + max);

for (n, p) in files {
let data = fs::read(p)?;
ar.write_file(n, &*data)?;
for f in files {
let data = fs::read(f.path.as_str())?;
ar.write_file(f.name.as_str(), &*data)?;
}
Ok(true)
}
Expand Down

0 comments on commit 17c21a1

Please sign in to comment.