make_virtuals.py 6.1 KB

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