-
Notifications
You must be signed in to change notification settings - Fork 0
Introduce server.toml
#71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
76f37d4
Add subcommand boilerplate
cb341 6da5f44
Implement default CONFIG for world persistence
cb341 696aa67
Implement config file loading
cb341 518ec47
Implement config commands
cb341 44c0509
Move commands
cb341 a21e97f
Update imports
cb341 8130511
Add world timers to config
cb341 c840293
Move world config
cb341 3c883ed
Move spawn area distance
cb341 7cc7c99
Fastcheck
cb341 dcfee99
Improve error handling
cb341 dd4a637
Refactor params config, improve errors
cb341 2f84fc9
Update src/server/config/commands.rs
cb341 8ebacdf
L
cb341 a49c314
Use config params
cb341 fd9d26a
L
cb341 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -4,5 +4,9 @@ | |
| tree-sitter-rust | ||
| *-E | ||
|
|
||
| # default world locations | ||
| backups/ | ||
| worlds/ | ||
|
|
||
| # local config files | ||
| server.toml | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,47 @@ | ||
| use crate::prelude::*; | ||
| use std::path::Path; | ||
|
|
||
| #[derive(Debug, Subcommand)] | ||
| pub enum ConfigCommands { | ||
| #[command(about = "Initialize config file with default settings")] | ||
| Init, | ||
| #[command(about = "Show currently applied configuration")] | ||
| Show, | ||
| #[command(about = "Show defaults")] | ||
| Defaults, | ||
| } | ||
|
|
||
| pub fn perform_command(commands: &ConfigCommands) { | ||
| match commands { | ||
| ConfigCommands::Init => { | ||
| if Path::new(&CONFIG_PATH).is_file() { | ||
| eprintln!("Config file is already initialized at '{CONFIG_PATH}'."); | ||
| eprintln!("If you want to reinitialize it with defaults, remove it:"); | ||
| eprintln!("rm '{CONFIG_PATH}'"); | ||
| } else { | ||
| let config = Config::default(); | ||
| let config_str = | ||
| toml::to_string(&config).expect("Default config should be serializable"); | ||
|
|
||
| if let Err(err) = std::fs::write(CONFIG_PATH, config_str) { | ||
| eprintln!("Error writing to file: {err}"); | ||
| } else { | ||
| println!("Initialized config file '{CONFIG_PATH}'"); | ||
| } | ||
| } | ||
| } | ||
| ConfigCommands::Show => { | ||
| println!( | ||
| "{}", | ||
| toml::to_string(&*CONFIG).expect("Loaded config should always be serializable") | ||
| ); | ||
| } | ||
| ConfigCommands::Defaults => { | ||
| println!( | ||
| "{}", | ||
| toml::to_string(&Config::default()) | ||
| .expect("Loaded config should always be serializable") | ||
| ); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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,42 @@ | ||
| use std::sync::LazyLock; | ||
|
|
||
| pub mod commands; | ||
|
|
||
| pub const CONFIG_PATH: &str = "server.toml"; | ||
|
|
||
| pub static CONFIG: LazyLock<Config> = LazyLock::new(|| { | ||
| #[cfg(test)] | ||
| { | ||
| Config::default() | ||
| } | ||
|
|
||
| #[cfg(not(test))] | ||
| { | ||
| use std::path::PathBuf; | ||
|
|
||
| match std::fs::read_to_string(PathBuf::from(CONFIG_PATH)) { | ||
| Ok(string) => match toml::from_str(&string) { | ||
| Ok(config) => config, | ||
| Err(_) => panic!("Could not parse config file at '{CONFIG_PATH}'"), | ||
| }, | ||
| Err(_) => { | ||
| eprintln!( | ||
| "Could not read config file at '{CONFIG_PATH}', proceeding with defaults" | ||
| ); | ||
| Config::default() | ||
| } | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| use serde::Deserialize; | ||
| use serde::Serialize; | ||
|
|
||
| use crate::prelude::*; | ||
|
|
||
| #[derive(Serialize, Deserialize, Default)] | ||
| #[serde(default)] | ||
| pub struct Config { | ||
| pub world: terrain_config::WorldConfig, | ||
| pub generator: terrain_config::TerrainGeneratorParams, | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.