1+ use std:: env;
12use std:: path:: Path ;
23use std:: { collections:: HashMap , io:: BufRead , path:: PathBuf } ;
34
45use heck:: { ToPascalCase , ToSnakeCase } ;
56use proc_macro2:: TokenStream ;
6- use protobuf :: reflect :: MessageDescriptor ;
7- use protobuf_codegen :: { Customize , CustomizeCallback } ;
7+ use prost :: Message ;
8+ use prost_types :: FileDescriptorSet ;
89use quote:: format_ident;
910use quote:: quote;
1011use quote:: ToTokens ;
@@ -45,22 +46,12 @@ impl Plugin {
4546 }
4647}
4748
48- struct AddHash ;
49-
50- impl CustomizeCallback for AddHash {
51- fn message ( & self , message : & MessageDescriptor ) -> Customize {
52- let mut customize = Customize :: default ( ) ;
53- if message. name ( ) == "MatPair" || message. name ( ) == "Coord" {
54- customize = customize. before ( "#[derive(Hash, Eq)]" ) ;
55- }
56- customize
57- }
58- }
59-
6049fn main ( ) {
6150 println ! ( "cargo:rerun-if-changed=build.rs" ) ;
6251 println ! ( "cargo:rerun-if-env-changed=DFHACK_REGEN" ) ;
6352
53+ let protoc = get_protoc ( ) ;
54+
6455 let proto_include_dir = dfhack_proto_srcs:: include_dir ( ) ;
6556 let protos = dfhack_proto_srcs:: protos ( ) ;
6657
@@ -82,30 +73,57 @@ fn main() {
8273 } ;
8374
8475 // Generate the protobuf message files
85- generate_messages_rs ( & protos, proto_include_dir, & out_path) ;
76+ let fd = generate_messages_rs ( & protoc , & protos, proto_include_dir, & out_path) ;
8677
8778 // Generate the plugin stubs
88- generate_stubs_rs ( & protos, & out_path)
79+ generate_stubs_rs ( & fd, & protos, & out_path)
80+ }
81+
82+ fn get_protoc ( ) -> PathBuf {
83+ let protoc_version = "33.5" ;
84+ let out_dir = env:: var ( "OUT_DIR" ) . unwrap ( ) ;
85+ protoc_fetcher:: protoc ( protoc_version, Path :: new ( & out_dir) ) . unwrap ( )
8986}
9087
91- fn generate_messages_rs ( protos : & Vec < PathBuf > , include_dir : & str , out_path : & Path ) {
88+ fn generate_messages_rs (
89+ protoc : & PathBuf ,
90+ protos : & Vec < PathBuf > ,
91+ include_dir : & str ,
92+ out_path : & Path ,
93+ ) -> FileDescriptorSet {
9294 let mut out_path = out_path. to_path_buf ( ) ;
9395 out_path. push ( "messages" ) ;
9496 std:: fs:: create_dir_all ( & out_path) . unwrap ( ) ;
95- messages_protoc_codegen ( protos, include_dir, & out_path) ;
96- messages_generate_mod_rs ( protos, & out_path) ;
97+ messages_protoc_codegen ( protoc , protos, include_dir, & out_path)
98+ // messages_generate_mod_rs(protos, &out_path);
9799}
98100
99101// Call the protoc code generation
100- fn messages_protoc_codegen ( protos : & Vec < PathBuf > , include_dir : & str , out_path : & Path ) {
101- protobuf_codegen:: Codegen :: new ( )
102- . customize ( Customize :: default ( ) . lite_runtime ( false ) )
103- . customize_callback ( AddHash )
104- . pure ( )
105- . include ( include_dir)
106- . inputs ( protos)
107- . out_dir ( out_path)
108- . run_from_script ( ) ;
102+ fn messages_protoc_codegen (
103+ protoc : & PathBuf ,
104+ protos : & Vec < PathBuf > ,
105+ include_dir : & str ,
106+ out_path : & Path ,
107+ ) -> FileDescriptorSet {
108+ let mut prost = prost_build:: Config :: new ( ) ;
109+ let fd = PathBuf :: from ( env:: var ( "OUT_DIR" ) . expect ( "OUT_DIR environment variable not set" ) )
110+ . join ( "file_descriptor_set.bin" ) ;
111+ prost. protoc_executable ( protoc) ;
112+ prost. file_descriptor_set_path ( & fd) ;
113+ prost. out_dir ( out_path) ;
114+ prost. compile_protos ( protos, & [ include_dir] ) . unwrap ( ) ;
115+
116+ let fd = std:: fs:: read ( fd) . unwrap ( ) ;
117+ prost_types:: FileDescriptorSet :: decode ( fd. as_slice ( ) ) . unwrap ( )
118+
119+ /*protobuf_codegen::Codegen::new()
120+ .customize(Customize::default().lite_runtime(false))
121+ .customize_callback(AddHash)
122+ .pure()
123+ .include(include_dir)
124+ .inputs(protos)
125+ .out_dir(out_path)
126+ .run_from_script();*/
109127}
110128
111129fn messages_generate_mod_rs ( protos : & Vec < PathBuf > , out_path : & Path ) {
@@ -136,7 +154,7 @@ fn messages_generate_mod_rs(protos: &Vec<PathBuf>, out_path: &Path) {
136154 std:: fs:: write ( mod_rs_path, formatted) . unwrap ( ) ;
137155}
138156
139- fn generate_stubs_rs ( protos : & Vec < PathBuf > , out_path : & Path ) {
157+ fn generate_stubs_rs ( fd : & FileDescriptorSet , protos : & Vec < PathBuf > , out_path : & Path ) {
140158 let plugins = read_protos_rpcs ( protos) ;
141159 let mut out_path = out_path. to_path_buf ( ) ;
142160 out_path. push ( "stubs" ) ;
@@ -146,7 +164,7 @@ fn generate_stubs_rs(protos: &Vec<PathBuf>, out_path: &Path) {
146164 generate_stubs_mod_rs ( & plugins, & mut file) ;
147165
148166 for plugin in & plugins {
149- generate_stub_rs ( plugin, & mut file) ;
167+ generate_stub_rs ( fd , plugin, & mut file) ;
150168 }
151169
152170 let mut mod_rs_path = out_path. clone ( ) ;
@@ -162,8 +180,6 @@ fn generate_stubs_mod_rs(plugins: &Vec<Plugin>, file: &mut TokenStream) {
162180
163181 file. extend ( quote ! {
164182 use crate :: messages:: * ;
165- #[ cfg( feature = "reflection" ) ]
166- use protobuf:: MessageFull ;
167183 } ) ;
168184
169185 let mut reflection_vec_building = quote ! ( ) ;
@@ -214,7 +230,18 @@ fn generate_stubs_mod_rs(plugins: &Vec<Plugin>, file: &mut TokenStream) {
214230 } ) ;
215231}
216232
217- fn generate_stub_rs ( plugin : & Plugin , file : & mut TokenStream ) {
233+ fn get_full_name ( fd : & FileDescriptorSet , message_name : & str ) -> String {
234+ for file in & fd. file {
235+ for message_type in & file. message_type {
236+ if message_type. name ( ) == message_name {
237+ return format ! ( "{}.{}" , file. package( ) , message_type. name( ) ) ;
238+ }
239+ }
240+ }
241+ panic ! ( "Could not find {}" , message_name) ;
242+ }
243+
244+ fn generate_stub_rs ( fd : & FileDescriptorSet , plugin : & Plugin , file : & mut TokenStream ) {
218245 let plugin_name = & plugin. plugin_name ;
219246 let plugin_doc = format ! ( "RPC for the \" {}\" plugin." , plugin_name) ;
220247 let struct_ident = plugin. struct_ident . clone ( ) ;
@@ -246,6 +273,9 @@ fn generate_stub_rs(plugin: &Plugin, file: &mut TokenStream) {
246273 let input_ident = format_ident ! ( "{}" , rpc. input) ;
247274 let output_ident = format_ident ! ( "{}" , rpc. output) ;
248275
276+ let input_full_name = get_full_name ( fd, & rpc. input ) ;
277+ let output_full_name = get_full_name ( fd, & rpc. output ) ;
278+
249279 let mut return_token = output_ident. to_token_stream ( ) ;
250280
251281 let mut parameters = quote ! {
@@ -262,7 +292,7 @@ fn generate_stub_rs(plugin: &Plugin, file: &mut TokenStream) {
262292 & mut self ,
263293 } ;
264294 prep = quote ! {
265- let request = EmptyMessage :: new ( ) ;
295+ let request = EmptyMessage :: default ( ) ;
266296 }
267297 }
268298
@@ -281,8 +311,8 @@ fn generate_stub_rs(plugin: &Plugin, file: &mut TokenStream) {
281311 value: i32 ,
282312 } ;
283313 prep = quote ! {
284- let mut request = IntMessage :: new ( ) ;
285- request. set_value ( value) ;
314+ let mut request = IntMessage :: default ( ) ;
315+ request. value = value ;
286316 }
287317 }
288318
@@ -291,7 +321,7 @@ fn generate_stub_rs(plugin: &Plugin, file: &mut TokenStream) {
291321 i32
292322 } ;
293323 post = quote ! {
294- let _response = crate :: Reply { reply: _response. value( ) , fragments: _response. fragments } ;
324+ let _response = crate :: Reply { reply: _response. value, fragments: _response. fragments } ;
295325 }
296326 }
297327
@@ -301,8 +331,8 @@ fn generate_stub_rs(plugin: &Plugin, file: &mut TokenStream) {
301331 value: bool ,
302332 } ;
303333 prep = quote ! {
304- let mut request = SingleBool :: new ( ) ;
305- request. set_Value ( value) ;
334+ let mut request = SingleBool :: default ( ) ;
335+ request. value = Some ( value) ;
306336 }
307337 }
308338
@@ -311,7 +341,7 @@ fn generate_stub_rs(plugin: &Plugin, file: &mut TokenStream) {
311341 bool
312342 } ;
313343 post = quote ! {
314- let _response = crate :: Reply { reply: _response. Value ( ) , fragments: _response. fragments } ;
344+ let _response = crate :: Reply { reply: _response. value ( ) , fragments: _response. fragments } ;
315345 }
316346 }
317347
@@ -320,7 +350,7 @@ fn generate_stub_rs(plugin: &Plugin, file: &mut TokenStream) {
320350 String
321351 } ;
322352 post = quote ! {
323- let _response = crate :: Reply { reply: _response. value( ) . to_string ( ) , fragments: _response. fragments } ;
353+ let _response = crate :: Reply { reply: _response. value. clone ( ) , fragments: _response. fragments } ;
324354 }
325355 }
326356
@@ -331,8 +361,8 @@ fn generate_stub_rs(plugin: &Plugin, file: &mut TokenStream) {
331361 ) -> Result <crate :: Reply <#return_token>, TChannel :: TError > {
332362 #prep
333363 let _response: crate :: Reply <#output_ident> = self . channel. request(
334- #plugin_name. to_string ( ) ,
335- #function_name. to_string ( ) ,
364+ #plugin_name,
365+ #function_name,
336366 request,
337367 ) ?;
338368 #post
@@ -342,14 +372,10 @@ fn generate_stub_rs(plugin: &Plugin, file: &mut TokenStream) {
342372
343373 reflection_vec. extend ( quote ! {
344374 crate :: reflection:: RemoteProcedureDescriptor {
345- name: #function_name. to_string( ) ,
346- plugin_name: #plugin_name. to_string( ) ,
347- input_type: #input_ident:: descriptor( )
348- . full_name( )
349- . to_string( ) ,
350- output_type: #output_ident:: descriptor( )
351- . full_name( )
352- . to_string( ) ,
375+ name: #function_name,
376+ plugin_name: #plugin_name,
377+ input_type: #input_full_name,
378+ output_type: #output_full_name,
353379 } ,
354380 } ) ;
355381 }
0 commit comments