summaryrefslogtreecommitdiff
path: root/src/manifest.rs
blob: 2cb474b6f6477c9113318ada7214121cc05b4a80 (plain)
#[cfg(feature = "tinywasm")]
use std::path::Path;

use etcetera::AppStrategyArgs;
use serde::{Deserialize, Serialize};

use crate::{scene::Scene, script::ScriptDeclaration};

#[derive(Clone)]
pub struct GameManifest {
	pub name: Box<str>,
	pub author: Box<str>,
	pub tld: Box<str>,
	pub first_scene: usize,
	pub scenes: Vec<Scene>,
	pub scripts: Vec<ScriptDeclaration>,
}

pub struct PartialGameManifest {
	pub scene: Box<[PartialScene]>,
	pub script: Box<[ScriptDeclaration]>,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct HumaneGameManifest {
	pub name: Box<str>,
	pub author: Box<str>,
	pub tld: Box<str>,
	pub first_scene: Box<str>,
	#[serde(default)]
	pub scene: Box<[PartialScene]>,
	#[serde(default)]
	pub script: Box<[ScriptDeclaration]>,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct DeserializedGameManifest {
	pub name: Box<str>,
	pub author: Box<str>,
	pub tld: Box<str>,
	pub first_scene: Box<str>,
	#[serde(default)]
	pub scene: Box<[PartialScene]>,
	#[serde(default)]
	pub script: Box<[Box<str>]>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PartialScene {
	pub name: Box<str>,
	#[serde(default)]
	pub scripts: Box<[Box<str>]>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GameManifestPatch {
	#[serde(default)]
	pub new_scenes: Box<[PartialScene]>,
	#[serde(default)]
	pub new_scripts: Box<[Box<str>]>,
	#[serde(default)]
	pub patched_scenes: Box<[ScenePatch]>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScenePatch {
	scene: Box<str>,
	#[serde(default)]
	pub add_scripts: Box<[Box<str>]>,
	#[serde(default)]
	pub remove_scripts: Box<[Box<str>]>,
}

fn get_id_by_name<T>(name: &str, scripts: &[T], get_name: impl Fn(&T) -> &str) -> Option<usize> {
	scripts
		.iter()
		.enumerate()
		.find_map(|(i, s)| (get_name(s) == &*name).then_some(i))
}

fn get_ids_by_name<T>(
	names: &[Box<str>],
	scripts: &[T],
	get_name: impl Fn(&T) -> &str,
) -> Box<[usize]> {
	names
		.iter()
		.filter_map(|script_name| get_id_by_name(script_name, scripts, &get_name))
		.collect()
}

impl From<HumaneGameManifest> for GameManifest {
	fn from(value: HumaneGameManifest) -> Self {
		let mut buffer = GameManifest {
			name: value.name,
			author: value.author,
			tld: value.tld,
			first_scene: 0,
			scenes: Vec::new(),
			scripts: value.script.into(),
		};
		buffer.add_manifest(PartialGameManifest {
			script: Box::from([]),
			scene: value.scene,
		});
		buffer.first_scene = buffer
			.scenes
			.iter()
			.enumerate()
			.find_map(|(i, scene)| (scene.name == value.first_scene).then_some(i))
			.unwrap_or_default();
		buffer
	}
}

impl GameManifest {
	#[cfg(feature = "tinywasm")]
	pub fn from_wasm(path: Box<Path>, manifest: DeserializedGameManifest) -> Self {
		HumaneGameManifest {
			name: manifest.name,
			author: manifest.author,
			tld: manifest.tld,
			first_scene: manifest.first_scene,
			scene: manifest.scene,
			script: manifest
				.script
				.into_iter()
				.map(|script| ScriptDeclaration::Wasm {
					name: script,
					path: path.clone(),
				})
				.collect(),
		}
		.into()
	}

	pub fn add_manifest(&mut self, partial: PartialGameManifest) {
		self.scripts.extend(partial.script);
		self.scenes
			.extend(partial.scene.into_iter().map(|scene| Scene {
				name: scene.name,
				scripts: get_ids_by_name(&scene.scripts, &self.scripts, |s| s.name()).into_vec(),
			}));
	}

	#[cfg(feature = "tinywasm")]
	pub fn apply_wasm_patch(&mut self, path: Box<Path>, patch: GameManifestPatch) {
		self.scenes
			.extend(patch.new_scenes.into_iter().map(|scene| Scene {
				name: scene.name,
				scripts: get_ids_by_name(&scene.scripts, &self.scripts, |s| s.name()).into_vec(),
			}));
		self.scripts.extend(
			patch
				.new_scripts
				.into_iter()
				.map(|script| ScriptDeclaration::Wasm {
					name: script,
					path: path.clone(),
				}),
		);

		for scene_patch in patch.patched_scenes {
			let Some(patched_scene) =
				get_id_by_name(&scene_patch.scene, &self.scenes, |scene| &scene.name)
			else {
				continue;
			};
			let patched_scene = &mut self.scenes[patched_scene];
			patched_scene.scripts.extend(get_ids_by_name(
				&scene_patch.add_scripts,
				&self.scripts,
				|script| script.name(),
			));
			patched_scene.scripts.retain(|script| {
				!scene_patch
					.remove_scripts
					.iter()
					.any(|s| **s == *self.scripts[*script].name())
			});
		}
	}

	pub fn app_strategy_args(&self) -> AppStrategyArgs {
		AppStrategyArgs {
			top_level_domain: self.tld.to_string(),
			author: self.author.to_string(),
			app_name: self.name.to_string(),
		}
	}
}