Skip to content

Commit

Permalink
More refactoring woo
Browse files Browse the repository at this point in the history
  • Loading branch information
Not-A-Normal-Robot committed Oct 28, 2024
1 parent 6197271 commit 6a2db78
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 178 deletions.
3 changes: 1 addition & 2 deletions src/cold_clear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::time::{Instant, Duration};
use std::sync::{mpsc, Arc, Mutex};
use slint::SharedString;
use tokio::runtime::Runtime;
use crate::consts::paths;
use crate::dirs::paths;

slint::include_modules!();

Expand Down Expand Up @@ -145,7 +145,6 @@ pub fn download_cold_clear() -> Result<(), reqwest::Error> {
let remaining_size = total_size - downloaded_size;
let eta_secs = remaining_size / dl_rate;

// TODO
let advance_message = LoadingIPCMessage::AdvanceTo(
downloaded_size,
dl_rate,
Expand Down
2 changes: 1 addition & 1 deletion src/conf.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::fs;
use crate::consts::paths;
use crate::dirs::paths;
use crate::git;
use serde::{Serialize, Deserialize};

Expand Down
55 changes: 55 additions & 0 deletions src/consts.rs → src/dirs.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,58 @@
use std::fs;

pub fn clear_temp_dir() {
let path = crate::dirs::paths::get_sandboxed_save_path();

println!("Dangerous operation: Clearing temporary directory at {}", path.to_string_lossy());

if !path.exists() {
return;
}

let entries = fs::read_dir(path);

if let Err(_) = entries {
return;
}

let entries = entries.unwrap();

for entry in entries {
let entry = entry.expect("Failed to read entry");
let path = entry.path();
if path.is_dir() {
fs::remove_dir_all(&path)
.expect(
format!(
"Failed to remove directory {}",
path.to_string_lossy()
).as_str()
)
} else {
fs::remove_file(&path)
.expect(
format!(
"Failed to remove file {}",
path.to_string_lossy()
).as_str()
)
}
}

println!("Cleared temporary directory");
}


pub fn is_dir_empty(path: &str) -> bool {
let files = fs::read_dir(path);
if let Err(_) = files {
return false;
}

let files = files.unwrap();
return files.count() == 0;
}

pub mod paths {
use std::path::PathBuf;

Expand Down
67 changes: 6 additions & 61 deletions src/game.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::conf::Config;
use crate::consts;
use crate::dirs;
use crate::error_window;
use crate::git;
use std::fs;
Expand All @@ -20,7 +20,7 @@ pub fn run(cfg: &Config) {
}

if cfg.clear_temp_dir {
clear_temp_dir();
dirs::clear_temp_dir();
}

if cfg.import_save_on_play {
Expand Down Expand Up @@ -48,49 +48,6 @@ pub fn run(cfg: &Config) {
.expect("Failed to restore repository using git");
}

// TODO: Deduplicate
fn clear_temp_dir() {
let path = consts::paths::get_sandboxed_save_path();

println!("Dangerous operation: Clearing temporary directory at {}", path.to_string_lossy());

if !path.exists() {
return;
}

let entries = fs::read_dir(path);

if let Err(_) = entries {
return;
}

let entries = entries.unwrap();

for entry in entries {
let entry = entry.expect("Failed to read entry");
let path = entry.path();
if path.is_dir() {
fs::remove_dir_all(&path)
.expect(
format!(
"Failed to remove directory {}",
path.to_string_lossy()
).as_str()
)
} else {
fs::remove_file(&path)
.expect(
format!(
"Failed to remove file {}",
path.to_string_lossy()
).as_str()
)
}
}

println!("Cleared temporary directory");
}

fn copy_dir_all(from: &str, to: &str) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let entries = fs::read_dir(from);

Expand All @@ -117,13 +74,13 @@ fn copy_dir_all(from: &str, to: &str) -> Result<(), Box<dyn std::error::Error +
}

fn overwrite_temp_dir() {
let sandboxed_path = consts::paths::get_sandboxed_save_path();
let sandboxed_path = dirs::paths::get_sandboxed_save_path();

if !is_dir_empty(sandboxed_path.to_str().unwrap()) {
clear_temp_dir();
if !dirs::is_dir_empty(sandboxed_path.to_str().unwrap()) {
dirs::clear_temp_dir();
}

let normal_path = consts::paths::get_normal_save_path();
let normal_path = dirs::paths::get_normal_save_path();

if !normal_path.exists() {
eprintln!("Could not find normal save directory (inferred location: '{}')", normal_path.to_string_lossy());
Expand All @@ -147,16 +104,4 @@ fn overwrite_temp_dir() {
}

println!("Overwritten temporary directory");
}


// TODO: Deduplicate
fn is_dir_empty(path: &str) -> bool {
let files = fs::read_dir(path);
if let Err(_) = files {
return false;
}

let files = files.unwrap();
return files.count() == 0;
}
46 changes: 1 addition & 45 deletions src/git.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::consts;
use crate::dirs;
use std::fs;
use std::io;
use std::process::{Command, ExitStatus, Stdio};
Expand Down Expand Up @@ -243,48 +243,4 @@ pub fn is_repo_valid(path: &str) -> bool {
}

return has_git && has_main_lua;
}

// TODO: Deduplicate

fn clear_temp_dir() {
let path = consts::paths::get_sandboxed_save_path();

println!("Dangerous operation: Clearing temporary directory at {}", path.to_string_lossy());

if !path.exists() {
return;
}

let entries = fs::read_dir(path);

if let Err(_) = entries {
return;
}

let entries = entries.unwrap();

for entry in entries {
let entry = entry.expect("Failed to read entry");
let path = entry.path();
if path.is_dir() {
fs::remove_dir_all(&path)
.expect(
format!(
"Failed to remove directory {}",
path.to_string_lossy()
).as_str()
)
} else {
fs::remove_file(&path)
.expect(
format!(
"Failed to remove file {}",
path.to_string_lossy()
).as_str()
)
}
}

println!("Cleared temporary directory");
}
9 changes: 1 addition & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,10 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

use std::fs;
use std::path::PathBuf;
use std::rc::Rc;
use std::cell::RefCell;
use std::process::{Command, Stdio};
use slint::{ModelRc, SharedString, VecModel};
use copypasta::ClipboardProvider;
use open;

mod cold_clear;
mod consts;
mod dirs;
mod conf;
mod game;
mod git;
Expand Down
52 changes: 4 additions & 48 deletions src/main_window.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use open as file_open;
use copypasta::ClipboardProvider;
use crate::consts;
use crate::dirs;
use crate::conf::Config;
use crate::game;
use crate::git;
use crate::error_window;
use slint::{ModelRc, VecModel, SharedString};
use std::fs;

slint::include_modules!();

Expand Down Expand Up @@ -45,7 +44,7 @@ pub fn open(cfg: &Config) -> Result<MainWindow, slint::PlatformError> {
});
main_window.set_selected_version("".into());
main_window.set_sandbox_path(
consts::paths::get_sandboxed_save_path()
dirs::paths::get_sandboxed_save_path()
.to_string_lossy()
.to_string()
.into()
Expand All @@ -56,7 +55,7 @@ pub fn open(cfg: &Config) -> Result<MainWindow, slint::PlatformError> {
copy_text_handled(string.as_str());
});
main_window.on_open_save_dir(|| {
let path = consts::paths::get_sandboxed_save_path();
let path = dirs::paths::get_sandboxed_save_path();
if let Err(err) = file_open::that(&path) {
error_window::open_safe(
Some("Failed to open save directory".to_string()),
Expand Down Expand Up @@ -84,7 +83,7 @@ pub fn open(cfg: &Config) -> Result<MainWindow, slint::PlatformError> {
)
)
);
main_window.on_clear_save_dir(clear_temp_dir);
main_window.on_clear_save_dir(dirs::clear_temp_dir);
main_window.on_filter(|arr: ModelRc<SharedString>, search: SharedString| -> ModelRc<SharedString> {
let search = search.as_str().to_lowercase();
let filtered = arr.filter(
Expand Down Expand Up @@ -145,46 +144,3 @@ fn open_link(url: slint::SharedString) {
);
});
}

// TODO: Deduplicate
fn clear_temp_dir() {
let path = consts::paths::get_sandboxed_save_path();

println!("Dangerous operation: Clearing temporary directory at {}", path.to_string_lossy());

if !path.exists() {
return;
}

let entries = fs::read_dir(path);

if let Err(_) = entries {
return;
}

let entries = entries.unwrap();

for entry in entries {
let entry = entry.expect("Failed to read entry");
let path = entry.path();
if path.is_dir() {
fs::remove_dir_all(&path)
.expect(
format!(
"Failed to remove directory {}",
path.to_string_lossy()
).as_str()
)
} else {
fs::remove_file(&path)
.expect(
format!(
"Failed to remove file {}",
path.to_string_lossy()
).as_str()
)
}
}

println!("Cleared temporary directory");
}
15 changes: 2 additions & 13 deletions src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::git;
use crate::conf;
use rfd::FileDialog;
use std::cell::RefCell;
use std::fs;
use crate::dirs;
use std::rc::Rc;

slint::include_modules!();
Expand All @@ -28,7 +28,7 @@ pub fn run_setup() -> Result<(), slint::PlatformError> {
let valid = git::is_repo_valid(path.as_str());
window_clone.set_repo_valid(valid);

let empty = is_dir_empty(path.as_str());
let empty = dirs::is_dir_empty(path.as_str());
window_clone.set_dir_empty(empty);
});

Expand Down Expand Up @@ -63,15 +63,4 @@ pub fn run_setup() -> Result<(), slint::PlatformError> {
} else {
panic!("Setup closed prematurely");
}
}

// TODO: Deduplicate
fn is_dir_empty(path: &str) -> bool {
let files = fs::read_dir(path);
if let Err(_) = files {
return false;
}

let files = files.unwrap();
return files.count() == 0;
}

0 comments on commit 6a2db78

Please sign in to comment.