diff options
-rw-r--r-- | src/devices/file_device.rs | 58 |
1 files changed, 37 insertions, 21 deletions
diff --git a/src/devices/file_device.rs b/src/devices/file_device.rs index 73f05cc..83f0a56 100644 --- a/src/devices/file_device.rs +++ b/src/devices/file_device.rs @@ -12,7 +12,7 @@ pub struct FileDevice { pub entry: Option<(Entry, BedrockFilePath, Instant)>, pub cached_dir: Option<(Entry, BedrockFilePath, Instant)>, - pub success: bool, + pub error: bool, pub pointer_write: u32, pub length_write: u32, @@ -30,7 +30,7 @@ impl Device for FileDevice { if !self.enable { return 0x00; } match port { 0x0 => read_b!(self.entry.is_some()), - 0x1 => read_b!(self.success), + 0x1 => read_b!(std::mem::take(&mut self.error)), 0x2 => self.read_byte(), 0x3 => self.read_byte(), 0x4 => self.path_buffer.read(), @@ -121,7 +121,7 @@ impl FileDevice { entry: None, cached_dir: None, - success: false, + error: false, pointer_write: 0, length_write: 0, @@ -134,6 +134,12 @@ impl FileDevice { } } + pub fn check_success(&mut self, success: bool) { + if !success { + self.error = true; + } + } + /// Safely close the current entry, cleaning up entry variables. pub fn close(&mut self) { self.entry_buffer.clear(); @@ -192,10 +198,16 @@ impl FileDevice { pub fn write_to_entry_port(&mut self, byte: u8) { if let Some(buffer) = self.entry_buffer.write(byte) { self.close(); - match BedrockFilePath::from_buffer(buffer, &self.base_path) { - Some(path) => self.success = self.open(path).is_ok(), - None => self.success = false, - }; + // Attempt to open file if buffer was not empty. + if buffer[0] != 0 { + let success = match BedrockFilePath::from_buffer(buffer, &self.base_path) { + Some(path) => self.open(path).is_ok(), + None => false, + }; + self.check_success(success); + } else { + self.check_success(true); + } } } @@ -204,22 +216,23 @@ impl FileDevice { if let Some(buffer) = self.action_buffer.write(byte) { let destination_blank = buffer[0] == 0x00; let destination = BedrockFilePath::from_buffer(buffer, &self.base_path); - self.success = false; if let Some((_, source, _)) = &self.entry { if destination_blank { if self.enable_delete { - self.success = delete_entry(&source.as_path()); + self.check_success(delete_entry(&source.as_path())); } } else if let Some(dest) = destination { if self.enable_move { - self.success = move_entry(&source.as_path(), &dest.as_path()); + self.check_success(move_entry(&source.as_path(), &dest.as_path())); } } } else if let Some(dest) = destination { if self.enable_create { - self.success = create_file(&dest.as_path()); + self.check_success(create_file(&dest.as_path())); } + } else { + self.check_success(false); } self.close(); } @@ -228,27 +241,30 @@ impl FileDevice { /// Attempt to open the parent directory of the current entry. pub fn ascend_to_parent(&mut self) { if let Some((_, path, _)) = &self.entry { - match path.parent() { - Some(parent) => self.success = self.open(parent).is_ok(), - None => self.success = false, + let success = match path.parent() { + Some(parent) => self.open(parent).is_ok(), + None => false, }; + self.check_success(success); } else { - match BedrockFilePath::from_path(&self.default_path, &self.base_path) { - Some(default) => self.success = self.open(default).is_ok(), - None => self.success = false, + let success = match BedrockFilePath::from_path(&self.default_path, &self.base_path) { + Some(default) => self.open(default).is_ok(), + None => false, }; + self.check_success(success); } } /// Attempt to open the selected child of the current directory. pub fn descend_to_child(&mut self) { if let Some((Entry::Directory(dir), _, _)) = &self.entry { - match dir.child_path() { - Some(child) => self.success = self.open(child).is_ok(), - None => self.success = false, + let success = match dir.child_path() { + Some(child) => self.open(child).is_ok(), + None => false, }; + self.check_success(success); } else { - self.success = false; + self.check_success(false); } } |