From 3a11402914d48e7b4a27ae8e6133ad2b1bce51ea Mon Sep 17 00:00:00 2001 From: Argon Date: Wed, 21 Sep 2022 19:13:48 +0800 Subject: [PATCH] update --- src/main.rs | 64 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 50 insertions(+), 14 deletions(-) diff --git a/src/main.rs b/src/main.rs index 30843e4..4e25371 100644 --- a/src/main.rs +++ b/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() { - - println!("Hello, world!"); + let args : Vec = std::env::args().collect(); + if args.len() < 3 { + println!("usage: {} ", 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> { - let file = fs::File::open(code_file)?; +fn read_codes(code_file : &str) -> io::Result> { + let file = fs::File::open(code_file).expect("cannot open patch_code"); let reader = BufReader::new(file); let mut codes = Vec::new(); - let mut line_count = 0; + let mut lines = 0u32; for line in reader.lines() { if let Ok(l) = line { let r_str = l.as_str(); let code_vec : Vec<&str> = r_str.split(' ').collect(); if code_vec.len() == 2 { - let val1 : u32 = code_vec[0].parse().unwrap_or_default(); - let val2 : u32 = code_vec[1].parse().unwrap_or_default(); - codes.push(val1); - codes.push(val2); - } else { - println!("code error at line {}.", line_count); - } + let val1 = u32::from_str_radix(code_vec[0], 16); + let val2 = u32::from_str_radix(code_vec[1], 16); + if val1.is_ok() && val2.is_ok() { + codes.push((lines, val1.unwrap())); + codes.push((lines, val2.unwrap())); + } + } // else skip } - line_count += 1; } 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(()) +}