make_virtuals.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. proto = """#define GDVIRTUAL$VER($RET m_name $ARG)\\
  2. StringName _gdvirtual_##m_name##_sn = #m_name;\\
  3. mutable bool _gdvirtual_##m_name##_initialized = false;\\
  4. mutable void *_gdvirtual_##m_name = nullptr;\\
  5. template <bool required>\\
  6. _FORCE_INLINE_ bool _gdvirtual_##m_name##_call($CALLARGS) $CONST {\\
  7. ScriptInstance *_script_instance = ((Object *)(this))->get_script_instance();\\
  8. if (_script_instance) {\\
  9. Callable::CallError ce;\\
  10. $CALLSIARGS\\
  11. $CALLSIBEGIN_script_instance->callp(_gdvirtual_##m_name##_sn, $CALLSIARGPASS, ce);\\
  12. if (ce.error == Callable::CallError::CALL_OK) {\\
  13. $CALLSIRET\\
  14. return true;\\
  15. }\\
  16. }\\
  17. if (unlikely(_get_extension() && !_gdvirtual_##m_name##_initialized)) {\\
  18. _gdvirtual_##m_name = nullptr;\\
  19. if (_get_extension()->get_virtual_call_data && _get_extension()->call_virtual_with_data) {\\
  20. _gdvirtual_##m_name = _get_extension()->get_virtual_call_data(_get_extension()->class_userdata, &_gdvirtual_##m_name##_sn);\\
  21. } else if (_get_extension()->get_virtual) {\\
  22. _gdvirtual_##m_name = (void *)_get_extension()->get_virtual(_get_extension()->class_userdata, &_gdvirtual_##m_name##_sn);\\
  23. }\\
  24. GDVIRTUAL_TRACK(_gdvirtual_##m_name, _gdvirtual_##m_name##_initialized);\\
  25. _gdvirtual_##m_name##_initialized = true;\\
  26. }\\
  27. if (_gdvirtual_##m_name) {\\
  28. $CALLPTRARGS\\
  29. $CALLPTRRETDEF\\
  30. if (_get_extension()->get_virtual_call_data && _get_extension()->call_virtual_with_data) {\\
  31. _get_extension()->call_virtual_with_data(_get_extension_instance(), &_gdvirtual_##m_name##_sn, _gdvirtual_##m_name, $CALLPTRARGPASS, $CALLPTRRETPASS);\\
  32. $CALLPTRRET\\
  33. } else {\\
  34. ((GDExtensionClassCallVirtual)_gdvirtual_##m_name)(_get_extension_instance(), $CALLPTRARGPASS, $CALLPTRRETPASS);\\
  35. $CALLPTRRET\\
  36. }\\
  37. return true;\\
  38. }\\
  39. if (required) {\\
  40. ERR_PRINT_ONCE("Required virtual method " + get_class() + "::" + #m_name + " must be overridden before calling.");\\
  41. $RVOID\\
  42. }\\
  43. return false;\\
  44. }\\
  45. _FORCE_INLINE_ bool _gdvirtual_##m_name##_overridden() const {\\
  46. ScriptInstance *_script_instance = ((Object *)(this))->get_script_instance();\\
  47. if (_script_instance && _script_instance->has_method(_gdvirtual_##m_name##_sn)) {\\
  48. return true;\\
  49. }\\
  50. if (unlikely(_get_extension() && !_gdvirtual_##m_name##_initialized)) {\\
  51. _gdvirtual_##m_name = nullptr;\\
  52. if (_get_extension()->get_virtual_call_data && _get_extension()->call_virtual_with_data) {\\
  53. _gdvirtual_##m_name = _get_extension()->get_virtual_call_data(_get_extension()->class_userdata, &_gdvirtual_##m_name##_sn);\\
  54. } else if (_get_extension()->get_virtual) {\\
  55. _gdvirtual_##m_name = (void *)_get_extension()->get_virtual(_get_extension()->class_userdata, &_gdvirtual_##m_name##_sn);\\
  56. }\\
  57. GDVIRTUAL_TRACK(_gdvirtual_##m_name, _gdvirtual_##m_name##_initialized);\\
  58. _gdvirtual_##m_name##_initialized = true;\\
  59. }\\
  60. if (_gdvirtual_##m_name) {\\
  61. return true;\\
  62. }\\
  63. return false;\\
  64. }\\
  65. _FORCE_INLINE_ static MethodInfo _gdvirtual_##m_name##_get_method_info() {\\
  66. MethodInfo method_info;\\
  67. method_info.name = #m_name;\\
  68. method_info.flags = $METHOD_FLAGS;\\
  69. $FILL_METHOD_INFO\\
  70. return method_info;\\
  71. }
  72. """
  73. def generate_version(argcount, const=False, returns=False):
  74. s = proto
  75. sproto = str(argcount)
  76. method_info = ""
  77. if returns:
  78. sproto += "R"
  79. s = s.replace("$RET", "m_ret,")
  80. s = s.replace("$RVOID", "(void)r_ret;") # If required, may lead to uninitialized errors
  81. s = s.replace("$CALLPTRRETDEF", "PtrToArg<m_ret>::EncodeT ret;")
  82. method_info += "method_info.return_val = GetTypeInfo<m_ret>::get_class_info();\\\n"
  83. method_info += "\t\tmethod_info.return_val_metadata = GetTypeInfo<m_ret>::METADATA;"
  84. else:
  85. s = s.replace("$RET ", "")
  86. s = s.replace("\t\t\t$RVOID\\\n", "")
  87. s = s.replace("\t\t\t$CALLPTRRETDEF\\\n", "")
  88. if const:
  89. sproto += "C"
  90. s = s.replace("$CONST", "const")
  91. s = s.replace("$METHOD_FLAGS", "METHOD_FLAG_VIRTUAL | METHOD_FLAG_CONST")
  92. else:
  93. s = s.replace("$CONST ", "")
  94. s = s.replace("$METHOD_FLAGS", "METHOD_FLAG_VIRTUAL")
  95. s = s.replace("$VER", sproto)
  96. argtext = ""
  97. callargtext = ""
  98. callsiargs = ""
  99. callsiargptrs = ""
  100. callptrargsptr = ""
  101. if argcount > 0:
  102. argtext += ", "
  103. callsiargs = f"Variant vargs[{argcount}] = {{ "
  104. callsiargptrs = f"\t\t\tconst Variant *vargptrs[{argcount}] = {{ "
  105. callptrargsptr = f"\t\t\tGDExtensionConstTypePtr argptrs[{argcount}] = {{ "
  106. callptrargs = ""
  107. for i in range(argcount):
  108. if i > 0:
  109. argtext += ", "
  110. callargtext += ", "
  111. callsiargs += ", "
  112. callsiargptrs += ", "
  113. callptrargs += "\t\t\t"
  114. callptrargsptr += ", "
  115. argtext += f"m_type{i + 1}"
  116. callargtext += f"m_type{i + 1} arg{i + 1}"
  117. callsiargs += f"Variant(arg{i + 1})"
  118. callsiargptrs += f"&vargs[{i}]"
  119. callptrargs += (
  120. f"PtrToArg<m_type{i + 1}>::EncodeT argval{i + 1} = (PtrToArg<m_type{i + 1}>::EncodeT)arg{i + 1};\\\n"
  121. )
  122. callptrargsptr += f"&argval{i + 1}"
  123. if method_info:
  124. method_info += "\\\n\t\t"
  125. method_info += f"method_info.arguments.push_back(GetTypeInfo<m_type{i + 1}>::get_class_info());\\\n"
  126. method_info += f"\t\tmethod_info.arguments_metadata.push_back(GetTypeInfo<m_type{i + 1}>::METADATA);"
  127. if argcount:
  128. callsiargs += " };\\\n"
  129. callsiargptrs += " };"
  130. s = s.replace("$CALLSIARGS", callsiargs + callsiargptrs)
  131. s = s.replace("$CALLSIARGPASS", f"(const Variant **)vargptrs, {argcount}")
  132. callptrargsptr += " };"
  133. s = s.replace("$CALLPTRARGS", callptrargs + callptrargsptr)
  134. s = s.replace("$CALLPTRARGPASS", "reinterpret_cast<GDExtensionConstTypePtr *>(argptrs)")
  135. else:
  136. s = s.replace("\t\t\t$CALLSIARGS\\\n", "")
  137. s = s.replace("$CALLSIARGPASS", "nullptr, 0")
  138. s = s.replace("\t\t\t$CALLPTRARGS\\\n", "")
  139. s = s.replace("$CALLPTRARGPASS", "nullptr")
  140. if returns:
  141. if argcount > 0:
  142. callargtext += ", "
  143. callargtext += "m_ret &r_ret"
  144. s = s.replace("$CALLSIBEGIN", "Variant ret = ")
  145. s = s.replace("$CALLSIRET", "r_ret = VariantCaster<m_ret>::cast(ret);")
  146. s = s.replace("$CALLPTRRETPASS", "&ret")
  147. s = s.replace("$CALLPTRRET", "r_ret = (m_ret)ret;")
  148. else:
  149. s = s.replace("$CALLSIBEGIN", "")
  150. s = s.replace("\t\t\t\t$CALLSIRET\\\n", "")
  151. s = s.replace("$CALLPTRRETPASS", "nullptr")
  152. s = s.replace("\t\t\t\t$CALLPTRRET\\\n", "")
  153. s = s.replace(" $ARG", argtext)
  154. s = s.replace("$CALLARGS", callargtext)
  155. if method_info:
  156. s = s.replace("$FILL_METHOD_INFO", method_info)
  157. else:
  158. s = s.replace("\t\t$FILL_METHOD_INFO\\\n", method_info)
  159. return s
  160. def run(target, source, env):
  161. max_versions = 12
  162. txt = """/* THIS FILE IS GENERATED DO NOT EDIT */
  163. #ifndef GDVIRTUAL_GEN_H
  164. #define GDVIRTUAL_GEN_H
  165. #include "core/object/script_instance.h"
  166. #ifdef TOOLS_ENABLED
  167. #define GDVIRTUAL_TRACK(m_virtual, m_initialized)\\
  168. if (_get_extension()->reloadable) {\\
  169. VirtualMethodTracker *tracker = memnew(VirtualMethodTracker);\\
  170. tracker->method = (void **)&m_virtual;\\
  171. tracker->initialized = &m_initialized;\\
  172. tracker->next = virtual_method_list;\\
  173. virtual_method_list = tracker;\\
  174. }
  175. #else
  176. #define GDVIRTUAL_TRACK(m_virtual, m_initialized)
  177. #endif
  178. """
  179. for i in range(max_versions + 1):
  180. txt += f"/* {i} Arguments */\n\n"
  181. txt += generate_version(i, False, False)
  182. txt += generate_version(i, False, True)
  183. txt += generate_version(i, True, False)
  184. txt += generate_version(i, True, True)
  185. txt += "#endif // GDVIRTUAL_GEN_H\n"
  186. with open(target[0], "w", encoding="utf-8", newline="\n") as f:
  187. f.write(txt)
  188. if __name__ == "__main__":
  189. from platform_methods import subprocess_main
  190. subprocess_main(globals())