-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from gilzoide/global-enums
Add global enum bindings
- Loading branch information
Showing
13 changed files
with
139 additions
and
114 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
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,65 @@ | ||
import json | ||
from os import path | ||
|
||
SRC_DIR = path.dirname(__file__) | ||
DEST_DIR = path.join(SRC_DIR, "generated") | ||
API_JSON_PATH = path.join(SRC_DIR, "..", "lib", "godot-cpp", "gdextension", "extension_api.json") | ||
GODOT_CPP_DIR = path.join(SRC_DIR, "..", "lib", "godot-cpp") | ||
GODOT_CPP_UTILITY_FUNCTIONS = path.join(GODOT_CPP_DIR, "gen", "include", "godot_cpp", "variant", "utility_functions.hpp") | ||
PRIMITIVE_VARIANTS = [ | ||
"bool", | ||
"int", | ||
"float", | ||
] | ||
|
||
|
||
def generate_utility_functions(utility_functions): | ||
lines = [ | ||
"#undef register_utility_functions\n#define register_utility_functions(state)" | ||
] | ||
with open(GODOT_CPP_UTILITY_FUNCTIONS) as f: | ||
godot_cpp_utility_functions = f.read() | ||
for f in utility_functions: | ||
name = f["name"] | ||
if name not in godot_cpp_utility_functions: | ||
# godot-cpp does not implement "typeof", just skip anything that is | ||
# not mentioned in the header | ||
continue | ||
if f.get("is_vararg", False): | ||
lines.append(f'\tstate.set("{name}", wrap_function(&UtilityFunctions::{name}_internal));') | ||
elif ( | ||
f.get("return_type") not in PRIMITIVE_VARIANTS | ||
or any(arg["type"] not in PRIMITIVE_VARIANTS for arg in f.get("arguments", [])) | ||
): | ||
lines.append(f'\tstate.set("{name}", wrap_function(&UtilityFunctions::{name}));') | ||
else: | ||
lines.append(f'\tstate.set("{name}", &UtilityFunctions::{name});') | ||
return " \\\n".join(lines) + "\n" | ||
|
||
|
||
def generate_enums(global_enums): | ||
lines = [ | ||
"#undef register_global_enums\n#define register_global_enums(state)" | ||
] | ||
for enum in global_enums: | ||
lines.append(f"\t/* {enum['name']} */") | ||
for value in enum["values"]: | ||
lines.append(f'\tstate.set("{value["name"]}", {value["value"]});') | ||
return " \\\n".join(lines) + "\n" | ||
|
||
|
||
def main(): | ||
with open(API_JSON_PATH) as f: | ||
api = json.load(f) | ||
|
||
with open(path.join(DEST_DIR, "utility_functions.hpp"), "w") as f: | ||
code = generate_utility_functions(api["utility_functions"]) | ||
f.write(code) | ||
|
||
with open(path.join(DEST_DIR, "global_enums.hpp"), "w") as f: | ||
code = generate_enums(api["global_enums"]) | ||
f.write(code) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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,36 @@ | ||
/** | ||
* Copyright (C) 2023 Gil Barbosa Reis. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
* this software and associated documentation files (the “Software”), to deal in | ||
* the Software without restriction, including without limitation the rights to | ||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | ||
* of the Software, and to permit persons to whom the Software is furnished to do | ||
* so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
#include "../generated/global_enums.hpp" | ||
#include "../utils/module_names.hpp" | ||
|
||
#include <sol/sol.hpp> | ||
|
||
using namespace luagdextension; | ||
|
||
extern "C" int luaopen_godot_enums(lua_State *L) { | ||
sol::state_view state = L; | ||
|
||
register_global_enums(state); | ||
|
||
return 0; | ||
} | ||
|
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
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,4 @@ | ||
assert(SIDE_LEFT == 0) | ||
assert(SIDE_TOP == 1) | ||
assert(SIDE_RIGHT == 2) | ||
assert(SIDE_BOTTOM == 3) |