mirror of
https://codeberg.org/icewind/vmt-parser.git
synced 2026-06-03 12:04:06 +02:00
patch resolving
This commit is contained in:
parent
b6a7ee888b
commit
1e1733f5f7
7 changed files with 106 additions and 13 deletions
10
Cargo.lock
generated
10
Cargo.lock
generated
|
|
@ -567,18 +567,18 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "1.0.50"
|
||||
version = "1.0.51"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f9a7210f5c9a7156bb50aa36aed4c95afb51df0df00713949448cf9e97d382d2"
|
||||
checksum = "f11c217e1416d6f036b870f14e0413d480dbf28edbee1f877abaf0206af43bb7"
|
||||
dependencies = [
|
||||
"thiserror-impl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror-impl"
|
||||
version = "1.0.50"
|
||||
version = "1.0.51"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "266b2e40bc00e5a6c09c3584011e08b06f123c00362c92b975ba9843aaaa14b8"
|
||||
checksum = "01742297787513b79cf8e29d1056ede1313e2420b7b3b15d0a768b4921f549df"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
|
@ -606,7 +606,7 @@ checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85"
|
|||
[[package]]
|
||||
name = "vdf-reader"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/icewind1991/vdf-reader#8d207944b545c05e9491f52659f6ded8dbfc31c7"
|
||||
source = "git+https://github.com/icewind1991/vdf-reader#31977b9340e915944eeb27c96f3199f7c5560376"
|
||||
dependencies = [
|
||||
"logos",
|
||||
"miette",
|
||||
|
|
|
|||
|
|
@ -5,8 +5,10 @@ mod worldvertextransition;
|
|||
|
||||
pub use lightmappedgeneric::LightMappedGenericMaterial;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
pub use unlitgeneric::UnlitGenericMaterial;
|
||||
use vdf_reader::entry::{Entry, Table};
|
||||
use vdf_reader::error::UnknownError;
|
||||
use vdf_reader::{from_entry, VdfError};
|
||||
pub use water::WaterMaterial;
|
||||
pub use worldvertextransition::WorldVertexTransitionMaterial;
|
||||
|
||||
|
|
@ -24,8 +26,49 @@ pub enum Material {
|
|||
Patch(PatchMaterial),
|
||||
}
|
||||
|
||||
impl Material {
|
||||
/// If the material is a patch, apply it to the included material
|
||||
pub fn resolve<E, Loader>(self, loader: Loader) -> Result<Material, E>
|
||||
where
|
||||
Loader: FnOnce(&str) -> Result<String, E>,
|
||||
E: From<VdfError>,
|
||||
{
|
||||
match self {
|
||||
Material::Patch(patch) => patch.resolve(loader),
|
||||
mat => Ok(mat),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct PatchMaterial {
|
||||
include: String,
|
||||
replace: HashMap<String, String>,
|
||||
replace: Table,
|
||||
}
|
||||
|
||||
impl PatchMaterial {
|
||||
/// Load the included material and apply the patch
|
||||
pub fn resolve<E, Loader>(&self, loader: Loader) -> Result<Material, E>
|
||||
where
|
||||
Loader: FnOnce(&str) -> Result<String, E>,
|
||||
E: From<VdfError>,
|
||||
{
|
||||
let base = loader(&self.include)?.to_ascii_lowercase();
|
||||
let mut material = Table::load_from_str(&base)?;
|
||||
|
||||
let material_values = match material.iter_mut().next() {
|
||||
Some((_, Entry::Table(table))) => table,
|
||||
_ => {
|
||||
return Err(VdfError::from(UnknownError::from(
|
||||
"included vdf doesn't look like a material",
|
||||
))
|
||||
.into())
|
||||
}
|
||||
};
|
||||
for (key, value) in self.replace.iter() {
|
||||
material_values.insert(key.clone(), value.clone());
|
||||
}
|
||||
|
||||
Ok(from_entry(Entry::Table(material))?)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
"$basetexture2" "cp_mountainlab/nature/grass001"
|
||||
"$blendmodulatetexture" "nature/grass_blendmask"
|
||||
"$bumpmap" "nature/rockwall009_height-ssbump"
|
||||
"$ssbump" "1"
|
||||
"$ssbump" "1"
|
||||
"%keywords" "tf"
|
||||
"$surfaceprop" "dirt"
|
||||
}
|
||||
|
|
@ -1,8 +1,8 @@
|
|||
"patch"
|
||||
{
|
||||
"include" "materials/glass/glasswindow001a.vmt"
|
||||
"include" "tests/data/concretefloor003.vmt"
|
||||
"replace"
|
||||
{
|
||||
"$envmap" "maps/koth_bagel_rc2a/c1920_704_41"
|
||||
"$ssbump" "0"
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,17 @@
|
|||
use miette::{GraphicalReportHandler, GraphicalTheme};
|
||||
use std::fs::read_to_string;
|
||||
use test_case::test_case;
|
||||
use thiserror::Error;
|
||||
use vmt_parser::from_str;
|
||||
use vmt_parser::material::Material;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
enum LoaderError {
|
||||
#[error(transparent)]
|
||||
Io(#[from] std::io::Error),
|
||||
#[error(transparent)]
|
||||
Vdf(#[from] vdf_reader::VdfError),
|
||||
}
|
||||
|
||||
#[test_case("tests/data/concretefloor003.vmt")]
|
||||
#[test_case("tests/data/mvm_backpack.vmt")]
|
||||
|
|
@ -11,7 +21,15 @@ use vmt_parser::from_str;
|
|||
fn test_serde(path: &str) {
|
||||
let raw = read_to_string(path).unwrap();
|
||||
match from_str(&raw) {
|
||||
Ok(result) => insta::assert_ron_snapshot!(path, result),
|
||||
Ok(result) => {
|
||||
insta::assert_ron_snapshot!(path, result);
|
||||
if let Material::Patch(patch) = result {
|
||||
let patched = patch
|
||||
.resolve(|path| read_to_string(path).map_err(LoaderError::from))
|
||||
.unwrap();
|
||||
insta::assert_ron_snapshot!(format!("{}_resolved", path), patched);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
let handler = GraphicalReportHandler::new_themed(GraphicalTheme::unicode_nocolor());
|
||||
let mut out = String::new();
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@ source: tests/parse.rs
|
|||
expression: result
|
||||
---
|
||||
patch(PatchMaterial(
|
||||
include: "materials/glass/glasswindow001a.vmt",
|
||||
include: "tests/data/concretefloor003.vmt",
|
||||
replace: {
|
||||
"$envmap": "maps/koth_bagel_rc2a/c1920_704_41",
|
||||
"$ssbump": "0",
|
||||
},
|
||||
))
|
||||
|
|
|
|||
32
tests/snapshots/parse__tests__data__patch.vmt_resolved.snap
Normal file
32
tests/snapshots/parse__tests__data__patch.vmt_resolved.snap
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
---
|
||||
source: tests/parse.rs
|
||||
expression: patched
|
||||
---
|
||||
lightmappedgeneric(LightMappedGenericMaterial(
|
||||
r#$basetexture: "cp_mountainlab/concrete/concretefloor003",
|
||||
r#$decal: false,
|
||||
r#$detail: Some("overlays/detail001"),
|
||||
r#$surfaceprop: None,
|
||||
r#$basetexturetransform: "center 0.5 0.5 scale 1 1 rotate 0 translate 0 0",
|
||||
r#$color: Vec3((1.0, 1.0, 1.0)),
|
||||
r#$decalscale: 1.0,
|
||||
r#$decalscale: Vec2((4.0, 4.0)),
|
||||
r#$detailblendfactor: 1.0,
|
||||
r#$detailblendmode: 0,
|
||||
r#$modelmaterial: None,
|
||||
r#$pointsamplemagfilter: false,
|
||||
r#$seamless_scale: 1.0,
|
||||
r#$alpha: 1.0,
|
||||
r#$alphatest: false,
|
||||
r#$distancealpha: false,
|
||||
r#$nocull: false,
|
||||
r#$translucent: false,
|
||||
r#$bumpmap: Some("concrete/concretefloor007b_height-ssbump"),
|
||||
r#$lightwarptexture: None,
|
||||
r#$selfillum: false,
|
||||
r#$ssbump: false,
|
||||
r#$envmap: None,
|
||||
r#$phong: false,
|
||||
r#$nofog: false,
|
||||
r#$ignorez: false,
|
||||
))
|
||||
Loading…
Add table
Add a link
Reference in a new issue