summaryrefslogtreecommitdiff
path: root/world/src/main.rs
blob: 28a4ec4dba92792182f302d1c2097439e416ef39 (plain)
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
use anyhow::Result;
use clap::Parser;
use std::{
    net::{SocketAddr, TcpStream},
    path::PathBuf,
};
use weareshared::{
    packets::{Object, Packet},
    resources::{Part, Prefab, VertexAttributes},
    store::ResourceStore,
};

#[derive(Parser)]
struct Args {
    address: SocketAddr,
    scene: PathBuf,
}

fn main() -> Result<()> {
    env_logger::init_from_env("LOG");
    let args = Args::parse();

    let mut sock = TcpStream::connect(args.address)?;
    let store = ResourceStore::new_memory();

    let (gltf, buffers, _) = gltf::import(args.scene)?;

    let mut parts = Vec::new();
    for node in gltf.nodes() {
        if let Some(mesh) = node.mesh() {
            for p in mesh.primitives() {
                let reader = p.reader(|buf| Some(&buffers[buf.index()]));
                let mut attrs = vec![vec![]; 3];
                for p in reader.read_positions().unwrap() {
                    attrs[0].push(p[0]);
                    attrs[1].push(p[1]);
                    attrs[2].push(p[2]);
                }
                let part = Part {
                    vertex: attrs
                        .into_iter()
                        .map(|d| {
                            let mut out = Vec::new();
                            VertexAttributes(d).serialize(&mut out)?;
                            store.set(&out)
                        })
                        .collect::<Result<Vec<_>, _>>()?,
                    ..Part::default()
                };
                let mut out = Vec::new();
                part.serialize(&mut out)?;
                parts.push(store.set(&out)?);
            }
        }
    }

    let mut out = Vec::new();
    Prefab(parts).serialize(&mut out)?;
    let prefab = store.set(&out)?;

    Packet::Add(Object::new(), prefab).serialize(&mut sock)?;

    loop {
        let packet = Packet::deserialize(&mut sock)?;
        match packet {
            Packet::RequestResource(hash) => {
                if let Some(d) = store.get(hash)? {
                    Packet::RespondResource(d).serialize(&mut sock)?;
                }
            }
            _ => (),
        }
    }
}