Skip to content

Commit a494391

Browse files
automatically export defines, by @ashleysommer
1 parent be45bef commit a494391

File tree

4 files changed

+1088
-8
lines changed

4 files changed

+1088
-8
lines changed

create_define_consts.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Copyright (c) 2021 Richard Smith and others
2+
#
3+
# This program and the accompanying materials are made available under the
4+
# terms of the Eclipse Public License 2.0 which is available at
5+
# http://www.eclipse.org/legal/epl-2.0.
6+
#
7+
# This Source Code may also be made available under the following Secondary
8+
# licenses when the conditions for such availability set forth in the Eclipse
9+
# Public License, v. 2.0 are satisfied: GNU General Public License, version 2
10+
# with the GNU Classpath Exception which is
11+
# available at https://www.gnu.org/software/classpath/license.html.
12+
#
13+
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
14+
15+
from raylib import rl, ffi
16+
17+
from inspect import ismethod, getmembers, isbuiltin
18+
import inflection, sys, json, re
19+
20+
two_or = re.compile(r'''\(\s*([^\s]*)\s*\|\s*([^\s]*)\s*\)''')
21+
three_or = re.compile(r'''\(\s*([^\s]*)\s*\|\s*([^\s]*)\s*\|\s*(^\s*)\s*\)''')
22+
two_mult = re.compile(r'''\(\s*([^\s]*)\s*\*\s*([^\s]*)\s*\)''')
23+
two_div = re.compile(r'''\(\s*([^\s]*)\s*/\s*([^\s]*)\s*\)''')
24+
25+
def process(filename):
26+
f = open(filename, "r")
27+
js = json.load(f)
28+
known_define = []
29+
known_enum = []
30+
for e in js['enums']:
31+
if e['name'] and e['values']:
32+
for v in e['values']:
33+
if v['name']:
34+
known_enum.append(str(v['name']).strip())
35+
for e in js['defines']:
36+
if e['type'] in ('INT', 'FLOAT', 'STRING'):
37+
if e['type'] == 'INT':
38+
print(e['name'] + ": int = " + str(e['value']).strip())
39+
elif e['type'] == 'FLOAT':
40+
print(e['name'] + ": float = " + str(e['value']).strip())
41+
else:
42+
print(e['name'] + ": str = \"" + str(e['value']).strip() + '"')
43+
known_define.append(str(e['name']).strip())
44+
elif e['type'] == "UNKNOWN":
45+
strval = str(e['value']).strip()
46+
if strval.startswith("__"):
47+
continue
48+
if strval in known_enum:
49+
print(e['name'] + " = raylib." + strval)
50+
elif strval in known_define:
51+
print(e['name'] + " = " + strval)
52+
else:
53+
matches = two_or.match(strval)
54+
if not matches:
55+
matches = three_or.match(strval)
56+
if matches:
57+
match_defs = [str(m).strip() for m in matches.groups()]
58+
if all(d in known_enum for d in match_defs):
59+
print(e['name'] + " = " + " | ".join(("raylib."+m) for m in match_defs))
60+
elif all(d in known_define for d in match_defs):
61+
print(e['name'] + " = " + " | ".join(match_defs))
62+
else:
63+
continue
64+
known_define.append(str(e['name']).strip())
65+
elif e['type'] == "FLOAT_MATH":
66+
strval = str(e['value']).strip()
67+
matches = two_mult.match(strval)
68+
if matches:
69+
match_defs = [str(m).strip() for m in matches.groups()]
70+
match_parts = []
71+
for m in match_defs:
72+
if "." in m:
73+
match_parts.append(m.rstrip("f"))
74+
else:
75+
match_parts.append(m)
76+
if all(d in known_enum for d in match_parts):
77+
print(e['name'] + " = " + " * ".join(("raylib." + m) for m in match_parts))
78+
elif all(d in known_define for d in match_parts):
79+
print(e['name'] + " = " + " * ".join(match_parts))
80+
else:
81+
matches = two_div.match(strval)
82+
if matches:
83+
match_defs = [str(m).strip() for m in matches.groups()]
84+
match_parts = []
85+
for m in match_defs:
86+
if "." in m:
87+
match_parts.append(m.rstrip("f"))
88+
else:
89+
match_parts.append(m)
90+
if any(d in known_enum for d in match_parts):
91+
print(e['name'] + " = " + " / ".join(("raylib." + m) for m in match_parts))
92+
elif any(d in known_define for d in match_parts):
93+
print(e['name'] + " = " + " / ".join(match_parts))
94+
else:
95+
continue
96+
97+
print("import raylib\n")
98+
99+
process("raylib.json")
100+
process("raymath.json")
101+
process("rlgl.json")
102+
process("raygui.json")
103+
process("physac.json")
104+
process("glfw3.json")
105+

0 commit comments

Comments
 (0)