bevy_archive 0.3.0

An experimental ECS world snapshot system built on Bevy, featuring structured archetype storage and manifest-based serialization.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
use csv::Reader;
use csv::Writer;
use serde::Deserialize;
use serde::Serialize;
use serde_json::Value;
use std::collections::HashMap;
use std::collections::HashSet;
use std::io::{Read, Result as IoResult, Write};

use super::archetype_archive::ArchetypeSnapshot;
use super::archetype_archive::StorageTypeFlag;

#[derive(Debug, Clone)]
pub struct ComponentColumnGroup {
    pub component: String,   // "TestComponentA"
    pub fields: Vec<String>, // ["TestComponentA.value"]
}

#[derive(Debug, Serialize, Deserialize)]
pub struct ColumnarCsv {
    pub headers: Vec<String>,
    pub columns: Vec<Vec<Value>>,
    pub row_index: Vec<u32>,
    #[serde(skip)]
    pub header_index_map: HashMap<String, usize>,
}

impl ColumnarCsv {
    pub fn new(row_count: usize) -> Self {
        Self {
            headers: Vec::new(),
            columns: Vec::new(),
            row_index: (0..row_count as u32).collect(),
            header_index_map: HashMap::new(),
        }
    }

    fn add_column(&mut self, name: String) -> Result<(), String> {
        if self.header_index_map.contains_key(&name) {
            return Err(format!("Column '{}' already exists", name));
        }
        let idx = self.headers.len();
        self.headers.push(name.clone());
        self.header_index_map.insert(name, idx);
        self.columns.push(vec![Value::Null; self.row_index.len()]);
        Ok(())
    }

    fn add_columns<I: IntoIterator<Item = String>>(&mut self, names: I) -> Result<(), String> {
        for n in names {
            self.add_column(n)?;
        }
        Ok(())
    }

    pub fn get_column_mut(&mut self, name: &str) -> Option<&mut Vec<Value>> {
        self.header_index_map
            .get(name)
            .map(|&i| &mut self.columns[i])
    }

    pub fn to_csv<W: Write>(&self, mut w: W) -> IoResult<()> {
        let mut writer = csv::Writer::from_writer(&mut w);
        writer
            .write_record(std::iter::once("id").chain(self.headers.iter().map(|s| s.as_str())))?;

        for (row, &id) in self.row_index.iter().enumerate() {
            let mut record = Vec::with_capacity(self.headers.len() + 1);
            record.push(id.to_string());
            for col in &self.columns {
                let v = &col[row];
                record.push(if v.is_null() {
                    "".into()
                } else {
                    v.to_string()
                });
            }
            writer.write_record(&record)?;
        }
        writer.flush()
    }

    pub fn from_csv<R: Read>(r: R) -> Result<Self, Box<dyn std::error::Error>> {
        let mut reader = csv::Reader::from_reader(r);
        let mut headers: Vec<String> = reader.headers()?.iter().map(|s| s.to_string()).collect();
        assert_eq!(headers.first().map(String::as_str), Some("id"));
        headers.remove(0);

        let mut row_index = Vec::new();
        let mut columns = vec![Vec::new(); headers.len()];

        for rec in reader.records() {
            let rec = rec?;
            row_index.push(rec[0].parse::<u32>()?);
            for (j, f) in rec.iter().skip(1).enumerate() {
                let v = if f.trim().is_empty() {
                    Value::Null
                } else {
                    serde_json::from_str(f).unwrap_or(Value::String(f.to_string()))
                };
                columns[j].push(v);
            }
        }

        let header_index_map = headers
            .iter()
            .enumerate()
            .map(|(i, h)| (h.clone(), i))
            .collect();

        Ok(Self {
            headers,
            columns,
            row_index,
            header_index_map,
        })
    }
}
impl From<&ArchetypeSnapshot> for ColumnarCsv {
    fn from(snap: &ArchetypeSnapshot) -> Self {
        columnar_from_snapshot(snap)
    }
}

fn build_schema(snapshot: &ArchetypeSnapshot, strict: bool) -> Vec<ComponentColumnGroup> {
    snapshot
        .columns
        .iter()
        .zip(snapshot.component_types.iter())
        .map(|(col, comp)| {
            let fields: Vec<String> = if strict {
                // 扫描所有行,收集完整字段集合
                let mut set = HashSet::new();
                for v in col {
                    set.extend(infer_schema(comp, v).fields);
                }
                set.into_iter().collect()
            } else {
                // 只看第一行,假定 schema 固定
                infer_schema(comp, col.first().unwrap()).fields
            };
            ComponentColumnGroup {
                component: comp.clone(),
                fields,
            }
        })
        .collect()
}

pub fn columnar_from_snapshot(snapshot: &ArchetypeSnapshot) -> ColumnarCsv {
    columnar_core(snapshot, true) // strict
}

pub unsafe fn columnar_from_snapshot_unchecked(snapshot: &ArchetypeSnapshot) -> ColumnarCsv {
    columnar_core(snapshot, false) // fast but unsafe
}

fn columnar_core(snapshot: &ArchetypeSnapshot, strict: bool) -> ColumnarCsv {
    let schemas = build_schema(snapshot, strict);

    let mut csv = ColumnarCsv::new(snapshot.entities.len());
    csv.add_columns(schemas.iter().flat_map(|s| s.fields.clone()))
        .unwrap();
    csv.row_index.clone_from(&snapshot.entities());

    // 填充数据
    for (values, schema) in snapshot.columns.iter().zip(schemas) {
        for field in schema.fields {
            let suffix = field
                .strip_prefix(&format!("{}.", schema.component))
                .unwrap_or("");
            let col = csv.get_column_mut(&field).unwrap();
            for (i, item) in values.iter().enumerate() {
                col[i] = if let Value::Object(map) = item {
                    map.get(suffix).cloned().unwrap_or(Value::Null)
                } else {
                    item.clone()
                };
            }
        }
    }
    csv
}

pub fn infer_schema(component: &str, value: &Value) -> ComponentColumnGroup {
    match value {
        Value::Object(map) => {
            let mut fields = Vec::new();
            let mut values = Vec::new();

            for (k, v) in map {
                fields.push(format!("{}.{}", component, k));
                values.push(v.clone());
            }

            ComponentColumnGroup {
                component: component.to_string(),
                fields,
            }
        }
        _other => ComponentColumnGroup {
            component: component.to_string(),
            fields: vec![component.to_string()], // 整体值
        },
    }
}

impl ColumnarCsv {
    pub fn to_csv_writer<W: Write>(&self, w: W) -> IoResult<()> {
        let mut writer = Writer::from_writer(w);

        // 写入 header 行
        writer
            .write_record(std::iter::once("id").chain(self.headers.iter().map(|s| s.as_str())))?;

        let row_count = self.row_index.len();
        for row in 0..row_count {
            let mut record = Vec::with_capacity(self.headers.len() + 1);
            record.push(self.row_index[row].to_string());
            for col in &self.columns {
                let value = &col[row];
                record.push(match value {
                    Value::Null => "".into(),
                    _ => value.to_string(),
                });
            }
            writer.write_record(&record)?;
        }

        writer.flush()
    }
}

impl ColumnarCsv {
    pub fn from_csv_reader<R: Read>(r: R) -> Result<Self, Box<dyn std::error::Error>> {
        let mut reader = Reader::from_reader(r);
        let mut headers = reader
            .headers()?
            .iter()
            .map(|s| s.to_string())
            .collect::<Vec<_>>();
        assert!(headers.first() == Some(&"id".to_string()));

        headers.remove(0); // remove id from header list
        let mut row_index = Vec::new();
        let mut columns = vec![Vec::new(); headers.len()];

        for result in reader.records() {
            let record = result?;
            row_index.push(record.get(0).unwrap().parse::<u32>()?); // 👈 ID 列

            for (j, field) in record.iter().skip(1).enumerate() {
                let value = if field.trim().is_empty() {
                    Value::Null
                } else {
                    serde_json::from_str(field).unwrap_or(Value::String(field.to_string()))
                };
                columns[j].push(value);
            }
        }

        let header_index_map = headers
            .iter()
            .enumerate()
            .map(|(i, h)| (h.clone(), i))
            .collect::<HashMap<_, _>>();

        Ok(Self {
            headers,
            columns,
            row_index,
            header_index_map,
        })
    }
}
fn to_archetype_snapshot(csv: &ColumnarCsv) -> ArchetypeSnapshot {
    let mut component_fields: HashMap<String, Vec<(Option<String>, usize)>> = HashMap::new();

    for (i, header) in csv.headers.iter().enumerate() {
        if let Some((comp, field)) = header.split_once('.') {
            component_fields
                .entry(comp.to_string())
                .or_default()
                .push((Some(field.to_string()), i));
        } else {
            // 整体组件(非结构)
            component_fields
                .entry(header.clone())
                .or_default()
                .push((None, i));
        }
    }

    let mut component_types = Vec::new();
    let mut storage_types = Vec::new();
    let mut columns = Vec::new();
    let entities = csv.row_index.clone();

    for (comp, fields) in component_fields {
        let mut component_column = Vec::new();

        for row in 0..csv.row_index.len() {
            if fields.len() == 1 && fields[0].0.is_none() {
                // 直接是 value
                let col_idx = fields[0].1;
                component_column.push(csv.columns[col_idx][row].clone());
            } else {
                let mut map = serde_json::Map::new();
                for (field_name, col_idx) in &fields {
                    let name = field_name.as_ref().unwrap();
                    map.insert(name.clone(), csv.columns[*col_idx][row].clone());
                }
                component_column.push(Value::Object(map));
            }
        }

        component_types.push(comp);
        storage_types.push(StorageTypeFlag::Table); // default
        columns.push(component_column);
    }

    ArchetypeSnapshot {
        component_types,
        storage_types,
        columns,
        entities,
    }
}

impl From<&ColumnarCsv> for ArchetypeSnapshot {
    fn from(csv: &ColumnarCsv) -> Self {
        to_archetype_snapshot(csv)
    }
}

#[cfg(test)]
mod tests {
    use std::io;

    use super::*;
    use crate::archetype_archive::load_world_arch_snapshot;
    use crate::archetype_archive::save_world_arch_snapshot;
    use crate::bevy_registry::SnapshotRegistry;
    use bevy_ecs::prelude::*;
    use serde::Deserialize;
    use serde::Serialize;
    #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Component)]
    struct TestComponentA {
        pub value: i32,
    }

    #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Component)]
    struct TestComponentB {
        pub value: f32,
    }

    #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Component)]
    struct TestComponentC {
        pub value: String,
    }

    #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Component)]
    struct TestComponentD {
        pub value: bool,
    }

    #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Component)]
    struct TestComponentE(Vec<f64>);
    #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Component)]
    struct TestComponentF(TestComponentC);
    fn init_world() -> (World, SnapshotRegistry) {
        let mut world = World::new();
        let mut registry = SnapshotRegistry::default();

        // 注册组件类型
        registry.register::<TestComponentA>();
        registry.register::<TestComponentB>();
        registry.register::<TestComponentC>();
        registry.register::<TestComponentD>();
        registry.register::<TestComponentE>();
        registry.register::<TestComponentF>();
        // 构建不同组合的 archetype
        for i in 0..10 {
            world.spawn((
                TestComponentA { value: i },
                TestComponentB {
                    value: i as f32 * 0.1,
                },
            ));
            world.spawn((
                TestComponentB {
                    value: i as f32 * 0.2,
                },
                TestComponentC {
                    value: format!("EntityC{}", i),
                },
            ));
            world.spawn((
                TestComponentA { value: i * 2 },
                TestComponentC {
                    value: format!("EntityAC{}", i),
                },
                TestComponentD { value: i % 2 == 0 },
            ));
            world.spawn((
                TestComponentD { value: i % 3 == 0 },
                TestComponentE(vec![i as f64, i as f64 + 1.0]),
            ));
            world.spawn((
                TestComponentA { value: -i },
                TestComponentB {
                    value: -i as f32 * 0.3,
                },
                TestComponentC {
                    value: format!("Combo{}", i),
                },
                TestComponentD { value: i % 5 == 0 },
                TestComponentE(vec![0.0; i as usize % 10 + 1]),
                TestComponentF(TestComponentC {
                    value: format!("Nested{}", i),
                }),
            ));
        }

        (world, registry)
    }

    #[test]
    fn test_csv_archetype_snapshot() {
        let (world, registry) = init_world();
        let snapshot = save_world_arch_snapshot(&world, &registry);
        assert_eq!(snapshot.entities.len(), 10 * 5);
        let csv = unsafe { columnar_from_snapshot_unchecked(&snapshot.archetypes[0]) };
        assert_eq!(csv.headers.len(), snapshot.archetypes[0].columns.len());
        println!("CSV Headers: {:?}", csv.headers);
        println!("CSV Row Index: {:?}", csv.row_index);
        println!("CSV Columns: {:?}", csv.columns);

        csv.to_csv_writer(io::stdout()).unwrap();
    }
    #[test]
    fn test_csv_snapshot_roundtrip() {
        let (mut world, registry) = init_world();
        let mut snapshot = save_world_arch_snapshot(&world, &registry);
        let csv = unsafe { columnar_from_snapshot_unchecked(&snapshot.archetypes[0]) };
        let new_snap: ArchetypeSnapshot = (&csv).into();

        assert_eq!(
            new_snap.entities.len(),
            snapshot.archetypes[0].entities.len()
        );
        snapshot.archetypes[0] = new_snap;
        load_world_arch_snapshot(&mut world, &snapshot, &registry);
    }
    #[test]
    fn test_csv_archetype_snapshot_roundtrip() {
        let (world, registry) = init_world();
        let snapshot = save_world_arch_snapshot(&world, &registry);
        assert_eq!(snapshot.entities.len(), 10 * 5);
        let csv = unsafe { columnar_from_snapshot_unchecked(&snapshot.archetypes[0]) };
        assert_eq!(csv.headers.len(), snapshot.archetypes[0].columns.len());
        println!("CSV Headers: {:?}", csv.headers);
        println!("CSV Row Index: {:?}", csv.row_index);
        println!("CSV Columns: {:?}", csv.columns);
        let mut v = Vec::new();
        csv.to_csv_writer(&mut v).unwrap();
        let new_csv = ColumnarCsv::from_csv_reader(v.as_slice()).unwrap();
        let mut nv = Vec::new();
        new_csv.to_csv_writer(&mut nv).unwrap();
        assert_eq!(nv, v);
    }
}