debug user input support

This commit is contained in:
Mateusz Faderewski 2023-03-04 23:25:40 +01:00
parent 857ebb36c7
commit 86435640dd
2 changed files with 63 additions and 3 deletions

View File

@ -116,10 +116,67 @@ impl TryFrom<Vec<u8>> for ScreenshotMetadata {
}
impl Handler {
pub fn process_user_input(&self) {
pub fn process_user_input(&self) -> Option<sc64::DebugPacket> {
if let Ok(line) = self.line_rx.try_recv() {
print!("Line: {line}");
if line.len() == 0 {
return None;
}
let mut data: Vec<u8> = Vec::new();
if line.matches("@").count() != 2 {
data.append(&mut line.as_bytes().to_vec());
data.append(&mut [b'\0'].to_vec());
return Some(sc64::DebugPacket {
datatype: DataType::Text.into(),
data,
});
} else {
let start = line.find("@").unwrap();
let end = line.rfind("@").unwrap();
let path = &line[start + 1..end];
if path.len() == 0 {
println!("Invalid path provided");
return None;
}
let mut file = match File::open(path) {
Ok(file) => file,
Err(error) => {
println!("Couldn't open file: {error}");
return None;
}
};
let length = match file.metadata() {
Ok(metadata) => metadata.len(),
Err(error) => {
println!("Couldn't get file length: {error}");
return None;
}
};
let mut data = vec![0u8; length as usize];
if let Err(error) = file.read_exact(&mut data) {
println!("Couldn't read file contents: {error}");
return None;
}
if line.starts_with("@") && line.ends_with("@") {
return Some(sc64::DebugPacket {
datatype: DataType::RawBinary.into(),
data,
});
} else {
let mut combined_data: Vec<u8> = Vec::new();
combined_data.append(&mut line[0..start].as_bytes().to_vec());
combined_data.append(&mut [b'@'].to_vec());
combined_data.append(&mut format!("{length}").into_bytes());
combined_data.append(&mut [b'@'].to_vec());
combined_data.append(&mut data);
combined_data.append(&mut [b'\0'].to_vec());
return Some(sc64::DebugPacket {
datatype: DataType::Text.into(),
data: combined_data,
});
}
}
}
None
}
pub fn handle_debug_packet(&mut self, debug_packet: sc64::DebugPacket) {

View File

@ -311,6 +311,8 @@ fn handle_64dd_command(sn: Option<String>, args: &_64DDArgs) -> Result<(), sc64:
// TODO: handle 64DD stuff
// TODO: print BIG warning to not use this mode together with real 64DD
println!("{}", "Sorry nothing".yellow());
Ok(())
@ -366,8 +368,9 @@ fn handle_debug_command(sn: Option<String>, args: &DebugArgs) -> Result<(), sc64
}
} else if let Some(gdb_packet) = debug_handler.receive_gdb_packet() {
sc64.send_debug_packet(gdb_packet)?;
} else if let Some(debug_packet) = debug_handler.process_user_input() {
sc64.send_debug_packet(debug_packet)?;
} else {
debug_handler.process_user_input();
thread::sleep(Duration::from_millis(1));
}
}