|
3 | 3 | use core::any::TypeId; |
4 | 4 |
|
5 | 5 | use anyhow::{anyhow, Result as AnyhowResult}; |
| 6 | +use bevy_dev_tools::schedule_data::serde::ScheduleData; |
6 | 7 | use bevy_ecs::{ |
7 | 8 | component::ComponentId, |
8 | 9 | entity::Entity, |
@@ -526,25 +527,10 @@ pub struct BrpScheduleListResponse { |
526 | 527 | /// If a system (f2) is placed in a developer-created set (S1), then there is a hierarchy edge (S1 -> f2). |
527 | 528 | /// |
528 | 529 | /// If a schedule adds a condition f1.after(S1) , then a dependency edge is added (S1 -> f1) |
529 | | -#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)] |
| 530 | +#[derive(Debug, Serialize, Deserialize, Clone)] |
530 | 531 | pub struct BrpScheduleGraphResponse { |
531 | | - systemsets: Vec<BrpSystemSet>, |
532 | | - |
533 | | - hierarchy_nodes: Vec<String>, |
534 | | - hierarchy_edges: Vec<(String, String)>, |
535 | | - |
536 | | - dependency_nodes: Vec<String>, |
537 | | - dependency_edges: Vec<(String, String)>, |
538 | | -} |
539 | | - |
540 | | -/// Details on a system set |
541 | | -/// |
542 | | -/// The `key` is used in the nodes and edges in [`BrpScheduleGraphResponse`] |
543 | | -/// The `method` is either the fully qualified system name, or the system set name |
544 | | -#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)] |
545 | | -pub struct BrpSystemSet { |
546 | | - key: String, |
547 | | - method: String, |
| 532 | + /// The extracted data for the requested schedule. |
| 533 | + pub schedule_data: ScheduleData, |
548 | 534 | } |
549 | 535 |
|
550 | 536 | /// One query match result: a single entity paired with the requested components. |
@@ -1627,50 +1613,29 @@ pub fn schedule_graph(In(params): In<Option<Value>>, world: &mut World) -> BrpRe |
1627 | 1613 |
|
1628 | 1614 | let schedules = world.resource::<Schedules>(); |
1629 | 1615 |
|
1630 | | - let matching_schedule = schedules |
| 1616 | + let Some((label, schedule)) = schedules |
1631 | 1617 | .iter() |
1632 | | - .find(|(label, _schedule)| format!("{:?}", label) == schedule_label); |
1633 | | - |
1634 | | - if matching_schedule.is_none() { |
| 1618 | + .find(|(label, _schedule)| format!("{:?}", label) == schedule_label) |
| 1619 | + else { |
1635 | 1620 | return Err(BrpError::resource_error(format!( |
1636 | | - "Schedule with label={:} not found", |
| 1621 | + "Schedule with label={:} not found. This may be because this schedule is currently running", |
1637 | 1622 | schedule_label |
1638 | 1623 | ))); |
1639 | | - } |
1640 | | - |
1641 | | - let mut response: BrpScheduleGraphResponse = BrpScheduleGraphResponse::default(); |
1642 | | - |
1643 | | - let (_label, schedule) = matching_schedule.unwrap(); |
1644 | | - |
1645 | | - let g = schedule.graph(); |
1646 | | - for (systemsetkey, method, _b) in g.system_sets.iter() { |
1647 | | - response.systemsets.push(BrpSystemSet { |
1648 | | - key: format!("{:?}", systemsetkey), |
1649 | | - method: format!("{:?}", method), |
1650 | | - }); |
1651 | | - } |
1652 | | - |
1653 | | - let hie = g.hierarchy(); |
1654 | | - for node in hie.nodes() { |
1655 | | - response.hierarchy_nodes.push(format!("{:?}", node)); |
1656 | | - } |
1657 | | - for (n1, n2) in hie.all_edges() { |
1658 | | - response |
1659 | | - .hierarchy_edges |
1660 | | - .push((format!("{:?}", n1), format!("{:?}", n2))); |
1661 | | - } |
| 1624 | + }; |
1662 | 1625 |
|
1663 | | - let dep = g.dependency(); |
1664 | | - for node in dep.nodes() { |
1665 | | - response.dependency_nodes.push(format!("{:?}", node)); |
1666 | | - } |
1667 | | - for (n1, n2) in dep.all_edges() { |
1668 | | - response |
1669 | | - .dependency_edges |
1670 | | - .push((format!("{:?}", n1), format!("{:?}", n2))); |
1671 | | - } |
| 1626 | + // TODO: We should consider saving all the `ScheduleBuilt` events when BRP is enabled, and |
| 1627 | + // looking it up here (or even building the schedule here to get the metadata to fill it in). |
| 1628 | + let schedule_data = match ScheduleData::from_schedule(schedule, world.components(), None) { |
| 1629 | + Ok(schedule_data) => schedule_data, |
| 1630 | + Err(err) => { |
| 1631 | + return Err(BrpError::internal(format!( |
| 1632 | + "Failed to collect the schedule data for {:?}: {err}", |
| 1633 | + label |
| 1634 | + ))); |
| 1635 | + } |
| 1636 | + }; |
1672 | 1637 |
|
1673 | | - serde_json::to_value(response).map_err(BrpError::internal) |
| 1638 | + serde_json::to_value(BrpScheduleGraphResponse { schedule_data }).map_err(BrpError::internal) |
1674 | 1639 | } |
1675 | 1640 |
|
1676 | 1641 | /// Immutably retrieves an entity from the [`World`], returning an error if the |
@@ -2215,20 +2180,21 @@ mod tests { |
2215 | 2180 | // Each system creates a corresponding system set. |
2216 | 2181 | // In the below notation we use f1 for the system, and F1 for the corresponding system set. |
2217 | 2182 | // Above schedule should have the following layout: |
2218 | | - // - 4 systems (f1, f2, f3, f4) |
| 2183 | + // - 5 systems (f1, f2, f3, f4, apply_deferred) |
2219 | 2184 | // - 6 system sets (F1, F2, F3, F4, S1, S2) |
2220 | | - // - 10 hierarchy nodes and 10 dependency nodes (4 + 6) |
2221 | 2185 | // - 6 hierarchy edges: F1 -> f1, F2 -> f2, F3 -> f3, F4 -> f4, S1 -> f3, S2 -> f4 |
2222 | 2186 | // - 2 dependency edges: f1 -> f2, S1 -> f4 |
2223 | 2187 |
|
2224 | 2188 | let res = schedule_graph(In(Some(params)), &mut world); |
2225 | 2189 | let res2 = res.expect("expect to work"); |
2226 | 2190 | let res3 = serde_json::from_value::<BrpScheduleGraphResponse>(res2).unwrap(); |
2227 | 2191 |
|
2228 | | - assert_eq!(res3.systemsets.len(), 6); |
2229 | | - assert_eq!(res3.hierarchy_nodes.len(), 10); |
2230 | | - assert_eq!(res3.dependency_nodes.len(), 10); |
2231 | | - assert_eq!(res3.hierarchy_edges.len(), 6); |
2232 | | - assert_eq!(res3.dependency_edges.len(), 2); |
| 2192 | + assert_eq!(res3.schedule_data.systems.len(), 5,); |
| 2193 | + assert_eq!(res3.schedule_data.system_sets.len(), 6); |
| 2194 | + assert_eq!(res3.schedule_data.hierarchy.len(), 6); |
| 2195 | + assert_eq!(res3.schedule_data.dependency.len(), 2); |
| 2196 | + // Components are only currently recorded for conflicts - this may change in the future. |
| 2197 | + assert_eq!(res3.schedule_data.components.len(), 0); |
| 2198 | + assert_eq!(res3.schedule_data.conflicts.len(), 0); |
2233 | 2199 | } |
2234 | 2200 | } |
0 commit comments