mirror of
https://github.com/italicsjenga/agb.git
synced 2025-01-11 09:31:34 +11:00
Add support for reading config data from a toml file
This commit is contained in:
parent
383c5454ea
commit
53952d8e34
31
agb-image-converter/Cargo.lock
generated
31
agb-image-converter/Cargo.lock
generated
|
@ -13,6 +13,8 @@ name = "agb_image_converter"
|
||||||
version = "0.4.0"
|
version = "0.4.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"image",
|
"image",
|
||||||
|
"serde",
|
||||||
|
"toml",
|
||||||
"typed-builder",
|
"typed-builder",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -166,6 +168,26 @@ dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde"
|
||||||
|
version = "1.0.126"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ec7505abeacaec74ae4778d9d9328fe5a5d04253220a85c4ee022239fc996d03"
|
||||||
|
dependencies = [
|
||||||
|
"serde_derive",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_derive"
|
||||||
|
version = "1.0.126"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "963a7dbc9895aeac7ac90e74f34a5d5261828f79df35cbed41e10189d3804d43"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "1.0.73"
|
version = "1.0.73"
|
||||||
|
@ -177,6 +199,15 @@ dependencies = [
|
||||||
"unicode-xid",
|
"unicode-xid",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "toml"
|
||||||
|
version = "0.5.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa"
|
||||||
|
dependencies = [
|
||||||
|
"serde",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "typed-builder"
|
name = "typed-builder"
|
||||||
version = "0.9.0"
|
version = "0.9.0"
|
||||||
|
|
|
@ -9,3 +9,5 @@ description = "Library for converting graphics for use on the Game Boy Advance"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
image = { version = "0.23.14", default-features = false, features = [ "png", "bmp" ] }
|
image = { version = "0.23.14", default-features = false, features = [ "png", "bmp" ] }
|
||||||
typed-builder = "0.9.0"
|
typed-builder = "0.9.0"
|
||||||
|
toml = "0.5.8"
|
||||||
|
serde = { version = "1.0", features = ["derive"] }
|
96
agb-image-converter/src/config.rs
Normal file
96
agb-image-converter/src/config.rs
Normal file
|
@ -0,0 +1,96 @@
|
||||||
|
use serde::Deserialize;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
|
use crate::{TileSize, Colour};
|
||||||
|
|
||||||
|
pub fn parse(filename: &str) -> Box<dyn Config> {
|
||||||
|
let config_toml = fs::read_to_string(filename).expect("Failed to read file");
|
||||||
|
|
||||||
|
let config: ConfigV1 = toml::from_str(&config_toml).expect("Failed to parse file");
|
||||||
|
|
||||||
|
if config.version != "1.0" {
|
||||||
|
panic!("Expected version of {} to be 1.0, got {}", filename, config.version);
|
||||||
|
}
|
||||||
|
|
||||||
|
Box::new(config)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait Config {
|
||||||
|
fn crate_prefix(&self) -> String;
|
||||||
|
fn images(&self) -> HashMap<String, &dyn Image>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait Image {
|
||||||
|
fn filename(&self) -> String;
|
||||||
|
fn transparent_colour(&self) -> Option<Colour>;
|
||||||
|
fn tilesize(&self) -> TileSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct ConfigV1 {
|
||||||
|
version: String,
|
||||||
|
crate_prefix: Option<String>,
|
||||||
|
|
||||||
|
image: HashMap<String, ImageV1>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Config for ConfigV1 {
|
||||||
|
fn crate_prefix(&self) -> String {
|
||||||
|
self.crate_prefix.clone().unwrap_or("agb".to_owned())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn images(&self) -> HashMap<String, &dyn Image> {
|
||||||
|
self.image.iter()
|
||||||
|
.map(|(filename, image)| (
|
||||||
|
filename.clone(), image as &dyn Image
|
||||||
|
)).collect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct ImageV1 {
|
||||||
|
filename: String,
|
||||||
|
transparent_colour: Option<String>,
|
||||||
|
tile_size: TileSizeV1,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Image for ImageV1 {
|
||||||
|
fn filename(&self) -> String {
|
||||||
|
self.filename.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn transparent_colour(&self) -> Option<Colour> {
|
||||||
|
if let Some(colour) = &self.transparent_colour {
|
||||||
|
if colour.len() != 6 {
|
||||||
|
panic!("Expected colour to be 6 characters, got {}", colour);
|
||||||
|
}
|
||||||
|
|
||||||
|
let r = u8::from_str_radix(&colour[0..2], 16).unwrap();
|
||||||
|
let g = u8::from_str_radix(&colour[2..4], 16).unwrap();
|
||||||
|
let b = u8::from_str_radix(&colour[4..6], 16).unwrap();
|
||||||
|
|
||||||
|
return Some(Colour::from_rgb(r, g, b));
|
||||||
|
}
|
||||||
|
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
fn tilesize(&self) -> TileSize {
|
||||||
|
self.tile_size.into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Clone, Copy)]
|
||||||
|
pub enum TileSizeV1 {
|
||||||
|
#[serde(rename = "8x8")]
|
||||||
|
Tile8,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Into<TileSize> for TileSizeV1 {
|
||||||
|
fn into(self) -> TileSize {
|
||||||
|
match self {
|
||||||
|
TileSizeV1::Tile8 => TileSize::Tile8,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,6 +8,7 @@ mod colour;
|
||||||
mod image_loader;
|
mod image_loader;
|
||||||
mod palette16;
|
mod palette16;
|
||||||
mod rust_generator;
|
mod rust_generator;
|
||||||
|
mod config;
|
||||||
|
|
||||||
use image_loader::Image;
|
use image_loader::Image;
|
||||||
|
|
||||||
|
|
31
agb/Cargo.lock
generated
31
agb/Cargo.lock
generated
|
@ -21,6 +21,8 @@ name = "agb_image_converter"
|
||||||
version = "0.4.0"
|
version = "0.4.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"image",
|
"image",
|
||||||
|
"serde",
|
||||||
|
"toml",
|
||||||
"typed-builder",
|
"typed-builder",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -174,6 +176,26 @@ dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde"
|
||||||
|
version = "1.0.126"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ec7505abeacaec74ae4778d9d9328fe5a5d04253220a85c4ee022239fc996d03"
|
||||||
|
dependencies = [
|
||||||
|
"serde_derive",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_derive"
|
||||||
|
version = "1.0.126"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "963a7dbc9895aeac7ac90e74f34a5d5261828f79df35cbed41e10189d3804d43"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "1.0.73"
|
version = "1.0.73"
|
||||||
|
@ -185,6 +207,15 @@ dependencies = [
|
||||||
"unicode-xid",
|
"unicode-xid",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "toml"
|
||||||
|
version = "0.5.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa"
|
||||||
|
dependencies = [
|
||||||
|
"serde",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "typed-builder"
|
name = "typed-builder"
|
||||||
version = "0.9.0"
|
version = "0.9.0"
|
||||||
|
|
Loading…
Reference in a new issue