update
This commit is contained in:
parent
dcd876a636
commit
3a11402914
64
src/main.rs
64
src/main.rs
|
@ -1,29 +1,65 @@
|
||||||
use std::{fs, io::{self, BufRead, BufReader}};
|
use std::{fs, io::{self, BufRead, BufReader, Write}, fmt::format};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
let args : Vec<String> = std::env::args().collect();
|
||||||
println!("Hello, world!");
|
if args.len() < 3 {
|
||||||
|
println!("usage: {} <patch_file> <patch_code>", args[0]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let codes = match read_codes(args[2].as_str()) {
|
||||||
|
Ok(code) => code,
|
||||||
|
Err(e) => {
|
||||||
|
println!("read code error: {}", e.to_string());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if let Err(e) = apply_code(args[1].as_str(), &codes) {
|
||||||
|
println!("apply code error: {}", e.to_string());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_codes(code_file : &str) -> io::Result<Vec<u32>> {
|
fn read_codes(code_file : &str) -> io::Result<Vec<(u32, u32)>> {
|
||||||
let file = fs::File::open(code_file)?;
|
let file = fs::File::open(code_file).expect("cannot open patch_code");
|
||||||
let reader = BufReader::new(file);
|
let reader = BufReader::new(file);
|
||||||
let mut codes = Vec::new();
|
let mut codes = Vec::new();
|
||||||
let mut line_count = 0;
|
let mut lines = 0u32;
|
||||||
for line in reader.lines() {
|
for line in reader.lines() {
|
||||||
if let Ok(l) = line {
|
if let Ok(l) = line {
|
||||||
let r_str = l.as_str();
|
let r_str = l.as_str();
|
||||||
let code_vec : Vec<&str> = r_str.split(' ').collect();
|
let code_vec : Vec<&str> = r_str.split(' ').collect();
|
||||||
if code_vec.len() == 2 {
|
if code_vec.len() == 2 {
|
||||||
let val1 : u32 = code_vec[0].parse().unwrap_or_default();
|
let val1 = u32::from_str_radix(code_vec[0], 16);
|
||||||
let val2 : u32 = code_vec[1].parse().unwrap_or_default();
|
let val2 = u32::from_str_radix(code_vec[1], 16);
|
||||||
codes.push(val1);
|
if val1.is_ok() && val2.is_ok() {
|
||||||
codes.push(val2);
|
codes.push((lines, val1.unwrap()));
|
||||||
} else {
|
codes.push((lines, val2.unwrap()));
|
||||||
println!("code error at line {}.", line_count);
|
}
|
||||||
}
|
} // else skip
|
||||||
}
|
}
|
||||||
line_count += 1;
|
|
||||||
}
|
}
|
||||||
Ok(codes)
|
Ok(codes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn verify_code(codes : Vec<(u32, u32)>) -> Result<(), String> {
|
||||||
|
let pos = 0usize;
|
||||||
|
while pos < codes.len() {
|
||||||
|
let first_code = codes[pos].1;
|
||||||
|
let op_code = first_code >> 28;
|
||||||
|
let mut search_code = false;
|
||||||
|
match op_code {
|
||||||
|
0x0 | 0x1 |0x2 => {
|
||||||
|
return Err(format!("{}", 123));
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
pos += 1;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn apply_code(patch_file : &str, codes : &Vec<(u32, u32)>) -> io::Result<()> {
|
||||||
|
let data = fs::read(patch_file)?;
|
||||||
|
let mut write_file = fs::OpenOptions::new().write(true).truncate(true).open(patch_file)?;
|
||||||
|
write_file.write(data.as_slice())?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue