-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathlib.rs
More file actions
314 lines (258 loc) · 10.1 KB
/
Copy pathlib.rs
File metadata and controls
314 lines (258 loc) · 10.1 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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#![feature(lazy_cell)]
use std::{
collections::{HashMap, HashSet},
path::PathBuf,
sync::{LazyLock, Mutex, RwLock},
};
use log::info;
use semver::Version;
use skyline::nn;
use skyline_config::*;
use smash_arc::{Hash40, Region};
use crate::utils::env::get_arcropolis_version;
mod utils;
pub static GLOBAL_CONFIG: LazyLock<Mutex<StorageHolder<ArcStorage>>> = LazyLock::new(|| {
let mut storage = StorageHolder::new(ArcStorage::new());
let version: Result<Version, _> = storage.get_field("version");
if let Ok(config_version) = version {
let curr_version = get_arcropolis_version();
// Check if the configuration is from a previous version
if curr_version > config_version {
// TODO: Code to perform changes for each version
if Version::new(3, 2, 0) > config_version {
let mut default_workspace = HashMap::<&str, &str>::new();
default_workspace.insert("Default", "presets");
storage.set_field_json("workspace_list", &default_workspace).unwrap();
storage.set_field("workspace", "Default").unwrap();
}
// Update the version in the config
storage.set_field("version", get_arcropolis_version().to_string()).unwrap();
}
} else {
// Version file does not exist
generate_default_config(&mut storage)
.unwrap_or_else(|err| panic!("ARCropolis encountered an error when generating the default configuration: {}", err));
}
Mutex::new(storage)
});
fn generate_default_config<CS: ConfigStorage>(storage: &mut StorageHolder<CS>) -> Result<(), ConfigError> {
info!("Populating ConfigStorage with default values.");
// Just so we don't keep outdated fields
storage.clear_storage();
storage.set_field("version", get_arcropolis_version().to_string())?;
storage.set_field("logging_level", "Warn")?;
storage.set_flag("auto_update", true)?;
storage.set_field_json("presets", &HashSet::<Hash40>::new())?;
let mut default_workspace = HashMap::<&str, &str>::new();
default_workspace.insert("Default", "presets");
storage.set_field_json("workspace_list", &default_workspace)?;
storage.set_field("workspace", "Default")
}
pub fn auto_update_enabled() -> bool {
GLOBAL_CONFIG.lock().unwrap().get_flag("auto_update")
}
pub fn debug_enabled() -> bool {
GLOBAL_CONFIG.lock().unwrap().get_flag("debug")
}
pub fn beta_updates() -> bool {
GLOBAL_CONFIG.lock().unwrap().get_flag("beta_updates")
}
pub fn skip_cutscene() -> bool {
GLOBAL_CONFIG.lock().unwrap().get_flag("skip_cutscene")
}
pub fn skip_title_scene() -> bool {
GLOBAL_CONFIG.lock().unwrap().get_flag("skip_title_scene")
}
pub static REGION: RwLock<Region> = RwLock::new(Region::UsEnglish);
pub fn region() -> Region {
*REGION.read().unwrap()
}
pub fn logger_level() -> String {
let level: String = GLOBAL_CONFIG
.lock()
.unwrap()
.get_field("logging_level")
.unwrap_or_else(|_| String::from("Warn"));
level
}
pub fn file_logging_enabled() -> bool {
GLOBAL_CONFIG.lock().unwrap().get_flag("log_to_file")
}
pub fn legacy_discovery() -> bool {
GLOBAL_CONFIG.lock().unwrap().get_flag("legacy_discovery")
}
pub fn use_folder_name() -> bool {
GLOBAL_CONFIG.lock().unwrap().get_flag("use_folder_name")
}
pub fn set_mod_cache(cache: &HashSet<Hash40>) -> Result<(), ConfigError> {
GLOBAL_CONFIG.lock().unwrap().set_field_json("mod_cache", &cache)
}
pub fn get_mod_cache() -> Result<HashSet<Hash40>, ConfigError> {
GLOBAL_CONFIG.lock().unwrap().get_field_json("mod_cache")
}
pub mod workspaces {
use super::*;
use std::collections::HashMap;
use skyline_config::ConfigError;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum WorkspaceError {
#[error("a configuration error happened: {0}")]
ConfigError(#[from] ConfigError),
#[error("a workspace with this name already exists")]
AlreadyExists,
#[error("failed to find workspace with name: {0}")]
MissingWorkspace(String), // #[error("failed to call from_str for the desired type")]
// FromStrErr,
}
pub fn get_list() -> Result<HashMap<String, String>, WorkspaceError> {
GLOBAL_CONFIG
.lock()
.unwrap()
.get_field_json("workspace_list")
.map_err(WorkspaceError::ConfigError)
}
pub fn create_new_workspace(name: String) -> Result<(), WorkspaceError> {
let mut list = get_list()?;
if let std::collections::hash_map::Entry::Vacant(e) = list.entry(name.clone()) {
e.insert(name);
GLOBAL_CONFIG
.lock()
.unwrap()
.set_field_json("workspace_list", &list)
.map_err(WorkspaceError::ConfigError)
} else {
Err(WorkspaceError::AlreadyExists)
}
}
pub fn set_active_workspace(name: String) -> Result<(), WorkspaceError> {
let workspace_list = get_list()?;
// Make sure the workspace actually exists before setting it
if workspace_list.contains_key(&name) {
// If we couldn't write the new active workspace, return an error
GLOBAL_CONFIG
.lock()
.unwrap()
.set_field("workspace", name)
.map_err(WorkspaceError::ConfigError)
} else {
// Couldn't find the workspace in our list, something is wrong
Err(WorkspaceError::MissingWorkspace(name))
}
}
pub fn get_active_workspace_name() -> Result<String, WorkspaceError> {
GLOBAL_CONFIG.lock().unwrap().get_field("workspace").map_err(WorkspaceError::ConfigError)
}
pub fn get_active_workspace() -> Result<String, WorkspaceError> {
let workspace_list = get_list()?;
let workspace_name: String = GLOBAL_CONFIG.lock().unwrap().get_field("workspace")?;
workspace_list
.get(&workspace_name)
.map(|x| x.to_owned())
.ok_or(WorkspaceError::MissingWorkspace(workspace_name))
}
pub fn get_workspace_by_name(name: &str) -> Result<String, WorkspaceError> {
let workspace_list = get_list()?;
workspace_list
.get(name)
.map(|x| x.to_owned())
.ok_or(WorkspaceError::MissingWorkspace(name.to_string()))
}
pub fn rename_workspace(from: &str, to: &str) -> Result<(), WorkspaceError> {
let mut workspace_list = get_list()?;
// Remove the workspace if we find it and get back the associate preset name, but if we don't, return an error.
let preset_name = workspace_list
.remove(from)
.ok_or_else(|| WorkspaceError::MissingWorkspace(from.to_string()))?;
// Reinsert the preset name with the new workspace name
workspace_list.insert(to.to_string(), preset_name);
// Overwrite the list with the changes
GLOBAL_CONFIG
.lock()
.unwrap()
.set_field_json("workspace_list", &workspace_list)
.map_err(WorkspaceError::ConfigError)
}
}
pub mod presets {
use super::*;
use std::collections::HashSet;
use skyline_config::ConfigError;
use smash_arc::Hash40;
use thiserror::Error;
use super::workspaces::WorkspaceError;
#[derive(Debug, Error)]
pub enum PresetError {
#[error("a configuration error happened: {0}")]
ConfigError(#[from] ConfigError),
#[error("a workspace error happened: {0}")]
WorkspaceError(#[from] WorkspaceError),
#[error("failed to find the preset file for this workspace")]
MissingPreset,
// #[error("failed to call from_str for the desired type")]
// FromStrErr,
}
pub fn get_active_preset() -> Result<HashSet<Hash40>, PresetError> {
let preset_name = workspaces::get_active_workspace()?;
GLOBAL_CONFIG
.lock()
.unwrap()
.get_field_json(preset_name)
.map_err(PresetError::ConfigError)
}
pub fn get_preset(workspace_name: &str) -> Result<HashSet<Hash40>, PresetError> {
let preset_name = workspaces::get_workspace_by_name(workspace_name)?;
GLOBAL_CONFIG
.lock()
.unwrap()
.get_field_json(preset_name)
.map_err(PresetError::ConfigError)
}
pub fn replace_preset(workspace_name: &str, preset: &HashSet<Hash40>) -> Result<(), PresetError> {
let preset_name = workspaces::get_workspace_by_name(workspace_name)?;
GLOBAL_CONFIG
.lock()
.unwrap()
.set_field_json(preset_name, preset)
.map_err(PresetError::ConfigError)
}
pub fn replace_active_preset(preset: &HashSet<Hash40>) -> Result<(), PresetError> {
let preset_name = workspaces::get_active_workspace()?;
GLOBAL_CONFIG
.lock()
.unwrap()
.set_field_json(preset_name, preset)
.map_err(PresetError::ConfigError)
}
}
pub struct ArcStorage(std::path::PathBuf);
impl ArcStorage {
pub fn new() -> Self {
unsafe {
nn::account::Initialize();
}
// This provides a UserHandle and sets the User in a Open state to be used.
let handle = nn::account::try_open_preselected_user().expect("TryOpenPreselectedUser should open the current user");
// Obtain the UID for this user
let uid = nn::account::get_user_id(&handle).expect("GetUserId should return a valid Uid");
nn::account::close_user(handle);
let path = PathBuf::from(uid.id[0].to_string()).join(uid.id[1].to_string());
Self(path)
}
}
impl ConfigStorage for ArcStorage {
fn initialize(&self) -> Result<(), ConfigError> {
// TODO: Check if the SD is mounted or something
let path = self.storage_path();
if !path.exists() {
std::fs::create_dir_all(&path)?;
}
Ok(())
}
fn root_path(&self) -> PathBuf {
PathBuf::from("sd:/ultimate/arcropolis/config/")
}
fn storage_path(&self) -> PathBuf {
self.root_path().join(&self.0)
}
}