forked from NG-SD-Plugins/PowerTools
Add multi-profile per-game functionality for #82, change to JSON to RON format
This commit is contained in:
parent
a90932d813
commit
3aa9680bae
18 changed files with 253 additions and 159 deletions
26
backend/Cargo.lock
generated
26
backend/Cargo.lock
generated
|
@ -153,7 +153,7 @@ version = "0.64.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c4243e6031260db77ede97ad86c27e501d646a27ab57b59a574f725d98ab1fb4"
|
checksum = "c4243e6031260db77ede97ad86c27e501d646a27ab57b59a574f725d98ab1fb4"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags",
|
"bitflags 1.3.2",
|
||||||
"cexpr",
|
"cexpr",
|
||||||
"clang-sys",
|
"clang-sys",
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
|
@ -175,6 +175,15 @@ version = "1.3.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bitflags"
|
||||||
|
version = "2.4.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07"
|
||||||
|
dependencies = [
|
||||||
|
"serde",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "block-buffer"
|
name = "block-buffer"
|
||||||
version = "0.10.4"
|
version = "0.10.4"
|
||||||
|
@ -599,7 +608,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f3e372db8e5c0d213e0cd0b9be18be2aca3d44cf2fe30a9d46a65581cd454584"
|
checksum = "f3e372db8e5c0d213e0cd0b9be18be2aca3d44cf2fe30a9d46a65581cd454584"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"base64 0.13.1",
|
"base64 0.13.1",
|
||||||
"bitflags",
|
"bitflags 1.3.2",
|
||||||
"bytes",
|
"bytes",
|
||||||
"headers-core",
|
"headers-core",
|
||||||
"http",
|
"http",
|
||||||
|
@ -1059,6 +1068,7 @@ dependencies = [
|
||||||
"limits_core",
|
"limits_core",
|
||||||
"log",
|
"log",
|
||||||
"regex",
|
"regex",
|
||||||
|
"ron",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"simplelog",
|
"simplelog",
|
||||||
|
@ -1161,6 +1171,18 @@ version = "0.7.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e5ea92a5b6195c6ef2a0295ea818b312502c6fc94dde986c5553242e18fd4ce2"
|
checksum = "e5ea92a5b6195c6ef2a0295ea818b312502c6fc94dde986c5553242e18fd4ce2"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ron"
|
||||||
|
version = "0.8.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b91f7eff05f748767f183df4320a63d6936e9c6107d97c9e6bdd9784f4289c94"
|
||||||
|
dependencies = [
|
||||||
|
"base64 0.21.2",
|
||||||
|
"bitflags 2.4.1",
|
||||||
|
"serde",
|
||||||
|
"serde_derive",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rustc-demangle"
|
name = "rustc-demangle"
|
||||||
version = "0.1.23"
|
version = "0.1.23"
|
||||||
|
|
|
@ -15,6 +15,7 @@ readme = "../README.md"
|
||||||
usdpl-back = { version = "0.10.1", features = ["blocking"] }#, path = "../../usdpl-rs/usdpl-back"}
|
usdpl-back = { version = "0.10.1", features = ["blocking"] }#, path = "../../usdpl-rs/usdpl-back"}
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
|
ron = "0.8"
|
||||||
sysfuss = { version = "0.2", features = ["derive"] }#,path = "../../sysfs-nav"}
|
sysfuss = { version = "0.2", features = ["derive"] }#,path = "../../sysfs-nav"}
|
||||||
|
|
||||||
# async
|
# async
|
||||||
|
|
|
@ -55,18 +55,34 @@ pub fn load_settings(
|
||||||
sender: Sender<ApiMessage>,
|
sender: Sender<ApiMessage>,
|
||||||
) -> impl Fn(super::ApiParameterType) -> super::ApiParameterType {
|
) -> impl Fn(super::ApiParameterType) -> super::ApiParameterType {
|
||||||
let sender = Mutex::new(sender); // Sender is not Sync; this is required for safety
|
let sender = Mutex::new(sender); // Sender is not Sync; this is required for safety
|
||||||
let setter = move |path: u64, name: String| {
|
let setter = move |id: u64, name: String, variant: u64, variant_name: Option<String>| {
|
||||||
sender
|
sender
|
||||||
.lock()
|
.lock()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.send(ApiMessage::LoadSettings(path, name))
|
.send(ApiMessage::LoadSettings(id, name, variant, variant_name.unwrap_or_else(|| crate::consts::DEFAULT_SETTINGS_VARIANT_NAME.to_owned())))
|
||||||
.expect("load_settings send failed")
|
.expect("load_settings send failed")
|
||||||
};
|
};
|
||||||
move |params_in: super::ApiParameterType| {
|
move |params_in: super::ApiParameterType| {
|
||||||
if let Some(Primitive::String(id)) = params_in.get(0) {
|
if let Some(Primitive::String(id)) = params_in.get(0) {
|
||||||
if let Some(Primitive::String(name)) = params_in.get(1) {
|
if let Some(Primitive::String(name)) = params_in.get(1) {
|
||||||
setter(id.parse().unwrap_or_default(), name.to_owned());
|
if let Some(Primitive::F64(variant_id)) = params_in.get(2) {
|
||||||
|
if let Some(Primitive::String(variant_name)) = params_in.get(3) {
|
||||||
|
setter(id.parse().unwrap_or_default(),
|
||||||
|
name.to_owned(),
|
||||||
|
*variant_id as _,
|
||||||
|
Some(variant_name.to_owned()));
|
||||||
vec![true.into()]
|
vec![true.into()]
|
||||||
|
} else {
|
||||||
|
setter(id.parse().unwrap_or_default(),
|
||||||
|
name.to_owned(),
|
||||||
|
*variant_id as _,
|
||||||
|
None);
|
||||||
|
vec![true.into()]
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log::warn!("load_settings missing variant id parameter");
|
||||||
|
vec!["load_settings missing variant id parameter".into()]
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
log::warn!("load_settings missing name parameter");
|
log::warn!("load_settings missing name parameter");
|
||||||
vec!["load_settings missing name parameter".into()]
|
vec!["load_settings missing name parameter".into()]
|
||||||
|
|
|
@ -5,7 +5,6 @@ use crate::persist::SettingsJson;
|
||||||
use crate::settings::{
|
use crate::settings::{
|
||||||
MinMax, OnPowerEvent, OnResume, OnSet, PowerMode, Settings, TBattery, TCpus, TGeneral, TGpu,
|
MinMax, OnPowerEvent, OnResume, OnSet, PowerMode, Settings, TBattery, TCpus, TGeneral, TGpu,
|
||||||
};
|
};
|
||||||
use crate::utility::unwrap_maybe_fatal;
|
|
||||||
|
|
||||||
type Callback<T> = Box<dyn FnOnce(T) + Send>;
|
type Callback<T> = Box<dyn FnOnce(T) + Send>;
|
||||||
|
|
||||||
|
@ -23,7 +22,7 @@ pub enum ApiMessage {
|
||||||
OnChargeChange(f64), // battery fill amount: 0 = empty, 1 = full
|
OnChargeChange(f64), // battery fill amount: 0 = empty, 1 = full
|
||||||
PowerVibeCheck,
|
PowerVibeCheck,
|
||||||
WaitForEmptyQueue(Callback<()>),
|
WaitForEmptyQueue(Callback<()>),
|
||||||
LoadSettings(u64, String), // (path, name)
|
LoadSettings(u64, String, u64, String), // (path, name, variant, variant name)
|
||||||
LoadMainSettings,
|
LoadMainSettings,
|
||||||
LoadSystemSettings,
|
LoadSystemSettings,
|
||||||
GetLimits(Callback<super::SettingsLimits>),
|
GetLimits(Callback<super::SettingsLimits>),
|
||||||
|
@ -287,21 +286,14 @@ impl ApiMessageHandler {
|
||||||
log::debug!("api_worker is saving...");
|
log::debug!("api_worker is saving...");
|
||||||
let is_persistent = *settings.general.persistent();
|
let is_persistent = *settings.general.persistent();
|
||||||
let save_path =
|
let save_path =
|
||||||
crate::utility::settings_dir().join(settings.general.get_path().clone());
|
crate::utility::settings_dir().join(settings.general.get_path());
|
||||||
if is_persistent {
|
if is_persistent {
|
||||||
let settings_clone = settings.json();
|
let settings_clone = settings.json();
|
||||||
let save_json: SettingsJson = settings_clone.into();
|
let save_json: SettingsJson = settings_clone.into();
|
||||||
unwrap_maybe_fatal(save_json.save(&save_path), "Failed to save settings");
|
if let Err(e) = crate::persist::FileJson::update_variant_or_create(&save_path, save_json, settings.general.get_name().to_owned()) {
|
||||||
if let Some(event) = &settings.general.on_event().on_save {
|
log::error!("Failed to create/update settings file {}: {}", save_path.display(), e);
|
||||||
if !event.is_empty() {
|
|
||||||
unwrap_maybe_fatal(
|
|
||||||
std::process::Command::new("/bin/bash")
|
|
||||||
.args(&["-c", event])
|
|
||||||
.spawn(),
|
|
||||||
"Failed to start on_save event command",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
//unwrap_maybe_fatal(save_json.save(&save_path), "Failed to save settings");
|
||||||
log::debug!("Saved settings to {}", save_path.display());
|
log::debug!("Saved settings to {}", save_path.display());
|
||||||
if let Err(e) = crate::utility::chown_settings_dir() {
|
if let Err(e) = crate::utility::chown_settings_dir() {
|
||||||
log::error!("Failed to change config dir permissions: {}", e);
|
log::error!("Failed to change config dir permissions: {}", e);
|
||||||
|
@ -375,9 +367,9 @@ impl ApiMessageHandler {
|
||||||
self.on_empty.push(callback);
|
self.on_empty.push(callback);
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
ApiMessage::LoadSettings(id, name) => {
|
ApiMessage::LoadSettings(id, name, variant_id, variant_name) => {
|
||||||
let path = format!("{}.json", id);
|
let path = format!("{}.json", id);
|
||||||
match settings.load_file(path.into(), name, false) {
|
match settings.load_file(path.into(), name, variant_id, variant_name, false) {
|
||||||
Ok(success) => log::info!("Loaded settings file? {}", success),
|
Ok(success) => log::info!("Loaded settings file? {}", success),
|
||||||
Err(e) => log::warn!("Load file err: {}", e),
|
Err(e) => log::warn!("Load file err: {}", e),
|
||||||
}
|
}
|
||||||
|
@ -387,6 +379,8 @@ impl ApiMessageHandler {
|
||||||
match settings.load_file(
|
match settings.load_file(
|
||||||
crate::consts::DEFAULT_SETTINGS_FILE.into(),
|
crate::consts::DEFAULT_SETTINGS_FILE.into(),
|
||||||
crate::consts::DEFAULT_SETTINGS_NAME.to_owned(),
|
crate::consts::DEFAULT_SETTINGS_NAME.to_owned(),
|
||||||
|
0,
|
||||||
|
crate::consts::DEFAULT_SETTINGS_VARIANT_NAME.to_owned(),
|
||||||
true,
|
true,
|
||||||
) {
|
) {
|
||||||
Ok(success) => log::info!("Loaded main settings file? {}", success),
|
Ok(success) => log::info!("Loaded main settings file? {}", success),
|
||||||
|
@ -395,7 +389,7 @@ impl ApiMessageHandler {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
ApiMessage::LoadSystemSettings => {
|
ApiMessage::LoadSystemSettings => {
|
||||||
settings.load_system_default(settings.general.get_name().to_owned());
|
settings.load_system_default(settings.general.get_name().to_owned(), settings.general.get_variant_id(), settings.general.get_variant_name().to_owned());
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
ApiMessage::GetLimits(cb) => {
|
ApiMessage::GetLimits(cb) => {
|
||||||
|
|
|
@ -5,6 +5,7 @@ pub const PACKAGE_VERSION: &'static str = env!("CARGO_PKG_VERSION");
|
||||||
|
|
||||||
pub const DEFAULT_SETTINGS_FILE: &str = "default_settings.json";
|
pub const DEFAULT_SETTINGS_FILE: &str = "default_settings.json";
|
||||||
pub const DEFAULT_SETTINGS_NAME: &str = "Main";
|
pub const DEFAULT_SETTINGS_NAME: &str = "Main";
|
||||||
|
pub const DEFAULT_SETTINGS_VARIANT_NAME: &str = "Primary";
|
||||||
|
|
||||||
pub const LIMITS_FILE: &str = "limits_cache.json";
|
pub const LIMITS_FILE: &str = "limits_cache.json";
|
||||||
pub const LIMITS_OVERRIDE_FILE: &str = "limits_override.json";
|
pub const LIMITS_OVERRIDE_FILE: &str = "limits_override.json";
|
||||||
|
|
|
@ -75,12 +75,20 @@ fn main() -> Result<(), ()> {
|
||||||
let _limits_handle = crate::settings::limits_worker_spawn();
|
let _limits_handle = crate::settings::limits_worker_spawn();
|
||||||
|
|
||||||
let mut loaded_settings =
|
let mut loaded_settings =
|
||||||
persist::SettingsJson::open(utility::settings_dir().join(DEFAULT_SETTINGS_FILE))
|
persist::FileJson::open(utility::settings_dir().join(DEFAULT_SETTINGS_FILE))
|
||||||
.map(|settings| settings::Settings::from_json(settings, DEFAULT_SETTINGS_FILE.into()))
|
.map(|mut file| file.variants.remove("0")
|
||||||
|
.map(|settings| settings::Settings::from_json(DEFAULT_SETTINGS_NAME.into(), settings, DEFAULT_SETTINGS_FILE.into()))
|
||||||
|
.unwrap_or_else(|| settings::Settings::system_default(
|
||||||
|
DEFAULT_SETTINGS_FILE.into(),
|
||||||
|
DEFAULT_SETTINGS_NAME.into(),
|
||||||
|
0,
|
||||||
|
DEFAULT_SETTINGS_VARIANT_NAME.into())))
|
||||||
.unwrap_or_else(|_| {
|
.unwrap_or_else(|_| {
|
||||||
settings::Settings::system_default(
|
settings::Settings::system_default(
|
||||||
DEFAULT_SETTINGS_FILE.into(),
|
DEFAULT_SETTINGS_FILE.into(),
|
||||||
DEFAULT_SETTINGS_NAME.into(),
|
DEFAULT_SETTINGS_NAME.into(),
|
||||||
|
0,
|
||||||
|
DEFAULT_SETTINGS_VARIANT_NAME.into(),
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum JsonError {
|
pub enum SerdeError {
|
||||||
Serde(serde_json::Error),
|
Serde(RonError),
|
||||||
Io(std::io::Error),
|
Io(std::io::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for JsonError {
|
impl std::fmt::Display for SerdeError {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Self::Serde(e) => (e as &dyn std::fmt::Display).fmt(f),
|
Self::Serde(e) => (e as &dyn std::fmt::Display).fmt(f),
|
||||||
|
@ -12,3 +12,32 @@ impl std::fmt::Display for JsonError {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for SerdeError {}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum RonError {
|
||||||
|
General(ron::error::Error),
|
||||||
|
Spanned(ron::error::SpannedError),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for RonError {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
Self::General(e) => (e as &dyn std::fmt::Display).fmt(f),
|
||||||
|
Self::Spanned(e) => (e as &dyn std::fmt::Display).fmt(f),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<ron::error::Error> for RonError {
|
||||||
|
fn from(value: ron::error::Error) -> Self {
|
||||||
|
Self::General(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<ron::error::SpannedError> for RonError {
|
||||||
|
fn from(value: ron::error::SpannedError) -> Self {
|
||||||
|
Self::Spanned(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
62
backend/src/persist/file.rs
Normal file
62
backend/src/persist/file.rs
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use super::SerdeError;
|
||||||
|
use super::SettingsJson;
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub struct FileJson {
|
||||||
|
pub version: u64,
|
||||||
|
pub name: String,
|
||||||
|
pub variants: HashMap<String, SettingsJson>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FileJson {
|
||||||
|
pub fn save<P: AsRef<std::path::Path>>(&self, path: P) -> Result<(), SerdeError> {
|
||||||
|
let path = path.as_ref();
|
||||||
|
|
||||||
|
if !self.variants.is_empty() {
|
||||||
|
if let Some(parent) = path.parent() {
|
||||||
|
std::fs::create_dir_all(parent).map_err(SerdeError::Io)?;
|
||||||
|
}
|
||||||
|
let mut file = std::fs::File::create(path).map_err(SerdeError::Io)?;
|
||||||
|
ron::ser::to_writer_pretty(&mut file, &self, crate::utility::ron_pretty_config()).map_err(|e| SerdeError::Serde(e.into()))
|
||||||
|
} else {
|
||||||
|
if path.exists() {
|
||||||
|
// remove settings file when persistence is turned off, to prevent it from be loaded next time.
|
||||||
|
std::fs::remove_file(path).map_err(SerdeError::Io)
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn open<P: AsRef<std::path::Path>>(path: P) -> Result<Self, SerdeError> {
|
||||||
|
let mut file = std::fs::File::open(path).map_err(SerdeError::Io)?;
|
||||||
|
ron::de::from_reader(&mut file).map_err(|e| SerdeError::Serde(e.into()))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update_variant_or_create<P: AsRef<std::path::Path>>(path: P, setting: SettingsJson, given_name: String) -> Result<(), SerdeError> {
|
||||||
|
if !setting.persistent {
|
||||||
|
return Ok(())
|
||||||
|
}
|
||||||
|
let path = path.as_ref();
|
||||||
|
|
||||||
|
let file = if path.exists() {
|
||||||
|
let mut file = Self::open(path)?;
|
||||||
|
file.variants.insert(setting.variant.to_string(), setting);
|
||||||
|
file
|
||||||
|
} else {
|
||||||
|
let mut setting_variants = HashMap::with_capacity(1);
|
||||||
|
setting_variants.insert(setting.variant.to_string(), setting);
|
||||||
|
Self {
|
||||||
|
version: 0,
|
||||||
|
name: given_name,
|
||||||
|
variants: setting_variants,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
file.save(path)
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,38 +2,18 @@ use std::default::Default;
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use super::JsonError;
|
|
||||||
use super::{BatteryJson, CpuJson, DriverJson, GpuJson};
|
use super::{BatteryJson, CpuJson, DriverJson, GpuJson};
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
|
||||||
pub struct OnEventJson {
|
|
||||||
pub on_save: Option<String>,
|
|
||||||
pub on_load: Option<String>,
|
|
||||||
pub on_set: Option<String>,
|
|
||||||
pub on_resume: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for OnEventJson {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
on_save: None,
|
|
||||||
on_load: None,
|
|
||||||
on_set: None,
|
|
||||||
on_resume: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct SettingsJson {
|
pub struct SettingsJson {
|
||||||
pub version: u64,
|
pub version: u64,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
pub variant: u64,
|
||||||
pub persistent: bool,
|
pub persistent: bool,
|
||||||
pub cpus: Vec<CpuJson>,
|
pub cpus: Vec<CpuJson>,
|
||||||
pub gpu: GpuJson,
|
pub gpu: GpuJson,
|
||||||
pub battery: BatteryJson,
|
pub battery: BatteryJson,
|
||||||
pub provider: Option<DriverJson>,
|
pub provider: Option<DriverJson>,
|
||||||
pub events: Option<OnEventJson>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for SettingsJson {
|
impl Default for SettingsJson {
|
||||||
|
@ -41,42 +21,16 @@ impl Default for SettingsJson {
|
||||||
Self {
|
Self {
|
||||||
version: 0,
|
version: 0,
|
||||||
name: crate::consts::DEFAULT_SETTINGS_NAME.to_owned(),
|
name: crate::consts::DEFAULT_SETTINGS_NAME.to_owned(),
|
||||||
|
variant: 0,
|
||||||
persistent: false,
|
persistent: false,
|
||||||
cpus: Vec::with_capacity(8),
|
cpus: Vec::with_capacity(8),
|
||||||
gpu: GpuJson::default(),
|
gpu: GpuJson::default(),
|
||||||
battery: BatteryJson::default(),
|
battery: BatteryJson::default(),
|
||||||
provider: None,
|
provider: None,
|
||||||
events: None,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SettingsJson {
|
|
||||||
pub fn save<P: AsRef<std::path::Path>>(&self, path: P) -> Result<(), JsonError> {
|
|
||||||
let path = path.as_ref();
|
|
||||||
|
|
||||||
if self.persistent {
|
|
||||||
if let Some(parent) = path.parent() {
|
|
||||||
std::fs::create_dir_all(parent).map_err(JsonError::Io)?;
|
|
||||||
}
|
|
||||||
let mut file = std::fs::File::create(path).map_err(JsonError::Io)?;
|
|
||||||
serde_json::to_writer_pretty(&mut file, &self).map_err(JsonError::Serde)
|
|
||||||
} else {
|
|
||||||
if path.exists() {
|
|
||||||
// remove settings file when persistence is turned off, to prevent it from be loaded next time.
|
|
||||||
std::fs::remove_file(path).map_err(JsonError::Io)
|
|
||||||
} else {
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn open<P: AsRef<std::path::Path>>(path: P) -> Result<Self, JsonError> {
|
|
||||||
let mut file = std::fs::File::open(path).map_err(JsonError::Io)?;
|
|
||||||
serde_json::from_reader(&mut file).map_err(JsonError::Serde)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone)]
|
#[derive(Serialize, Deserialize, Clone)]
|
||||||
pub struct MinMaxJson<T> {
|
pub struct MinMaxJson<T> {
|
||||||
pub max: Option<T>,
|
pub max: Option<T>,
|
||||||
|
|
|
@ -2,13 +2,15 @@ mod battery;
|
||||||
mod cpu;
|
mod cpu;
|
||||||
mod driver;
|
mod driver;
|
||||||
mod error;
|
mod error;
|
||||||
|
mod file;
|
||||||
mod general;
|
mod general;
|
||||||
mod gpu;
|
mod gpu;
|
||||||
|
|
||||||
pub use battery::{BatteryEventJson, BatteryJson};
|
pub use battery::{BatteryEventJson, BatteryJson};
|
||||||
pub use cpu::CpuJson;
|
pub use cpu::CpuJson;
|
||||||
pub use driver::DriverJson;
|
pub use driver::DriverJson;
|
||||||
pub use general::{MinMaxJson, OnEventJson, SettingsJson};
|
pub use file::FileJson;
|
||||||
|
pub use general::{MinMaxJson, SettingsJson};
|
||||||
pub use gpu::GpuJson;
|
pub use gpu::GpuJson;
|
||||||
|
|
||||||
pub use error::JsonError;
|
pub use error::{SerdeError, RonError};
|
||||||
|
|
|
@ -10,7 +10,7 @@ use crate::settings::{Driver, General, TBattery, TCpus, TGeneral, TGpu, Provider
|
||||||
fn get_limits() -> limits_core::json_v2::Base {
|
fn get_limits() -> limits_core::json_v2::Base {
|
||||||
let limits_path = super::utility::limits_path();
|
let limits_path = super::utility::limits_path();
|
||||||
match File::open(&limits_path) {
|
match File::open(&limits_path) {
|
||||||
Ok(f) => match serde_json::from_reader(f) {
|
Ok(f) => match ron::de::from_reader(f) {
|
||||||
Ok(lim) => lim,
|
Ok(lim) => lim,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::warn!(
|
log::warn!(
|
||||||
|
@ -35,7 +35,7 @@ fn get_limits() -> limits_core::json_v2::Base {
|
||||||
fn get_limits_overrides() -> Option<Limits> {
|
fn get_limits_overrides() -> Option<Limits> {
|
||||||
let limits_override_path = super::utility::limits_override_path();
|
let limits_override_path = super::utility::limits_override_path();
|
||||||
match File::open(&limits_override_path) {
|
match File::open(&limits_override_path) {
|
||||||
Ok(f) => match serde_json::from_reader(f) {
|
Ok(f) => match ron::de::from_reader(f) {
|
||||||
Ok(lim) => Some(lim),
|
Ok(lim) => Some(lim),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::warn!(
|
log::warn!(
|
||||||
|
@ -63,6 +63,8 @@ pub fn auto_detect_provider() -> DriverJson {
|
||||||
None,
|
None,
|
||||||
crate::utility::settings_dir().join("autodetect.json"),
|
crate::utility::settings_dir().join("autodetect.json"),
|
||||||
"".to_owned(),
|
"".to_owned(),
|
||||||
|
0,
|
||||||
|
crate::consts::DEFAULT_SETTINGS_VARIANT_NAME.to_owned(),
|
||||||
)
|
)
|
||||||
.battery
|
.battery
|
||||||
.provider();
|
.provider();
|
||||||
|
@ -72,16 +74,19 @@ pub fn auto_detect_provider() -> DriverJson {
|
||||||
|
|
||||||
/// Device detection logic
|
/// Device detection logic
|
||||||
pub fn auto_detect0(
|
pub fn auto_detect0(
|
||||||
settings_opt: Option<SettingsJson>,
|
settings_opt: Option<&SettingsJson>,
|
||||||
json_path: std::path::PathBuf,
|
json_path: std::path::PathBuf,
|
||||||
name: String,
|
name: String,
|
||||||
|
variant_id: u64,
|
||||||
|
variant_name: String,
|
||||||
) -> Driver {
|
) -> Driver {
|
||||||
let mut general_driver = Box::new(General {
|
let mut general_driver = Box::new(General {
|
||||||
persistent: false,
|
persistent: false,
|
||||||
path: json_path,
|
path: json_path,
|
||||||
name,
|
name,
|
||||||
|
variant_id,
|
||||||
|
variant_name,
|
||||||
driver: DriverJson::AutoDetect,
|
driver: DriverJson::AutoDetect,
|
||||||
events: Default::default(),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let cpu_info: String = usdpl_back::api::files::read_single("/proc/cpuinfo").unwrap_or_default();
|
let cpu_info: String = usdpl_back::api::files::read_single("/proc/cpuinfo").unwrap_or_default();
|
||||||
|
|
|
@ -15,7 +15,7 @@ pub fn spawn() -> JoinHandle<()> {
|
||||||
// try to load limits from file, fallback to built-in default
|
// try to load limits from file, fallback to built-in default
|
||||||
let base = if limits_path.exists() {
|
let base = if limits_path.exists() {
|
||||||
match std::fs::File::open(&limits_path) {
|
match std::fs::File::open(&limits_path) {
|
||||||
Ok(f) => match serde_json::from_reader(f) {
|
Ok(f) => match ron::de::from_reader(f) {
|
||||||
Ok(b) => b,
|
Ok(b) => b,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::error!("Cannot parse {}: {}", limits_path.display(), e);
|
log::error!("Cannot parse {}: {}", limits_path.display(), e);
|
||||||
|
@ -72,7 +72,7 @@ pub fn get_limits_cached() -> Base {
|
||||||
let limits_path = super::utility::limits_path();
|
let limits_path = super::utility::limits_path();
|
||||||
if limits_path.is_file() {
|
if limits_path.is_file() {
|
||||||
match std::fs::File::open(&limits_path) {
|
match std::fs::File::open(&limits_path) {
|
||||||
Ok(f) => match serde_json::from_reader(f) {
|
Ok(f) => match ron::de::from_reader(f) {
|
||||||
Ok(b) => b,
|
Ok(b) => b,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::error!("Cannot parse {}: {}", limits_path.display(), e);
|
log::error!("Cannot parse {}: {}", limits_path.display(), e);
|
||||||
|
@ -93,7 +93,7 @@ pub fn get_limits_cached() -> Base {
|
||||||
fn save_base(new_base: &Base, path: impl AsRef<std::path::Path>) {
|
fn save_base(new_base: &Base, path: impl AsRef<std::path::Path>) {
|
||||||
let limits_path = path.as_ref();
|
let limits_path = path.as_ref();
|
||||||
match std::fs::File::create(&limits_path) {
|
match std::fs::File::create(&limits_path) {
|
||||||
Ok(f) => match serde_json::to_writer_pretty(f, &new_base) {
|
Ok(f) => match ron::ser::to_writer_pretty(f, &new_base, crate::utility::ron_pretty_config()) {
|
||||||
Ok(_) => log::info!("Successfully saved new limits to {}", limits_path.display()),
|
Ok(_) => log::info!("Successfully saved new limits to {}", limits_path.display()),
|
||||||
Err(e) => log::error!(
|
Err(e) => log::error!(
|
||||||
"Failed to save limits json to file `{}`: {}",
|
"Failed to save limits json to file `{}`: {}",
|
||||||
|
|
|
@ -10,15 +10,17 @@ pub struct Driver {
|
||||||
|
|
||||||
impl Driver {
|
impl Driver {
|
||||||
pub fn init(
|
pub fn init(
|
||||||
settings: SettingsJson,
|
name: String,
|
||||||
|
settings: &SettingsJson,
|
||||||
json_path: std::path::PathBuf,
|
json_path: std::path::PathBuf,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let name_bup = settings.name.clone();
|
let name_bup = settings.name.clone();
|
||||||
auto_detect0(Some(settings), json_path, name_bup)
|
let id_bup = settings.variant;
|
||||||
|
auto_detect0(Some(settings), json_path, name, id_bup, name_bup)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn system_default(json_path: std::path::PathBuf, name: String) -> Self {
|
pub fn system_default(json_path: std::path::PathBuf, name: String, variant_id: u64, variant_name: String) -> Self {
|
||||||
auto_detect0(None, json_path, name)
|
auto_detect0(None, json_path, name, variant_id, variant_name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ use std::path::PathBuf;
|
||||||
//use super::{Battery, Cpus, Gpu};
|
//use super::{Battery, Cpus, Gpu};
|
||||||
use super::{OnResume, OnSet, SettingError};
|
use super::{OnResume, OnSet, SettingError};
|
||||||
use super::{TBattery, TCpus, TGeneral, TGpu};
|
use super::{TBattery, TCpus, TGeneral, TGpu};
|
||||||
use crate::persist::SettingsJson;
|
use crate::persist::{SettingsJson, FileJson};
|
||||||
//use crate::utility::unwrap_lock;
|
//use crate::utility::unwrap_lock;
|
||||||
|
|
||||||
const LATEST_VERSION: u64 = 0;
|
const LATEST_VERSION: u64 = 0;
|
||||||
|
@ -33,44 +33,19 @@ pub struct General {
|
||||||
pub persistent: bool,
|
pub persistent: bool,
|
||||||
pub path: PathBuf,
|
pub path: PathBuf,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
pub variant_id: u64,
|
||||||
|
pub variant_name: String,
|
||||||
pub driver: crate::persist::DriverJson,
|
pub driver: crate::persist::DriverJson,
|
||||||
pub events: crate::persist::OnEventJson,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl OnSet for General {
|
impl OnSet for General {
|
||||||
fn on_set(&mut self) -> Result<(), Vec<SettingError>> {
|
fn on_set(&mut self) -> Result<(), Vec<SettingError>> {
|
||||||
if let Some(event) = &self.events.on_set {
|
|
||||||
if !event.is_empty() {
|
|
||||||
std::process::Command::new("/bin/bash")
|
|
||||||
.args(&["-c", event])
|
|
||||||
.spawn()
|
|
||||||
.map_err(|e| {
|
|
||||||
vec![SettingError {
|
|
||||||
msg: format!("on_set event command error: {}", e),
|
|
||||||
setting: SettingVariant::General,
|
|
||||||
}]
|
|
||||||
})?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl OnResume for General {
|
impl OnResume for General {
|
||||||
fn on_resume(&self) -> Result<(), Vec<SettingError>> {
|
fn on_resume(&self) -> Result<(), Vec<SettingError>> {
|
||||||
if let Some(event) = &self.events.on_resume {
|
|
||||||
if !event.is_empty() {
|
|
||||||
std::process::Command::new("/bin/bash")
|
|
||||||
.args(&["-c", event])
|
|
||||||
.spawn()
|
|
||||||
.map_err(|e| {
|
|
||||||
vec![SettingError {
|
|
||||||
msg: format!("on_resume event command error: {}", e),
|
|
||||||
setting: SettingVariant::General,
|
|
||||||
}]
|
|
||||||
})?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -106,12 +81,24 @@ impl TGeneral for General {
|
||||||
self.name = name;
|
self.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn provider(&self) -> crate::persist::DriverJson {
|
fn get_variant_id(&self) -> u64 {
|
||||||
self.driver.clone()
|
self.variant_id
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_event(&self) -> &crate::persist::OnEventJson {
|
fn variant_id(&mut self, id: u64) {
|
||||||
&self.events
|
self.variant_id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_variant_name(&self) -> &'_ str {
|
||||||
|
&self.variant_name
|
||||||
|
}
|
||||||
|
|
||||||
|
fn variant_name(&mut self, name: String) {
|
||||||
|
self.variant_name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn provider(&self) -> crate::persist::DriverJson {
|
||||||
|
self.driver.clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -155,8 +142,8 @@ impl OnSet for Settings {
|
||||||
|
|
||||||
impl Settings {
|
impl Settings {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn from_json(other: SettingsJson, json_path: PathBuf) -> Self {
|
pub fn from_json(name: String, other: SettingsJson, json_path: PathBuf) -> Self {
|
||||||
let x = super::Driver::init(other, json_path.clone());
|
let x = super::Driver::init(name, &other, json_path.clone());
|
||||||
log::info!(
|
log::info!(
|
||||||
"Loaded settings with drivers general:{:?},cpus:{:?},gpu:{:?},battery:{:?}",
|
"Loaded settings with drivers general:{:?},cpus:{:?},gpu:{:?},battery:{:?}",
|
||||||
x.general.provider(),
|
x.general.provider(),
|
||||||
|
@ -172,8 +159,8 @@ impl Settings {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn system_default(json_path: PathBuf, name: String) -> Self {
|
pub fn system_default(json_path: PathBuf, name: String, variant_id: u64, variant_name: String) -> Self {
|
||||||
let driver = super::Driver::system_default(json_path, name);
|
let driver = super::Driver::system_default(json_path, name, variant_id, variant_name);
|
||||||
Self {
|
Self {
|
||||||
general: driver.general,
|
general: driver.general,
|
||||||
cpus: driver.cpus,
|
cpus: driver.cpus,
|
||||||
|
@ -182,26 +169,40 @@ impl Settings {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_system_default(&mut self, name: String) {
|
pub fn load_system_default(&mut self, name: String, variant_id: u64, variant_name: String) {
|
||||||
let driver = super::Driver::system_default(self.general.get_path().to_owned(), name);
|
let driver = super::Driver::system_default(self.general.get_path().to_owned(), name, variant_id, variant_name);
|
||||||
self.cpus = driver.cpus;
|
self.cpus = driver.cpus;
|
||||||
self.gpu = driver.gpu;
|
self.gpu = driver.gpu;
|
||||||
self.battery = driver.battery;
|
self.battery = driver.battery;
|
||||||
self.general = driver.general;
|
self.general = driver.general;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_variant<'a>(settings_file: &'a FileJson, variant_id: u64, variant_name: String) -> Result<&'a SettingsJson, SettingError> {
|
||||||
|
if let Some(variant) = settings_file.variants.get(&variant_id.to_string()) {
|
||||||
|
Ok(variant)
|
||||||
|
} else {
|
||||||
|
Err(SettingError {
|
||||||
|
msg: format!("Cannot get non-existent variant `{}` (id:{})", variant_name, variant_id),
|
||||||
|
setting: SettingVariant::General,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn load_file(
|
pub fn load_file(
|
||||||
&mut self,
|
&mut self,
|
||||||
filename: PathBuf,
|
filename: PathBuf,
|
||||||
name: String,
|
name: String,
|
||||||
|
variant: u64,
|
||||||
|
variant_name: String,
|
||||||
system_defaults: bool,
|
system_defaults: bool,
|
||||||
) -> Result<bool, SettingError> {
|
) -> Result<bool, SettingError> {
|
||||||
let json_path = crate::utility::settings_dir().join(&filename);
|
let json_path = crate::utility::settings_dir().join(&filename);
|
||||||
if json_path.exists() {
|
if json_path.exists() {
|
||||||
let settings_json = SettingsJson::open(&json_path).map_err(|e| SettingError {
|
let file_json = FileJson::open(&json_path).map_err(|e| SettingError {
|
||||||
msg: e.to_string(),
|
msg: format!("Failed to open settings {}: {}", json_path.display(), e),
|
||||||
setting: SettingVariant::General,
|
setting: SettingVariant::General,
|
||||||
})?;
|
})?;
|
||||||
|
let settings_json = Self::get_variant(&file_json, variant, variant_name)?;
|
||||||
if !settings_json.persistent {
|
if !settings_json.persistent {
|
||||||
log::warn!(
|
log::warn!(
|
||||||
"Loaded persistent config `{}` ({}) with persistent=false",
|
"Loaded persistent config `{}` ({}) with persistent=false",
|
||||||
|
@ -211,7 +212,7 @@ impl Settings {
|
||||||
*self.general.persistent() = false;
|
*self.general.persistent() = false;
|
||||||
self.general.name(name);
|
self.general.name(name);
|
||||||
} else {
|
} else {
|
||||||
let x = super::Driver::init(settings_json, json_path.clone());
|
let x = super::Driver::init(name, settings_json, json_path.clone());
|
||||||
log::info!("Loaded settings with drivers general:{:?},cpus:{:?},gpu:{:?},battery:{:?}", x.general.provider(), x.cpus.provider(), x.gpu.provider(), x.battery.provider());
|
log::info!("Loaded settings with drivers general:{:?},cpus:{:?},gpu:{:?},battery:{:?}", x.general.provider(), x.cpus.provider(), x.gpu.provider(), x.battery.provider());
|
||||||
self.general = x.general;
|
self.general = x.general;
|
||||||
self.cpus = x.cpus;
|
self.cpus = x.cpus;
|
||||||
|
@ -220,24 +221,15 @@ impl Settings {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if system_defaults {
|
if system_defaults {
|
||||||
self.load_system_default(name);
|
self.load_system_default(name, variant, variant_name);
|
||||||
} else {
|
} else {
|
||||||
self.general.name(name);
|
self.general.name(name);
|
||||||
|
self.general.variant_name(variant_name);
|
||||||
}
|
}
|
||||||
*self.general.persistent() = false;
|
*self.general.persistent() = false;
|
||||||
}
|
}
|
||||||
self.general.path(filename);
|
self.general.path(filename);
|
||||||
if let Some(event) = &self.general.on_event().on_load {
|
self.general.variant_id(variant);
|
||||||
if !event.is_empty() {
|
|
||||||
std::process::Command::new("/bin/bash")
|
|
||||||
.args(&["-c", event])
|
|
||||||
.spawn()
|
|
||||||
.map_err(|e| SettingError {
|
|
||||||
msg: format!("on_save event command error: {}", e),
|
|
||||||
setting: SettingVariant::General,
|
|
||||||
})?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(*self.general.persistent())
|
Ok(*self.general.persistent())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -275,13 +267,13 @@ impl Settings {
|
||||||
pub fn json(&self) -> SettingsJson {
|
pub fn json(&self) -> SettingsJson {
|
||||||
SettingsJson {
|
SettingsJson {
|
||||||
version: LATEST_VERSION,
|
version: LATEST_VERSION,
|
||||||
name: self.general.get_name().to_owned(),
|
name: self.general.get_variant_name().to_owned(),
|
||||||
|
variant: self.general.get_variant_id(),
|
||||||
persistent: self.general.get_persistent(),
|
persistent: self.general.get_persistent(),
|
||||||
cpus: self.cpus.json(),
|
cpus: self.cpus.json(),
|
||||||
gpu: self.gpu.json(),
|
gpu: self.gpu.json(),
|
||||||
battery: self.battery.json(),
|
battery: self.battery.json(),
|
||||||
provider: Some(self.general.provider()),
|
provider: Some(self.general.provider()),
|
||||||
events: Some(self.general.on_event().clone()),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -109,9 +109,15 @@ pub trait TGeneral: OnSet + OnResume + OnPowerEvent + Debug + Send {
|
||||||
|
|
||||||
fn name(&mut self, name: String);
|
fn name(&mut self, name: String);
|
||||||
|
|
||||||
fn provider(&self) -> crate::persist::DriverJson;
|
fn get_variant_id(&self) -> u64;
|
||||||
|
|
||||||
fn on_event(&self) -> &'_ crate::persist::OnEventJson;
|
fn variant_id(&mut self, id: u64);
|
||||||
|
|
||||||
|
fn get_variant_name(&self) -> &'_ str;
|
||||||
|
|
||||||
|
fn variant_name(&mut self, name: String);
|
||||||
|
|
||||||
|
fn provider(&self) -> crate::persist::DriverJson;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait TBattery: OnSet + OnResume + OnPowerEvent + Debug + Send {
|
pub trait TBattery: OnSet + OnResume + OnPowerEvent + Debug + Send {
|
||||||
|
|
|
@ -1,19 +1,8 @@
|
||||||
use std::fmt::Display;
|
|
||||||
//use std::sync::{LockResult, MutexGuard};
|
//use std::sync::{LockResult, MutexGuard};
|
||||||
//use std::fs::{Permissions, metadata};
|
//use std::fs::{Permissions, metadata};
|
||||||
use std::io::{Read, Write};
|
use std::io::{Read, Write};
|
||||||
use std::os::unix::fs::PermissionsExt;
|
use std::os::unix::fs::PermissionsExt;
|
||||||
|
|
||||||
pub fn unwrap_maybe_fatal<T: Sized, E: Display>(result: Result<T, E>, message: &str) -> T {
|
|
||||||
match result {
|
|
||||||
Ok(x) => x,
|
|
||||||
Err(e) => {
|
|
||||||
log::error!("{}: {}", message, e);
|
|
||||||
panic!("{}: {}", message, e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*pub fn unwrap_lock<'a, T: Sized>(
|
/*pub fn unwrap_lock<'a, T: Sized>(
|
||||||
result: LockResult<MutexGuard<'a, T>>,
|
result: LockResult<MutexGuard<'a, T>>,
|
||||||
lock_name: &str,
|
lock_name: &str,
|
||||||
|
@ -27,6 +16,12 @@ pub fn unwrap_maybe_fatal<T: Sized, E: Display>(result: Result<T, E>, message: &
|
||||||
}
|
}
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
|
pub fn ron_pretty_config() -> ron::ser::PrettyConfig {
|
||||||
|
ron::ser::PrettyConfig::default()
|
||||||
|
.struct_names(true)
|
||||||
|
.compact_arrays(true)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn settings_dir() -> std::path::PathBuf {
|
pub fn settings_dir() -> std::path::PathBuf {
|
||||||
usdpl_back::api::dirs::home()
|
usdpl_back::api::dirs::home()
|
||||||
.unwrap_or_else(|| "/tmp/".into())
|
.unwrap_or_else(|| "/tmp/".into())
|
||||||
|
|
|
@ -247,8 +247,13 @@ export async function getGeneralPersistent(): Promise<boolean> {
|
||||||
return (await call_backend("GENERAL_get_persistent", []))[0];
|
return (await call_backend("GENERAL_get_persistent", []))[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function loadGeneralSettings(id: string, name: string): Promise<boolean> {
|
export async function loadGeneralSettings(id: string, name: string, variant_id: number, variant_name: string | undefined): Promise<boolean> {
|
||||||
return (await call_backend("GENERAL_load_settings", [id, name]))[0];
|
if (variant_name) {
|
||||||
|
return (await call_backend("GENERAL_load_settings", [id, name, variant_id, variant_name]))[0];
|
||||||
|
} else {
|
||||||
|
return (await call_backend("GENERAL_load_settings", [id, name, variant_id]))[0];
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function loadGeneralDefaultSettings(): Promise<boolean> {
|
export async function loadGeneralDefaultSettings(): Promise<boolean> {
|
||||||
|
|
|
@ -191,7 +191,7 @@ const reload = function() {
|
||||||
backend.log(backend.LogLevel.Info, "RegisterForGameActionStart callback(" + actionType + ", " + id + ")");
|
backend.log(backend.LogLevel.Info, "RegisterForGameActionStart callback(" + actionType + ", " + id + ")");
|
||||||
// don't use gameInfo.appid, haha
|
// don't use gameInfo.appid, haha
|
||||||
backend.resolve(
|
backend.resolve(
|
||||||
backend.loadGeneralSettings(id.toString(), gameInfo.display_name),
|
backend.loadGeneralSettings(id.toString(), gameInfo.display_name, 0, undefined),
|
||||||
(ok: boolean) => {backend.log(backend.LogLevel.Debug, "Loading settings ok? " + ok)}
|
(ok: boolean) => {backend.log(backend.LogLevel.Debug, "Loading settings ok? " + ok)}
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue