Skip to content

Commit 99a1eb7

Browse files
committed
Update binding_generator.py
Update module_converter.py also collect defines Add module converter file that converts module based projects to godot_compat Update ci.yml update docs Update compat_generator.py lint python files generate compat generate compat generate compat generate compat Update ci.yml
1 parent 4901b0e commit 99a1eb7

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed

compat_generator.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
def parse_header_file(file_path):
88
print(file_path)
9-
types = {"classes": [], "structs": []}
9+
types = {"classes": [], "structs": [], "defines": []}
1010
# print(file_path)
1111

1212
with open(file_path, "r", encoding="utf-8") as file:
@@ -15,13 +15,19 @@ def parse_header_file(file_path):
1515
# Regular expressions to match different types
1616
class_pattern = r"class\s+([a-zA-Z_]\w*)\s*[:{]"
1717
struct_pattern = r"struct\s+([a-zA-Z_]\w*)\s*[:{]"
18+
define_pattern = r"#define\s+([a-zA-Z_]\w*)"
1819

1920
# Extract classes
2021
types["classes"] += re.findall(class_pattern, content)
2122

2223
# Extract structs
2324
types["structs"] += re.findall(struct_pattern, content)
24-
if len(types["classes"]) == 0 and len(types["structs"]) == 0:
25+
26+
# Extract defines
27+
define_matches = re.findall(define_pattern, content)
28+
types["defines"] += define_matches
29+
30+
if len(types["classes"]) == 0 and len(types["structs"]) == 0 and len(types["defines"]) == 0:
2531
print(f"{file_path} missing things")
2632
return types
2733

header_matcher.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ def match_headers(mapping1, mapping2):
55
matches = {}
66
for header_file, data1 in mapping1.items():
77
for header_file2, data2 in mapping2.items():
8-
# Check if classes in header_file1 are present in header_file2
9-
if any(class_name in data2["classes"] for class_name in data1["classes"]):
8+
# Check if classes/defines/structs in header_file1 are present in header_file2
9+
if (any(class_name in data2["classes"] for class_name in data1["classes"]) or
10+
any(define_name in data2["defines"] for define_name in data1["defines"]) or
11+
any(define_name in data2["structs"] for define_name in data1["structs"])):
1012
if header_file not in matches:
1113
matches[header_file] = []
1214
matches[header_file].append(header_file2)

module_converter.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Using output_header_mapping.json convert all imports in specified source folder location from godot imports to godot-compat imports
2+
3+
4+
import json
5+
import os
6+
import sys
7+
8+
from compat_generator import map_header_files
9+
from header_matcher import match_headers
10+
11+
if __name__ == "__main__":
12+
if len(sys.argv) > 2:
13+
current_directory = os.path.join(os.getcwd(), sys.argv[1])
14+
godot_cpp_directory = os.path.join(os.getcwd(), sys.argv[2])
15+
# Load the godot mappings
16+
with open(f"{godot_cpp_directory}/output_header_mapping.json", "r") as file:
17+
godot_mappings = json.load(file)
18+
19+
# Generate mappings for godot-cpp
20+
godot_cpp_mappings = map_header_files(godot_cpp_directory)
21+
matches = match_headers(godot_mappings, godot_cpp_mappings)
22+
# Save matches to a file
23+
with open("header_matches.json", "w") as outfile:
24+
json.dump(matches, outfile, indent=4)
25+
current_directory = os.getcwd()
26+
# Go through folder specified through all files with .cpp, .h or .hpp
27+
for root, dirs, files in os.walk(current_directory):
28+
for file in files:
29+
if file.endswith(".cpp") or file.endswith(".h") or file.endswith(".hpp"):
30+
with open(os.path.join(root, file), "r") as f:
31+
content = f.read()
32+
33+
# Replace imports to godot imports with godot_compat imports
34+
for match in matches:
35+
generate_imports = matches[match]
36+
godot_compat_imports = ""
37+
for generate_import in generate_imports:
38+
godot_compat_import = generate_import.replace("gen/include/godot_cpp/", "godot_compat/")
39+
godot_compat_import = godot_compat_import.replace("include/godot_cpp/", "godot_compat/")
40+
godot_compat_imports += f"#include <{godot_compat_import}>\n"
41+
# Remove last 'n from imports
42+
godot_compat_imports = godot_compat_imports[:-1]
43+
content = content.replace(f"#include \"{match}\"", godot_compat_imports)
44+
# Write the modified content back to the file
45+
with open(os.path.join(root, file), "w") as f:
46+
f.write(content)

0 commit comments

Comments
 (0)