rFactor2_vk_hud/src/lib.rs

132 lines
3.5 KiB
Rust
Raw Normal View History

2023-01-12 09:10:09 +00:00
mod overlay;
2023-01-20 05:41:09 +00:00
mod vk_layer;
2023-01-10 13:24:04 +00:00
2023-01-10 18:33:45 +00:00
use std::{
2023-01-16 16:07:39 +00:00
fs::{self, File, OpenOptions},
2023-01-10 18:33:45 +00:00
io::Write,
2023-01-16 16:07:39 +00:00
path::Path,
2023-01-10 18:33:45 +00:00
};
2023-01-10 17:20:18 +00:00
2023-01-16 16:07:39 +00:00
use overlay::{Overlay, OverlayConfig};
2023-01-10 13:24:04 +00:00
2023-01-14 19:15:43 +00:00
static mut LOG_ENABLED: bool = true;
2023-01-14 07:08:40 +00:00
static mut LOG_FILE: String = String::new();
2023-01-12 09:10:09 +00:00
static mut OVERLAY: Overlay = Overlay::new();
2023-01-12 08:17:24 +00:00
2023-01-14 19:15:43 +00:00
pub(crate) fn logging() -> bool {
unsafe { LOG_ENABLED }
}
macro_rules! write_log {
($msg:expr) => {
2023-01-23 06:21:52 +00:00
println!("{}", $msg);
2023-01-14 19:15:43 +00:00
if crate::logging() {
crate::log($msg);
}
};
}
pub(crate) use write_log;
2023-01-16 21:29:23 +00:00
fn get_config(home: &str) -> OverlayConfig {
2023-01-16 16:07:39 +00:00
let config_path = Path::new(&home).join(".config/rFactorHUD/config.json");
2023-01-16 21:29:23 +00:00
if config_path.exists() {
2023-01-23 06:21:52 +00:00
match fs::read_to_string(&config_path) {
Ok(s) => serde_json::from_str(&s).unwrap_or_else(|err| {
write_log!(format!(
"failed to deserialize config file from {} \n({:?})",
s, err
));
2023-01-16 16:07:39 +00:00
OverlayConfig::new()
2023-01-23 06:21:52 +00:00
}),
Err(err) => {
write_log!(format!(
"failed to open config file: {:?} due to {:?}",
config_path, err
));
OverlayConfig::new()
}
}
2023-01-16 16:07:39 +00:00
} else {
2023-01-23 06:21:52 +00:00
write_log!("create parent dir structure");
2023-01-16 16:07:39 +00:00
if let Err(err) = std::fs::create_dir_all(config_path.parent().unwrap()) {
write_log!(format!("failed to create dirs for config file: {:?}", err));
}
let config = OverlayConfig::new();
match File::create(config_path) {
2023-01-16 21:29:23 +00:00
Ok(mut file) => match serde_json::to_string_pretty(&config) {
2023-01-16 16:07:39 +00:00
Ok(conf_str) => {
2023-01-23 06:21:52 +00:00
write_log!("create config file with default values");
2023-01-16 16:07:39 +00:00
if let Err(err) = file.write_all(conf_str.as_bytes()) {
write_log!(format!("failed to write to config file: {:?}", err));
}
}
Err(err) => {
write_log!(format!("failed to serialize config: {:?}", err));
}
},
Err(err) => {
write_log!(format!("failed to create config file: {:?}", err));
}
}
config
2023-01-16 21:29:23 +00:00
}
}
2023-01-23 06:21:52 +00:00
pub fn check_logging(home: &str) {
let log_enabled = match std::env::var("RFACTOR_HUD_LOG") {
Ok(var) => {
let i: u32 = var.parse().unwrap_or(0);
i == 1
}
Err(_) => false,
};
unsafe { LOG_ENABLED = log_enabled };
if logging() {
unsafe {
LOG_FILE = format!("{}/rf2_vk_hud.log", home);
}
if let Err(_) = File::create(unsafe { &LOG_FILE }) {}
write_log!(" ==================================================================");
write_log!(" ======================= New Negotiation ==========================");
write_log!(" ==================================================================");
}
}
2023-01-14 19:15:43 +00:00
pub fn log(msg: impl ToString) {
assert!(logging());
2023-01-14 07:08:40 +00:00
if let Ok(mut file) = OpenOptions::new()
.append(true)
.create(true)
.open(unsafe { &LOG_FILE })
{
2023-01-12 09:10:09 +00:00
if let Err(_) = file.write_all(format!("{}\n", msg.to_string()).as_bytes()) {}
}
2023-01-10 13:24:04 +00:00
}
2023-01-16 21:29:23 +00:00
#[cfg(test)]
mod test {
2023-01-23 06:21:52 +00:00
use crate::{check_logging, get_config};
2023-01-16 21:29:23 +00:00
#[test]
fn config() {
let home = std::env::var("HOME").unwrap();
2023-01-23 06:21:52 +00:00
check_logging(&home);
let config = get_config(&home);
println!("{:#?}", config);
2023-01-16 21:29:23 +00:00
}
}