bevy_yoleck/entity_upgrading.rs
1use std::collections::BTreeMap;
2
3use bevy::prelude::*;
4
5use crate::YoleckRawLevel;
6
7/// Support upgrading of entities when the layout of the Yoleck entities and components change.
8///
9/// ```no_run
10/// # use bevy::prelude::*;
11/// # use bevy_yoleck::prelude::*;
12/// # let mut app = App::new();
13/// app.add_plugins(YoleckEntityUpgradingPlugin {
14/// app_format_version: 5,
15/// });
16///
17/// // The newest upgrade, from 4 to 5
18/// app.add_yoleck_entity_upgrade_for(5, "Foo", |data| {
19/// let mut old_data = data.remove("OldFooComponent").unwrap();
20/// data["NewFooComponent"] = old_data;
21/// });
22///
23/// // Some older upgrade, from 2 to 3
24/// app.add_yoleck_entity_upgrade(3, |_type_name, data| {
25/// if let Some(component_data) = data.get_mut("Bar") {
26/// component_data["some_new_field"] = 42.into();
27/// }
28/// });
29/// ```
30pub struct YoleckEntityUpgradingPlugin {
31 /// The current version of the app data.
32 ///
33 /// If `YoleckEntityUpgradingPlugin` is not added, the current version is considered 0
34 /// ("unversioned")
35 pub app_format_version: usize,
36}
37
38impl Plugin for YoleckEntityUpgradingPlugin {
39 fn build(&self, app: &mut App) {
40 app.insert_resource(YoleckEntityUpgrading {
41 app_format_version: self.app_format_version,
42 upgrade_functions: Default::default(),
43 });
44 }
45}
46
47#[derive(Resource)]
48pub(crate) struct YoleckEntityUpgrading {
49 pub app_format_version: usize,
50 #[allow(clippy::type_complexity)]
51 pub upgrade_functions: BTreeMap<
52 usize,
53 Vec<
54 Box<
55 dyn 'static
56 + Send
57 + Sync
58 + Fn(&str, &mut serde_json::Map<String, serde_json::Value>),
59 >,
60 >,
61 >,
62}
63
64impl YoleckEntityUpgrading {
65 pub fn upgrade_raw_level_file(&self, levels_file: &mut YoleckRawLevel) {
66 let first_target_version = levels_file.0.app_format_version + 1;
67 for (target_version, upgrade_functions) in
68 self.upgrade_functions.range(first_target_version..)
69 {
70 for entity in levels_file.2.iter_mut() {
71 for function in upgrade_functions.iter() {
72 function(&entity.header.type_name, &mut entity.data);
73 }
74 }
75 levels_file.0.app_format_version = *target_version;
76 }
77 }
78}