299 lines
7.0 KiB
Rust
299 lines
7.0 KiB
Rust
#![warn(missing_docs)]
|
|
|
|
//! This subcrate is responsible for loading, parsing, validating game content,
|
|
//! which is usually stored in `./content`.
|
|
|
|
mod handle;
|
|
mod part;
|
|
mod util;
|
|
|
|
use anyhow::{Context, Result};
|
|
use std::{
|
|
collections::HashMap,
|
|
fs::File,
|
|
io::Read,
|
|
path::{Path, PathBuf},
|
|
};
|
|
use toml;
|
|
use walkdir::WalkDir;
|
|
|
|
pub use handle::{FactionHandle, TextureHandle};
|
|
pub use part::{
|
|
EnginePoint, Faction, Gun, GunPoint, Outfit, OutfitSpace, Relationship, Ship, System, Texture,
|
|
};
|
|
|
|
mod syntax {
|
|
use anyhow::{bail, Result};
|
|
use serde::Deserialize;
|
|
use std::collections::HashMap;
|
|
|
|
use crate::part::{faction, gun, outfit, ship, system, texture};
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
pub struct Root {
|
|
pub gun: Option<HashMap<String, gun::syntax::Gun>>,
|
|
pub ship: Option<HashMap<String, ship::syntax::Ship>>,
|
|
pub system: Option<HashMap<String, system::syntax::System>>,
|
|
pub outfit: Option<HashMap<String, outfit::syntax::Outfit>>,
|
|
pub texture: Option<HashMap<String, texture::syntax::Texture>>,
|
|
pub faction: Option<HashMap<String, faction::syntax::Faction>>,
|
|
}
|
|
|
|
impl Root {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
gun: None,
|
|
ship: None,
|
|
system: None,
|
|
outfit: None,
|
|
texture: None,
|
|
faction: None,
|
|
}
|
|
}
|
|
|
|
pub fn merge(&mut self, other: Root) -> Result<()> {
|
|
// Insert if not exists
|
|
// TODO: replace with a macro and try_insert once that is stable
|
|
if let Some(a) = other.gun {
|
|
if self.gun.is_none() {
|
|
self.gun = Some(a);
|
|
} else {
|
|
let sg = self.gun.as_mut().unwrap();
|
|
for (k, v) in a {
|
|
if sg.contains_key(&k) {
|
|
bail!("Repeated gun name {k}");
|
|
} else {
|
|
sg.insert(k, v);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if let Some(a) = other.ship {
|
|
if self.ship.is_none() {
|
|
self.ship = Some(a);
|
|
} else {
|
|
let sg = self.ship.as_mut().unwrap();
|
|
for (k, v) in a {
|
|
if sg.contains_key(&k) {
|
|
bail!("Repeated ship name {k}");
|
|
} else {
|
|
sg.insert(k, v);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if let Some(a) = other.system {
|
|
if self.system.is_none() {
|
|
self.system = Some(a);
|
|
} else {
|
|
let sg = self.system.as_mut().unwrap();
|
|
for (k, v) in a {
|
|
if sg.contains_key(&k) {
|
|
bail!("Repeated system name {k}");
|
|
} else {
|
|
sg.insert(k, v);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if let Some(a) = other.outfit {
|
|
if self.outfit.is_none() {
|
|
self.outfit = Some(a);
|
|
} else {
|
|
let sg = self.outfit.as_mut().unwrap();
|
|
for (k, v) in a {
|
|
if sg.contains_key(&k) {
|
|
bail!("Repeated engine name {k}");
|
|
} else {
|
|
sg.insert(k, v);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if let Some(a) = other.texture {
|
|
if self.texture.is_none() {
|
|
self.texture = Some(a);
|
|
} else {
|
|
let sg = self.texture.as_mut().unwrap();
|
|
for (k, v) in a {
|
|
if sg.contains_key(&k) {
|
|
bail!("Repeated texture name {k}");
|
|
} else {
|
|
sg.insert(k, v);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if let Some(a) = other.faction {
|
|
if self.faction.is_none() {
|
|
self.faction = Some(a);
|
|
} else {
|
|
let sg = self.faction.as_mut().unwrap();
|
|
for (k, v) in a {
|
|
if sg.contains_key(&k) {
|
|
bail!("Repeated faction name {k}");
|
|
} else {
|
|
sg.insert(k, v);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return Ok(());
|
|
}
|
|
}
|
|
}
|
|
|
|
trait Build {
|
|
type InputSyntax;
|
|
|
|
/// Build a processed System struct from raw serde data
|
|
fn build(root: Self::InputSyntax, ct: &mut Content) -> Result<()>
|
|
where
|
|
Self: Sized;
|
|
}
|
|
|
|
/// Represents static game content
|
|
#[derive(Debug)]
|
|
pub struct Content {
|
|
/// Star systems
|
|
pub systems: Vec<part::system::System>,
|
|
|
|
/// Ship bodies
|
|
pub ships: Vec<part::ship::Ship>,
|
|
|
|
/// Ship guns
|
|
pub guns: Vec<part::gun::Gun>,
|
|
|
|
/// Outfits
|
|
pub outfits: Vec<part::outfit::Outfit>,
|
|
|
|
/// Textures
|
|
pub textures: Vec<part::texture::Texture>,
|
|
|
|
/// Factions
|
|
pub factions: Vec<part::faction::Faction>,
|
|
|
|
/// Map strings to texture handles
|
|
/// This is never used outside this crate.
|
|
texture_index: HashMap<String, handle::TextureHandle>,
|
|
|
|
/// The texture to use for starfield stars
|
|
starfield_handle: Option<handle::TextureHandle>,
|
|
|
|
/// Root directory for textures
|
|
texture_root: PathBuf,
|
|
|
|
/// Name of starfield texture
|
|
starfield_texture_name: String,
|
|
}
|
|
|
|
impl Content {
|
|
fn try_parse(path: &Path) -> Result<syntax::Root> {
|
|
let mut file_string = String::new();
|
|
let _ = File::open(path)?.read_to_string(&mut file_string);
|
|
let file_string = file_string.trim();
|
|
return Ok(toml::from_str(&file_string)?);
|
|
}
|
|
|
|
/// Get the texture handle for the starfield texture
|
|
pub fn get_starfield_handle(&self) -> TextureHandle {
|
|
match self.starfield_handle {
|
|
Some(h) => h,
|
|
None => unreachable!("Starfield texture hasn't been loaded yet!"),
|
|
}
|
|
}
|
|
|
|
/// Get a texture from a handle
|
|
pub fn get_texture_handle(&self, name: &str) -> TextureHandle {
|
|
return *self.texture_index.get(name).unwrap();
|
|
// TODO: handle errors
|
|
}
|
|
|
|
/// Get a texture from a handle
|
|
pub fn get_texture(&self, h: TextureHandle) -> &Texture {
|
|
// In theory, this could fail if h has a bad index, but that shouldn't ever happen.
|
|
// The only TextureHandles that exist should be created by this crate.
|
|
return &self.textures[h.index];
|
|
}
|
|
|
|
/// Get a faction from a handle
|
|
pub fn get_faction(&self, h: FactionHandle) -> &Faction {
|
|
return &self.factions[h.index];
|
|
}
|
|
|
|
/// Load content from a directory.
|
|
pub fn load_dir(
|
|
path: PathBuf,
|
|
texture_root: PathBuf,
|
|
starfield_texture_name: String,
|
|
) -> Result<Self> {
|
|
let mut root = syntax::Root::new();
|
|
|
|
for e in WalkDir::new(path).into_iter().filter_map(|e| e.ok()) {
|
|
if e.metadata().unwrap().is_file() {
|
|
// TODO: better warnings
|
|
match e.path().extension() {
|
|
Some(t) => {
|
|
if t.to_str() != Some("toml") {
|
|
println!("[WARNING] {e:#?} is not a toml file, skipping.");
|
|
continue;
|
|
}
|
|
}
|
|
None => {
|
|
println!("[WARNING] {e:#?} is not a toml file, skipping.");
|
|
continue;
|
|
}
|
|
}
|
|
|
|
let path = e.path();
|
|
let this_root = Self::try_parse(path)
|
|
.with_context(|| format!("Could not read {}", path.display()))?;
|
|
|
|
root.merge(this_root)
|
|
.with_context(|| format!("Could not parse {}", path.display()))?;
|
|
}
|
|
}
|
|
|
|
let mut content = Self {
|
|
systems: Vec::new(),
|
|
ships: Vec::new(),
|
|
guns: Vec::new(),
|
|
outfits: Vec::new(),
|
|
textures: Vec::new(),
|
|
factions: Vec::new(),
|
|
texture_index: HashMap::new(),
|
|
starfield_handle: None,
|
|
texture_root,
|
|
starfield_texture_name,
|
|
};
|
|
|
|
// Order here matters, usually
|
|
if root.texture.is_some() {
|
|
part::texture::Texture::build(root.texture.take().unwrap(), &mut content)?;
|
|
}
|
|
if root.ship.is_some() {
|
|
part::ship::Ship::build(root.ship.take().unwrap(), &mut content)?;
|
|
}
|
|
if root.gun.is_some() {
|
|
part::gun::Gun::build(root.gun.take().unwrap(), &mut content)?;
|
|
}
|
|
if root.outfit.is_some() {
|
|
part::outfit::Outfit::build(root.outfit.take().unwrap(), &mut content)?;
|
|
}
|
|
if root.system.is_some() {
|
|
part::system::System::build(root.system.take().unwrap(), &mut content)?;
|
|
}
|
|
if root.faction.is_some() {
|
|
part::faction::Faction::build(root.faction.take().unwrap(), &mut content)?;
|
|
}
|
|
|
|
return Ok(content);
|
|
}
|
|
}
|