mirror of
https://github.com/Drop-OSS/drop.git
synced 2026-07-26 01:34:38 +10:00
8ff7604502
* chore: Major refactoring Still needs a massive go-over because there shouldn't be anything referencing tauri in any of the workspaces except the original one. Process manager has been refactored as an example Signed-off-by: quexeky <git@quexeky.dev> * fix: Remote tauri dependency from process Signed-off-by: quexeky <git@quexeky.dev> * refactor: Improvements to src-tauri Signed-off-by: quexeky <git@quexeky.dev> * refactor: Builds, but some logic still left to move back Signed-off-by: quexeky <git@quexeky.dev> * refactor: Finish refactor Signed-off-by: quexeky <git@quexeky.dev> * chore: Run cargo clippy && cargo fmt Signed-off-by: quexeky <git@quexeky.dev> * refactor: Move everything into src-tauri Signed-off-by: quexeky <git@quexeky.dev> --------- Signed-off-by: quexeky <git@quexeky.dev>
95 lines
2.6 KiB
Rust
95 lines
2.6 KiB
Rust
use std::{
|
|
collections::HashMap,
|
|
fs::File,
|
|
io::{self, Read, Write},
|
|
path::{Path, PathBuf},
|
|
};
|
|
|
|
use log::error;
|
|
use native_model::{Decode, Encode};
|
|
use utils::lock;
|
|
|
|
pub type DropData = v1::DropData;
|
|
|
|
pub static DROP_DATA_PATH: &str = ".dropdata";
|
|
|
|
pub mod v1 {
|
|
use std::{collections::HashMap, path::PathBuf, sync::Mutex};
|
|
|
|
use native_model::native_model;
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
#[native_model(id = 9, version = 1, with = native_model::rmp_serde_1_3::RmpSerde)]
|
|
pub struct DropData {
|
|
pub game_id: String,
|
|
pub game_version: String,
|
|
pub contexts: Mutex<HashMap<String, bool>>,
|
|
pub base_path: PathBuf,
|
|
}
|
|
|
|
impl DropData {
|
|
pub fn new(game_id: String, game_version: String, base_path: PathBuf) -> Self {
|
|
Self {
|
|
base_path,
|
|
game_id,
|
|
game_version,
|
|
contexts: Mutex::new(HashMap::new()),
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
impl DropData {
|
|
pub fn generate(game_id: String, game_version: String, base_path: PathBuf) -> Self {
|
|
match DropData::read(&base_path) {
|
|
Ok(v) => v,
|
|
Err(_) => DropData::new(game_id, game_version, base_path),
|
|
}
|
|
}
|
|
pub fn read(base_path: &Path) -> Result<Self, io::Error> {
|
|
let mut file = File::open(base_path.join(DROP_DATA_PATH))?;
|
|
|
|
let mut s = Vec::new();
|
|
file.read_to_end(&mut s)?;
|
|
|
|
native_model::rmp_serde_1_3::RmpSerde::decode(s).map_err(|e| {
|
|
io::Error::new(
|
|
io::ErrorKind::InvalidData,
|
|
format!("Failed to decode drop data: {e}"),
|
|
)
|
|
})
|
|
}
|
|
pub fn write(&self) {
|
|
let manifest_raw = match native_model::rmp_serde_1_3::RmpSerde::encode(&self) {
|
|
Ok(data) => data,
|
|
Err(_) => return,
|
|
};
|
|
|
|
let mut file = match File::create(self.base_path.join(DROP_DATA_PATH)) {
|
|
Ok(file) => file,
|
|
Err(e) => {
|
|
error!("{e}");
|
|
return;
|
|
}
|
|
};
|
|
|
|
match file.write_all(&manifest_raw) {
|
|
Ok(()) => {}
|
|
Err(e) => error!("{e}"),
|
|
}
|
|
}
|
|
pub fn set_contexts(&self, completed_contexts: &[(String, bool)]) {
|
|
*lock!(self.contexts) = completed_contexts
|
|
.iter()
|
|
.map(|s| (s.0.clone(), s.1))
|
|
.collect();
|
|
}
|
|
pub fn set_context(&self, context: String, state: bool) {
|
|
lock!(self.contexts).entry(context).insert_entry(state);
|
|
}
|
|
pub fn get_contexts(&self) -> HashMap<String, bool> {
|
|
lock!(self.contexts).clone()
|
|
}
|
|
}
|