Skip to content

Commit

Permalink
change config file loading
Browse files Browse the repository at this point in the history
  • Loading branch information
0x6273 committed Oct 16, 2019
1 parent f6a1bae commit efd1e1c
Show file tree
Hide file tree
Showing 36 changed files with 57 additions and 58 deletions.
2 changes: 1 addition & 1 deletion amethyst_config/examples/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub struct ExampleConfig {

fn main() {
let path = format!("{}/examples/display_config.ron", env!("CARGO_MANIFEST_DIR"));
let res = ExampleConfig::load_no_fallback(&path);
let res = ExampleConfig::load(&path);

match res {
Ok(cfg) => {
Expand Down
25 changes: 7 additions & 18 deletions amethyst_config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use std::{
path::{Path, PathBuf},
};

use log::error;
use ron::{self, de::Error as DeError, ser::Error as SerError};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -99,11 +98,13 @@ where
Self: Sized,
{
/// Loads a configuration structure from a file.
/// Defaults if the file fails in any way.
fn load<P: AsRef<Path>>(path: P) -> Self;
fn load<P: AsRef<Path>>(path: P) -> Result<Self, ConfigError>;

/// Loads a configuration structure from a file.
fn load_no_fallback<P: AsRef<Path>>(path: P) -> Result<Self, ConfigError>;
#[deprecated(note = "use `load` instead")]
fn load_no_fallback<P: AsRef<Path>>(path: P) -> Result<Self, ConfigError> {
Self::load(path)
}

/// Loads configuration structure from raw bytes.
fn load_bytes(bytes: &[u8]) -> Result<Self, ConfigError>;
Expand All @@ -114,21 +115,9 @@ where

impl<T> Config for T
where
T: for<'a> Deserialize<'a> + Serialize + Default,
T: for<'a> Deserialize<'a> + Serialize,
{
fn load<P: AsRef<Path>>(path: P) -> Self {
Self::load_no_fallback(path.as_ref()).unwrap_or_else(|e| {
if let Some(path) = path.as_ref().to_str() {
error!("Failed to load config file '{}': {}", path, e);
} else {
error!("Failed to load config: {}", e);
}

Self::default()
})
}

fn load_no_fallback<P: AsRef<Path>>(path: P) -> Result<Self, ConfigError> {
fn load<P: AsRef<Path>>(path: P) -> Result<Self, ConfigError> {
use std::{fs::File, io::Read};

let path = path.as_ref();
Expand Down
2 changes: 1 addition & 1 deletion amethyst_input/src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl<T: BindingTypes> InputBundle<T> {
where
Bindings<T>: Config,
{
let mut bindings = Bindings::load_no_fallback(file)?;
let mut bindings = Bindings::load(file)?;
bindings.check_invariants()?;
Ok(self.with_bindings(bindings))
}
Expand Down
6 changes: 3 additions & 3 deletions amethyst_rendy/src/plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mod window {
bundle::{ImageOptions, OutputColor},
Format, Kind,
};
use amethyst_config::Config;
use amethyst_config::{Config, ConfigError};
use amethyst_core::{
ecs::{ReadExpect, SystemData},
SystemBundle,
Expand All @@ -45,8 +45,8 @@ mod window {

impl RenderToWindow {
/// Create RenderToWindow plugin with [`WindowBundle`] using specified config path.
pub fn from_config_path(path: impl AsRef<Path>) -> Self {
Self::from_config(DisplayConfig::load(path))
pub fn from_config_path(path: impl AsRef<Path>) -> Result<Self, ConfigError> {
Ok(Self::from_config(DisplayConfig::load(path)?))
}

/// Create RenderToWindow plugin with [`WindowBundle`] using specified config.
Expand Down
8 changes: 5 additions & 3 deletions amethyst_window/src/bundle.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{DisplayConfig, EventsLoopSystem, WindowSystem};
use amethyst_config::Config;
use amethyst_config::{Config, ConfigError};
use amethyst_core::{bundle::SystemBundle, ecs::World, shred::DispatcherBuilder};
use amethyst_error::Error;
use winit::EventsLoop;
Expand Down Expand Up @@ -27,8 +27,10 @@ impl WindowBundle {
/// Builds a new window bundle by loading the `DisplayConfig` from `path`.
///
/// Will fall back to `DisplayConfig::default()` in case of an error.
pub fn from_config_path(path: impl AsRef<std::path::Path>) -> Self {
WindowBundle::from_config(DisplayConfig::load(path.as_ref()))
pub fn from_config_path(path: impl AsRef<std::path::Path>) -> Result<Self, ConfigError> {
Ok(WindowBundle::from_config(DisplayConfig::load(
path.as_ref(),
)?))
}

/// Builds a new window bundle with a predefined `DisplayConfig`.
Expand Down
10 changes: 7 additions & 3 deletions amethyst_window/src/system.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{config::DisplayConfig, resources::ScreenDimensions};
use amethyst_config::Config;
use amethyst_config::{Config, ConfigError};
use amethyst_core::{
ecs::{ReadExpect, RunNow, System, SystemData, World, Write, WriteExpect},
shrev::EventChannel,
Expand All @@ -18,8 +18,12 @@ impl WindowSystem {
world: &mut World,
events_loop: &EventsLoop,
path: impl AsRef<Path>,
) -> Self {
Self::from_config(world, events_loop, DisplayConfig::load(path.as_ref()))
) -> Result<Self, ConfigError> {
Ok(Self::from_config(
world,
events_loop,
DisplayConfig::load(path.as_ref())?,
))
}

/// Builds and spawns a new `Window`, using the provided `DisplayConfig` and `EventsLoop` as
Expand Down
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ The format is based on [Keep a Changelog][kc], and this project adheres to
- Cleaned up tiles example. Added rotation and translation tests, fixed raycast debug box. Added default zoom to `PROJECT` perspective projection since no one knew to zoom out. ([#1974])
- `AmethystApplication::with_fn` constraint relaxed from `Fn` to `FnOnce`. ([#1983])
- ScreenDimensions now consistently reports window size in physical pixels. ([#1988])
- `Config::load` now returns an error or failure rather than silently falling back to the default config. Same is true for the `from_config_file` methods on `RenderToWindow`, `WindowBundle`, and `WindowSystem` ([#1989])

### Deprecated
- `Config::load_no_fallback`, use `Config::load` instead ([#1989])

### Removed

### Fixed

- Tilemap rotation was incorrect and not transposed. Fixed and uses component rotation. ([#1974])
- `Config` types no longer require a `Default` impl ([#1989])

### Security

Expand All @@ -40,6 +43,7 @@ The format is based on [Keep a Changelog][kc], and this project adheres to
[#1974]: https://github.com/amethyst/amethyst/pull/1974
[#1978]: https://github.com/amethyst/amethyst/pull/1978
[#1983]: https://github.com/amethyst/amethyst/pull/1983
[#1989]: https://github.com/amethyst/amethyst/pull/1989

## [0.13.3] - 2019-10-4

Expand Down
2 changes: 1 addition & 1 deletion examples/animation/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ fn main() -> amethyst::Result<()> {
.with_bundle(
RenderingBundle::<DefaultBackend>::new()
.with_plugin(
RenderToWindow::from_config_path(display_config_path).with_clear(CLEAR_COLOR),
RenderToWindow::from_config_path(display_config_path)?.with_clear(CLEAR_COLOR),
)
.with_plugin(RenderPbr3D::default()),
)?;
Expand Down
2 changes: 1 addition & 1 deletion examples/arc_ball_camera/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ fn main() -> Result<(), Error> {
)
.with_bundle(
RenderingBundle::<DefaultBackend>::new()
.with_plugin(RenderToWindow::from_config_path(display_config_path))
.with_plugin(RenderToWindow::from_config_path(display_config_path)?)
.with_plugin(RenderShaded3D::default())
.with_plugin(RenderSkybox::with_colors(
Srgb::new(0.82, 0.51, 0.50),
Expand Down
2 changes: 1 addition & 1 deletion examples/asset_loading/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ fn main() -> Result<(), Error> {
.with_bundle(TransformBundle::new())?
.with_bundle(
RenderingBundle::<DefaultBackend>::new()
.with_plugin(RenderToWindow::from_config_path(display_config_path))
.with_plugin(RenderToWindow::from_config_path(display_config_path)?)
.with_plugin(RenderShaded3D::default())
.with_plugin(RenderSkybox::with_colors(
Srgb::new(0.82, 0.51, 0.50),
Expand Down
2 changes: 1 addition & 1 deletion examples/auto_fov/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn main() -> Result<(), Error> {
.with_bundle(
RenderingBundle::<DefaultBackend>::new()
.with_plugin(
RenderToWindow::from_config_path(display_config_path).with_clear(CLEAR_COLOR),
RenderToWindow::from_config_path(display_config_path)?.with_clear(CLEAR_COLOR),
)
.with_plugin(RenderShaded3D::default())
.with_plugin(RenderUi::default()),
Expand Down
2 changes: 1 addition & 1 deletion examples/custom_game_data/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ fn main() -> Result<(), Error> {
.with_base_bundle(
RenderingBundle::<DefaultBackend>::new()
.with_plugin(
RenderToWindow::from_config_path(display_config_path)
RenderToWindow::from_config_path(display_config_path)?
.with_clear([0.0, 0.0, 0.0, 1.0]),
)
.with_plugin(RenderShaded3D::default())
Expand Down
2 changes: 1 addition & 1 deletion examples/custom_render_pass/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ fn main() -> amethyst::Result<()> {
.with_bundle(
RenderingBundle::<DefaultBackend>::new()
.with_plugin(
RenderToWindow::from_config_path(display_config_path)
RenderToWindow::from_config_path(display_config_path)?
.with_clear([1.0, 1.0, 1.0, 1.0]),
)
// Add our custom render plugin to the rendering bundle.
Expand Down
2 changes: 1 addition & 1 deletion examples/custom_ui/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ fn main() -> amethyst::Result<()> {
.with_bundle(
RenderingBundle::<DefaultBackend>::new()
.with_plugin(
RenderToWindow::from_config_path(display_config_path)
RenderToWindow::from_config_path(display_config_path)?
.with_clear([0.34, 0.36, 0.52, 1.0]),
)
.with_plugin(RenderUi::default()),
Expand Down
2 changes: 1 addition & 1 deletion examples/debug_lines/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ fn main() -> amethyst::Result<()> {
.with_bundle(TransformBundle::new().with_dep(&["fly_movement"]))?
.with_bundle(
RenderingBundle::<DefaultBackend>::new()
.with_plugin(RenderToWindow::from_config_path(display_config_path))
.with_plugin(RenderToWindow::from_config_path(display_config_path)?)
.with_plugin(RenderDebugLines::default())
.with_plugin(RenderSkybox::default()),
)?;
Expand Down
2 changes: 1 addition & 1 deletion examples/debug_lines_ortho/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ fn main() -> amethyst::Result<()> {
.with_bundle(
RenderingBundle::<DefaultBackend>::new()
.with_plugin(
RenderToWindow::from_config_path(display_config_path)
RenderToWindow::from_config_path(display_config_path)?
.with_clear([0.0, 0.0, 0.0, 1.0]),
)
.with_plugin(RenderDebugLines::default()),
Expand Down
2 changes: 1 addition & 1 deletion examples/fly_camera/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn main() -> Result<(), Error> {
.with_bundle(
RenderingBundle::<DefaultBackend>::new()
.with_plugin(
RenderToWindow::from_config_path(display_config_path)
RenderToWindow::from_config_path(display_config_path)?
.with_clear([0.34, 0.36, 0.52, 1.0]),
)
.with_plugin(RenderShaded3D::default()),
Expand Down
2 changes: 1 addition & 1 deletion examples/material/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ fn main() -> amethyst::Result<()> {
.with_bundle(
RenderingBundle::<DefaultBackend>::new()
.with_plugin(
RenderToWindow::from_config_path(display_config_path)
RenderToWindow::from_config_path(display_config_path)?
.with_clear([0.34, 0.36, 0.52, 1.0]),
)
.with_plugin(RenderPbr3D::default()),
Expand Down
2 changes: 1 addition & 1 deletion examples/mouse_raycast/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ fn main() -> amethyst::Result<()> {
.with_bundle(
RenderingBundle::<DefaultBackend>::new()
.with_plugin(
RenderToWindow::from_config_path(display_config_path)
RenderToWindow::from_config_path(display_config_path)?
.with_clear([0.34, 0.36, 0.52, 1.0]),
)
.with_plugin(RenderUi::default())
Expand Down
2 changes: 1 addition & 1 deletion examples/pong/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fn main() -> amethyst::Result<()> {
// The RenderToWindow plugin provides all the scaffolding for opening a window and
// drawing on it
.with_plugin(
RenderToWindow::from_config_path(display_config_path)
RenderToWindow::from_config_path(display_config_path)?
.with_clear([0.34, 0.36, 0.52, 1.0]),
)
.with_plugin(RenderFlat2D::default())
Expand Down
2 changes: 1 addition & 1 deletion examples/pong_tutorial_01/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn main() -> amethyst::Result<()> {
// The RenderToWindow plugin provides all the scaffolding for opening a window and
// drawing on it
.with_plugin(
RenderToWindow::from_config_path(display_config_path)
RenderToWindow::from_config_path(display_config_path)?
.with_clear([0.0, 0.0, 0.0, 1.0]),
)
// RenderFlat2D plugin is used to render entities with `SpriteRender` component.
Expand Down
2 changes: 1 addition & 1 deletion examples/pong_tutorial_02/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn main() -> amethyst::Result<()> {
// The RenderToWindow plugin provides all the scaffolding for opening a window and
// drawing on it
.with_plugin(
RenderToWindow::from_config_path(display_config_path)
RenderToWindow::from_config_path(display_config_path)?
.with_clear([0.0, 0.0, 0.0, 1.0]),
)
// RenderFlat2D plugin is used to render entities with `SpriteRender` component.
Expand Down
2 changes: 1 addition & 1 deletion examples/pong_tutorial_03/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn main() -> amethyst::Result<()> {
// The RenderToWindow plugin provides all the scaffolding for opening a window and
// drawing on it
.with_plugin(
RenderToWindow::from_config_path(display_config_path)
RenderToWindow::from_config_path(display_config_path)?
.with_clear([0.0, 0.0, 0.0, 1.0]),
)
// RenderFlat2D plugin is used to render entities with `SpriteRender` component.
Expand Down
2 changes: 1 addition & 1 deletion examples/pong_tutorial_04/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn main() -> amethyst::Result<()> {
// The RenderToWindow plugin provides all the scaffolding for opening a window and
// drawing on it
.with_plugin(
RenderToWindow::from_config_path(display_config_path)
RenderToWindow::from_config_path(display_config_path)?
.with_clear([0.0, 0.0, 0.0, 1.0]),
)
// RenderFlat2D plugin is used to render entities with `SpriteRender` component.
Expand Down
2 changes: 1 addition & 1 deletion examples/pong_tutorial_05/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn main() -> amethyst::Result<()> {
// The RenderToWindow plugin provides all the scaffolding for opening a window and
// drawing on it
.with_plugin(
RenderToWindow::from_config_path(display_config_path)
RenderToWindow::from_config_path(display_config_path)?
.with_clear([0.0, 0.0, 0.0, 1.0]),
)
// RenderFlat2D plugin is used to render entities with `SpriteRender` component.
Expand Down
2 changes: 1 addition & 1 deletion examples/prefab/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn main() -> Result<(), Error> {
.with_bundle(
RenderingBundle::<DefaultBackend>::new()
.with_plugin(
RenderToWindow::from_config_path(display_config_path)
RenderToWindow::from_config_path(display_config_path)?
.with_clear([0.34, 0.36, 0.52, 1.0]),
)
.with_plugin(RenderShaded3D::default()),
Expand Down
2 changes: 1 addition & 1 deletion examples/renderable/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ fn main() -> Result<(), Error> {
.with_bundle(
RenderingBundle::<DefaultBackend>::new()
.with_plugin(
RenderToWindow::from_config_path(display_config_path)
RenderToWindow::from_config_path(display_config_path)?
.with_clear([0.34, 0.36, 0.52, 1.0]),
)
.with_plugin(RenderShaded3D::default())
Expand Down
2 changes: 1 addition & 1 deletion examples/renderable_custom/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ fn main() -> Result<(), Error> {
&[],
)
.with(Processor::<Material>::new(), "material_processor", &[])
.with_bundle(WindowBundle::from_config_path(display_config_path))?
.with_bundle(WindowBundle::from_config_path(display_config_path)?)?
// The renderer must be executed on the same thread consecutively, so we initialize it as thread_local
// which will always execute on the main thread.
.with_thread_local(RenderingSystem::<DefaultBackend, _>::new(
Expand Down
2 changes: 1 addition & 1 deletion examples/sphere/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn main() -> amethyst::Result<()> {
.with_bundle(
RenderingBundle::<DefaultBackend>::new()
.with_plugin(
RenderToWindow::from_config_path(display_config_path)
RenderToWindow::from_config_path(display_config_path)?
.with_clear([0.34, 0.36, 0.52, 1.0]),
)
.with_plugin(RenderShaded3D::default()),
Expand Down
2 changes: 1 addition & 1 deletion examples/spotlights/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn main() -> amethyst::Result<()> {
.with_bundle(
RenderingBundle::<DefaultBackend>::new()
.with_plugin(
RenderToWindow::from_config_path(display_config_path)
RenderToWindow::from_config_path(display_config_path)?
.with_clear([0.34, 0.36, 0.52, 1.0]),
)
.with_plugin(RenderPbr3D::default()),
Expand Down
2 changes: 1 addition & 1 deletion examples/sprite_animation/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ fn main() -> amethyst::Result<()> {
.with_bundle(
RenderingBundle::<DefaultBackend>::new()
.with_plugin(
RenderToWindow::from_config_path(display_config_path)
RenderToWindow::from_config_path(display_config_path)?
.with_clear([0.34, 0.36, 0.52, 1.0]),
)
.with_plugin(RenderFlat2D::default()),
Expand Down
2 changes: 1 addition & 1 deletion examples/sprite_camera_follow/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ fn main() -> amethyst::Result<()> {
.with_bundle(
RenderingBundle::<DefaultBackend>::new()
.with_plugin(
RenderToWindow::from_config_path(display_config_path)
RenderToWindow::from_config_path(display_config_path)?
.with_clear([0.34, 0.36, 0.52, 1.0]),
)
.with_plugin(RenderFlat2D::default()),
Expand Down
2 changes: 1 addition & 1 deletion examples/sprites_ordered/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ fn main() -> amethyst::Result<()> {
.with_bundle(
RenderingBundle::<DefaultBackend>::new()
.with_plugin(
RenderToWindow::from_config_path(display_config_path)
RenderToWindow::from_config_path(display_config_path)?
.with_clear([0.34, 0.36, 0.52, 1.0]),
)
.with_plugin(RenderFlat2D::default()),
Expand Down
2 changes: 1 addition & 1 deletion examples/tiles/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ fn main() -> amethyst::Result<()> {
.with_bundle(
RenderingBundle::<DefaultBackend>::new()
.with_plugin(
RenderToWindow::from_config_path(display_config_path)
RenderToWindow::from_config_path(display_config_path)?
.with_clear([0.34, 0.36, 0.52, 1.0]),
)
.with_plugin(RenderDebugLines::default())
Expand Down
Loading

0 comments on commit efd1e1c

Please sign in to comment.