diff options
author | Ben Bridle <bridle.benjamin@gmail.com> | 2024-10-28 20:25:01 +1300 |
---|---|---|
committer | Ben Bridle <bridle.benjamin@gmail.com> | 2024-10-28 20:29:12 +1300 |
commit | 1a830a3d1b9d99653322d5ae49ea8165de7ed9d0 (patch) | |
tree | 798e77b6fcf2438b1c2538a67efe856a2f7cb979 /src/devices/file/directory_listing.rs | |
parent | 03c4b069e1806af256730639cefdae115b24401a (diff) | |
download | bedrock-pc-1.0.0-alpha1.zip |
Rewrite emulatorv1.0.0-alpha1
This is a complete rewrite and restructure of the entire emulator
project, as part of the effort in locking down the Bedrock specification
and in creating much better tooling for creating and using Bedrock
programs.
This commit adds a command-line argument scheme, an embedded assembler,
a headless emulator for use in non-graphical environments, deferred
window creation for programs that do not access the screen device,
and new versions of phosphor and bedrock-core. The new version of
phosphor supports multi-window programs, which will make it possible to
implement program forking in the system device later on, and the new
version of bedrock-core implements the final core specification.
Diffstat (limited to 'src/devices/file/directory_listing.rs')
-rw-r--r-- | src/devices/file/directory_listing.rs | 115 |
1 files changed, 0 insertions, 115 deletions
diff --git a/src/devices/file/directory_listing.rs b/src/devices/file/directory_listing.rs deleted file mode 100644 index febc5c2..0000000 --- a/src/devices/file/directory_listing.rs +++ /dev/null @@ -1,115 +0,0 @@ -use super::*; - - -pub struct DirectoryListing { - children: Vec<BedrockFilePath>, - length: u32, - selected: Option<u32>, - name_buffer: CircularPathBuffer, -} - - -impl DirectoryListing { - pub fn from_path(path: &BedrockFilePath) -> Option<Self> { - macro_rules! unres { - ($result:expr) => { match $result { Ok(v) => v, Err(_) => continue} }; - } - macro_rules! unopt { - ($option:expr) => { match $option { Some(v) => v, None => continue} }; - } - - #[cfg(target_family = "windows")] { - if path.as_path().components().count() == 0 { - return Some(Self::construct_virtual_root()) - } - } - - let mut children = Vec::new(); - if let Ok(dir_listing) = std::fs::read_dir(path.as_path()) { - for (i, entry_result) in dir_listing.enumerate() { - // Firebreak to prevent emulator from consuming an absurd amount - // of memory when opening too large of a directory. - if i == (u16::MAX as usize) { - break; - } - - let entry = unres!(entry_result); - let entry_path = unopt!(BedrockFilePath::from_path(&entry.path(), path.base())); - if entry_path.is_hidden() { - continue; - } - - children.push(entry_path); - } - } - - children.sort(); - let length = u32::try_from(children.len()).ok()?; - let selected = None; - let name_buffer = CircularPathBuffer::new(); - Some( Self { children, length, selected, name_buffer } ) - } - - /// Generate entries for a virtual root directory. - #[cfg(target_family = "windows")] - fn construct_virtual_root() -> Self { - let mut children = Vec::new(); - let base = PathBuf::from(""); - let drive_bits = unsafe { - windows::Win32::Storage::FileSystem::GetLogicalDrives() - }; - for i in 0..26 { - if drive_bits & (0x1 << i) != 0 { - let letter: char = (b'A' + i).into(); - let path = PathBuf::from(format!("{letter}:/")); - if let Some(drive) = BedrockFilePath::from_path(&path, &base) { - children.push(drive); - } - } - } - - let length = children.len() as u32; - let selected = None; - let name_buffer = CircularPathBuffer::new(); - Self { children, length, selected, name_buffer } - } - - /// Attempts to return a directory child by index. - pub fn get(&self, index: u32) -> Option<&BedrockFilePath> { - self.children.get(index as usize) - } - - pub fn length(&self) -> u32 { - self.length - } - - /// Returns the index of the selected child, or zero if no child is selected. - pub fn selected(&self) -> u32 { - self.selected.unwrap_or(0) - } - - /// Attempts to select a child by index. - pub fn set_selected(&mut self, index: u32) { - if let Some(child) = self.get(index) { - let buffer = child.as_buffer(); - self.name_buffer.populate(buffer); - self.selected = Some(index); - } else { - self.name_buffer.clear(); - self.selected = None; - } - } - - pub fn child_name(&mut self) -> &mut CircularPathBuffer { - &mut self.name_buffer - } - - pub fn child_type(&self) -> Option<EntryType> { - self.selected.and_then(|s| self.get(s).and_then(|i| i.entry_type())) - } - - pub fn child_path(&self) -> Option<BedrockFilePath> { - self.selected.and_then(|s| self.get(s).and_then(|i| Some(i.clone()))) - } -} - |