aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 4a395ad24670dbaeffbed24fa83bcbb2503e04d9 (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
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
#![feature(array_chunks)]
pub mod cache;
pub mod client;
pub mod mesh;

use anyhow::Result;
use cache::Cache;
use clap::Parser;
use client::GeClient;
use futures_util::{StreamExt, stream::FuturesUnordered};
use glam::{DMat4, DVec3};
use mesh::{convert_mesh, decode_normal_table};
use std::{f32::consts::PI, path::PathBuf, pin::Pin, sync::Arc};
use tokio::sync::Semaphore;
use weareshared::{
    Affine3A,
    helper::AABB,
    packets::Resource,
    resources::{Prefab, RespackEntry, SpatialIndex},
    respack::save_full_respack,
    store::ResourceStore,
};

#[derive(Parser)]
struct Args {
    #[arg(short, long, default_value = "16")]
    par_limit: usize,
    #[arg(short, long)]
    db_cache: Option<PathBuf>,
    #[clap(subcommand)]
    action: Action,
}

#[derive(Parser)]
enum Action {
    Cache { level: usize },
    Export { level: usize },
}

#[tokio::main]
async fn main() -> Result<()> {
    env_logger::init_from_env("LOG");

    let args = Args::parse();

    let cache = if let Some(path) = args.db_cache {
        Cache::new_db(&path)?
    } else {
        Cache::new_directory()?
    };
    let c = GeClient::new(16, cache).await?;

    match args.action {
        Action::Cache { level } => {
            let entry = c.planetoid_metdata().await?;
            let epoch = entry.root_node_metadata.unwrap().bulk_metadata_epoch();
            cache_all(
                Arc::new(c),
                Arc::new(Semaphore::new(args.par_limit * 3 / 2)),
                "".to_string(),
                epoch,
                level,
            )
            .await?;
        }
        Action::Export { level } => {
            let entry = c.planetoid_metdata().await?;

            let store = Arc::new(ResourceStore::new_memory());

            let (_, root) = do_node(
                Arc::new(c),
                "".to_string(),
                store.clone(),
                entry.root_node_metadata.unwrap().bulk_metadata_epoch(),
                level,
            )
            .await?;

            let entry = store.set(&RespackEntry {
                c_spatial_index: vec![root],
                ..Default::default()
            })?;
            let file = std::fs::File::create("/tmp/a.respack")?;
            save_full_respack(file, &store, Some(entry))?;
        }
    }

    Ok(())
}

fn do_node(
    c: Arc<GeClient>,
    path: String,
    store: Arc<ResourceStore>,
    epoch: u32,
    level: usize,
) -> Pin<Box<dyn Future<Output = Result<(AABB, Resource<SpatialIndex>)>>>> {
    Box::pin(async move {
        let bulk = c.bulk_metadata(&path, epoch).await?;

        let mut fu = FuturesUnordered::new();
        let mut meshes = Vec::new();
        let mut children = Vec::new();

        for node_meta in &bulk.node_metadata {
            let (cpath, flags) = unpack_path_and_id(node_meta.path_and_flags());
            // eprintln!("{path}+{cpath} {flags:?}");
            let abspath = format!("{path}{cpath}");
            if flags.has_node && cpath.len() == 4 {
                let node = c.node_data(&abspath, flags, &bulk, node_meta).await?;
                let transform = DMat4::from_cols_slice(&node.matrix_globe_from_mesh);
                let for_normals = decode_normal_table(node.for_normals());
                eprintln!(
                    "{:?} {:?}",
                    node_meta.oriented_bounding_box, bulk.head_node_center
                );
                for m in node.meshes {
                    let mesh = convert_mesh(m, &store, &for_normals)?;
                    meshes.push((
                        Affine3A::from_rotation_x(-PI / 2.)
                            * Affine3A::from_mat4((transform / 3_000_000.).as_mat4()),
                        mesh,
                    ))
                }
            }
            if cpath.len() == 4 && flags.has_metadata && abspath.len() < level {
                fu.push(do_node(c.clone(), abspath, store.clone(), epoch, level));
            }
        }

        while let Some(res) = fu.next().await {
            children.push(res?);
        }

        let center = DVec3::from_slice(&bulk.head_node_center).as_vec3();
        let size = 10_000_0000. / (1 << path.len()) as f32;
        let bounds = AABB {
            min: center - size,
            max: center + size,
        };

        let prefab = store.set(&Prefab {
            mesh: meshes,
            ..Default::default()
        })?;
        Ok((
            bounds,
            store.set(&SpatialIndex {
                prefab: Some(prefab),
                child: children,
                ..Default::default()
            })?,
        ))
    })
}

fn cache_all(
    c: Arc<GeClient>,
    par: Arc<Semaphore>,
    path: String,
    epoch: u32,
    level: usize,
) -> Pin<Box<dyn Future<Output = Result<()>>>> {
    Box::pin(async move {
        let _permit = par.acquire().await?;
        let bulk = c.bulk_metadata(&path, epoch).await?;

        let mut fu = FuturesUnordered::new();

        for node_meta in &bulk.node_metadata {
            let (cpath, flags) = unpack_path_and_id(node_meta.path_and_flags());
            let abspath = format!("{path}{cpath}");
            if flags.has_node && abspath.len() < level {
                c.node_data(&abspath, flags, &bulk, node_meta).await?;
            }
            if cpath.len() == 4 && flags.has_metadata && abspath.len() < level {
                fu.push(cache_all(c.clone(), par.clone(), abspath, epoch, level));
            }
        }
        drop(_permit);

        while let Some(res) = fu.next().await {
            res?;
        }
        Ok(())
    })
}

#[derive(Debug, Clone, Copy)]
pub struct Flags {
    has_node: bool,
    has_metadata: bool,
    use_image_epoch: bool,
}

fn unpack_path_and_id(mut path_id: u32) -> (String, Flags) {
    let level = 1 + (path_id & 3);
    path_id >>= 2;
    let mut path = String::new();
    for _ in 0..level {
        path += &(path_id & 7).to_string();
        path_id >>= 3;
    }
    let flags = path_id;
    (
        path,
        Flags {
            has_node: flags & 8 == 0,
            has_metadata: flags & 4 == 0,
            use_image_epoch: flags & 16 != 0,
        },
    )
}

pub mod proto {
    include!(concat!(env!("OUT_DIR"), "/earth.proto.rs"));
}