Skip to content

Commit

Permalink
create configs in a folder
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowiiii committed Dec 24, 2024
1 parent 5394910 commit f2aef84
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions pumpkin-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use query::QueryConfig;
use serde::{de::DeserializeOwned, Deserialize, Serialize};

use std::{
fs,
env, fs,
net::{Ipv4Addr, SocketAddr},
num::NonZeroU8,
path::Path,
Expand Down Expand Up @@ -36,6 +36,8 @@ mod server_links;
use proxy::ProxyConfig;
use resource_pack::ResourcePackConfig;

const CONFIG_ROOT_FOLDER: &str = "config/";

pub static ADVANCED_CONFIG: LazyLock<AdvancedConfiguration> =
LazyLock::new(AdvancedConfiguration::load);

Expand Down Expand Up @@ -126,26 +128,33 @@ trait LoadConfiguration {
where
Self: Sized + Default + Serialize + DeserializeOwned,
{
let path = Self::get_path();
let exe_path = env::current_exe().unwrap();
let exe_dir = exe_path.parent().unwrap();
let config_dir = exe_dir.join(CONFIG_ROOT_FOLDER);
if !config_dir.exists() {
log::debug!("creating new config root folder");
fs::create_dir(&config_dir).expect("Failed to create Config root folder");
}
let path = config_dir.join(Self::get_path());

let config = if path.exists() {
let file_content = fs::read_to_string(path)
.unwrap_or_else(|_| panic!("Couldn't read configuration file at {:?}", path));
let file_content = fs::read_to_string(&path)
.unwrap_or_else(|_| panic!("Couldn't read configuration file at {:?}", &path));

toml::from_str(&file_content).unwrap_or_else(|err| {
panic!(
"Couldn't parse config at {:?}. Reason: {}. This is is proberbly caused by an Config update, Just delete the old Config and start Pumpkin again",
path,
&path,
err.message()
)
})
} else {
let content = Self::default();

if let Err(err) = fs::write(path, toml::to_string(&content).unwrap()) {
if let Err(err) = fs::write(&path, toml::to_string(&content).unwrap()) {
warn!(
"Couldn't write default config to {:?}. Reason: {}. This is is proberbly caused by an Config update, Just delete the old Config and start Pumpkin again",
path, err
&path, err
);
}

Expand Down

0 comments on commit f2aef84

Please sign in to comment.