mirror of
https://github.com/Drop-OSS/drop.git
synced 2026-07-27 10:14:40 +10:00
chore: Moved generateGameMeta.ts to composables, using PathBuf instead of String for install_dirs
Signed-off-by: quexeky <git@quexeky.dev>
This commit is contained in:
+22
-37
@@ -1,8 +1,5 @@
|
|||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
collections::HashMap, fs::{self, create_dir_all}, path::{Path, PathBuf}, sync::{LazyLock, Mutex, RwLockWriteGuard}
|
||||||
fs::{self, create_dir_all},
|
|
||||||
path::{Path, PathBuf},
|
|
||||||
sync::{LazyLock, Mutex, RwLockWriteGuard},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use chrono::Utc;
|
use chrono::Utc;
|
||||||
@@ -60,10 +57,10 @@ pub struct GameVersion {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[serde_as]
|
#[serde_as]
|
||||||
#[derive(Serialize, Clone, Deserialize)]
|
#[derive(Serialize, Clone, Deserialize, Default)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct DatabaseApplications {
|
pub struct DatabaseApplications {
|
||||||
pub install_dirs: Vec<String>,
|
pub install_dirs: Vec<PathBuf>,
|
||||||
// Guaranteed to exist if the game also exists in the app state map
|
// Guaranteed to exist if the game also exists in the app state map
|
||||||
pub game_statuses: HashMap<String, GameDownloadStatus>,
|
pub game_statuses: HashMap<String, GameDownloadStatus>,
|
||||||
pub game_versions: HashMap<String, HashMap<String, GameVersion>>,
|
pub game_versions: HashMap<String, HashMap<String, GameVersion>>,
|
||||||
@@ -74,7 +71,7 @@ pub struct DatabaseApplications {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone)]
|
#[derive(Serialize, Deserialize, Clone, Default)]
|
||||||
pub struct Database {
|
pub struct Database {
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub settings: Settings,
|
pub settings: Settings,
|
||||||
@@ -83,6 +80,18 @@ pub struct Database {
|
|||||||
pub applications: DatabaseApplications,
|
pub applications: DatabaseApplications,
|
||||||
pub prev_database: Option<PathBuf>,
|
pub prev_database: Option<PathBuf>,
|
||||||
}
|
}
|
||||||
|
impl Database {
|
||||||
|
fn new<T: Into<PathBuf>>(games_base_dir: T, prev_database: Option<PathBuf>) -> Self {
|
||||||
|
Self {
|
||||||
|
applications: DatabaseApplications {
|
||||||
|
install_dirs: vec![games_base_dir.into()],
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
prev_database,
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
pub static DATA_ROOT_DIR: LazyLock<Mutex<PathBuf>> =
|
pub static DATA_ROOT_DIR: LazyLock<Mutex<PathBuf>> =
|
||||||
LazyLock::new(|| Mutex::new(BaseDirs::new().unwrap().data_dir().join("drop")));
|
LazyLock::new(|| Mutex::new(BaseDirs::new().unwrap().data_dir().join("drop")));
|
||||||
|
|
||||||
@@ -130,19 +139,7 @@ impl DatabaseImpls for DatabaseInterface {
|
|||||||
Err(e) => handle_invalid_database(e, db_path, games_base_dir),
|
Err(e) => handle_invalid_database(e, db_path, games_base_dir),
|
||||||
},
|
},
|
||||||
false => {
|
false => {
|
||||||
let default = Database {
|
let default = Database::new(games_base_dir, None);
|
||||||
settings: Settings::default(),
|
|
||||||
auth: None,
|
|
||||||
base_url: "".to_string(),
|
|
||||||
applications: DatabaseApplications {
|
|
||||||
install_dirs: vec![games_base_dir.to_str().unwrap().to_string()],
|
|
||||||
game_statuses: HashMap::new(),
|
|
||||||
transient_statuses: HashMap::new(),
|
|
||||||
game_versions: HashMap::new(),
|
|
||||||
installed_game_version: HashMap::new(),
|
|
||||||
},
|
|
||||||
prev_database: None,
|
|
||||||
};
|
|
||||||
debug!(
|
debug!(
|
||||||
"Creating database at path {}",
|
"Creating database at path {}",
|
||||||
db_path.as_os_str().to_str().unwrap()
|
db_path.as_os_str().to_str().unwrap()
|
||||||
@@ -164,7 +161,7 @@ impl DatabaseImpls for DatabaseInterface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn add_download_dir(new_dir: String) -> Result<(), String> {
|
pub fn add_download_dir(new_dir: PathBuf) -> Result<(), String> {
|
||||||
// Check the new directory is all good
|
// Check the new directory is all good
|
||||||
let new_dir_path = Path::new(&new_dir);
|
let new_dir_path = Path::new(&new_dir);
|
||||||
if new_dir_path.exists() {
|
if new_dir_path.exists() {
|
||||||
@@ -210,7 +207,7 @@ pub fn delete_download_dir(index: usize) -> Result<(), String> {
|
|||||||
// Will, in future, return disk/remaining size
|
// Will, in future, return disk/remaining size
|
||||||
// Just returns the directories that have been set up
|
// Just returns the directories that have been set up
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn fetch_download_dir_stats() -> Result<Vec<String>, String> {
|
pub fn fetch_download_dir_stats() -> Result<Vec<PathBuf>, String> {
|
||||||
let lock = DB.borrow_data().unwrap();
|
let lock = DB.borrow_data().unwrap();
|
||||||
let directories = lock.applications.install_dirs.clone();
|
let directories = lock.applications.install_dirs.clone();
|
||||||
drop(lock);
|
drop(lock);
|
||||||
@@ -241,26 +238,14 @@ fn handle_invalid_database(
|
|||||||
) -> rustbreak::Database<Database, rustbreak::backend::PathBackend, DropDatabaseSerializer> {
|
) -> rustbreak::Database<Database, rustbreak::backend::PathBackend, DropDatabaseSerializer> {
|
||||||
let new_path = {
|
let new_path = {
|
||||||
let time = Utc::now().timestamp();
|
let time = Utc::now().timestamp();
|
||||||
let mut base = db_path.clone().into_os_string();
|
let mut base = db_path.clone();
|
||||||
base.push(".");
|
base.push(".");
|
||||||
base.push(time.to_string());
|
base.push(time.to_string());
|
||||||
base
|
base
|
||||||
};
|
};
|
||||||
fs::copy(&db_path, &new_path).unwrap();
|
fs::rename(&db_path, &new_path).unwrap();
|
||||||
|
|
||||||
let db = Database {
|
let db = Database ::new(games_base_dir.into_os_string().into_string().unwrap(), Some(new_path.into()));
|
||||||
auth: None,
|
|
||||||
base_url: "".to_string(),
|
|
||||||
applications: DatabaseApplications {
|
|
||||||
install_dirs: vec![games_base_dir.to_str().unwrap().to_string()],
|
|
||||||
game_statuses: HashMap::new(),
|
|
||||||
transient_statuses: HashMap::new(),
|
|
||||||
game_versions: HashMap::new(),
|
|
||||||
installed_game_version: HashMap::new(),
|
|
||||||
},
|
|
||||||
prev_database: Some(new_path.into()),
|
|
||||||
settings: Settings::default(),
|
|
||||||
};
|
|
||||||
|
|
||||||
PathDatabase::create_at_path(db_path, db).expect("Database could not be created")
|
PathDatabase::create_at_path(db_path, db).expect("Database could not be created")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user