1use std::collections::BTreeSet;
2
3use bevy::asset::io::Reader;
4use bevy::asset::{AssetLoader, LoadContext};
5use bevy::platform::collections::HashMap;
6use bevy::prelude::*;
7use bevy::reflect::TypePath;
8use serde::{Deserialize, Serialize};
9use uuid::Uuid;
10
11use crate::editor::YoleckEditorState;
12use crate::entity_upgrading::YoleckEntityUpgrading;
13use crate::errors::YoleckAssetLoaderError;
14use crate::level_files_upgrading::upgrade_level_file;
15use crate::populating::PopulateReason;
16use crate::prelude::{YoleckEntityUuid, YoleckUuidRegistry};
17use crate::{
18 YoleckBelongsToLevel, YoleckEntityConstructionSpecs, YoleckEntityLifecycleStatus,
19 YoleckLevelJustLoaded, YoleckManaged, YoleckSchedule, YoleckState,
20};
21
22#[derive(Serialize, Deserialize, Debug, Clone)]
24pub struct YoleckEntryHeader {
25 #[serde(rename = "type")]
26 pub type_name: String,
27 #[serde(default)]
31 pub name: String,
32
33 #[serde(default, skip_serializing_if = "Option::is_none")]
38 pub uuid: Option<Uuid>,
39}
40
41#[derive(Component, Debug, Clone)]
43pub struct YoleckRawEntry {
44 pub header: YoleckEntryHeader,
45 pub data: serde_json::Value,
46}
47
48impl Serialize for YoleckRawEntry {
49 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
50 where
51 S: serde::Serializer,
52 {
53 (&self.header, &self.data).serialize(serializer)
54 }
55}
56
57impl<'de> Deserialize<'de> for YoleckRawEntry {
58 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
59 where
60 D: serde::Deserializer<'de>,
61 {
62 let (header, data): (YoleckEntryHeader, serde_json::Value) =
63 Deserialize::deserialize(deserializer)?;
64 Ok(Self { header, data })
65 }
66}
67
68pub(crate) fn yoleck_process_raw_entries(
69 editor_state: Res<State<YoleckEditorState>>,
70 mut commands: Commands,
71 mut raw_entries_query: Query<(Entity, &mut YoleckRawEntry), With<YoleckBelongsToLevel>>,
72 construction_specs: Res<YoleckEntityConstructionSpecs>,
73 mut uuid_registry: ResMut<YoleckUuidRegistry>,
74) {
75 let mut entities_by_type = HashMap::<String, Vec<Entity>>::new();
76 for (entity, mut raw_entry) in raw_entries_query.iter_mut() {
77 entities_by_type
78 .entry(raw_entry.header.type_name.clone())
79 .or_default()
80 .push(entity);
81 let mut cmd = commands.entity(entity);
82 cmd.remove::<YoleckRawEntry>();
83
84 let mut components_data = HashMap::new();
85
86 if let Some(entity_type_info) =
87 construction_specs.get_entity_type_info(&raw_entry.header.type_name)
88 {
89 if entity_type_info.has_uuid {
90 let uuid = raw_entry.header.uuid.unwrap_or_else(Uuid::new_v4);
91 cmd.insert(YoleckEntityUuid(uuid));
92 uuid_registry.0.insert(uuid, cmd.id());
93 }
94 for component_name in entity_type_info.components.iter() {
95 let Some(handler) = construction_specs.component_handlers.get(component_name)
96 else {
97 error!("Component type {:?} is not registered", component_name);
98 continue;
99 };
100 let raw_component_data = raw_entry
101 .data
102 .get_mut(handler.key())
103 .map(|component_data| component_data.take());
104 handler.init_in_entity(raw_component_data, &mut cmd, &mut components_data);
105 }
106 for dlg in entity_type_info.on_init.iter() {
107 dlg(*editor_state.get(), &mut cmd);
108 }
109 } else {
110 error!("Entity type {:?} is not registered", raw_entry.header.name);
111 }
112
113 cmd.insert(YoleckManaged {
114 name: raw_entry.header.name.to_owned(),
115 type_name: raw_entry.header.type_name.to_owned(),
116 lifecycle_status: YoleckEntityLifecycleStatus::JustCreated,
117 components_data,
118 });
119 }
120}
121
122pub(crate) fn yoleck_prepare_populate_schedule(
123 mut query: Query<(Entity, &mut YoleckManaged)>,
124 mut entities_to_populate: ResMut<EntitiesToPopulate>,
125 mut yoleck_state: Option<ResMut<YoleckState>>,
126 editor_state: Res<State<YoleckEditorState>>,
127) {
128 entities_to_populate.0.clear();
129 let mut level_needs_saving = false;
130 for (entity, mut yoleck_managed) in query.iter_mut() {
131 match yoleck_managed.lifecycle_status {
132 YoleckEntityLifecycleStatus::Synchronized => {}
133 YoleckEntityLifecycleStatus::JustCreated => {
134 let populate_reason = match editor_state.get() {
135 YoleckEditorState::EditorActive => PopulateReason::EditorInit,
136 YoleckEditorState::GameActive => PopulateReason::RealGame,
137 };
138 entities_to_populate.0.push((entity, populate_reason));
139 }
140 YoleckEntityLifecycleStatus::JustChanged => {
141 entities_to_populate
142 .0
143 .push((entity, PopulateReason::EditorUpdate));
144 level_needs_saving = true;
145 }
146 }
147 yoleck_managed.lifecycle_status = YoleckEntityLifecycleStatus::Synchronized;
148 }
149 if level_needs_saving {
150 if let Some(yoleck_state) = yoleck_state.as_mut() {
151 yoleck_state.level_needs_saving = true;
152 }
153 }
154}
155
156pub(crate) fn yoleck_run_populate_schedule(world: &mut World) {
157 world.run_schedule(YoleckSchedule::Populate);
158 world.run_schedule(YoleckSchedule::OverrideCommonComponents);
159}
160
161#[derive(Resource)]
162pub(crate) struct EntitiesToPopulate(pub Vec<(Entity, PopulateReason)>);
163
164pub(crate) fn process_loading_command(
165 query: Query<(Entity, &YoleckLoadLevel)>,
166 mut raw_levels_assets: ResMut<Assets<YoleckRawLevel>>,
167 entity_upgrading: Option<Res<YoleckEntityUpgrading>>,
168 mut commands: Commands,
169) {
170 for (level_entity, load_level) in query.iter() {
171 if let Some(raw_level) = raw_levels_assets.get_mut(&load_level.0) {
172 if let Some(entity_upgrading) = &entity_upgrading {
173 entity_upgrading.upgrade_raw_level_file(raw_level);
174 }
175 commands
176 .entity(level_entity)
177 .remove::<YoleckLoadLevel>()
178 .insert((YoleckLevelJustLoaded, YoleckKeepLevel));
179 for entry in raw_level.entries() {
180 commands.spawn((
181 entry.clone(),
182 YoleckBelongsToLevel {
183 level: level_entity,
184 },
185 ));
186 }
187 }
188 }
189}
190
191pub(crate) fn yoleck_run_level_loaded_schedule(world: &mut World) {
192 world.run_schedule(YoleckSchedule::LevelLoaded);
193}
194
195pub(crate) fn yoleck_remove_just_loaded_marker_from_levels(
196 query: Query<Entity, With<YoleckLevelJustLoaded>>,
197 mut commands: Commands,
198) {
199 for level_entity in query.iter() {
200 commands
201 .entity(level_entity)
202 .remove::<YoleckLevelJustLoaded>();
203 }
204}
205
206pub(crate) fn process_unloading_command(
207 mut removed_levels: RemovedComponents<YoleckKeepLevel>,
208 level_owned_entities_query: Query<(Entity, &YoleckBelongsToLevel)>,
209 mut commands: Commands,
210) {
211 if removed_levels.is_empty() {
212 return;
213 }
214 let removed_levels: BTreeSet<Entity> = removed_levels.read().collect();
215 for (entity, belongs_to_level) in level_owned_entities_query.iter() {
216 if removed_levels.contains(&belongs_to_level.level) {
217 commands.entity(entity).despawn();
218 }
219 }
220}
221
222#[derive(Component)]
250pub struct YoleckLoadLevel(pub Handle<YoleckRawLevel>);
251
252#[derive(Component)]
259pub struct YoleckKeepLevel;
260
261pub(crate) struct YoleckLevelAssetLoader;
262
263#[derive(Asset, TypePath, Debug, Serialize, Deserialize, Clone)]
265pub struct YoleckRawLevel(
266 pub(crate) YoleckRawLevelHeader,
267 serde_json::Value, pub(crate) Vec<YoleckRawEntry>,
269);
270
271#[derive(Debug, Serialize, Deserialize, Clone)]
273pub struct YoleckRawLevelHeader {
274 format_version: usize,
275 pub app_format_version: usize,
276}
277
278impl YoleckRawLevel {
279 pub fn new(
280 app_format_version: usize,
281 entries: impl IntoIterator<Item = YoleckRawEntry>,
282 ) -> Self {
283 Self(
284 YoleckRawLevelHeader {
285 format_version: 2,
286 app_format_version,
287 },
288 serde_json::Value::Object(Default::default()),
289 entries.into_iter().collect(),
290 )
291 }
292
293 pub fn entries(&self) -> &[YoleckRawEntry] {
294 &self.2
295 }
296
297 pub fn into_entries(self) -> impl Iterator<Item = YoleckRawEntry> {
298 self.2.into_iter()
299 }
300}
301
302impl AssetLoader for YoleckLevelAssetLoader {
303 type Asset = YoleckRawLevel;
304 type Settings = ();
305 type Error = YoleckAssetLoaderError;
306
307 fn extensions(&self) -> &[&str] {
308 &["yol"]
309 }
310
311 async fn load(
312 &self,
313 reader: &mut dyn Reader,
314 _settings: &Self::Settings,
315 _load_context: &mut LoadContext<'_>,
316 ) -> Result<Self::Asset, Self::Error> {
317 let mut bytes = Vec::new();
318 reader.read_to_end(&mut bytes).await?;
319 let json = std::str::from_utf8(&bytes)?;
320 let level: serde_json::Value = serde_json::from_str(json)?;
321 let level = upgrade_level_file(level)?;
322 let level: YoleckRawLevel = serde_json::from_value(level)?;
323 Ok(level)
324 }
325}