Skip to content

Commit e794899

Browse files
committed
Replace old protobuf with prost
1 parent f7f77a7 commit e794899

17 files changed

Lines changed: 2635 additions & 162 deletions

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ categories = ["api-bindings", "game-development", "games"]
1818
exclude = ["images/"]
1919

2020
[dependencies]
21-
protobuf = "=3.7.2"
21+
prost = "0.14"
2222
byteorder = "1.5.0"
2323
num_enum = "0.7.3"
2424
log = "0.4.26"

dfhack-proto/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ prost = "0.14"
1818
[build-dependencies]
1919
prost = "0.14"
2020
prost-build = "0.14"
21-
prost-types = "0.14.3"
2221
dfhack-proto-srcs = { version = "0.11.0", path = "../dfhack-proto-srcs" }
2322
regex = "1"
2423
heck = "0.5.0"

dfhack-proto/build.rs

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ use std::{collections::HashMap, io::BufRead, path::PathBuf};
44

55
use heck::{ToPascalCase, ToSnakeCase};
66
use proc_macro2::TokenStream;
7-
use prost::Message;
8-
use prost_types::FileDescriptorSet;
97
use quote::format_ident;
108
use quote::quote;
119
use quote::ToTokens;
@@ -75,10 +73,10 @@ fn main() {
7573
};
7674

7775
// Generate the protobuf message files
78-
let fd = generate_messages_rs(&protoc, &protos, proto_include_dir, &out_path);
76+
generate_messages_rs(&protoc, &protos, proto_include_dir, &out_path);
7977

8078
// Generate the plugin stubs
81-
generate_stubs_rs(&fd, &protos, &out_path)
79+
generate_stubs_rs(&protos, &out_path)
8280
}
8381

8482
fn get_protoc() -> PathBuf {
@@ -87,17 +85,11 @@ fn get_protoc() -> PathBuf {
8785
protoc_fetcher::protoc(protoc_version, Path::new(&out_dir)).unwrap()
8886
}
8987

90-
fn generate_messages_rs(
91-
protoc: &PathBuf,
92-
protos: &[PathBuf],
93-
include_dir: &str,
94-
out_path: &Path,
95-
) -> FileDescriptorSet {
88+
fn generate_messages_rs(protoc: &PathBuf, protos: &[PathBuf], include_dir: &str, out_path: &Path) {
9689
let mut out_path = out_path.to_path_buf();
9790
out_path.push("messages");
9891
std::fs::create_dir_all(&out_path).unwrap();
99-
messages_protoc_codegen(protoc, protos, include_dir, &out_path)
100-
//messages_generate_mod_rs(protos, &out_path);
92+
messages_protoc_codegen(protoc, protos, include_dir, &out_path);
10193
}
10294

10395
// Call the protoc code generation
@@ -106,20 +98,16 @@ fn messages_protoc_codegen(
10698
protos: &[PathBuf],
10799
include_dir: &str,
108100
out_path: &Path,
109-
) -> FileDescriptorSet {
110-
let mut prost = prost_build::Config::new();
111-
let fd = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR environment variable not set"))
112-
.join("file_descriptor_set.bin");
113-
prost.protoc_executable(protoc);
114-
prost.file_descriptor_set_path(&fd);
115-
prost.out_dir(out_path);
116-
prost.compile_protos(protos, &[include_dir]).unwrap();
117-
118-
let fd = std::fs::read(fd).unwrap();
119-
prost_types::FileDescriptorSet::decode(fd.as_slice()).unwrap()
101+
) {
102+
prost_build::Config::new()
103+
.enable_type_names()
104+
.protoc_executable(protoc)
105+
.out_dir(out_path)
106+
.compile_protos(protos, &[include_dir])
107+
.unwrap();
120108
}
121109

122-
fn generate_stubs_rs(fd: &FileDescriptorSet, protos: &Vec<PathBuf>, out_path: &Path) {
110+
fn generate_stubs_rs(protos: &Vec<PathBuf>, out_path: &Path) {
123111
let plugins = read_protos_rpcs(protos);
124112
let mut out_path = out_path.to_path_buf();
125113
out_path.push("stubs");
@@ -129,7 +117,7 @@ fn generate_stubs_rs(fd: &FileDescriptorSet, protos: &Vec<PathBuf>, out_path: &P
129117
generate_stubs_mod_rs(&plugins, &mut file);
130118

131119
for plugin in &plugins {
132-
generate_stub_rs(fd, plugin, &mut file);
120+
generate_stub_rs(plugin, &mut file);
133121
}
134122

135123
let mut mod_rs_path = out_path.clone();
@@ -145,6 +133,7 @@ fn generate_stubs_mod_rs(plugins: &Vec<Plugin>, file: &mut TokenStream) {
145133

146134
file.extend(quote! {
147135
use crate::messages::*;
136+
use prost::Name;
148137
});
149138

150139
let mut reflection_vec_building = quote!();
@@ -195,18 +184,7 @@ fn generate_stubs_mod_rs(plugins: &Vec<Plugin>, file: &mut TokenStream) {
195184
});
196185
}
197186

198-
fn get_full_name(fd: &FileDescriptorSet, message_name: &str) -> String {
199-
for file in &fd.file {
200-
for message_type in &file.message_type {
201-
if message_type.name() == message_name {
202-
return format!("{}.{}", file.package(), message_type.name());
203-
}
204-
}
205-
}
206-
panic!("Could not find {}", message_name);
207-
}
208-
209-
fn generate_stub_rs(fd: &FileDescriptorSet, plugin: &Plugin, file: &mut TokenStream) {
187+
fn generate_stub_rs(plugin: &Plugin, file: &mut TokenStream) {
210188
let plugin_name = &plugin.plugin_name;
211189
let plugin_doc = format!("RPC for the \"{}\" plugin.", plugin_name);
212190
let struct_ident = plugin.struct_ident.clone();
@@ -238,9 +216,6 @@ fn generate_stub_rs(fd: &FileDescriptorSet, plugin: &Plugin, file: &mut TokenStr
238216
let input_ident = format_ident!("{}", rpc.input);
239217
let output_ident = format_ident!("{}", rpc.output);
240218

241-
let input_full_name = get_full_name(fd, &rpc.input);
242-
let output_full_name = get_full_name(fd, &rpc.output);
243-
244219
let mut return_token = output_ident.to_token_stream();
245220

246221
let mut parameters = quote! {
@@ -337,8 +312,8 @@ fn generate_stub_rs(fd: &FileDescriptorSet, plugin: &Plugin, file: &mut TokenStr
337312
crate::reflection::RemoteProcedureDescriptor {
338313
name: #function_name,
339314
plugin_name: #plugin_name,
340-
input_type: #input_full_name,
341-
output_type: #output_full_name,
315+
input_type: #input_ident::full_name(),
316+
output_type: #output_ident::full_name(),
342317
},
343318
});
344319
}
@@ -379,9 +354,6 @@ fn read_protos_rpcs(protos: &Vec<PathBuf>) -> Vec<Plugin> {
379354

380355
plugins.sort_by(|a, b| a.plugin_name.cmp(&b.plugin_name));
381356

382-
dbg!(plugins);
383-
panic!();
384-
385357
plugins
386358
}
387359

dfhack-proto/src/generated/messages/adventure_control.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ pub struct MoveCommandParams {
44
#[prost(message, optional, tag = "1")]
55
pub direction: ::core::option::Option<super::remote_fortress_reader::Coord>,
66
}
7+
impl ::prost::Name for MoveCommandParams {
8+
const NAME: &'static str = "MoveCommandParams";
9+
const PACKAGE: &'static str = "AdventureControl";
10+
fn full_name() -> ::prost::alloc::string::String {
11+
"AdventureControl.MoveCommandParams".into()
12+
}
13+
fn type_url() -> ::prost::alloc::string::String {
14+
"/AdventureControl.MoveCommandParams".into()
15+
}
16+
}
717
#[derive(Clone, Copy, PartialEq, Eq, Hash, ::prost::Message)]
818
pub struct MovementOption {
919
#[prost(message, optional, tag = "1")]
@@ -15,18 +25,48 @@ pub struct MovementOption {
1525
#[prost(enumeration = "CarefulMovementType", optional, tag = "4")]
1626
pub movement_type: ::core::option::Option<i32>,
1727
}
28+
impl ::prost::Name for MovementOption {
29+
const NAME: &'static str = "MovementOption";
30+
const PACKAGE: &'static str = "AdventureControl";
31+
fn full_name() -> ::prost::alloc::string::String {
32+
"AdventureControl.MovementOption".into()
33+
}
34+
fn type_url() -> ::prost::alloc::string::String {
35+
"/AdventureControl.MovementOption".into()
36+
}
37+
}
1838
#[derive(Clone, PartialEq, ::prost::Message)]
1939
pub struct MenuContents {
2040
#[prost(enumeration = "AdvmodeMenu", optional, tag = "1")]
2141
pub current_menu: ::core::option::Option<i32>,
2242
#[prost(message, repeated, tag = "2")]
2343
pub movements: ::prost::alloc::vec::Vec<MovementOption>,
2444
}
45+
impl ::prost::Name for MenuContents {
46+
const NAME: &'static str = "MenuContents";
47+
const PACKAGE: &'static str = "AdventureControl";
48+
fn full_name() -> ::prost::alloc::string::String {
49+
"AdventureControl.MenuContents".into()
50+
}
51+
fn type_url() -> ::prost::alloc::string::String {
52+
"/AdventureControl.MenuContents".into()
53+
}
54+
}
2555
#[derive(Clone, Copy, PartialEq, Eq, Hash, ::prost::Message)]
2656
pub struct MiscMoveParams {
2757
#[prost(enumeration = "MiscMoveType", optional, tag = "1")]
2858
pub r#type: ::core::option::Option<i32>,
2959
}
60+
impl ::prost::Name for MiscMoveParams {
61+
const NAME: &'static str = "MiscMoveParams";
62+
const PACKAGE: &'static str = "AdventureControl";
63+
fn full_name() -> ::prost::alloc::string::String {
64+
"AdventureControl.MiscMoveParams".into()
65+
}
66+
fn type_url() -> ::prost::alloc::string::String {
67+
"/AdventureControl.MiscMoveParams".into()
68+
}
69+
}
3070
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
3171
#[repr(i32)]
3272
pub enum AdvmodeMenu {

0 commit comments

Comments
 (0)