-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmod.rs
More file actions
88 lines (65 loc) · 1.94 KB
/
mod.rs
File metadata and controls
88 lines (65 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
mod window;
mod graphics;
mod sound;
mod input;
use std::fs::OpenOptions;
use std::io::{Read, Write};
pub use self::window::WindowConfig;
pub use self::graphics::GraphicsConfig;
pub use self::sound::SoundConfig;
pub use self::input::{InputConfig, default_input};
use std::fs;
use std::env;
use serde_json;
/// Configuration object passed to the renderer.
#[derive(Serialize, Deserialize, Clone)]
pub struct Config {
pub window: WindowConfig,
pub graphics: GraphicsConfig,
pub sound: SoundConfig,
pub input: InputConfig
}
impl Config {
fn new() -> Config {
Config {
window: WindowConfig::new(),
graphics: GraphicsConfig::new(),
sound: SoundConfig::new(),
input: default_input()
}
}
}
/// Tries to read Config JSON data from the working directory, returns either defaults or what was in file.
pub fn read() -> Config {
let default_config = Config::new();
// Create codevr/ folder in WORKING_DIRECTORY
let mut working = env::var("APPDATA").unwrap();
if cfg!(target_os = "linux") {
working = env::var("HOME").unwrap();
}
working.push_str("/codevr");
fs::create_dir(working.as_str()).unwrap_or_default();
working.push_str("/config.json");
let open = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(working.as_str());
let mut contents = String::new();
let mut file = match open {
Err(_) => return default_config,
Ok(file) => file,
};
match file.read_to_string(&mut contents) {
Err(_) => return default_config,
Ok(_) => (),
}
if contents.is_empty() {
contents.insert_str(0, serde_json::to_string_pretty(&default_config).unwrap().as_str());
match file.write_all(contents.as_bytes()) {
Err(_) => return default_config,
Ok(_) => (),
}
}
serde_json::from_str(contents.as_str()).unwrap()
}