88use crate :: context:: Context ;
99use crate :: models:: domain:: TyName ;
1010use crate :: models:: json:: JsonClassConstant ;
11- use crate :: { conv , util} ;
11+ use crate :: util;
1212use proc_macro2:: { Ident , TokenStream } ;
13- use quote:: { format_ident , quote} ;
13+ use quote:: quote;
1414
1515pub fn make_notify_methods ( class_name : & TyName , ctx : & mut Context ) -> TokenStream {
1616 // Note: there are two more methods, but only from Node downwards, not from Object:
@@ -85,10 +85,10 @@ pub fn make_notification_enum(
8585 c = class_name. rust_ty
8686 ) ;
8787
88- let mut notification_enumerators_pascal = Vec :: new ( ) ;
88+ let mut notification_enumerators_shout = Vec :: new ( ) ;
8989 let mut notification_enumerators_ord = Vec :: new ( ) ;
9090 for ( constant_ident, constant_value) in all_constants {
91- notification_enumerators_pascal . push ( constant_ident) ;
91+ notification_enumerators_shout . push ( constant_ident) ;
9292 notification_enumerators_ord. push ( constant_value) ;
9393 }
9494
@@ -97,16 +97,22 @@ pub fn make_notification_enum(
9797 ///
9898 /// Makes it easier to keep an overview all possible notification variants for a given class, including
9999 /// notifications defined in base classes.
100+ ///
101+ /// Contains the [`Unknown`][Self::Unknown] variant for forward compatibility.
100102 #[ derive( Copy , Clone , Eq , PartialEq , Ord , PartialOrd , Hash , Debug ) ]
101103 #[ repr( i32 ) ]
104+ #[ allow( non_camel_case_types) ]
102105 #cfg_attributes
103106 pub enum #enum_name {
104107 #(
105- #notification_enumerators_pascal = #notification_enumerators_ord,
108+ #notification_enumerators_shout = #notification_enumerators_ord,
106109 ) *
107110
108111 /// Since Godot represents notifications as integers, it's always possible that a notification outside the known types
109112 /// is received. For example, the user can manually issue notifications through `Object::notify()`.
113+ ///
114+ /// This is also necessary if you develop an extension on a Godot version and want to be forward-compatible with newer
115+ /// versions. If Godot adds new notifications, they will be unknown to your extension, but you can still handle them.
110116 Unknown ( i32 ) ,
111117 }
112118
@@ -115,7 +121,7 @@ pub fn make_notification_enum(
115121 fn from( enumerator: i32 ) -> Self {
116122 match enumerator {
117123 #(
118- #notification_enumerators_ord => Self :: #notification_enumerators_pascal ,
124+ #notification_enumerators_ord => Self :: #notification_enumerators_shout ,
119125 ) *
120126 other_int => Self :: Unknown ( other_int) ,
121127 }
@@ -126,7 +132,7 @@ pub fn make_notification_enum(
126132 fn from( notification: #enum_name) -> i32 {
127133 match notification {
128134 #(
129- #enum_name:: #notification_enumerators_pascal => #notification_enumerators_ord,
135+ #enum_name:: #notification_enumerators_shout => #notification_enumerators_ord,
130136 ) *
131137 #enum_name:: Unknown ( int) => int,
132138 }
@@ -139,27 +145,20 @@ pub fn make_notification_enum(
139145
140146/// Tries to interpret the constant as a notification one, and transforms it to a Rust identifier on success.
141147pub fn try_to_notification ( constant : & JsonClassConstant ) -> Option < Ident > {
142- constant
143- . name
144- . strip_prefix ( "NOTIFICATION_" )
145- . map ( |s| util:: ident ( & conv:: shout_to_pascal ( s) ) )
148+ constant. name . strip_prefix ( "NOTIFICATION_" ) . map ( util:: ident) // used to be conv::shout_to_pascal(s)
146149}
147150
148151// ----------------------------------------------------------------------------------------------------------------------------------------------
149152// Implementation
150153
151- /// Workaround for Godot bug https://github.com/godotengine/godot/issues/75839
154+ /// Workaround for Godot bug https://github.com/godotengine/godot/issues/75839, fixed in 4.2.
152155///
153156/// Godot has a collision for two notification constants (DRAW, NODE_CACHE_REQUESTED) in the same inheritance branch (as of 4.0.2).
154157/// This cannot be represented in a Rust enum, so we merge the two constants into a single enumerator.
155158fn workaround_constant_collision ( all_constants : & mut Vec < ( Ident , i32 ) > ) {
156- for first in [ "Draw" , "VisibilityChanged" ] {
157- if let Some ( index_of_draw) = all_constants
158- . iter ( )
159- . position ( |( constant_name, _) | constant_name == first)
160- {
161- all_constants[ index_of_draw] . 0 = format_ident ! ( "{first}OrNodeRecacheRequested" ) ;
162- all_constants. retain ( |( constant_name, _) | constant_name != "NodeRecacheRequested" ) ;
163- }
164- }
159+ // This constant has never been used by the engine.
160+ #[ cfg( before_api = "4.2" ) ]
161+ all_constants. retain ( |( constant_name, _) | constant_name != "NODE_RECACHE_REQUESTED" ) ;
162+
163+ let _ = & all_constants; // unused warning
165164}
0 commit comments