Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions crates/analysis/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub enum CompletionKind {
Value,
Reference,
W3dModel,
W3dAnimation,
}

/// A single completion candidate.
Expand Down Expand Up @@ -154,6 +155,11 @@ fn classify_position(
.as_ref()
.and_then(|scope_node| scope_schema(analyzer, scope_node).field(&key))
.and_then(|field| {
// Animation's following tokens are optional distance and
// repeat count, despite its lenient string schema type.
if field.parse_fn == "parseAnimation" {
return None;
}
field
.value_type
.token_index_at_input(&input, raw_value_index)
Expand Down Expand Up @@ -412,6 +418,27 @@ fn field_value_completions(
let mut base = {
let scope = scope_schema(analyzer, scope_node);
if let Some(f) = scope.field(key) {
if f.parse_fn == "parseAnimation" {
return if value_index == 0 {
index
.map(|index| {
crate::model::condition_state_model(scope_node)
.into_iter()
.flat_map(|model| index.model_animations(&model))
.map(|name| Completion {
label: name.to_string(),
kind: CompletionKind::W3dAnimation,
detail: Some("W3D animation".into()),
documentation: None,
insert: None,
})
.collect()
})
.unwrap_or_default()
} else {
Vec::new()
};
}
if let Some(asset_completions) = model_asset_completions(
analyzer,
scope_node,
Expand Down Expand Up @@ -1290,6 +1317,7 @@ mod tests {
index.set_file_models(
"models/Good.w3d",
vec![crate::index::ModelAsset {
hierarchy: None,
name: "Good".into(),
members: vec!["Cargo01".into(), "Tire01".into()],
}],
Expand Down Expand Up @@ -1328,6 +1356,7 @@ End
index.set_file_models(
"models/Good.w3d",
vec![crate::index::ModelAsset {
hierarchy: None,
name: "Good".into(),
members: vec!["Turret01".into()],
}],
Expand Down Expand Up @@ -1362,6 +1391,7 @@ End
index.set_file_models(
"a10.w3d",
vec![crate::index::ModelAsset {
hierarchy: None,
name: "A10".into(),
members: vec!["WeaponA01".into()],
}],
Expand All @@ -1387,6 +1417,7 @@ End
index.set_file_models(
"models/Good.w3d",
vec![crate::index::ModelAsset {
hierarchy: None,
name: "Good".into(),
members: vec![],
}],
Expand All @@ -1408,6 +1439,7 @@ End
index.set_file_models(
"models/Good.w3d",
vec![crate::index::ModelAsset {
hierarchy: None,
name: "Good".into(),
members: vec!["Muzzle01".into(), "Muzzle02".into()],
}],
Expand Down
6 changes: 6 additions & 0 deletions crates/analysis/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2756,6 +2756,7 @@ End
index.set_file_models(
"models/Good.w3d",
vec![crate::index::ModelAsset {
hierarchy: None,
name: "Good".into(),
members: vec!["Tire01".into(), "Cargo01".into(), "Muzzle01".into()],
}],
Expand Down Expand Up @@ -2827,6 +2828,7 @@ End
index.set_file_models(
"models/Good.w3d",
vec![crate::index::ModelAsset {
hierarchy: None,
name: "Good".into(),
members: vec!["Turret01".into()],
}],
Expand Down Expand Up @@ -2869,6 +2871,7 @@ End
index.set_file_models(
"models/A10.w3d",
vec![crate::index::ModelAsset {
hierarchy: None,
name: "A10".into(),
members: vec!["WeaponA01".into(), "Missile01".into()],
}],
Expand Down Expand Up @@ -2922,6 +2925,7 @@ End
index.set_file_models(
"models/Good.w3d",
vec![crate::index::ModelAsset {
hierarchy: None,
name: "Good".into(),
members: vec![],
}],
Expand All @@ -2941,13 +2945,15 @@ End
index.set_file_models(
"a.w3d",
vec![crate::index::ModelAsset {
hierarchy: None,
name: "A".into(),
members: vec!["Bone01".into()],
}],
);
index.set_file_models(
"b.w3d",
vec![crate::index::ModelAsset {
hierarchy: None,
name: "B".into(),
members: vec![],
}],
Expand Down
128 changes: 125 additions & 3 deletions crates/analysis/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ pub struct FileAsset {
pub struct ModelAsset {
pub name: String,
pub members: Vec<String>,
pub hierarchy: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AnimationAsset {
pub name: String,
pub hierarchy: String,
}

/// A definition's location within a file.
Expand Down Expand Up @@ -138,6 +145,8 @@ pub struct WorkspaceIndex {
model_assets: HashMap<String, Vec<(Arc<str>, ModelAsset)>>,
/// Reverse map for removing/replacing models contributed by one asset file.
file_models: HashMap<String, Vec<String>>,
animation_assets: HashMap<String, Vec<(Arc<str>, AnimationAsset)>>,
file_animations: HashMap<String, Vec<String>>,
asset_names: HashMap<AssetKind, HashMap<String, Vec<(String, FileAsset)>>>,
texture_assets: HashMap<String, Vec<(String, FileAsset)>>,
file_assets: HashMap<String, Vec<FileAsset>>,
Expand Down Expand Up @@ -241,6 +250,7 @@ impl WorkspaceIndex {
self.remove_entries(file);
self.remove_site_entries(file);
self.remove_model_entries(file);
self.set_file_animations(file, Vec::new());
self.set_file_assets(file, Vec::new());
self.remove_object_model_entries(file);
self.remove_object_parent_entries(file);
Expand Down Expand Up @@ -275,6 +285,51 @@ impl WorkspaceIndex {
}
}

/// Replace animation metadata contributed by one loose or archived W3D.
pub fn set_file_animations(&mut self, file: &str, animations: Vec<AnimationAsset>) {
if let Some(names) = self.file_animations.remove(file) {
for name in names {
if let Some(entries) = self.animation_assets.get_mut(&name) {
entries.retain(|(source, _)| source.as_ref() != file);
if entries.is_empty() {
self.animation_assets.remove(&name);
}
}
}
}
let source: Arc<str> = Arc::from(file);
let mut names = Vec::new();
for animation in animations {
let name = animation.name.to_ascii_lowercase();
self.animation_assets
.entry(name.clone())
.or_default()
.push((source.clone(), animation));
names.push(name);
}
if !names.is_empty() {
self.file_animations.insert(file.to_string(), names);
}
// Animation metadata only drives uncached completion requests.
}

/// Animations compatible with the effective model's skeleton. Files may
/// contribute animations separately from the model and its hierarchy.
pub fn model_animations<'a>(&'a self, model: &str) -> impl Iterator<Item = &'a str> {
let hierarchy = self
.model_assets
.get(&model.to_ascii_lowercase())
.and_then(|entries| entries.last())
.and_then(|(_, model)| model.hierarchy.as_deref());
self.animation_assets
.values()
.filter_map(|entries| entries.last())
.filter(move |(_, animation)| {
hierarchy.is_some_and(|h| h.eq_ignore_ascii_case(&animation.hierarchy))
})
.map(|(_, animation)| animation.name.as_str())
}

/// Replace W3D model assets contributed by `file`.
pub fn set_file_models(&mut self, file: &str, models: Vec<ModelAsset>) {
let changed = match self.file_models.get(file) {
Expand Down Expand Up @@ -865,11 +920,13 @@ fn dedup_case_insensitive(values: &mut Vec<String>) {
values.retain(|value| seen.insert(value.to_ascii_lowercase()));
}

fn normalized_model_assets(models: &[ModelAsset]) -> Vec<(String, Vec<String>)> {
fn normalized_model_assets(models: &[ModelAsset]) -> Vec<(String, Vec<String>, Option<String>)> {
normalized_model_asset_refs(&models.iter().collect::<Vec<_>>())
}

fn normalized_model_asset_refs(models: &[&ModelAsset]) -> Vec<(String, Vec<String>)> {
fn normalized_model_asset_refs(
models: &[&ModelAsset],
) -> Vec<(String, Vec<String>, Option<String>)> {
let mut out = models
.iter()
.map(|model| {
Expand All @@ -880,7 +937,11 @@ fn normalized_model_asset_refs(models: &[&ModelAsset]) -> Vec<(String, Vec<Strin
.collect::<Vec<_>>();
members.sort();
members.dedup();
(model.name.to_ascii_lowercase(), members)
(
model.name.to_ascii_lowercase(),
members,
model.hierarchy.as_ref().map(|h| h.to_ascii_lowercase()),
)
})
.collect::<Vec<_>>();
out.sort();
Expand Down Expand Up @@ -1368,13 +1429,15 @@ mod tests {
idx.set_file_models(
"base.w3d",
vec![ModelAsset {
hierarchy: None,
name: "Tank".into(),
members: Vec::new(),
}],
);
idx.set_file_models(
"mod.w3d",
vec![ModelAsset {
hierarchy: None,
name: "TANK".into(),
members: Vec::new(),
}],
Expand Down Expand Up @@ -1445,10 +1508,63 @@ mod tests {
assert!(idx.is_defined(RefKind::Weapon, "New"));
}

#[test]
fn animation_lookup_tracks_model_overrides_and_file_removal() {
let mut idx = WorkspaceIndex::new();
idx.set_file_models(
"base.w3d",
vec![ModelAsset {
name: "Soldier".into(),
members: vec![],
hierarchy: Some("HumanSKL".into()),
}],
);
let animation = AnimationAsset {
name: "HumanSKL.Run".into(),
hierarchy: "HumanSKL".into(),
};
idx.set_file_animations("run.w3d", vec![animation.clone()]);
idx.set_file_animations("patch-run.w3d", vec![animation]);
assert_eq!(
idx.model_animations("soldier").collect::<Vec<_>>(),
["HumanSKL.Run"]
);
idx.remove_file("patch-run.w3d");
assert_eq!(
idx.model_animations("soldier").collect::<Vec<_>>(),
["HumanSKL.Run"]
);
idx.set_file_models(
"patch.w3d",
vec![ModelAsset {
name: "Soldier".into(),
members: vec![],
hierarchy: Some("OtherSKL".into()),
}],
);
assert_eq!(idx.model_animations("Soldier").count(), 0);
idx.remove_file("patch.w3d");
assert_eq!(idx.model_animations("Soldier").count(), 1);
idx.set_file_animations(
"run.w3d",
vec![AnimationAsset {
name: "HumanSKL.Idle".into(),
hierarchy: "HumanSKL".into(),
}],
);
assert_eq!(
idx.model_animations("Soldier").collect::<Vec<_>>(),
["HumanSKL.Idle"]
);
idx.remove_file("run.w3d");
assert_eq!(idx.model_animations("Soldier").count(), 0);
}

#[test]
fn model_asset_member_changes_bump_generation() {
let mut idx = WorkspaceIndex::new();
let original = vec![ModelAsset {
hierarchy: None,
name: "Tank".into(),
members: vec!["Tire01".into()],
}];
Expand All @@ -1460,6 +1576,7 @@ mod tests {
idx.set_file_models(
"model.w3d",
vec![ModelAsset {
hierarchy: None,
name: "Tank".into(),
members: vec!["Tire02".into()],
}],
Expand All @@ -1473,13 +1590,15 @@ mod tests {
idx.insert_file_models_prepared(
"base.w3d",
vec![ModelAsset {
hierarchy: None,
name: "Tank".into(),
members: vec!["Tire01".into()],
}],
);
idx.insert_file_models_prepared(
"patch.w3d",
vec![ModelAsset {
hierarchy: None,
name: "TANK".into(),
members: vec!["Cargo01".into()],
}],
Expand Down Expand Up @@ -1508,13 +1627,15 @@ mod tests {
idx.set_file_models(
"base.w3d",
vec![ModelAsset {
hierarchy: None,
name: "Tank".into(),
members: vec!["Tire01".into()],
}],
);
idx.set_file_models(
"patch.w3d",
vec![ModelAsset {
hierarchy: None,
name: "TANK".into(),
members: vec!["Cargo01".into()],
}],
Expand All @@ -1531,6 +1652,7 @@ mod tests {
idx.set_file_models(
"base.w3d",
vec![ModelAsset {
hierarchy: None,
name: "Tank".into(),
members: vec!["Tire01".into()],
}],
Expand Down
Loading