diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/bin/br/main.rs | 20 | ||||
-rw-r--r-- | src/devices/file_device.rs | 31 | ||||
-rw-r--r-- | src/emulators/graphical_emulator.rs | 2 | ||||
-rw-r--r-- | src/emulators/headless_emulator.rs | 2 | ||||
-rw-r--r-- | src/emulators/mod.rs | 3 |
5 files changed, 47 insertions, 11 deletions
diff --git a/src/bin/br/main.rs b/src/bin/br/main.rs index 81c5ec9..f4d79fa 100644 --- a/src/bin/br/main.rs +++ b/src/bin/br/main.rs @@ -55,6 +55,7 @@ fn main() { args.named("size").short('s'); args.named("decode-stdin").short('i'); args.named("encode-stdout").short('o'); + args.named("trust-files"); args.raise_errors(); let source = args.get("source").as_path_opt(); @@ -71,6 +72,7 @@ fn main() { }; let decode_stdin = args.get("decode-stdin").as_bool(); let encode_stdout = args.get("encode-stdout").as_bool(); + let trust_files = args.get("trust-files").as_bool(); // ----------------------------------------------------------------------- @@ -78,7 +80,9 @@ fn main() { let mut title = String::from("Bedrock program"); let mut icon = None; - if let Some(metadata) = Metadata::from(&bytecode) { + let metadata = Metadata::from(&bytecode); + + if let Some(ref metadata) = metadata { let name = metadata.name().unwrap_or("unnamed".to_string()); let authors = metadata.authors().unwrap_or_else(Vec::new); let mut metadata_string = format!("Program is '{name}'"); @@ -110,10 +114,20 @@ fn main() { path.add_extension("sym"); path }); + let name = metadata.and_then(|m| m.name()).and_then(|n| match n.split_once('/') { + Some((name, _)) => Some(name.to_string()), + None => Some(n), + }); + let identifier = name.as_ref().and_then( + |n| Some(n.to_lowercase().chars().filter_map( + |c| c.is_alphanumeric().then_some(c) + ).collect()) + ); + let config = EmulatorConfig { dimensions, fullscreen, zoom, palette, show_cursor, - decode_stdin, encode_stdout, - symbols_path, title, icon, + decode_stdin, encode_stdout, trust_files, + symbols_path, name, identifier, title, icon, }; if let Ok(phosphor) = Phosphor::new() { diff --git a/src/devices/file_device.rs b/src/devices/file_device.rs index ff5629b..43bb239 100644 --- a/src/devices/file_device.rs +++ b/src/devices/file_device.rs @@ -16,6 +16,7 @@ pub struct FileDevice { pub pointer_write: u32, pub length_write: u32, + pub enable: bool, pub enable_read: bool, pub enable_write: bool, pub enable_create: bool, @@ -26,6 +27,7 @@ pub struct FileDevice { impl Device for FileDevice { fn read(&mut self, port: u8) -> u8 { + if !self.enable { return 0x00; } match port { 0x0 => read_b!(self.entry.is_some()), 0x1 => read_b!(self.success), @@ -48,6 +50,7 @@ impl Device for FileDevice { } fn write(&mut self, port: u8, value: u8) -> Option<Signal> { + if !self.enable { return None; } match port { 0x0 => self.write_to_entry_port(value), 0x1 => self.write_to_action_port(value), @@ -81,20 +84,35 @@ impl Device for FileDevice { impl FileDevice { - pub fn new() -> Self { + pub fn new(config: &EmulatorConfig) -> Self { #[cfg(target_family = "unix")] let default_base: PathBuf = PathBuf::from("/"); #[cfg(target_family = "windows")] let default_base: PathBuf = PathBuf::from(""); + let current_dir = match std::env::current_dir() { + Ok(dir) => PathBuf::from(dir), + Err(_) => PathBuf::from(""), + }; + + let (enable, base_path, default_path) = if config.trust_files { + (true, default_base, current_dir) + } else if let Some(config_dir) = dirs_next::config_dir() { + let bedrock_dir = config_dir.join("bedrock"); + let identifier = config.identifier.clone().unwrap_or("default".to_string()); + let sandbox_dir = bedrock_dir.join(identifier); + vagabond::make_directory(&sandbox_dir).unwrap(); + (true, sandbox_dir.clone(), sandbox_dir) + } else { + error!("Could not determine sandbox path for file device"); + (false, default_base, current_dir) + }; + // TODO: I'm not at all confident that the default path is correct // when not being set as the current directory. Self { - base_path: default_base, - default_path: match std::env::current_dir() { - Ok(dir) => PathBuf::from(dir), - Err(_) => PathBuf::from(""), - }, + base_path, + default_path, entry_buffer: BedrockPathBuffer::new(), action_buffer: BedrockPathBuffer::new(), @@ -107,6 +125,7 @@ impl FileDevice { pointer_write: 0, length_write: 0, + enable, enable_read: true, enable_write: true, enable_create: true, diff --git a/src/emulators/graphical_emulator.rs b/src/emulators/graphical_emulator.rs index 2680d3f..03e3057 100644 --- a/src/emulators/graphical_emulator.rs +++ b/src/emulators/graphical_emulator.rs @@ -112,7 +112,7 @@ impl GraphicalDeviceBus { input: InputDevice::new(), screen: ScreenDevice::new(&config), stream: StreamDevice::new(&config), - file: FileDevice::new(), + file: FileDevice::new(&config), wake_queue: WakeQueue::new(), } } diff --git a/src/emulators/headless_emulator.rs b/src/emulators/headless_emulator.rs index cac58cf..770bae3 100644 --- a/src/emulators/headless_emulator.rs +++ b/src/emulators/headless_emulator.rs @@ -70,7 +70,7 @@ impl HeadlessDeviceBus { math: MathDevice::new(), clock: ClockDevice::new(), stream: StreamDevice::new(&config), - file: FileDevice::new(), + file: FileDevice::new(&config), wake_queue: WakeQueue::new(), } } diff --git a/src/emulators/mod.rs b/src/emulators/mod.rs index 8f04e4d..d4a58f9 100644 --- a/src/emulators/mod.rs +++ b/src/emulators/mod.rs @@ -15,7 +15,10 @@ pub struct EmulatorConfig { pub show_cursor: bool, pub decode_stdin: bool, pub encode_stdout: bool, + pub trust_files: bool, pub symbols_path: Option<PathBuf>, + pub name: Option<String>, + pub identifier: Option<String>, pub title: String, pub icon: Option<Icon>, } |