1212#
1313# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
1414
15+ from pathlib import Path
1516from raylib import rl , ffi
16-
1717from inspect import ismethod , getmembers , isbuiltin
1818import inflection , sys , json
1919
20- f = open ("raylib.json" , "r" )
21- js = json .load (f )
20+ known_functions = {}
21+ known_structs = {}
22+ for filename in (Path ("raylib.json" ), Path ("raymath.json" ), Path ("rlgl.json" ), Path ("raygui.json" ), Path ("physac.json" )):
23+ f = open (filename , "r" )
24+ js = json .load (f )
25+ for fn in js ["functions" ]:
26+ if fn ["name" ] not in known_functions :
27+ known_functions [fn ["name" ]] = fn
28+ for st in js ["structs" ]:
29+ if st ["name" ] not in known_structs :
30+ known_structs [st ["name" ]] = st
31+
2232
2333def ctype_to_python_type (t ):
2434 if t == '_Bool' :
@@ -51,16 +61,17 @@ def pointer(struct):
5161 ...
5262""" )
5363
54-
55-
64+ # These words can be used for c arg names, but not in python
65+ reserved_words = ( "in" , "list" , "tuple" , "set" , "dict" , "from" , "range" , "min" , "max" , "any" , "all" , "len" )
5666
5767for name , attr in getmembers (rl ):
5868 uname = inflection .underscore (name ).replace ('3_d' , '_3d' ).replace ('2_d' , '_2d' )
5969 if isbuiltin (attr ) or str (type (attr )) == "<class '_cffi_backend.__FFIFunctionWrapper'>" :
60- json_array = [x for x in js ['functions' ] if x ['name' ] == name ]
61- json_object = {}
62- if len (json_array ) > 0 :
63- json_object = json_array [0 ]
70+ json_object = known_functions .get (name , None )
71+ if json_object is None :
72+ # this is _not_ an exported function from raylib, raymath, rlgl raygui or physac
73+ # so we don't want it in the pyray API
74+ continue
6475 sig = ""
6576 for i , arg in enumerate (ffi .typeof (attr ).args ):
6677 param_name = arg .cname .replace ("struct" , "" ).replace ("char *" , "str" ).replace ("*" ,
@@ -69,7 +80,9 @@ def pointer(struct):
6980 if 'params' in json_object :
7081 p = json_object ['params' ]
7182 param_name = list (p )[i ]['name' ]
72-
83+ # don't use a python reserved word:
84+ if param_name in reserved_words :
85+ param_name = param_name + "_" + str (i )
7386 param_type = ctype_to_python_type (arg .cname )
7487 sig += f"{ param_name } : { param_type } ,"
7588
@@ -95,11 +108,13 @@ def pointer(struct):
95108
96109for struct in ffi .list_types ()[0 ]:
97110 print ("processing" , struct , file = sys .stderr )
98- # json_array = [x for x in js['structs'] if x['name'] == name]
99- # json_object = {}
100- # if len(json_array) > 0:
101- # json_object = json_array[0]
111+
102112 if ffi .typeof (struct ).kind == "struct" :
113+ json_object = known_structs .get (struct , None )
114+ if json_object is None :
115+ # this is _not_ an exported struct from raylib, raymath, rlgl raygui or physac
116+ # so we don't want it in the pyray API
117+ continue
103118 if ffi .typeof (struct ).fields is None :
104119 print ("weird empty struct, skipping " + struct , file = sys .stderr )
105120 continue
0 commit comments