194 lines
5.4 KiB
Rust
194 lines
5.4 KiB
Rust
use serde::{Deserialize, Serialize};
|
|
use std::collections::HashMap;
|
|
use std::env;
|
|
use std::fs;
|
|
use std::path::PathBuf;
|
|
use uuid::Uuid;
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct ConnectionConfig {
|
|
pub server_url: String,
|
|
pub access_token: Option<String>,
|
|
pub refresh_token: Option<String>,
|
|
pub username: Option<String>,
|
|
pub device_id: Option<String>,
|
|
pub device_name: String,
|
|
pub client_device_identifier: String,
|
|
}
|
|
|
|
impl Default for ConnectionConfig {
|
|
fn default() -> Self {
|
|
let platform = env::var("PLATFORM").unwrap_or_else(|_| "tg5040".to_string());
|
|
let label = match platform.as_str() {
|
|
"tg5050" => "TrimUI Smart Pro S (NextUI)",
|
|
"tg5040" => "TrimUI Handheld (NextUI)",
|
|
other => other,
|
|
};
|
|
Self {
|
|
server_url: String::new(),
|
|
access_token: None,
|
|
refresh_token: None,
|
|
username: None,
|
|
device_id: None,
|
|
device_name: label.to_string(),
|
|
client_device_identifier: Uuid::new_v4().to_string(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct ManualLink {
|
|
pub local_path: String,
|
|
pub rom_id: i64,
|
|
pub rom_name: String,
|
|
pub system_tag: String,
|
|
pub slot: String,
|
|
pub is_state: bool,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct NextUiSettings {
|
|
pub sync_states: bool,
|
|
pub max_backups_per_save: usize,
|
|
pub default_slot: String,
|
|
pub web_port: u16,
|
|
/// Optional custom NextUI tag -> RomM platform slug overrides (e.g. "PCE" -> "tg16")
|
|
#[serde(default)]
|
|
pub tag_overrides: HashMap<String, String>,
|
|
}
|
|
|
|
impl Default for NextUiSettings {
|
|
fn default() -> Self {
|
|
Self {
|
|
sync_states: true,
|
|
max_backups_per_save: 5,
|
|
default_slot: "default".to_string(),
|
|
web_port: 8765,
|
|
tag_overrides: HashMap::new(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct SyncLogEntry {
|
|
pub timestamp: String,
|
|
pub level: String,
|
|
pub action: String,
|
|
pub message: String,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct NextUiConfig {
|
|
pub connection: ConnectionConfig,
|
|
pub settings: NextUiSettings,
|
|
#[serde(default)]
|
|
pub manual_links: HashMap<String, ManualLink>,
|
|
#[serde(default)]
|
|
pub synced_state_hashes: HashMap<String, String>,
|
|
#[serde(default)]
|
|
pub sync_logs: Vec<SyncLogEntry>,
|
|
}
|
|
|
|
/// Resolves the NextUI SD card root directory (`$SDCARD_PATH` on TrimUI, or `/mnt/SDCARD`).
|
|
pub fn sdcard_root() -> PathBuf {
|
|
if let Ok(p) = env::var("SDCARD_PATH") {
|
|
if !p.trim().is_empty() {
|
|
return PathBuf::from(p);
|
|
}
|
|
}
|
|
let default_trimui = PathBuf::from("/mnt/SDCARD");
|
|
if default_trimui.exists() {
|
|
return default_trimui;
|
|
}
|
|
env::current_dir().unwrap_or_else(|_| PathBuf::from("."))
|
|
}
|
|
|
|
/// Resolves NextUI's Saves root (`$SAVES_PATH` or `$SDCARD_PATH/Saves`).
|
|
pub fn saves_root() -> PathBuf {
|
|
if let Ok(p) = env::var("SAVES_PATH") {
|
|
if !p.trim().is_empty() {
|
|
return PathBuf::from(p);
|
|
}
|
|
}
|
|
sdcard_root().join("Saves")
|
|
}
|
|
|
|
/// Resolves NextUI's Roms root (`$ROMS_PATH` or `$SDCARD_PATH/Roms`).
|
|
pub fn roms_root() -> PathBuf {
|
|
if let Ok(p) = env::var("ROMS_PATH") {
|
|
if !p.trim().is_empty() {
|
|
return PathBuf::from(p);
|
|
}
|
|
}
|
|
sdcard_root().join("Roms")
|
|
}
|
|
|
|
/// Resolves persistent config/data dir (`$USERDATA_PATH/romm-companion` or `$SDCARD_PATH/.userdata/shared/romm-companion`).
|
|
pub fn data_dir() -> PathBuf {
|
|
let base = if let Ok(p) = env::var("USERDATA_PATH") {
|
|
if !p.trim().is_empty() {
|
|
PathBuf::from(p)
|
|
} else {
|
|
sdcard_root().join(".userdata/shared")
|
|
}
|
|
} else {
|
|
sdcard_root().join(".userdata/shared")
|
|
};
|
|
let dir = base.join("romm-companion");
|
|
let _ = fs::create_dir_all(&dir);
|
|
dir
|
|
}
|
|
|
|
pub fn backups_dir() -> PathBuf {
|
|
let dir = data_dir().join("backups");
|
|
let _ = fs::create_dir_all(&dir);
|
|
dir
|
|
}
|
|
|
|
pub fn config_path() -> PathBuf {
|
|
data_dir().join("config.json")
|
|
}
|
|
|
|
pub fn conflicts_path() -> PathBuf {
|
|
data_dir().join("conflicts.json")
|
|
}
|
|
|
|
pub fn load_config() -> NextUiConfig {
|
|
let path = config_path();
|
|
if let Ok(raw) = fs::read_to_string(&path) {
|
|
if let Ok(cfg) = serde_json::from_str::<NextUiConfig>(&raw) {
|
|
return cfg;
|
|
}
|
|
}
|
|
NextUiConfig::default()
|
|
}
|
|
|
|
pub fn save_config(cfg: &NextUiConfig) -> Result<(), String> {
|
|
let path = config_path();
|
|
if let Some(parent) = path.parent() {
|
|
fs::create_dir_all(parent).map_err(|e| e.to_string())?;
|
|
}
|
|
let json = serde_json::to_string_pretty(cfg).map_err(|e| e.to_string())?;
|
|
fs::write(&path, json).map_err(|e| e.to_string())
|
|
}
|
|
|
|
pub fn push_log(cfg: &mut NextUiConfig, level: &str, action: &str, message: impl Into<String>) {
|
|
cfg.sync_logs.insert(
|
|
0,
|
|
SyncLogEntry {
|
|
timestamp: chrono::Utc::now().to_rfc3339(),
|
|
level: level.to_string(),
|
|
action: action.to_string(),
|
|
message: message.into(),
|
|
},
|
|
);
|
|
if cfg.sync_logs.len() > 150 {
|
|
cfg.sync_logs.truncate(150);
|
|
}
|
|
}
|