-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from mymatsubara/feat/configs
Add configs.json support
- Loading branch information
Showing
8 changed files
with
157 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/target | ||
/target | ||
configs.json |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,21 @@ | ||
[package] | ||
name = "osucraft" | ||
version = "0.1.0" | ||
version = "0.1.1" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
anyhow = "1.0.68" | ||
bevy_ecs = "0.9.1" | ||
colored = "2.0.0" | ||
directories = "5.0.0" | ||
fuzzy-matcher = "0.3.7" | ||
osu-file-parser = "1.1.0" | ||
rand = "0.8.5" | ||
rodio = "0.17.1" | ||
serde = "1.0.160" | ||
serde_json = "1.0.96" | ||
tracing = "0.1.37" | ||
tracing-subscriber = "0.3.16" | ||
valence = { git = "https://github.com/mymatsubara/valence", branch = "osucraft" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use anyhow::Result; | ||
use colored::Colorize; | ||
use directories::BaseDirs; | ||
use std::fmt::Display; | ||
use std::str; | ||
use std::{fs, path::PathBuf}; | ||
|
||
use bevy_ecs::system::Resource; | ||
use serde::{Deserialize, Serialize}; | ||
use tracing::warn; | ||
|
||
#[derive(Resource, Serialize, Deserialize, Debug)] | ||
pub struct Configs { | ||
songs_directory: String, | ||
} | ||
|
||
impl Configs { | ||
pub fn open() -> Self { | ||
Self::read().unwrap_or_else(|_| { | ||
let default_configs = Self::default(); | ||
|
||
if let Err(error) = default_configs.save() { | ||
warn!("Error while saving configs file: {}", error); | ||
} | ||
|
||
default_configs | ||
}) | ||
} | ||
|
||
pub fn path() -> PathBuf { | ||
PathBuf::from("configs.json") | ||
} | ||
|
||
fn read() -> Result<Self> { | ||
let path = Self::path(); | ||
let file_data = fs::read(path)?; | ||
let json = str::from_utf8(file_data.as_slice())?; | ||
Ok(serde_json::from_str(json)?) | ||
} | ||
|
||
fn save(&self) -> Result<()> { | ||
let json = serde_json::to_string_pretty(self)?; | ||
fs::write(Self::path(), json)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn songs_directory(&self) -> &str { | ||
&self.songs_directory | ||
} | ||
} | ||
|
||
impl Default for Configs { | ||
fn default() -> Self { | ||
let local_dir = BaseDirs::new() | ||
.map(|base_dirs| base_dirs.data_local_dir().to_path_buf()) | ||
.unwrap_or_else(|| PathBuf::from("./")); | ||
let songs_directory = local_dir.join("osu!").join("Songs"); | ||
|
||
Self { | ||
songs_directory: songs_directory.to_str().unwrap().to_owned(), | ||
} | ||
} | ||
} | ||
|
||
impl Display for Configs { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{}: {}", "Songs directory".cyan(), self.songs_directory) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters