2023-12-29 15:46:09 -08:00
|
|
|
#![warn(missing_docs)]
|
|
|
|
|
|
|
|
//! This subcrate is responsible for loading, parsing, validating game content,
|
|
|
|
//! which is usually stored in `./content`.
|
|
|
|
|
2023-12-30 16:57:03 -08:00
|
|
|
mod handle;
|
|
|
|
mod part;
|
2024-01-20 15:18:12 -08:00
|
|
|
mod spriteautomaton;
|
2023-12-29 15:14:04 -08:00
|
|
|
mod util;
|
2023-12-24 23:03:00 -08:00
|
|
|
|
2024-01-10 22:44:22 -08:00
|
|
|
use anyhow::{bail, Context, Result};
|
2024-01-04 17:17:55 -08:00
|
|
|
use galactica_packer::{SpriteAtlas, SpriteAtlasImage};
|
2023-12-30 10:58:17 -08:00
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
|
|
|
fs::File,
|
2024-01-20 12:42:20 -08:00
|
|
|
hash::Hash,
|
2023-12-30 10:58:17 -08:00
|
|
|
io::Read,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
};
|
2023-12-27 19:51:58 -08:00
|
|
|
use toml;
|
2023-12-25 09:01:12 -08:00
|
|
|
use walkdir::WalkDir;
|
|
|
|
|
2024-01-12 14:34:31 -08:00
|
|
|
pub use handle::*;
|
2024-01-05 18:04:30 -08:00
|
|
|
pub use part::*;
|
2024-01-20 15:18:12 -08:00
|
|
|
pub use spriteautomaton::*;
|
2023-12-30 16:57:03 -08:00
|
|
|
|
2023-12-27 19:51:58 -08:00
|
|
|
mod syntax {
|
2024-01-01 15:41:47 -08:00
|
|
|
use anyhow::{bail, Context, Result};
|
2023-12-27 19:51:58 -08:00
|
|
|
use serde::Deserialize;
|
2024-01-01 15:41:47 -08:00
|
|
|
use std::{collections::HashMap, fmt::Display, hash::Hash};
|
2023-12-30 16:57:03 -08:00
|
|
|
|
2024-01-10 22:44:22 -08:00
|
|
|
use crate::{
|
|
|
|
config,
|
|
|
|
part::{effect, faction, outfit, ship, sprite, system},
|
|
|
|
};
|
2023-12-27 19:51:58 -08:00
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
pub struct Root {
|
2023-12-27 20:13:39 -08:00
|
|
|
pub ship: Option<HashMap<String, ship::syntax::Ship>>,
|
|
|
|
pub system: Option<HashMap<String, system::syntax::System>>,
|
2023-12-30 20:27:53 -08:00
|
|
|
pub outfit: Option<HashMap<String, outfit::syntax::Outfit>>,
|
2024-01-04 17:17:55 -08:00
|
|
|
pub sprite: Option<HashMap<String, sprite::syntax::Sprite>>,
|
2023-12-30 16:57:03 -08:00
|
|
|
pub faction: Option<HashMap<String, faction::syntax::Faction>>,
|
2024-01-05 12:09:59 -08:00
|
|
|
pub effect: Option<HashMap<String, effect::syntax::Effect>>,
|
2024-01-10 22:44:22 -08:00
|
|
|
pub config: Option<config::syntax::Config>,
|
2023-12-30 10:58:17 -08:00
|
|
|
}
|
|
|
|
|
2024-01-01 15:41:47 -08:00
|
|
|
fn merge_hashmap<K, V>(
|
|
|
|
to: &mut Option<HashMap<K, V>>,
|
|
|
|
mut from: Option<HashMap<K, V>>,
|
|
|
|
) -> Result<()>
|
|
|
|
where
|
|
|
|
K: Hash + Eq + Display,
|
|
|
|
{
|
|
|
|
if to.is_none() {
|
|
|
|
*to = from.take();
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(from_inner) = from {
|
|
|
|
let to_inner = to.as_mut().unwrap();
|
|
|
|
for (k, v) in from_inner {
|
|
|
|
if to_inner.contains_key(&k) {
|
|
|
|
bail!("Found duplicate key `{k}`");
|
|
|
|
} else {
|
|
|
|
to_inner.insert(k, v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2023-12-30 10:58:17 -08:00
|
|
|
impl Root {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
ship: None,
|
|
|
|
system: None,
|
2023-12-30 20:27:53 -08:00
|
|
|
outfit: None,
|
2024-01-04 17:17:55 -08:00
|
|
|
sprite: None,
|
2023-12-30 16:57:03 -08:00
|
|
|
faction: None,
|
2024-01-05 12:09:59 -08:00
|
|
|
effect: None,
|
2024-01-10 22:44:22 -08:00
|
|
|
config: None,
|
2023-12-30 10:58:17 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn merge(&mut self, other: Root) -> Result<()> {
|
2024-01-01 15:41:47 -08:00
|
|
|
merge_hashmap(&mut self.ship, other.ship).with_context(|| "while merging ships")?;
|
|
|
|
merge_hashmap(&mut self.system, other.system)
|
|
|
|
.with_context(|| "while merging systems")?;
|
|
|
|
merge_hashmap(&mut self.outfit, other.outfit)
|
|
|
|
.with_context(|| "while merging outfits")?;
|
2024-01-04 17:17:55 -08:00
|
|
|
merge_hashmap(&mut self.sprite, other.sprite)
|
|
|
|
.with_context(|| "while merging sprites")?;
|
2024-01-01 15:41:47 -08:00
|
|
|
merge_hashmap(&mut self.faction, other.faction)
|
|
|
|
.with_context(|| "while merging factions")?;
|
2024-01-05 12:09:59 -08:00
|
|
|
merge_hashmap(&mut self.effect, other.effect)
|
|
|
|
.with_context(|| "while merging effects")?;
|
2024-01-10 22:44:22 -08:00
|
|
|
if self.config.is_some() {
|
|
|
|
if other.config.is_some() {
|
|
|
|
bail!("invalid content dir, multiple config tables")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.config = other.config;
|
|
|
|
}
|
2023-12-30 10:58:17 -08:00
|
|
|
return Ok(());
|
|
|
|
}
|
2023-12-27 19:51:58 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
trait Build {
|
2024-01-04 17:17:55 -08:00
|
|
|
type InputSyntaxType;
|
2023-12-30 10:58:17 -08:00
|
|
|
|
2023-12-27 19:51:58 -08:00
|
|
|
/// Build a processed System struct from raw serde data
|
2024-01-05 12:09:59 -08:00
|
|
|
fn build(
|
|
|
|
root: Self::InputSyntaxType,
|
|
|
|
build_context: &mut ContentBuildContext,
|
|
|
|
content: &mut Content,
|
|
|
|
) -> Result<()>
|
2023-12-27 19:51:58 -08:00
|
|
|
where
|
|
|
|
Self: Sized;
|
|
|
|
}
|
|
|
|
|
2024-01-05 12:09:59 -08:00
|
|
|
/// Stores temporary data while building context objects
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) struct ContentBuildContext {
|
2024-01-20 12:42:20 -08:00
|
|
|
/// Map effect names to handles
|
2024-01-05 12:09:59 -08:00
|
|
|
pub effect_index: HashMap<String, EffectHandle>,
|
2024-01-20 12:42:20 -08:00
|
|
|
|
|
|
|
/// Maps sprite handles to a map of section name -> section index
|
|
|
|
pub sprite_section_index: HashMap<SpriteHandle, HashMap<String, AnimSectionHandle>>,
|
2024-01-05 12:09:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ContentBuildContext {
|
|
|
|
fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
effect_index: HashMap::new(),
|
2024-01-20 12:42:20 -08:00
|
|
|
sprite_section_index: HashMap::new(),
|
2024-01-05 12:09:59 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-30 17:39:19 -08:00
|
|
|
/// Represents static game content
|
2024-01-11 20:21:07 -08:00
|
|
|
#[derive(Debug, Clone)]
|
2023-12-27 19:51:58 -08:00
|
|
|
pub struct Content {
|
2024-01-04 17:17:55 -08:00
|
|
|
/// Sprites
|
2024-01-05 12:09:59 -08:00
|
|
|
pub sprites: Vec<Sprite>,
|
2024-01-01 15:46:39 -08:00
|
|
|
/// Map strings to texture names.
|
|
|
|
/// This is only necessary because we need to hard-code a few texture names for UI elements.
|
2024-01-05 12:09:59 -08:00
|
|
|
sprite_index: HashMap<String, SpriteHandle>,
|
2024-01-04 17:17:55 -08:00
|
|
|
|
|
|
|
/// Keeps track of which images are in which texture
|
|
|
|
sprite_atlas: SpriteAtlas,
|
2023-12-30 10:58:17 -08:00
|
|
|
|
2024-01-05 12:09:59 -08:00
|
|
|
outfits: Vec<Outfit>,
|
|
|
|
guns: Vec<Gun>, // TODO: merge with outfit
|
|
|
|
ships: Vec<Ship>,
|
|
|
|
systems: Vec<System>,
|
|
|
|
factions: Vec<Faction>,
|
|
|
|
effects: Vec<Effect>,
|
2024-01-10 22:44:22 -08:00
|
|
|
config: Config,
|
2023-12-27 20:13:39 -08:00
|
|
|
}
|
2023-12-27 19:51:58 -08:00
|
|
|
|
2024-01-01 15:41:47 -08:00
|
|
|
// Loading methods
|
2023-12-27 20:13:39 -08:00
|
|
|
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)?);
|
|
|
|
}
|
2023-12-25 09:01:12 -08:00
|
|
|
|
2023-12-29 15:46:09 -08:00
|
|
|
/// Load content from a directory.
|
2024-01-10 22:44:22 -08:00
|
|
|
pub fn load_dir(path: PathBuf, asset_root: PathBuf, atlas_index: PathBuf) -> Result<Self> {
|
2023-12-30 10:58:17 -08:00
|
|
|
let mut root = syntax::Root::new();
|
2023-12-27 19:51:58 -08:00
|
|
|
|
|
|
|
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();
|
2023-12-30 10:58:17 -08:00
|
|
|
let this_root = Self::try_parse(path)
|
|
|
|
.with_context(|| format!("Could not read {}", path.display()))?;
|
|
|
|
|
|
|
|
root.merge(this_root)
|
2023-12-27 19:51:58 -08:00
|
|
|
.with_context(|| format!("Could not parse {}", path.display()))?;
|
2023-12-25 09:01:12 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-04 17:17:55 -08:00
|
|
|
let atlas: SpriteAtlas = {
|
|
|
|
let mut file_string = String::new();
|
|
|
|
let _ = File::open(atlas_index)?.read_to_string(&mut file_string);
|
|
|
|
let file_string = file_string.trim();
|
|
|
|
toml::from_str(&file_string)?
|
|
|
|
};
|
|
|
|
|
2024-01-05 12:09:59 -08:00
|
|
|
let mut build_context = ContentBuildContext::new();
|
|
|
|
|
2023-12-30 10:58:17 -08:00
|
|
|
let mut content = Self {
|
2024-01-10 22:44:22 -08:00
|
|
|
config: {
|
|
|
|
if let Some(c) = root.config {
|
2024-01-20 10:47:18 -08:00
|
|
|
c.build(&asset_root, &atlas)
|
2024-01-10 22:44:22 -08:00
|
|
|
.with_context(|| "while parsing config table")?
|
|
|
|
} else {
|
|
|
|
bail!("failed loading content: no config table specified")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2024-01-04 17:17:55 -08:00
|
|
|
sprite_atlas: atlas,
|
2023-12-30 10:58:17 -08:00
|
|
|
systems: Vec::new(),
|
|
|
|
ships: Vec::new(),
|
|
|
|
guns: Vec::new(),
|
2023-12-30 20:27:53 -08:00
|
|
|
outfits: Vec::new(),
|
2024-01-04 17:17:55 -08:00
|
|
|
sprites: Vec::new(),
|
2023-12-30 16:57:03 -08:00
|
|
|
factions: Vec::new(),
|
2024-01-05 12:09:59 -08:00
|
|
|
effects: Vec::new(),
|
2024-01-04 17:17:55 -08:00
|
|
|
sprite_index: HashMap::new(),
|
2023-12-30 10:58:17 -08:00
|
|
|
};
|
|
|
|
|
2024-01-05 12:09:59 -08:00
|
|
|
// TODO: enforce sprite and image limits
|
|
|
|
|
|
|
|
// Order matters. Some content types require another to be fully initialized
|
2024-01-04 17:17:55 -08:00
|
|
|
if root.sprite.is_some() {
|
2024-01-05 12:09:59 -08:00
|
|
|
part::sprite::Sprite::build(
|
|
|
|
root.sprite.take().unwrap(),
|
|
|
|
&mut build_context,
|
|
|
|
&mut content,
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
if root.effect.is_some() {
|
|
|
|
part::effect::Effect::build(
|
|
|
|
root.effect.take().unwrap(),
|
|
|
|
&mut build_context,
|
|
|
|
&mut content,
|
|
|
|
)?;
|
2023-12-30 10:58:17 -08:00
|
|
|
}
|
2024-01-04 17:17:55 -08:00
|
|
|
|
2024-01-05 12:09:59 -08:00
|
|
|
// Order below this line does not matter
|
2023-12-30 10:58:17 -08:00
|
|
|
if root.ship.is_some() {
|
2024-01-05 12:09:59 -08:00
|
|
|
part::ship::Ship::build(root.ship.take().unwrap(), &mut build_context, &mut content)?;
|
2023-12-30 10:58:17 -08:00
|
|
|
}
|
2023-12-30 20:27:53 -08:00
|
|
|
if root.outfit.is_some() {
|
2024-01-05 12:09:59 -08:00
|
|
|
part::outfit::Outfit::build(
|
|
|
|
root.outfit.take().unwrap(),
|
|
|
|
&mut build_context,
|
|
|
|
&mut content,
|
|
|
|
)?;
|
2023-12-30 10:58:17 -08:00
|
|
|
}
|
|
|
|
if root.system.is_some() {
|
2024-01-05 12:09:59 -08:00
|
|
|
part::system::System::build(
|
|
|
|
root.system.take().unwrap(),
|
|
|
|
&mut build_context,
|
|
|
|
&mut content,
|
|
|
|
)?;
|
2023-12-30 16:57:03 -08:00
|
|
|
}
|
|
|
|
if root.faction.is_some() {
|
2024-01-05 12:09:59 -08:00
|
|
|
part::faction::Faction::build(
|
|
|
|
root.faction.take().unwrap(),
|
|
|
|
&mut build_context,
|
|
|
|
&mut content,
|
|
|
|
)?;
|
2023-12-30 10:58:17 -08:00
|
|
|
}
|
|
|
|
|
2023-12-27 19:51:58 -08:00
|
|
|
return Ok(content);
|
|
|
|
}
|
2023-12-25 09:01:12 -08:00
|
|
|
}
|
2024-01-01 15:41:47 -08:00
|
|
|
|
|
|
|
// Access methods
|
|
|
|
impl Content {
|
2024-01-09 11:37:36 -08:00
|
|
|
/// Iterate over all valid system handles
|
|
|
|
pub fn iter_systems(&self) -> impl Iterator<Item = SystemHandle> {
|
|
|
|
(0..self.systems.len()).map(|x| SystemHandle { index: x })
|
|
|
|
}
|
|
|
|
|
2024-01-04 17:17:55 -08:00
|
|
|
/// Get a handle from a sprite name
|
|
|
|
pub fn get_sprite_handle(&self, name: &str) -> SpriteHandle {
|
|
|
|
return match self.sprite_index.get(name) {
|
2024-01-01 15:41:47 -08:00
|
|
|
Some(s) => *s,
|
2024-01-04 17:17:55 -08:00
|
|
|
None => unreachable!("get_sprite_handle was called with a bad name!"),
|
2024-01-01 15:41:47 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-01-04 17:17:55 -08:00
|
|
|
/// Get a sprite from a handle
|
|
|
|
pub fn get_sprite(&self, h: SpriteHandle) -> &Sprite {
|
2024-01-01 15:41:47 -08:00
|
|
|
// In theory, this could fail if h has a bad index, but that shouldn't ever happen.
|
2024-01-04 17:17:55 -08:00
|
|
|
// The only handles that exist should be created by this crate.
|
2024-01-04 18:15:30 -08:00
|
|
|
return &self.sprites[h.index as usize];
|
2024-01-04 17:17:55 -08:00
|
|
|
}
|
|
|
|
|
2024-01-04 22:17:34 -08:00
|
|
|
/// Get the list of atlas files we may use
|
|
|
|
pub fn atlas_files(&self) -> &Vec<String> {
|
|
|
|
return &self.sprite_atlas.atlas_list;
|
|
|
|
}
|
|
|
|
|
2024-01-20 09:36:12 -08:00
|
|
|
/// Get a texture by its index
|
|
|
|
pub fn get_image(&self, idx: u32) -> &SpriteAtlasImage {
|
|
|
|
&self.sprite_atlas.index[idx as usize]
|
2024-01-01 15:41:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get an outfit from a handle
|
|
|
|
pub fn get_outfit(&self, h: OutfitHandle) -> &Outfit {
|
|
|
|
return &self.outfits[h.index];
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a gun from a handle
|
|
|
|
pub fn get_gun(&self, h: GunHandle) -> &Gun {
|
|
|
|
return &self.guns[h.index];
|
|
|
|
}
|
|
|
|
|
2024-01-04 17:17:55 -08:00
|
|
|
/// Get a ship from a handle
|
2024-01-01 15:41:47 -08:00
|
|
|
pub fn get_ship(&self, h: ShipHandle) -> &Ship {
|
|
|
|
return &self.ships[h.index];
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a system from a handle
|
|
|
|
pub fn get_system(&self, h: SystemHandle) -> &System {
|
|
|
|
return &self.systems[h.index];
|
|
|
|
}
|
|
|
|
|
2024-01-12 14:34:31 -08:00
|
|
|
/// Get a system object from a handle
|
|
|
|
pub fn get_system_object(&self, h: SystemObjectHandle) -> &SystemObject {
|
|
|
|
return &self.get_system(h.system_handle).objects[h.body_index];
|
|
|
|
}
|
|
|
|
|
2024-01-01 15:41:47 -08:00
|
|
|
/// Get a faction from a handle
|
|
|
|
pub fn get_faction(&self, h: FactionHandle) -> &Faction {
|
|
|
|
return &self.factions[h.index];
|
|
|
|
}
|
2024-01-05 12:09:59 -08:00
|
|
|
|
|
|
|
/// Get an effect from a handle
|
|
|
|
pub fn get_effect(&self, h: EffectHandle) -> &Effect {
|
|
|
|
return &self.effects[h.index];
|
|
|
|
}
|
2024-01-10 22:44:22 -08:00
|
|
|
|
|
|
|
/// Get content configuration
|
|
|
|
pub fn get_config(&self) -> &Config {
|
|
|
|
return &self.config;
|
|
|
|
}
|
2024-01-01 15:41:47 -08:00
|
|
|
}
|