diff options
-rw-r--r-- | Cargo.lock | 302 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | a.md | 39 | ||||
-rw-r--r-- | server/Cargo.toml | 2 | ||||
-rw-r--r-- | server/src/main.rs | 7 | ||||
-rw-r--r-- | shared/Cargo.toml | 2 | ||||
-rw-r--r-- | shared/src/lib.rs | 2 | ||||
-rw-r--r-- | shared/src/packets.rs | 26 | ||||
-rw-r--r-- | shared/src/resources.rs | 141 | ||||
-rw-r--r-- | shared/src/store.rs | 14 | ||||
-rw-r--r-- | world/Cargo.toml | 12 | ||||
-rw-r--r-- | world/src/main.rs | 39 |
12 files changed, 536 insertions, 52 deletions
@@ -19,6 +19,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c71b1793ee61086797f5c80b6efa2b8ffa6d5dd703f118545808a7f2e27f7046" [[package]] +name = "adler2" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" + +[[package]] name = "ahash" version = "0.8.11" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -212,6 +218,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" [[package]] +name = "base64" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" + +[[package]] name = "bincode" version = "1.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -272,6 +284,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a" [[package]] +name = "block-buffer" +version = "0.11.0-rc.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fd016a0ddc7cb13661bf5576073ce07330a693f8608a1320b4e20561cc12cdc" +dependencies = [ + "hybrid-array", +] + +[[package]] name = "block2" version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -293,6 +314,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ef657dfab802224e671f5818e9a4935f9b1957ed18e58292690cc39e7a4092a3" [[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "byteorder-lite" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f1fe948ff07f4bd06c30984e69f5b4899c516a3ef74f34df92a2df2ab535495" + +[[package]] name = "bytes" version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -455,6 +488,12 @@ dependencies = [ ] [[package]] +name = "const-oid" +version = "0.10.0-rc.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68ff6be19477a1bd5441f382916a89bc2a0b2c35db6d41e0f6e8538bf6d6463f" + +[[package]] name = "core-foundation" version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -538,12 +577,41 @@ dependencies = [ ] [[package]] +name = "cpufeatures" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16b80225097f2e5ae4e7179dd2266824648f3e2f49d9134d584b76389d31c4c3" +dependencies = [ + "libc", +] + +[[package]] +name = "crc32fast" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3" +dependencies = [ + "cfg-if", +] + +[[package]] name = "crossbeam-utils" version = "0.8.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" [[package]] +name = "crypto-common" +version = "0.2.0-rc.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0b8ce8218c97789f16356e7896b3714f26c2ee1079b79c0b7ae7064bb9089fa" +dependencies = [ + "getrandom", + "hybrid-array", + "rand_core", +] + +[[package]] name = "cursor-icon" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -556,6 +624,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c87e182de0887fd5361989c677c4e8f5000cd9491d6d563161a8f3a5519fc7f" [[package]] +name = "digest" +version = "0.11.0-pre.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf2e3d6615d99707295a9673e889bf363a04b2a466bd320c65a72536f7577379" +dependencies = [ + "block-buffer", + "const-oid", + "crypto-common", +] + +[[package]] name = "dispatch" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -637,6 +716,25 @@ dependencies = [ ] [[package]] +name = "fdeflate" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e6853b52649d4ac5c0bd02320cddc5ba956bdb407c4b75a2c6b75bf51500f8c" +dependencies = [ + "simd-adler32", +] + +[[package]] +name = "flate2" +version = "1.0.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c936bfdafb507ebbf50b8074c54fa31c5be9a1e7e5f467dd659697041407d07c" +dependencies = [ + "crc32fast", + "miniz_oxide", +] + +[[package]] name = "foldhash" version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -729,6 +827,45 @@ dependencies = [ ] [[package]] +name = "gltf" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3ce1918195723ce6ac74e80542c5a96a40c2b26162c1957a5cd70799b8cacf7" +dependencies = [ + "base64", + "byteorder", + "gltf-json", + "image", + "lazy_static", + "serde_json", + "urlencoding", +] + +[[package]] +name = "gltf-derive" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14070e711538afba5d6c807edb74bcb84e5dbb9211a3bf5dea0dfab5b24f4c51" +dependencies = [ + "inflections", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "gltf-json" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6176f9d60a7eab0a877e8e96548605dedbde9190a7ae1e80bbcc1c9af03ab14" +dependencies = [ + "gltf-derive", + "serde", + "serde_derive", + "serde_json", +] + +[[package]] name = "glutin_wgl_sys" version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -822,6 +959,29 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" [[package]] +name = "hybrid-array" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2d35805454dc9f8662a98d6d61886ffe26bd465f5960e0e55345c70d5c0d2a9" +dependencies = [ + "typenum", +] + +[[package]] +name = "image" +version = "0.25.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd6f44aed642f18953a158afeb30206f4d50da59fbc66ecb53c66488de73563b" +dependencies = [ + "bytemuck", + "byteorder-lite", + "num-traits", + "png", + "zune-core", + "zune-jpeg", +] + +[[package]] name = "indexmap" version = "2.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -832,6 +992,12 @@ dependencies = [ ] [[package]] +name = "inflections" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a257582fdcde896fd96463bf2d40eefea0580021c0712a0e2b028b60b47a837a" + +[[package]] name = "is_terminal_polyfill" version = "1.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -847,6 +1013,12 @@ dependencies = [ ] [[package]] +name = "itoa" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" + +[[package]] name = "jni" version = "0.21.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -905,6 +1077,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2db585e1d738fc771bf08a151420d3ed193d9d895a36df7f6f8a9456b911ddc" [[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] name = "libc" version = "0.2.169" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1014,6 +1192,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" [[package]] +name = "miniz_oxide" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ffbe83022cedc1d264172192511ae958937694cd57ce297164951b8b3568394" +dependencies = [ + "adler2", + "simd-adler32", +] + +[[package]] name = "naga" version = "23.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1465,6 +1653,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" [[package]] +name = "png" +version = "0.17.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82151a2fc869e011c153adc57cf2789ccb8d9906ce52c0b39a6b5697749d7526" +dependencies = [ + "bitflags 1.3.2", + "crc32fast", + "fdeflate", + "flate2", + "miniz_oxide", +] + +[[package]] name = "polling" version = "3.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1534,6 +1735,15 @@ dependencies = [ ] [[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] name = "range-alloc" version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1627,6 +1837,12 @@ dependencies = [ ] [[package]] +name = "ryu" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" + +[[package]] name = "same-file" version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1681,15 +1897,26 @@ dependencies = [ ] [[package]] -name = "server" -version = "0.1.0" +name = "serde_json" +version = "1.0.134" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d00f4175c42ee48b15416f6193a959ba3a0d67fc699a0db9ad12df9f83991c7d" dependencies = [ - "anyhow", - "clap", - "env_logger", - "log", - "weareshared", - "xdg", + "itoa", + "memchr", + "ryu", + "serde", +] + +[[package]] +name = "sha2" +version = "0.11.0-pre.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "540c0893cce56cdbcfebcec191ec8e0f470dd1889b6e7a0b503e310a94a168f5" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", ] [[package]] @@ -1699,6 +1926,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" [[package]] +name = "simd-adler32" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d66dc143e6b11c1eddc06d5c423cfc97062865baf299914ab64caa38182078fe" + +[[package]] name = "slab" version = "0.4.9" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1888,6 +2121,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2df906b07856748fa3f6e0ad0cbaa047052d4a7dd609e231c4f72cee8c36f31" [[package]] +name = "typenum" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" + +[[package]] name = "unicode-ident" version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1912,6 +2151,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" [[package]] +name = "urlencoding" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da" + +[[package]] name = "utf8parse" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2131,13 +2376,39 @@ dependencies = [ ] [[package]] +name = "weareserver" +version = "0.1.0" +dependencies = [ + "anyhow", + "clap", + "env_logger", + "log", + "weareshared", + "xdg", +] + +[[package]] name = "weareshared" version = "0.1.0" dependencies = [ "anyhow", "bincode", "glam", + "log", "redb", + "sha2", +] + +[[package]] +name = "weareworld" +version = "0.1.0" +dependencies = [ + "anyhow", + "clap", + "env_logger", + "gltf", + "log", + "weareshared", ] [[package]] @@ -2722,3 +2993,18 @@ dependencies = [ "quote", "syn", ] + +[[package]] +name = "zune-core" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f423a2c17029964870cfaabb1f13dfab7d092a62a29a89264f4d36990ca414a" + +[[package]] +name = "zune-jpeg" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99a5bab8d7dedf81405c4bb1f2b83ea057643d9cb28778cea9eecddeedd2e028" +dependencies = [ + "zune-core", +] @@ -1,3 +1,3 @@ [workspace] -members = ["shared", "server", "client"] +members = ["shared", "server", "client", "world"] resolver = "3" @@ -2,9 +2,9 @@ ```rs type Obj = u128 -type Res = u128 +type Res = [u8; 32] 01 request_resource(name: Res) -02 respond_resource(name: Res, data: Vec<u8>) +02 respond_resource(data: Vec<u8>) 03 add(id: Obj, prefab: Res) 04 remove(id: Obj) 05 position(id: Obj, pos: Vec3, rot: Vec3) @@ -17,30 +17,25 @@ type Res = u128 ### Prefab -```ts -type Prefab = Part[]; ``` - -### Part - -```ts -type Part = { - mesh?: Mesh; - armature?: Armature; - fragment_shader?: Shader; - fragment_shader_data?: ShaderData; - vertex_shader?: Shader; - vertex_shader_data?: ShaderData; - texture?: Texture; -}; +([part])* ``` -### Mesh +### Part ``` -[vertex count:u16] [index count:u16] ([x:f32] [y:f32] [z:f32] [u:f32] [v:f32])* ([n:u16])* +([len_key:u8] [len_value:u8] ([key:u8])* ([value:u8])*)* ``` +- vertex_attributes: `[Res<[f32]>]` +- index: `Res<[[u16;3]]>` +- armature: ???? +- fragment_shader: Shader +- fragment_shader_data: `Res<[f32]>` +- vertex_shader: Shader +- vertex_shader_data: `Res<[f32]>` +- texture: Texture + ### Texture WebP @@ -49,12 +44,6 @@ WebP glsl source, todo -### ShaderData - -``` -([param:f32])* -``` - ## Player tree - LowerTorso (2x leg tilt) diff --git a/server/Cargo.toml b/server/Cargo.toml index 3a091bd..9f7f37a 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "server" +name = "weareserver" version = "0.1.0" edition = "2024" diff --git a/server/src/main.rs b/server/src/main.rs index 02ea03f..6b57d18 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -96,13 +96,14 @@ impl State { match packet { Packet::RequestResource(resource) => { if let Some(r) = self.store.get(resource)? { - self.send(conn, Packet::RespondResource(resource, r))?; + self.send(conn, Packet::RespondResource(r))?; } else { self.broadcast(Packet::RequestResource(resource))?; } } - Packet::RespondResource(resource, vec) => { - self.broadcast(Packet::RespondResource(resource, vec))?; + Packet::RespondResource(data) => { + self.store.set(&data)?; + self.broadcast(Packet::RespondResource(data))?; } Packet::Add(object, resource) => { self.broadcast(Packet::Add(object, resource))?; diff --git a/shared/Cargo.toml b/shared/Cargo.toml index c39f586..6c37d07 100644 --- a/shared/Cargo.toml +++ b/shared/Cargo.toml @@ -8,3 +8,5 @@ anyhow = "1.0.95" bincode = "1.3.3" glam = { version = "0.29.2", features = ["serde"] } redb = "2.4.0" +sha2 = "0.11.0-pre.4" +log = "0.4.22" diff --git a/shared/src/lib.rs b/shared/src/lib.rs index a692a82..fadbc35 100644 --- a/shared/src/lib.rs +++ b/shared/src/lib.rs @@ -1,4 +1,6 @@ +#![feature(iter_array_chunks)] pub mod packets; +pub mod resources; pub mod store; pub mod tree; diff --git a/shared/src/packets.rs b/shared/src/packets.rs index 79dd1ea..c480ba8 100644 --- a/shared/src/packets.rs +++ b/shared/src/packets.rs @@ -3,14 +3,14 @@ use glam::Vec3; use std::io::{Read, Write}; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct Resource(pub u128); +pub struct Resource(pub [u8; 32]); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct Object(pub u128); #[derive(Debug, Clone)] pub enum Packet { RequestResource(Resource), - RespondResource(Resource, Vec<u8>), + RespondResource(Vec<u8>), Add(Object, Resource), Remove(Object), Position(Object, Vec3, Vec3), @@ -24,18 +24,17 @@ impl Packet { match self { Packet::RequestResource(resource) => { w.write_all(&[0x01])?; - w.write_all(&resource.0.to_be_bytes())?; + w.write_all(&resource.0)?; } - Packet::RespondResource(resource, vec) => { + Packet::RespondResource(data) => { w.write_all(&[0x02])?; - w.write_all(&resource.0.to_be_bytes())?; - w.write_all(&(vec.len() as u16).to_be_bytes())?; - w.write_all(&vec)?; + w.write_all(&(data.len() as u16).to_be_bytes())?; + w.write_all(&data)?; } Packet::Add(object, resource) => { w.write_all(&[0x03])?; w.write_all(&object.0.to_be_bytes())?; - w.write_all(&resource.0.to_be_bytes())?; + w.write_all(&resource.0)?; } Packet::Remove(object) => { w.write_all(&[0x04])?; @@ -73,9 +72,9 @@ impl Packet { let mut tag = [0u8; 1]; r.read_exact(&mut tag)?; Ok(match tag[0] { - 0x01 => Packet::RequestResource(Resource(read_u128(r)?)), - 0x02 => Packet::RespondResource(Resource(read_u128(r)?), read_data(r)?), - 0x03 => Packet::Add(Object(read_u128(r)?), Resource(read_u128(r)?)), + 0x01 => Packet::RequestResource(read_res(r)?), + 0x02 => Packet::RespondResource(read_data(r)?), + 0x03 => Packet::Add(Object(read_u128(r)?), read_res(r)?), 0x04 => Packet::Remove(Object(read_u128(r)?)), 0x05 => Packet::Position( Object(read_u128(r)?), @@ -95,6 +94,11 @@ fn read_u128(r: &mut impl Read) -> Result<u128> { r.read_exact(&mut buf)?; Ok(u128::from_be_bytes(buf)) } +fn read_res(r: &mut impl Read) -> Result<Resource> { + let mut buf = [0; 32]; + r.read_exact(&mut buf)?; + Ok(Resource(buf)) +} fn read_data(r: &mut impl Read) -> Result<Vec<u8>> { let mut size = [0; 2]; r.read_exact(&mut size)?; diff --git a/shared/src/resources.rs b/shared/src/resources.rs new file mode 100644 index 0000000..c54d10b --- /dev/null +++ b/shared/src/resources.rs @@ -0,0 +1,141 @@ +use crate::packets::Resource; +use anyhow::{Result, anyhow}; +use log::warn; +use std::io::{Read, Write}; + +#[derive(Debug, Default, Clone)] +pub struct Prefab(pub Vec<Resource>); + +#[derive(Debug, Default, Clone)] +pub struct Part { + pub vertex: Vec<Resource>, + pub index: Option<Resource>, + pub armature: Option<Resource>, + pub fragment_shader: Option<Resource>, + pub fragment_shader_data: Option<Resource>, + pub vertex_shader: Option<Resource>, + pub vertex_shader_data: Option<Resource>, + pub texture: Option<Resource>, +} + +pub struct VertexAttributes(Vec<f32>); +pub struct Indecies(Vec<[u16; 3]>); + +impl Prefab { + pub fn serialize(&self, w: &mut Vec<u8>) -> Result<()> { + for x in self.0.clone() { + w.write_all(&x.0)?; + } + Ok(()) + } + pub fn deserialize(r: &[u8]) -> Result<Self> { + let mut s = Prefab::default(); + for x in r.iter().array_chunks::<32>() { + s.0.push(Resource(x.map(|x| *x))) + } + Ok(s) + } +} +impl Indecies { + pub fn serialize(&self, w: &mut Vec<u8>) -> Result<()> { + for x in self.0.clone() { + w.write_all(x.map(|x| x.to_be_bytes()).as_flattened())?; + } + Ok(()) + } + pub fn deserialize(r: &[u8]) -> Result<Self> { + let mut s = Self(Vec::new()); + for x in r.iter().array_chunks::<2>().array_chunks::<3>() { + s.0.push(x.map(|x| u16::from_be_bytes(x.map(|x| *x)))) + } + Ok(s) + } +} +impl VertexAttributes { + pub fn serialize(&self, w: &mut Vec<u8>) -> Result<()> { + for x in self.0.clone() { + w.write_all(&x.to_be_bytes())?; + } + Ok(()) + } + pub fn deserialize(r: &[u8]) -> Result<Self> { + let mut s = Self(Vec::new()); + for x in r.iter().array_chunks::<4>() { + s.0.push(f32::from_be_bytes(x.map(|x| *x))) + } + Ok(s) + } +} +impl Part { + pub fn serialize(&self, w: &mut Vec<u8>) -> Result<()> { + for x in &self.vertex { + write_kv(w, b"vertex", &x.0)?; + } + if let Some(x) = &self.index { + write_kv(w, b"index", &x.0)?; + } + if let Some(x) = &self.armature { + write_kv(w, b"armature", &x.0)?; + } + if let Some(x) = &self.fragment_shader { + write_kv(w, b"fragment_shader", &x.0)?; + } + if let Some(x) = &self.fragment_shader_data { + write_kv(w, b"fragment_shader_data", &x.0)?; + } + if let Some(x) = &self.vertex_shader { + write_kv(w, b"vertex_shader", &x.0)?; + } + if let Some(x) = &self.vertex_shader_data { + write_kv(w, b"vertex_shader_data", &x.0)?; + } + if let Some(x) = &self.texture { + write_kv(w, b"texture", &x.0)?; + } + Ok(()) + } + pub fn deserialize(mut r: &[u8]) -> Result<Self> { + let mut s = Self::default(); + while !r.is_empty() { + let (k, v) = read_kv(&mut r)?; + match k.as_slice() { + b"vertex" => s.vertex.push(slice_to_res(&v)?), + b"index" => s.index = Some(slice_to_res(&v)?), + b"armature" => s.armature = Some(slice_to_res(&v)?), + b"fragment_shader" => s.fragment_shader = Some(slice_to_res(&v)?), + b"fragment_shader_data" => s.fragment_shader_data = Some(slice_to_res(&v)?), + b"vertex_shader" => s.vertex_shader = Some(slice_to_res(&v)?), + b"vertex_shader_data" => s.vertex_shader_data = Some(slice_to_res(&v)?), + b"texture" => s.texture = Some(slice_to_res(&v)?), + _ => warn!("unknown part key"), + } + } + Ok(s) + } +} +fn slice_to_res(s: &[u8]) -> Result<Resource> { + Ok(Resource( + s.try_into() + .map_err(|_| anyhow!("resource length incorrect"))?, + )) +} +fn read_kv(r: &mut &[u8]) -> Result<(Vec<u8>, Vec<u8>)> { + let mut key_size = [0; 2]; + let mut value_size = [0; 2]; + r.read_exact(&mut key_size)?; + r.read_exact(&mut value_size)?; + let key_size = u16::from_be_bytes(key_size); + let value_size = u16::from_be_bytes(value_size); + let mut key = vec![0; key_size as usize]; + let mut value = vec![0; value_size as usize]; + r.read_exact(&mut value)?; + r.read_exact(&mut key)?; + Ok((key, value)) +} +fn write_kv(w: &mut Vec<u8>, key: &[u8], value: &[u8]) -> Result<()> { + w.write_all(&(key.len() as u16).to_be_bytes())?; + w.write_all(&(value.len() as u16).to_be_bytes())?; + w.write_all(key)?; + w.write_all(value)?; + Ok(()) +} diff --git a/shared/src/store.rs b/shared/src/store.rs index 83f1a25..44e5e3d 100644 --- a/shared/src/store.rs +++ b/shared/src/store.rs @@ -1,9 +1,10 @@ use crate::packets::Resource; use anyhow::Result; use redb::{Database, TableDefinition}; +use sha2::{Digest, Sha256}; use std::path::Path; -const T_ENTRIES: TableDefinition<u128, &[u8]> = TableDefinition::new("e"); +const T_ENTRIES: TableDefinition<[u8; 32], &[u8]> = TableDefinition::new("e"); pub struct ResourceStore { db: Database, @@ -22,12 +23,19 @@ impl ResourceStore { None => Ok(None), } } - pub fn set(&self, key: Resource, value: &[u8]) -> Result<()> { + pub fn set(&self, value: &[u8]) -> Result<()> { + let key = sha256(value); let txn = self.db.begin_write()?; let mut ent = txn.open_table(T_ENTRIES)?; - ent.insert(key.0, value)?; + ent.insert(key, value)?; drop(ent); txn.commit()?; Ok(()) } } + +pub fn sha256(x: &[u8]) -> [u8; 32] { + let mut hasher = Sha256::new(); + hasher.update(x); + hasher.finalize().into() +} diff --git a/world/Cargo.toml b/world/Cargo.toml new file mode 100644 index 0000000..ee74e2b --- /dev/null +++ b/world/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "weareworld" +version = "0.1.0" +edition = "2024" + +[dependencies] +anyhow = "1.0.95" +clap = { version = "4.5.23", features = ["derive"] } +env_logger = "0.11.6" +gltf = { version = "1.4.1", features = ["extras", "names"] } +log = "0.4.22" +weareshared = { path = "../shared" } diff --git a/world/src/main.rs b/world/src/main.rs new file mode 100644 index 0000000..56b62c9 --- /dev/null +++ b/world/src/main.rs @@ -0,0 +1,39 @@ +use anyhow::Result; +use clap::Parser; +use gltf::Gltf; +use std::{ + fs::File, + io::BufReader, + net::{SocketAddr, TcpStream}, + path::PathBuf, +}; +use weareshared::resources::Part; + +#[derive(Parser)] +struct Args { + address: SocketAddr, + scene: PathBuf, +} + +fn main() -> Result<()> { + env_logger::init_from_env("LOG"); + let args = Args::parse(); + + let sock = TcpStream::connect(args.address)?; + + let scenefile = Gltf::from_reader(BufReader::new(File::open(args.scene)?))?; + let scene = scenefile + .default_scene() + .unwrap_or(scenefile.scenes().next().unwrap()); + + for node in scene.nodes() { + if let Some(mesh) = node.mesh() { + for p in mesh.primitives() { + for (sem, iter) in p.attributes() {} + } + let part = Part { ..Part::default() }; + } + } + + Ok(()) +} |