valence/build/main.rs
Ryan Johnson 420f2d1b7c
Move protocol code to valence_protocol + redesigns (#153)
Closes #83 

This PR aims to move all of Valence's networking code to the new
`valence_protocol` crate. Anything not specific to valence is going in
the new crate. It also redesigns the way packets are defined and makes a
huge number of small additions and improvements. It should be much
easier to see where code is supposed to go from now on.

`valence_protocol` is a new library which enables interactions with
Minecraft's protocol. It is completely decoupled from valence and can be
used to build new clients, servers, tools, etc.

There are two additions that will help with #5 especially:
- It is now easy to define new packets or modifications of existing
packets. Not all packets need to be bidirectional.
- The `CachedEncode` type has been created. This is used to safely cache
redundant calls to `Encode::encode`.
2022-11-13 06:10:42 -08:00

43 lines
1.1 KiB
Rust

use std::path::Path;
use std::process::Command;
use std::{env, fs};
use anyhow::Context;
use proc_macro2::{Ident, Span};
mod entity;
mod entity_event;
pub fn main() -> anyhow::Result<()> {
println!("cargo:rerun-if-changed=extracted/");
let generators = [
(entity::build as fn() -> _, "entity.rs"),
(entity_event::build, "entity_event.rs"),
];
let out_dir = env::var_os("OUT_DIR").context("can't get OUT_DIR env var")?;
for (g, file_name) in generators {
let path = Path::new(&out_dir).join(file_name);
let code = g()?.to_string();
fs::write(&path, code)?;
// Format the output for debugging purposes.
// Doesn't matter if rustfmt is unavailable.
let _ = Command::new("rustfmt").arg(path).output();
}
Ok(())
}
fn ident(s: impl AsRef<str>) -> Ident {
let s = s.as_ref().trim();
match s.as_bytes() {
// TODO: check for the other rust keywords.
[b'0'..=b'9', ..] | b"type" => Ident::new(&format!("_{s}"), Span::call_site()),
_ => Ident::new(s, Span::call_site()),
}
}