-
-
Notifications
You must be signed in to change notification settings - Fork 586
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate godot compat for dual build
generate compat generate compat Update ci.yml Update binding_generator.py generate compat generate compat lint python files Update compat_generator.py update docs 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 fix path issue when caling from outside Update binding_generator.py update to also take missing classes/structs Update binding_generator.py Generate godot compat for dual build generate compat generate compat Update ci.yml Update binding_generator.py generate compat generate compat lint python files Update compat_generator.py update docs 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 fix path issue when caling from outside Add support for build profiles. Allow enabling or disabling specific classes (which will not be built). Allow forwarding from `ClassDB` to `ClassDBSingleton` to support enumerations update to also take missing classes/structs Update binding_generator.py
- Loading branch information
Showing
6 changed files
with
223 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import re | ||
import os | ||
import json | ||
import sys | ||
|
||
|
||
def parse_header_file(file_path): | ||
types = {"classes": [], "structs": [], "defines": []} | ||
|
||
with open(file_path, "r", encoding="utf-8") as file: | ||
content = file.read() | ||
|
||
# Regular expressions to match different types | ||
struct_pattern = r"struct\s+[\w\s]*?([a-zA-Z_]\w*)\s*[:{]" | ||
class_pattern = r"class\s+[\w\s]*?([a-zA-Z_]\w*)\s*[:{]" | ||
define_pattern = r"#define\s+([a-zA-Z_]\w*)" | ||
|
||
# Extract classes | ||
types["classes"] += re.findall(class_pattern, content) | ||
|
||
# Extract structs | ||
types["structs"] += re.findall(struct_pattern, content) | ||
|
||
# Extract defines | ||
define_matches = re.findall(define_pattern, content) | ||
types["defines"] += define_matches | ||
|
||
if len(types["classes"]) == 0 and len(types["structs"]) == 0 and len(types["defines"]) == 0: | ||
print(f"{file_path} missing things") | ||
return types | ||
|
||
|
||
def map_header_files(directory): | ||
file_types_mapping = {} | ||
|
||
for root, dirs, files in os.walk(directory): | ||
if "thirdparty" in dirs: | ||
dirs.remove("thirdparty") | ||
if "tests" in dirs: | ||
dirs.remove("tests") | ||
if "test" in dirs: | ||
dirs.remove("test") | ||
if "misc" in dirs: | ||
dirs.remove("misc") | ||
for file in files: | ||
if file.endswith(".h") or file.endswith(".hpp"): | ||
relative_path = os.path.relpath(root, directory) | ||
file_path = os.path.join(root, file) | ||
file_types_mapping[f"{relative_path}/{file}"] = parse_header_file(file_path) | ||
|
||
return file_types_mapping | ||
|
||
|
||
if __name__ == "__main__": | ||
# Get current directory | ||
current_directory = os.getcwd() | ||
|
||
if len(sys.argv) > 1: | ||
current_directory = os.path.join(os.getcwd(), sys.argv[1]) | ||
|
||
file_types_mapping = map_header_files(current_directory) | ||
with open("output_header_mapping.json", "w") as json_file: | ||
json.dump(file_types_mapping, json_file, indent=4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import json | ||
|
||
|
||
def match_headers(mapping1, mapping2): | ||
matches = {} | ||
for header_file, data1 in mapping1.items(): | ||
for header_file2, data2 in mapping2.items(): | ||
# Check if classes/defines/structs in header_file1 are present in header_file2 | ||
if (any(class_name in data2["classes"] for class_name in data1["classes"]) or | ||
any(define_name in data2["defines"] for define_name in data1["defines"]) or | ||
any(define_name in data2["structs"] for define_name in data1["structs"])): | ||
if header_file not in matches: | ||
matches[header_file] = [] | ||
matches[header_file].append(header_file2) | ||
return matches | ||
|
||
|
||
if __name__ == "__main__": | ||
# Load the two header mappings | ||
with open("output_header_mapping.json", "r") as file: | ||
mapping1 = json.load(file) | ||
|
||
with open("output_header_mapping_godot.json", "r") as file: | ||
mapping2 = json.load(file) | ||
|
||
# Match the headers | ||
matches = match_headers(mapping1, mapping2) | ||
|
||
# Optionally, you can save the matches to a file | ||
with open("header_matches.json", "w") as outfile: | ||
json.dump(matches, outfile, indent=4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Using output_header_mapping.json convert all imports in specified source folder location from godot imports to godot-compat imports | ||
|
||
|
||
import json | ||
import os | ||
import sys | ||
|
||
from compat_generator import map_header_files | ||
from header_matcher import match_headers | ||
|
||
if __name__ == "__main__": | ||
if len(sys.argv) > 3: | ||
src_directory = os.path.join(os.getcwd(), sys.argv[1]) | ||
godot_directory = os.path.join(os.getcwd(), sys.argv[2]) | ||
godot_cpp_directory = os.path.join(os.getcwd(), sys.argv[3]) | ||
else: | ||
raise Exception("Usage: python module_converter.py <source directory> <godot directory> <godot-cpp directory>") | ||
# Load the godot mappings | ||
with open(f"{godot_directory}/output_header_mapping_godot.json", "r") as file: | ||
godot_mappings = json.load(file) | ||
|
||
# Generate mappings for godot-cpp | ||
godot_cpp_mappings = map_header_files(godot_cpp_directory) | ||
matches = match_headers(godot_mappings, godot_cpp_mappings) | ||
# Save matches to a file | ||
with open("header_matches.json", "w") as outfile: | ||
json.dump(matches, outfile, indent=4) | ||
src_directory = os.getcwd() | ||
# Go through folder specified through all files with .cpp, .h or .hpp | ||
for root, dirs, files in os.walk(src_directory): | ||
for file in files: | ||
if file.endswith(".cpp") or file.endswith(".h") or file.endswith(".hpp"): | ||
with open(os.path.join(root, file), "r") as f: | ||
content = f.read() | ||
|
||
# Replace imports to godot imports with godot_compat imports | ||
for match in matches: | ||
generate_imports = matches[match] | ||
godot_compat_imports = "" | ||
for generate_import in generate_imports: | ||
godot_compat_import = generate_import.replace("gen/include/godot_cpp/", "godot_compat/") | ||
godot_compat_import = godot_compat_import.replace("include/godot_cpp/", "godot_compat/") | ||
godot_compat_imports += f"#include <{godot_compat_import}>\n" | ||
# Remove last 'n from imports | ||
godot_compat_imports = godot_compat_imports[:-1] | ||
content = content.replace(f"#include \"{match}\"", godot_compat_imports) | ||
# Write the modified content back to the file | ||
with open(os.path.join(root, file), "w") as f: | ||
f.write(content) |