Fix config loading
This commit is contained in:
parent
1b3c553661
commit
da0cd5656a
5 changed files with 85 additions and 39 deletions
28
.vscode/launch.json
vendored
Normal file
28
.vscode/launch.json
vendored
Normal file
|
@ -0,0 +1,28 @@
|
|||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"type": "lldb",
|
||||
"request": "launch",
|
||||
"name": "Debug selected unit test",
|
||||
"cargo": {
|
||||
"args": [
|
||||
"test",
|
||||
// "--no-run",
|
||||
"test::${selectedText}"
|
||||
],
|
||||
// "filter": {
|
||||
// "name": "vk_layer_rs",
|
||||
// "kind": "lib"
|
||||
// }
|
||||
},
|
||||
"cwd": "${workspaceFolder}",
|
||||
"env": {
|
||||
"RFACTOR_HUD_LOG": "1"
|
||||
}
|
||||
},
|
||||
]
|
||||
}
|
62
src/lib.rs
62
src/lib.rs
|
@ -19,6 +19,7 @@ pub(crate) fn logging() -> bool {
|
|||
|
||||
macro_rules! write_log {
|
||||
($msg:expr) => {
|
||||
println!("{}", $msg);
|
||||
if crate::logging() {
|
||||
crate::log($msg);
|
||||
}
|
||||
|
@ -31,18 +32,25 @@ fn get_config(home: &str) -> OverlayConfig {
|
|||
let config_path = Path::new(&home).join(".config/rFactorHUD/config.json");
|
||||
|
||||
if config_path.exists() {
|
||||
fs::read_to_string(&config_path)
|
||||
.map(|s| {
|
||||
serde_json::from_str(&s).unwrap_or({
|
||||
write_log!("failed to deserialize config file");
|
||||
OverlayConfig::new()
|
||||
})
|
||||
})
|
||||
.unwrap_or({
|
||||
write_log!(format!("failed to open config file: {:?}", config_path));
|
||||
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
|
||||
));
|
||||
OverlayConfig::new()
|
||||
})
|
||||
}),
|
||||
Err(err) => {
|
||||
write_log!(format!(
|
||||
"failed to open config file: {:?} due to {:?}",
|
||||
config_path, err
|
||||
));
|
||||
OverlayConfig::new()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
write_log!("create parent dir structure");
|
||||
|
||||
if let Err(err) = std::fs::create_dir_all(config_path.parent().unwrap()) {
|
||||
write_log!(format!("failed to create dirs for config file: {:?}", err));
|
||||
}
|
||||
|
@ -52,6 +60,8 @@ fn get_config(home: &str) -> OverlayConfig {
|
|||
match File::create(config_path) {
|
||||
Ok(mut file) => match serde_json::to_string_pretty(&config) {
|
||||
Ok(conf_str) => {
|
||||
write_log!("create config file with default values");
|
||||
|
||||
if let Err(err) = file.write_all(conf_str.as_bytes()) {
|
||||
write_log!(format!("failed to write to config file: {:?}", err));
|
||||
}
|
||||
|
@ -69,6 +79,31 @@ fn get_config(home: &str) -> OverlayConfig {
|
|||
}
|
||||
}
|
||||
|
||||
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!(" ==================================================================");
|
||||
}
|
||||
}
|
||||
|
||||
pub fn log(msg: impl ToString) {
|
||||
assert!(logging());
|
||||
|
||||
|
@ -83,11 +118,14 @@ pub fn log(msg: impl ToString) {
|
|||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::get_config;
|
||||
use crate::{check_logging, get_config};
|
||||
|
||||
#[test]
|
||||
fn config() {
|
||||
let home = std::env::var("HOME").unwrap();
|
||||
get_config(&home);
|
||||
check_logging(&home);
|
||||
let config = get_config(&home);
|
||||
|
||||
println!("{:#?}", config);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,7 +22,7 @@ use crate::write_log;
|
|||
|
||||
use bg_generator::BackgroundGenerator;
|
||||
|
||||
#[derive(Default, Deserialize, Serialize, Clone, Copy)]
|
||||
#[derive(Default, Deserialize, Serialize, Clone, Copy, Debug)]
|
||||
pub struct LeaderBoardConfig {
|
||||
first_board_color: [f32; 3],
|
||||
second_board_color: [f32; 3],
|
||||
|
|
|
@ -25,7 +25,7 @@ use serde::{Deserialize, Serialize};
|
|||
|
||||
pub trait UiOverlay: DataReceiver {}
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
pub struct UiSelectorConfig {
|
||||
pub enable_watermark: bool,
|
||||
|
||||
|
@ -45,7 +45,7 @@ impl UiSelectorConfig {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
pub struct OverlayConfig {
|
||||
pub ui_config: UiSelectorConfig,
|
||||
pub radar_config: RadarConfig,
|
||||
|
|
|
@ -2,47 +2,27 @@ mod enums;
|
|||
mod structs;
|
||||
mod vk_handles;
|
||||
|
||||
use crate::check_logging;
|
||||
use enums::*;
|
||||
use structs::*;
|
||||
use vk_handles::*;
|
||||
use vulkan_rs::prelude::*;
|
||||
|
||||
use std::{ffi::c_void, fs::File, mem, os::raw::c_char, ptr};
|
||||
use std::{ffi::c_void, mem, os::raw::c_char, ptr};
|
||||
|
||||
static mut ACQUIRE_NEXT_IMAGE: PFN_vkAcquireNextImageKHR =
|
||||
unsafe { mem::transmute(vkVoidFunction as *const c_void) };
|
||||
|
||||
use crate::{get_config, logging, write_log, LOG_ENABLED, LOG_FILE, OVERLAY};
|
||||
use crate::{get_config, logging, write_log, OVERLAY};
|
||||
|
||||
#[no_mangle]
|
||||
#[allow(non_snake_case)]
|
||||
pub(crate) extern "C" fn vkNegotiateLoaderLayerInterfaceVersion(
|
||||
pVersionStruct: *mut VkNegotiateLayerInterface,
|
||||
) -> VkResult {
|
||||
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 };
|
||||
|
||||
let home = std::env::var("HOME").unwrap();
|
||||
|
||||
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!(" ==================================================================");
|
||||
}
|
||||
check_logging(&home);
|
||||
|
||||
unsafe {
|
||||
OVERLAY.set_config(get_config(&home));
|
||||
|
|
Loading…
Reference in a new issue