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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
use std::path::{Path, PathBuf};
use anyhow::Result;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use typeshare::typeshare;
use crate::{
config::Config,
constants::OWML_DEFAULT_CONFIG_NAME,
file::{deserialize_from_json, serialize_to_json},
};
const fn _default_true() -> bool {
true
}
const fn _default_false() -> bool {
false
}
/// Represents the configuration for OWML
#[typeshare]
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[allow(non_snake_case)] // Have to allow non_snake_case here because OWML's config uses "incrementalGC", which isn't proper camelCase
pub struct OWMLConfig {
/// The path to the game
pub game_path: String,
#[serde(default = "_default_false")]
debug_mode: bool,
/// Whether to launch the game directly
#[serde(default = "_default_false")]
pub force_exe: bool,
#[serde(default = "_default_true")]
incremental_GC: bool,
/// The path to OWML
#[serde(skip_serializing_if = "Option::is_none")]
owml_path: Option<String>,
/// Mods that OWML has run a prepatcher for
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub prepatched_mods: Vec<String>,
/// The port to use for sending logs to
pub socket_port: u16,
/// Don't do a popup when the game is out of date
#[serde(default = "_default_false")]
pub disable_version_popup: bool,
#[typeshare(skip)]
#[serde(flatten)]
extra: Map<String, Value>,
}
impl OWMLConfig {
fn path(config: &Config) -> PathBuf {
Path::new(&config.owml_path).join("OWML.Config.json")
}
fn read(config: &Config) -> Result<OWMLConfig> {
deserialize_from_json(&Self::path(config))
}
/// Read the config from a specific path
///
/// ## Returns
///
/// The configuration at that path
///
/// ## Errors
///
/// If we can't deserialize the object or can't access the file.
///
pub fn get_from_path(path: &Path) -> Result<OWMLConfig> {
deserialize_from_json(path)
}
/// Save the config at the given path
///
/// ## Errors
///
/// If we can't save to the file.
///
pub fn save_to_path(&self, path: &Path) -> Result<()> {
serialize_to_json(self, path, true)
}
/// Get the default OWML config (OWML.DefaultConfig.json)
///
/// ## Errors
///
/// If we can't read the default config or can't get the user data dir. (Linux only)
///
#[cfg(not(windows))]
pub fn default(config: &Config) -> Result<OWMLConfig> {
use anyhow::Context;
use directories::UserDirs;
const LINUX_GAME_PATH: &str = ".steam/steam/steamapps/common/Outer Wilds/";
let path = Path::new(&config.owml_path).join(OWML_DEFAULT_CONFIG_NAME);
let mut conf: OWMLConfig = deserialize_from_json(&path)?;
let dirs = UserDirs::new().context("Can't get user data dir")?;
conf.game_path = dirs
.home_dir()
.join(LINUX_GAME_PATH)
.to_str()
.unwrap()
.to_string();
Ok(conf)
}
/// Get the default OWML config (OWML.DefaultConfig.json)
///
/// ## Errors
///
/// If we can't read the default config or can't get the user data dir. (Linux only)
///
#[cfg(windows)]
pub fn default(config: &Config) -> Result<OWMLConfig> {
deserialize_from_json(&Path::new(&config.owml_path).join(OWML_DEFAULT_CONFIG_NAME))
}
fn write(owml_config: &OWMLConfig, config: &Config) -> Result<()> {
serialize_to_json(owml_config, &Self::path(config), true)?;
Ok(())
}
/// Get the OWML config located in `config.owml_path`.
/// This will copy the default config if it doesn't exist.
///
/// ## Returns
///
/// The configuration for OWML
///
/// ## Errors
///
/// If we can't read the current config or copy the default one.
///
/// ## Examples
///
/// ```no_run
/// use owmods_core::config::Config;
/// use owmods_core::owml::OWMLConfig;
///
/// let config = Config::get(None).unwrap();
/// let owml_config = OWMLConfig::get(&config).unwrap();
/// println!("Game Path: {}", owml_config.game_path);
/// ```
///
/// ```no_run
/// use owmods_core::config::Config;
/// use owmods_core::owml::OWMLConfig;
///
/// let config = Config::get(None).unwrap();
/// std::fs::remove_file(&config.owml_path).unwrap();
///
/// let owml_config = OWMLConfig::get(&config).unwrap();
/// println!("Game Path: {}", owml_config.game_path);
/// assert!(std::path::Path::new(&config.owml_path).is_file());
/// ```
///
pub fn get(config: &Config) -> Result<OWMLConfig> {
if Self::path(config).is_file() {
Self::read(config)
} else {
let new_conf = Self::default(config)?;
new_conf.save(config)?;
Ok(new_conf)
}
}
/// Save this config to the path specified in `config.owml_path`
///
/// ## Errors
///
/// If we can't save the file or serialize the object.
///
/// ## Examples
///
/// ```no_run
/// use owmods_core::config::Config;
/// use owmods_core::owml::OWMLConfig;
///
/// let config = Config::get(None).unwrap();
/// let mut owml_config = OWMLConfig::get(&config).unwrap();
///
/// owml_config.force_exe = true;
/// owml_config.save(&config).unwrap();
/// ```
///
pub fn save(&self, config: &Config) -> Result<()> {
Self::write(self, config)
}
}
#[cfg(test)]
mod tests {
use std::fs;
use crate::test_utils::{TestContext, get_test_file};
use super::*;
fn setup_default_conf(ctx: &TestContext) {
fs::create_dir_all(ctx.temp_dir.path().join("OWML")).unwrap();
fs::write(
ctx.temp_dir
.path()
.join("OWML")
.join("OWML.DefaultConfig.json"),
include_str!("../test_files/OWML.Config.json"),
)
.unwrap();
}
#[test]
fn test_owml_config_read() {
let mut ctx = TestContext::new();
ctx.config.owml_path = get_test_file("").to_str().unwrap().to_string();
let conf = OWMLConfig::read(&ctx.config).unwrap();
assert!(conf.debug_mode);
assert!(conf.force_exe);
assert!(conf.incremental_GC);
}
#[test]
fn test_owml_config_save() {
let ctx = TestContext::new();
let owml_conf: OWMLConfig =
serde_json::from_str(include_str!("../test_files/OWML.Config.json")).unwrap();
owml_conf.save(&ctx.config).unwrap();
assert!(
ctx.temp_dir
.path()
.join("OWML")
.join("OWML.Config.json")
.is_file()
);
}
#[test]
fn test_owml_config_get() {
let ctx = TestContext::new();
setup_default_conf(&ctx);
let mut conf = OWMLConfig::get(&ctx.config).unwrap();
conf.debug_mode = true;
conf.save(&ctx.config).unwrap();
let conf = OWMLConfig::get(&ctx.config).unwrap();
assert!(conf.debug_mode);
}
#[test]
fn test_owml_config_get_default() {
let ctx = TestContext::new();
setup_default_conf(&ctx);
let conf = OWMLConfig::get(&ctx.config).unwrap();
assert!(conf.debug_mode);
assert!(
ctx.temp_dir
.path()
.join("OWML")
.join("OWML.Config.json")
.is_file()
);
}
}