initial import from steam-vent repo

This commit is contained in:
Robin Appelman 2025-09-07 22:35:14 +02:00
commit d936a6049b
237 changed files with 607341 additions and 0 deletions

606
build/Cargo.lock generated Normal file
View file

@ -0,0 +1,606 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "ahash"
version = "0.8.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011"
dependencies = [
"cfg-if",
"getrandom",
"once_cell",
"version_check",
"zerocopy",
]
[[package]]
name = "aho-corasick"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
dependencies = [
"memchr",
]
[[package]]
name = "anstream"
version = "0.6.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is_terminal_polyfill",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1"
[[package]]
name = "anstyle-parse"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb47de1e80c2b463c735db5b217a0ddc39d612e7ac9e2e96a5aed1f57616c1cb"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a"
dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8"
dependencies = [
"anstyle",
"windows-sys 0.52.0",
]
[[package]]
name = "anyhow"
version = "1.0.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86fdf8605db99b54d3cd748a44c6d04df638eb5dafb219b135d0149bd0db01f6"
[[package]]
name = "bitflags"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "4.5.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0956a43b323ac1afaffc053ed5c4b7c1f1800bacd1683c353aabbb752515dd3"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.5.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d72166dd41634086d5803a47eb71ae740e61d84709c36f3c34110173db3961b"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.5.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ac6a0c7b1a9e9a5186361f67dfa1b88213572f427fb9ab038efb2bd8c582dab"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97"
[[package]]
name = "colorchoice"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0"
[[package]]
name = "either"
version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
[[package]]
name = "equivalent"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "errno"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba"
dependencies = [
"libc",
"windows-sys 0.52.0",
]
[[package]]
name = "fastrand"
version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8c02a5121d4ea3eb16a80748c74f5549a5665e4c21333c6098f283870fbdea6"
[[package]]
name = "getrandom"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "hashbrown"
version = "0.14.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "home"
version = "0.5.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3d1354bf6b7235cb4a0576c2619fd4ed18183f689b12b006a0ee7329eeff9a5"
dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "indexmap"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68b900aa2f7301e21c36462b170ee99994de34dff39a4a6a528e80e7376d07e5"
dependencies = [
"equivalent",
"hashbrown",
]
[[package]]
name = "is_terminal_polyfill"
version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
[[package]]
name = "libc"
version = "0.2.159"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "561d97a539a36e26a9a5fad1ea11a3039a67714694aaa379433e580854bc3dc5"
[[package]]
name = "linux-raw-sys"
version = "0.4.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89"
[[package]]
name = "log"
version = "0.4.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
[[package]]
name = "memchr"
version = "2.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
[[package]]
name = "once_cell"
version = "1.20.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "82881c4be219ab5faaf2ad5e5e5ecdff8c66bd7402ca3160975c93b24961afd1"
dependencies = [
"portable-atomic",
]
[[package]]
name = "portable-atomic"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc9c68a3f6da06753e9335d63e27f6b9754dd1920d941135b7ea8224f141adb2"
[[package]]
name = "prettyplease"
version = "0.2.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "479cf940fbbb3426c32c5d5176f62ad57549a0bb84773423ba8be9d089f5faba"
dependencies = [
"proc-macro2",
"syn",
]
[[package]]
name = "proc-macro2"
version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77"
dependencies = [
"unicode-ident",
]
[[package]]
name = "protobuf"
version = "3.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0bcc343da15609eaecd65f8aa76df8dc4209d325131d8219358c0aaaebab0bf6"
dependencies = [
"once_cell",
"protobuf-support",
"thiserror",
]
[[package]]
name = "protobuf-codegen"
version = "3.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4d0cde5642ea4df842b13eb9f59ea6fafa26dcb43e3e1ee49120e9757556189"
dependencies = [
"anyhow",
"once_cell",
"protobuf",
"protobuf-parse",
"regex",
"tempfile",
"thiserror",
]
[[package]]
name = "protobuf-parse"
version = "3.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b0e9b447d099ae2c4993c0cbb03c7a9d6c937b17f2d56cfc0b1550e6fcfdb76"
dependencies = [
"anyhow",
"indexmap",
"log",
"protobuf",
"protobuf-support",
"tempfile",
"thiserror",
"which",
]
[[package]]
name = "protobuf-support"
version = "3.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0766e3675a627c327e4b3964582594b0e8741305d628a98a5de75a1d15f99b9"
dependencies = [
"thiserror",
]
[[package]]
name = "quote"
version = "1.0.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
dependencies = [
"proc-macro2",
]
[[package]]
name = "regex"
version = "1.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38200e5ee88914975b69f657f0801b6f6dccafd44fd9326302a4aaeecfacb1d8"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "368758f23274712b504848e9d5a6f010445cc8b87a7cdb4d7cbee666c1288da3"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
[[package]]
name = "rustix"
version = "0.38.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8acb788b847c24f28525660c4d7758620a7210875711f79e7f663cc152726811"
dependencies = [
"bitflags",
"errno",
"libc",
"linux-raw-sys",
"windows-sys 0.52.0",
]
[[package]]
name = "same-file"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
dependencies = [
"winapi-util",
]
[[package]]
name = "steam-vent-proto-build"
version = "0.5.0"
dependencies = [
"ahash",
"clap",
"prettyplease",
"proc-macro2",
"protobuf",
"protobuf-codegen",
"protobuf-parse",
"quote",
"syn",
"walkdir",
]
[[package]]
name = "strsim"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "syn"
version = "2.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89132cd0bf050864e1d38dc3bbc07a0eb8e7530af26344d3d2bbbef83499f590"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "tempfile"
version = "3.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0f2c9fc62d0beef6951ccffd757e241266a2c833136efbe35af6cd2567dca5b"
dependencies = [
"cfg-if",
"fastrand",
"once_cell",
"rustix",
"windows-sys 0.59.0",
]
[[package]]
name = "thiserror"
version = "1.0.64"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d50af8abc119fb8bb6dbabcfa89656f46f84aa0ac7688088608076ad2b459a84"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.64"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08904e7672f5eb876eaaf87e0ce17857500934f4981c4a0ab2b4aa98baac7fc3"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "unicode-ident"
version = "1.0.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe"
[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "version_check"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
[[package]]
name = "walkdir"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b"
dependencies = [
"same-file",
"winapi-util",
]
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "which"
version = "4.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87ba24419a2078cd2b0f2ede2691b6c66d8e47836da3b6db8265ebad47afbfc7"
dependencies = [
"either",
"home",
"once_cell",
"rustix",
]
[[package]]
name = "winapi-util"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
dependencies = [
"windows-sys 0.59.0",
]
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-sys"
version = "0.59.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_gnullvm",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]]
name = "windows_i686_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
[[package]]
name = "windows_i686_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]]
name = "windows_i686_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]]
name = "zerocopy"
version = "0.7.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0"
dependencies = [
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.7.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e"
dependencies = [
"proc-macro2",
"quote",
"syn",
]

20
build/Cargo.toml Normal file
View file

@ -0,0 +1,20 @@
[package]
name = "steam-vent-proto-build"
description = "Build steam-vent compatible protobuf bindings"
version = "0.5.0"
edition = "2021"
authors = ["Robin Appelman <robin@icewind.nl>"]
license = "MIT"
repository = "https://codeberg.org/steam-vent/proto"
[dependencies]
protobuf-codegen = "=3.5.1"
protobuf-parse = "=3.5.1"
protobuf = "=3.5.1"
walkdir = "2.3.3"
clap = { version = "4.3.19", features = ["derive"] }
quote = "1.0.32"
prettyplease = "0.2.12"
syn = { version = "2.0.28", features = ["full"] }
proc-macro2 = "1.0.66"
ahash = "0.8.3"

9
build/README.md Normal file
View file

@ -0,0 +1,9 @@
# steam-vent protobuf builder
Code generator for protobufs to be used with steam-vent.
## Usage
```bash
steam-vent-proto-build <protobuf directory> <output directory>
```

147
build/src/kinds.rs Normal file
View file

@ -0,0 +1,147 @@
use crate::proto_path_to_rust_mod;
use proc_macro2::{Ident, Span, TokenStream};
use protobuf_parse::Parser;
use quote::quote;
use std::path::{Path, PathBuf};
pub fn get_kinds(base: &Path, protos: &[PathBuf]) -> Vec<Kind> {
let mut parser = Parser::new();
parser.pure().include(base).inputs(protos);
let mut parsed = parser.parse_and_typecheck().unwrap().file_descriptors;
let kinds_enums = parsed
.iter_mut()
.flat_map(|parsed| {
let mod_name = proto_path_to_rust_mod(parsed.name());
parsed
.enum_type
.iter_mut()
.map(move |e| (mod_name.clone(), e))
})
.filter(|(_, e)| {
e.name().starts_with("E")
&& (e.name().ends_with("Msg") || e.name().ends_with("Messages"))
});
let mut kinds = kinds_enums
.flat_map(|(mod_name, kinds_enum)| {
let enum_name = kinds_enum.take_name();
kinds_enum
.value
.iter_mut()
.map(move |opt| Kind::new(&mod_name, &enum_name, opt.name()))
})
.collect::<Vec<_>>();
// sort kinds with prefix in front
kinds.sort_by(|a, b| a.enum_prefix.len().cmp(&b.enum_prefix.len()).reverse());
kinds
}
#[derive(Debug, Clone)]
pub struct Kind {
mod_name: String,
enum_name: String,
enum_prefix: String,
variant_prefix: String,
variant_prefix_alt: String,
variant_prefix_alt2: String,
variant: String,
is_gc: bool,
struct_name_prefix_alt_len: usize,
}
impl Kind {
pub fn new(mod_name: &str, enum_name: &str, variant_name: &str) -> Self {
let prefix: String = enum_name
.chars()
.skip(1)
.take_while(char::is_ascii_uppercase)
.collect();
let prefix = prefix[0..prefix.len() - 1].to_string();
let variant_prefix = format!("k_EMsg{}", prefix);
let variant_prefix_alt = format!("k_E{}Msg_", prefix);
let variant_prefix_alt2 = "k_EMsg".to_string();
let enum_prefix = prefix.to_ascii_lowercase();
Kind {
is_gc: variant_prefix.contains("GC"),
mod_name: mod_name.to_string(),
enum_name: enum_name.to_string(),
enum_prefix,
variant_prefix,
variant_prefix_alt,
variant_prefix_alt2,
variant: variant_name.to_string(),
struct_name_prefix_alt_len: prefix.len(),
}
}
pub fn matches(&self, struct_name: &str, file_name: Option<&str>) -> bool {
let struct_name = struct_name.strip_prefix('C').unwrap_or(struct_name);
let struct_name = struct_name.strip_prefix("Msg").unwrap_or(struct_name);
let Some(stripped) = self
.variant
.strip_prefix(&self.variant_prefix)
.or_else(|| self.variant.strip_prefix(&self.variant_prefix_alt))
.or_else(|| self.variant.strip_prefix(&self.variant_prefix_alt2))
else {
return false;
};
if let Some(file_name) = file_name {
if !(file_name.contains(&self.enum_prefix)
|| file_name.replace('_', "").contains(&self.enum_prefix))
{
return false;
}
}
struct_name.eq_ignore_ascii_case(stripped)
|| (self.is_gc
&& stripped
.strip_prefix("GC")
.unwrap_or_default()
.eq_ignore_ascii_case(struct_name))
|| struct_name
.get(self.struct_name_prefix_alt_len..)
.unwrap_or_default()
.eq_ignore_ascii_case(stripped)
}
pub fn ident(&self) -> TokenStream {
let path = Ident::new(&self.mod_name, Span::call_site());
let enum_ident = Ident::new(&self.enum_name, Span::call_site());
let variant_ident = Ident::new(&self.variant, Span::call_site());
quote!(crate::#path::#enum_ident::#variant_ident)
}
pub fn enum_ident(&self) -> TokenStream {
let path = Ident::new(&self.mod_name, Span::call_site());
let enum_ident = Ident::new(&self.enum_name, Span::call_site());
quote!(crate::#path::#enum_ident)
}
}
#[test]
fn test_find_kind() {
assert!(Kind::new(
"enums_clientserver",
"EMsg",
"k_EMsgClientSiteLicenseCheckout",
)
.matches(
"CMsgClientSiteLicenseCheckout",
Some("steammessages_sitelicenseclient")
));
assert!(
Kind::new("econ_gcmessages", "EGCItemMsg", "k_EMsgGCApplyAutograph",)
.matches("CMsgApplyAutograph", Some("econ_gcmessages"))
);
assert!(
Kind::new("dota_gcmessages_msgid", "EDOTAGCMsg", "k_EMsgGCLobbyList").matches(
"CMsgLobbyList",
Some("dota_gcmessages_client_match_management")
)
);
}

414
build/src/main.rs Normal file
View file

@ -0,0 +1,414 @@
mod kinds;
use ahash::{AHashMap, AHashSet, RandomState};
use kinds::{get_kinds, Kind};
use proc_macro2::{Ident, Span, TokenStream};
use protobuf::reflect::{FileDescriptor, MessageDescriptor, ServiceDescriptor};
use protobuf::{Message, SpecialFields, UnknownValueRef};
use protobuf_codegen::{Codegen, Customize, CustomizeCallback};
use quote::{quote, ToTokens};
use std::cell::RefCell;
use std::cmp::Ordering;
use std::fs::{read_to_string, OpenOptions};
use std::hash::{Hash, Hasher};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use syn::__private::str;
use walkdir::WalkDir;
fn get_protos(path: impl AsRef<Path>) -> impl Iterator<Item = PathBuf> {
WalkDir::new(path)
.into_iter()
.map(|res| res.expect("failed to read entry"))
.filter(|entry| entry.path().is_file())
.filter(|entry| {
!entry
.file_name()
.to_str()
.expect("invalid filename")
.starts_with('.')
})
.map(|entry| entry.into_path())
}
#[derive(clap::Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
/// Folder containing the proto buffers
protos: PathBuf,
/// Target directory
target: PathBuf,
}
fn main() {
use clap::Parser;
let args: Args = Args::parse();
let mut protos = get_protos(&args.protos).collect::<Vec<_>>();
protos.sort();
let kinds = get_kinds(&args.protos, &protos);
let service_generator = ServiceGenerator::new(kinds);
let service_files = service_generator.files.clone();
let builder_version_check = format!(
"const _VENT_PROTO_VERSION_CHECK: () = ::steam_vent_proto_common::VERSION_{};",
env!("CARGO_PKG_VERSION").replace('.', "_")
);
Codegen::new()
.pure()
.out_dir(&args.target)
.include(&args.protos)
.inputs(protos.iter())
.customize_callback(service_generator)
.customize(Customize::default().lite_runtime(true))
.run_from_script();
for (file, services) in service_files.borrow().iter() {
let mut file = proto_path_to_rust_mod(&file);
file.push_str(".rs");
let source_file = args.target.join(&file);
if source_file.exists() {
let mut code = read_to_string(&source_file).unwrap();
let extra_code = if !services.is_empty() {
let service_tokens = services.services.iter().map(Service::gen);
let method_tokens = services.methods().map(|method| method.gen());
let message_tokens = services.messages.iter().map(ServiceMessage::gen);
let import_tokens = services.imports.iter().map(|file| {
let path = proto_path_to_rust_mod(file);
let path = Ident::new(&path, Span::call_site());
quote! {
#[allow(unused_imports)]
use crate::#path::*;
}
});
let enum_kind_tokens = services.kind_enums.iter().map(|enum_name| {
let ident = Ident::new(&enum_name, Span::call_site());
quote!(
impl ::steam_vent_proto_common::MsgKindEnum for #ident {}
)
});
let tokens = quote! {
#(#import_tokens)*
#(#message_tokens)*
#(#service_tokens)*
#(#method_tokens)*
#(#enum_kind_tokens)*
};
let syntax_tree = syn::parse2(tokens).unwrap();
let formatted = prettyplease::unparse(&syntax_tree);
format!("{}\n\n{}", builder_version_check, formatted)
} else {
builder_version_check.clone()
};
code = format!("{}\n\n{}", code, extra_code);
code = code.replace("::protobuf::", "::steam_vent_proto_common::protobuf::");
let mut file = OpenOptions::new()
.write(true)
.truncate(true)
.open(&source_file)
.unwrap();
file.write_all(code.as_bytes()).unwrap();
}
}
}
#[derive(Debug, Clone)]
struct ServiceMethod {
name: String,
service_name: String,
description: Option<String>,
response: String,
request: String,
}
impl Hash for ServiceMethod {
fn hash<H: Hasher>(&self, state: &mut H) {
self.request.hash(state)
}
}
impl PartialEq for ServiceMethod {
fn eq(&self, other: &Self) -> bool {
self.request.eq(&other.request)
}
}
impl Eq for ServiceMethod {}
impl PartialOrd for ServiceMethod {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.request.partial_cmp(&other.request)
}
}
impl Ord for ServiceMethod {
fn cmp(&self, other: &Self) -> Ordering {
self.request.cmp(&other.request)
}
}
impl ServiceMethod {
fn gen(&self) -> TokenStream {
let name = format!("{}.{}#1", self.service_name, self.name);
let request_ident = Ident::new(&self.request, Span::call_site());
let response_ident = if self.response == "NoResponse" {
quote! {()}
} else {
Ident::new(&self.response, Span::call_site()).to_token_stream()
};
quote! {
impl ::steam_vent_proto_common::RpcMethod for #request_ident {
const METHOD_NAME: &'static str = #name;
type Response = #response_ident;
}
}
}
}
#[derive(Debug)]
struct Service {
name: String,
description: Option<String>,
methods: Vec<ServiceMethod>,
}
struct ServiceMessage {
name: String,
kind: Option<Kind>,
}
impl ServiceMessage {
fn gen(&self) -> TokenStream {
let message_ident = Ident::new(&self.name, Span::call_site());
let kind_tokens = self.kind.as_ref().map(|kind| {
let kind_ident = kind.ident();
let enum_ident = kind.enum_ident();
quote! {
impl ::steam_vent_proto_common::RpcMessageWithKind for #message_ident {
type KindEnum = #enum_ident;
const KIND: Self::KindEnum = #kind_ident;
}
}
});
quote! {
impl ::steam_vent_proto_common::RpcMessage for #message_ident {
fn parse(reader: &mut dyn std::io::Read) -> ::protobuf::Result<Self> {
<Self as ::protobuf::Message>::parse_from_reader(reader)
}
fn write(&self, writer: &mut dyn std::io::Write) -> ::protobuf::Result<()> {
use ::protobuf::Message;
self.write_to_writer(writer)
}
fn encode_size(&self) -> usize {
use ::protobuf::Message;
self.compute_size() as usize
}
}
#kind_tokens
}
}
}
struct FileServices {
services: Vec<Service>,
imports: Vec<String>,
messages: Vec<ServiceMessage>,
kind_enums: Vec<String>,
}
impl FileServices {
fn is_empty(&self) -> bool {
// we don't check imports, since we only need to import stuff if we generate code
self.services.is_empty() && self.messages.is_empty() && self.kind_enums.is_empty()
}
fn methods(&self) -> impl Iterator<Item = ServiceMethod> {
let methods: AHashSet<ServiceMethod> = self
.services
.iter()
.flat_map(|service| service.methods.iter())
.cloned()
.collect();
let mut methods: Vec<_> = methods.into_iter().collect();
methods.sort();
methods.into_iter()
}
}
struct ServiceGenerator {
files: Rc<RefCell<AHashMap<String, FileServices>>>,
descriptions: Rc<RefCell<AHashMap<String, String>>>,
kinds: Vec<Kind>,
}
impl ServiceGenerator {
pub fn new(kinds: Vec<Kind>) -> Self {
Self {
files: Rc::new(RefCell::new(AHashMap::with_capacity_and_hasher(
16,
RandomState::with_seeds(1, 2, 3, 4),
))),
descriptions: Default::default(),
kinds,
}
}
fn find_kind(&self, message_type: &str, file_name: Option<&str>) -> Option<Kind> {
self.kinds
.iter()
.find(|e_kind| e_kind.matches(message_type, file_name))
.cloned()
}
}
fn get_description(fields: &SpecialFields) -> Option<String> {
for option in fields.unknown_fields().iter() {
if let UnknownValueRef::LengthDelimited(bytes) = option.1 {
if let Ok(desc) = String::from_utf8(bytes.into()) {
return Some(desc);
}
}
}
None
}
impl From<ServiceDescriptor> for Service {
fn from(value: ServiceDescriptor) -> Self {
let name = value.proto().name.clone().unwrap_or_default();
let methods = value
.methods()
.map(|method| ServiceMethod {
name: method.proto().name.clone().unwrap_or_default(),
service_name: name.clone(),
description: get_description(method.proto().options.special_fields()),
request: (method.input_type().full_name().into()),
response: method.output_type().full_name().into(),
})
.collect();
Service {
name,
description: get_description(value.proto().options.special_fields()),
methods,
}
}
}
impl Service {
fn gen(&self) -> TokenStream {
let name = &self.name;
let desc = self.description.as_deref().unwrap_or_default();
let struct_name = Ident::new(&self.name, Span::call_site());
quote! {
#[doc = #desc]
struct #struct_name {}
impl ::steam_vent_proto_common::RpcService for #struct_name {
const SERVICE_NAME: &'static str = #name;
}
}
}
}
impl CustomizeCallback for ServiceGenerator {
fn file(&self, file: &FileDescriptor) -> Customize {
let services: Vec<Service> = file.services().map(Service::from).collect();
let imports = file
.deps()
.iter()
.map(|dep| dep.name().to_string())
.filter(|import| !import.starts_with("google"))
.collect();
let messages: Vec<_> = file
.messages()
.map(|msg| ServiceMessage {
name: msg.name().into(),
kind: self
.find_kind(msg.name(), Some(file.name()))
.or_else(|| self.find_kind(msg.name(), None)),
})
.collect();
let kind_enums: Vec<_> = file
.enums()
.filter_map(|enum_type| {
(enum_type.name().starts_with("E")
&& (enum_type.name().ends_with("Msg")
|| enum_type.name().ends_with("Messages")))
.then(|| enum_type.name().to_string())
})
.collect();
for service in services.iter() {
for method in service.methods.iter() {
if let Some(description) = method.description.clone() {
self.descriptions
.borrow_mut()
.insert(method.request.clone(), description);
}
}
}
self.files.borrow_mut().insert(
file.name().to_string(),
FileServices {
services,
imports,
messages,
kind_enums,
},
);
Customize::default()
}
fn message(&self, message: &MessageDescriptor) -> Customize {
if let Some(description) = self.descriptions.borrow().get(message.name()) {
Customize::default().before(&format!("#[doc = \"{description}\"]"))
} else {
Customize::default()
}
}
}
fn proto_path_to_rust_mod(path: &str) -> String {
let without_suffix = path
.rsplit("/")
.next()
.unwrap()
.strip_suffix(".proto")
.unwrap();
without_suffix
.chars()
.enumerate()
.map(|(i, c)| {
let valid = if i == 0 {
ident_start(c)
} else {
ident_continue(c)
};
if valid {
c
} else {
'_'
}
})
.collect::<String>()
}
// Copy-pasted from libsyntax.
fn ident_start(c: char) -> bool {
(c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_'
}
// Copy-pasted from libsyntax.
fn ident_continue(c: char) -> bool {
(c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_'
}