-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkpsxiso.rs
150 lines (129 loc) · 3.48 KB
/
mkpsxiso.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use async_std::fs;
use quick_xml::de::from_str;
use serde::Deserialize;
use tokio::process::Command;
#[derive(Deserialize, Debug)]
#[serde(rename_all = "snake_case")]
pub struct IsoProject {
track: Vec<Track>,
}
impl IsoProject {
pub fn flatten(&self) -> anyhow::Result<Vec<File>> {
let mut result: Vec<File> = Vec::new();
let data = self
.track
.iter()
.find(|t| t.r#type == "data")
.ok_or(anyhow::anyhow!("Missing data track"))?;
for entry in data.directory_tree.field.iter() {
match entry {
DirEntry::File(file) => {
result.push(file.clone());
}
DirEntry::Dir(dir) => rflatten(dir, &mut result),
}
}
Ok(result)
}
}
fn rflatten(dir: &Dir, result: &mut Vec<File>) {
for entry in dir.field.iter() {
match entry {
DirEntry::File(file) => {
result.push(file.clone());
}
DirEntry::Dir(dir) => rflatten(dir, result),
}
}
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "snake_case")]
struct Track {
#[serde(rename = "@type")]
r#type: String,
directory_tree: DirectoryTree,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "snake_case")]
enum DirEntry {
Dir(Dir),
File(File),
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "snake_case")]
struct DirectoryTree {
#[serde(rename = "$value")]
field: Vec<DirEntry>,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "snake_case")]
struct Dir {
#[serde(rename = "$value")]
field: Vec<DirEntry>,
#[serde(rename = "@name")]
_name: String,
#[serde(rename = "@source")]
_source: String,
}
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "snake_case")]
pub struct File {
#[serde(rename = "@name")]
pub name: String,
#[serde(rename = "@source")]
_source: String,
#[serde(rename = "@offs")]
pub offs: u32,
#[serde(rename = "@type")]
_type: String,
}
async fn exists(exec: &str) -> anyhow::Result<bool> {
Ok(Command::new(exec).output().await?.status.success())
}
async fn find_bin(name: &str) -> anyhow::Result<String> {
if exists(name).await? {
return Ok(String::from(name));
}
let built = format!("mkpsxiso/build/{name}");
if exists(&built).await? {
return Ok(built);
}
Err(anyhow::anyhow!("Can't find bin"))
}
pub async fn extract(path: &std::path::PathBuf) -> anyhow::Result<bool> {
let bin = find_bin("dumpsxiso").await?;
Ok(Command::new(bin)
.arg("-x")
.arg(format!(
"extract/{}/",
path.file_name().unwrap().to_str().unwrap()
))
.arg("-s")
.arg("extract/out.xml")
.arg("-pt")
.arg(path)
.output()
.await?
.status
.success())
}
pub async fn xml_file() -> anyhow::Result<IsoProject> {
let xml = fs::read_to_string("extract/out.xml").await?;
Ok(from_str(&xml)?)
}
pub async fn build(rom_name: &str, file_name: &str) -> anyhow::Result<bool> {
let binf = find_bin("mkpsxiso").await?;
let bin = format!("randomized/{}/{}/new.bin", rom_name, file_name);
let cue = format!("randomized/{}/{}/new.cue", rom_name, file_name);
Ok(Command::new(binf)
.arg("-o")
.arg(&bin)
.arg("-c")
.arg(&cue)
.arg("extract/out.xml")
.arg("-y")
.output()
.await?
.status
.success())
}