| 1 | /**************************************************************************/ |
| 2 | /* gdextension_interface_header_generator.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #ifdef TOOLS_ENABLED |
| 32 | |
| 33 | #include "gdextension_interface_header_generator.h" |
| 34 | |
| 35 | #include "core/io/json.h" |
| 36 | #include "gdextension_interface_dump.gen.h" |
| 37 | |
| 38 | static const char *FILE_HEADER = |
| 39 | "/**************************************************************************/\n" |
| 40 | "/* gdextension_interface.h */\n"; |
| 41 | |
| 42 | static const char *INTRO = |
| 43 | "\n" |
| 44 | "#pragma once\n" |
| 45 | "\n" |
| 46 | "/* This is a C class header, you can copy it and use it directly in your own binders.\n" |
| 47 | " * Together with the `extension_api.json` file, you should be able to generate any binder.\n" |
| 48 | " */\n" |
| 49 | "\n" |
| 50 | "#ifndef __cplusplus\n" |
| 51 | "#include <stddef.h>\n" |
| 52 | "#include <stdint.h>\n" |
| 53 | "\n" |
| 54 | "typedef uint32_t char32_t;\n" |
| 55 | "typedef uint16_t char16_t;\n" |
| 56 | "#else\n" |
| 57 | "#include <cstddef>\n" |
| 58 | "#include <cstdint>\n" |
| 59 | "\n" |
| 60 | "extern \"C\" {\n" |
| 61 | "#endif\n" |
| 62 | "\n"; |
| 63 | |
| 64 | static const char *OUTRO = |
| 65 | "#ifdef __cplusplus\n" |
| 66 | "}\n" |
| 67 | "#endif\n"; |
| 68 | |
| 69 | void GDExtensionInterfaceHeaderGenerator::generate_gdextension_interface_header(const String &p_path) { |
| 70 | Ref<FileAccess> fa = FileAccess::open(p_path, FileAccess::WRITE); |
| 71 | ERR_FAIL_COND_MSG(fa.is_null(), vformat("Cannot open file '%s' for writing.", p_path)); |
| 72 | |
| 73 | Vector<uint8_t> bytes = GDExtensionInterfaceDump::load_gdextension_interface_file(); |
| 74 | String json_string = String::utf8(Span<char>((char *)bytes.ptr(), bytes.size())); |
| 75 | |
| 76 | Ref<JSON> json; |
| 77 | json.instantiate(); |
| 78 | Error err = json->parse(json_string); |
| 79 | ERR_FAIL_COND(err); |
| 80 | |
| 81 | Dictionary data = json->get_data(); |
| 82 | ERR_FAIL_COND(data.is_empty()); |
| 83 | |
| 84 | fa->store_string(FILE_HEADER); |
| 85 | |
| 86 | Array copyright = data["_copyright"]; |
| 87 | for (const Variant &line : copyright) { |
| 88 | fa->store_line(line); |
| 89 | } |
| 90 | |
| 91 | fa->store_string(INTRO); |
| 92 | |
| 93 | Array types = data["types"]; |
| 94 | for (Dictionary type_dict : types) { |
| 95 | if (type_dict.has("description")) { |
| 96 | write_doc(fa, type_dict["description"]); |
| 97 | } |
| 98 | String kind = type_dict["kind"]; |
| 99 | if (kind == "handle") { |
| 100 | type_dict["type"] = type_dict.get("is_const", false) ? "const void*" : "void*"; |
| 101 | write_simple_type(fa, type_dict); |
| 102 | } else if (kind == "alias") { |
| 103 | write_simple_type(fa, type_dict); |
| 104 | } else if (kind == "enum") { |
| 105 | write_enum_type(fa, type_dict); |
| 106 | } else if (kind == "function") { |
| 107 | write_function_type(fa, type_dict); |
| 108 | } else if (kind == "struct") { |
| 109 | write_struct_type(fa, type_dict); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | Array interfaces = data["interface"]; |
| 114 | for (const Variant &interface : interfaces) { |
| 115 | write_interface(fa, interface); |
| 116 | } |
| 117 | |
| 118 | fa->store_string(OUTRO); |
| 119 | } |
| 120 | |
| 121 | void GDExtensionInterfaceHeaderGenerator::write_doc(const Ref<FileAccess> &p_fa, const Array &p_doc, const String &p_indent) { |
| 122 | if (p_doc.size() == 1) { |
| 123 | p_fa->store_string(vformat("%s/* %s */\n", p_indent, p_doc[0])); |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | bool first = true; |
| 128 | for (const Variant &line : p_doc) { |
| 129 | if (first) { |
| 130 | p_fa->store_string(p_indent + "/*"); |
| 131 | first = false; |
| 132 | } else { |
| 133 | p_fa->store_string(p_indent + " *"); |
| 134 | } |
| 135 | |
| 136 | if (line == "") { |
| 137 | p_fa->store_string("\n"); |
| 138 | } else { |
| 139 | p_fa->store_line(String(" ") + (String)line); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | p_fa->store_string(p_indent + " */\n"); |
| 144 | } |
| 145 | |
| 146 | void GDExtensionInterfaceHeaderGenerator::write_simple_type(const Ref<FileAccess> &p_fa, const Dictionary &p_type) { |
| 147 | String type_and_name = format_type_and_name(p_type["type"], p_type["name"]); |
| 148 | p_fa->store_string(vformat("typedef %s;%s\n", type_and_name, make_deprecated_comment_for_type(p_type))); |
| 149 | } |
| 150 | |
| 151 | void GDExtensionInterfaceHeaderGenerator::write_enum_type(const Ref<FileAccess> &p_fa, const Dictionary &p_enum) { |
| 152 | p_fa->store_string("typedef enum {\n"); |
| 153 | Array values = p_enum["values"]; |
| 154 | for (Dictionary value_dict : values) { |
| 155 | if (value_dict.has("description")) { |
| 156 | write_doc(p_fa, value_dict["description"], "\t"); |
| 157 | } |
| 158 | p_fa->store_string(vformat("\t%s = %s,\n", value_dict["name"], (int)value_dict["value"])); |
| 159 | } |
| 160 | p_fa->store_string(vformat("} %s;%s\n\n", p_enum["name"], make_deprecated_comment_for_type(p_enum))); |
| 161 | } |
| 162 | |
| 163 | void GDExtensionInterfaceHeaderGenerator::write_function_type(const Ref<FileAccess> &p_fa, const Dictionary &p_func) { |
| 164 | String args_text = p_func.has("arguments") ? make_args_text(p_func["arguments"]) : ""; |
| 165 | String name_and_args = vformat("(*%s)(%s)", p_func["name"], args_text); |
| 166 | String return_type; |
| 167 | if (p_func.has("return_value")) { |
| 168 | Dictionary ret = p_func["return_value"]; |
| 169 | return_type = ret["type"]; |
| 170 | } else { |
| 171 | return_type = "void"; |
| 172 | } |
| 173 | p_fa->store_string(vformat("typedef %s;%s\n", format_type_and_name(return_type, name_and_args), make_deprecated_comment_for_type(p_func))); |
| 174 | } |
| 175 | |
| 176 | void GDExtensionInterfaceHeaderGenerator::write_struct_type(const Ref<FileAccess> &p_fa, const Dictionary &p_struct) { |
| 177 | p_fa->store_string("typedef struct {\n"); |
| 178 | Array members = p_struct["members"]; |
| 179 | for (Dictionary member_dict : members) { |
| 180 | if (member_dict.has("description")) { |
| 181 | write_doc(p_fa, member_dict["description"], "\t"); |
| 182 | } |
| 183 | p_fa->store_string(vformat("\t%s;\n", format_type_and_name(member_dict["type"], member_dict["name"]))); |
| 184 | } |
| 185 | p_fa->store_string(vformat("} %s;%s\n\n", p_struct["name"], make_deprecated_comment_for_type(p_struct))); |
| 186 | } |
| 187 | |
| 188 | String GDExtensionInterfaceHeaderGenerator::format_type_and_name(const String &p_type, const String &p_name) { |
| 189 | String ret = p_type; |
| 190 | bool is_pointer = false; |
| 191 | if (ret.ends_with("*")) { |
| 192 | ret = ret.substr(0, ret.size() - 2) + " *"; |
| 193 | is_pointer = true; |
| 194 | } |
| 195 | if (!p_name.is_empty()) { |
| 196 | if (is_pointer) { |
| 197 | ret = ret + p_name; |
| 198 | } else { |
| 199 | ret = ret + " " + p_name; |
| 200 | } |
| 201 | } |
| 202 | return ret; |
| 203 | } |
| 204 | |
| 205 | String GDExtensionInterfaceHeaderGenerator::make_deprecated_message(const Dictionary &p_data) { |
| 206 | PackedStringArray parts; |
| 207 | parts.push_back(vformat("Deprecated in Godot %s.", p_data["since"])); |
| 208 | if (p_data.has("message")) { |
| 209 | parts.push_back(p_data["message"]); |
| 210 | } |
| 211 | if (p_data.has("replace_with")) { |
| 212 | parts.push_back(vformat("Use `%s` instead.", p_data["replace_with"])); |
| 213 | } |
| 214 | return String(" ").join(parts); |
| 215 | } |
| 216 | |
| 217 | String GDExtensionInterfaceHeaderGenerator::make_deprecated_comment_for_type(const Dictionary &p_type) { |
| 218 | if (!p_type.has("deprecated")) { |
| 219 | return ""; |
| 220 | } |
| 221 | return vformat(" /* %s */", make_deprecated_message(p_type["deprecated"])); |
| 222 | } |
| 223 | |
| 224 | String GDExtensionInterfaceHeaderGenerator::make_args_text(const Array &p_args) { |
| 225 | Vector<String> combined; |
| 226 | for (Dictionary arg_dict : p_args) { |
| 227 | combined.push_back(format_type_and_name(arg_dict["type"], arg_dict.get("name", String()))); |
| 228 | } |
| 229 | return String(", ").join(combined); |
| 230 | } |
| 231 | |
| 232 | void GDExtensionInterfaceHeaderGenerator::write_interface(const Ref<FileAccess> &p_fa, const Dictionary &p_interface) { |
| 233 | Vector<String> doc; |
| 234 | |
| 235 | doc.push_back(String("@name ") + (String)p_interface["name"]); |
| 236 | doc.push_back(String("@since ") + (String)p_interface["since"]); |
| 237 | |
| 238 | if (p_interface.has("deprecated")) { |
| 239 | doc.push_back(String("@deprecated ") + make_deprecated_message(p_interface["deprecated"])); |
| 240 | } |
| 241 | |
| 242 | Array orig_doc = p_interface["description"]; |
| 243 | for (int i = 0; i < orig_doc.size(); i++) { |
| 244 | // Put an empty line before the 1st and 2nd lines. |
| 245 | if (i <= 1) { |
| 246 | doc.push_back(""); |
| 247 | } |
| 248 | doc.push_back(orig_doc[i]); |
| 249 | } |
| 250 | |
| 251 | if (p_interface.has("arguments")) { |
| 252 | Array args = p_interface["arguments"]; |
| 253 | if (args.size() > 0) { |
| 254 | doc.push_back(""); |
| 255 | for (Dictionary arg_dict : args) { |
| 256 | String arg_string = String("@param ") + (String)arg_dict["name"]; |
| 257 | if (arg_dict.has("description")) { |
| 258 | Array arg_doc = arg_dict["description"]; |
| 259 | for (const Variant &d : arg_doc) { |
| 260 | arg_string += String(" ") + (String)d; |
| 261 | } |
| 262 | } |
| 263 | doc.push_back(arg_string); |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | if (p_interface.has("return_value")) { |
| 269 | Dictionary ret = p_interface["return_value"]; |
| 270 | String ret_string = String("@return"); |
| 271 | if (ret.has("description")) { |
| 272 | Array arg_doc = ret["description"]; |
| 273 | for (const Variant &d : arg_doc) { |
| 274 | ret_string += String(" ") + (String)d; |
| 275 | } |
| 276 | } |
| 277 | doc.push_back(""); |
| 278 | doc.push_back(ret_string); |
| 279 | } |
| 280 | |
| 281 | if (p_interface.has("see")) { |
| 282 | Array see_array = p_interface["see"]; |
| 283 | if (see_array.size() > 0) { |
| 284 | doc.push_back(""); |
| 285 | for (const Variant &see : see_array) { |
| 286 | doc.push_back(String("@see ") + (String)see); |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | p_fa->store_string("/**\n"); |
| 292 | for (const String &d : doc) { |
| 293 | if (d == "") { |
| 294 | p_fa->store_string(" *\n"); |
| 295 | } else { |
| 296 | p_fa->store_string(vformat(" * %s\n", d)); |
| 297 | } |
| 298 | } |
| 299 | p_fa->store_string(" */\n"); |
| 300 | |
| 301 | Dictionary func = p_interface.duplicate(); |
| 302 | func.erase("deprecated"); |
| 303 | if (p_interface.has("legacy_type_name")) { |
| 304 | // @todo When we can break compat, remove this! This maintains legacy type-o's in some type names. |
| 305 | func["name"] = p_interface["legacy_type_name"]; |
| 306 | } else { |
| 307 | // Cannot use `to_pascal_case()` because it'll capitalize after numbers. |
| 308 | Vector<String> words = ((String)p_interface["name"]).split("_"); |
| 309 | for (String &word : words) { |
| 310 | // Cannot use `capitalize()` on the whole string, because it'll separate numbers with a space. |
| 311 | word[0] = String::char_uppercase(word[0]); |
| 312 | } |
| 313 | func["name"] = String("GDExtensionInterface") + String().join(words); |
| 314 | } |
| 315 | write_function_type(p_fa, func); |
| 316 | |
| 317 | p_fa->store_string("\n"); |
| 318 | } |
| 319 | |
| 320 | #endif // TOOLS_ENABLED |