2022-06-02 04:13:43 +02:00
|
|
|
class CallParameters:
|
|
|
|
var p_name = null
|
|
|
|
var default = null
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
func _init(n, d):
|
2022-06-02 04:13:43 +02:00
|
|
|
p_name = n
|
|
|
|
default = d
|
|
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2022-11-09 22:52:54 +01:00
|
|
|
# This class will generate method declaration lines based on method meta
|
2022-06-02 04:13:43 +02:00
|
|
|
# data. It will create defaults that match the method data.
|
|
|
|
#
|
|
|
|
# --------------------
|
|
|
|
# function meta data
|
|
|
|
# --------------------
|
|
|
|
# name:
|
|
|
|
# flags:
|
|
|
|
# args: [{
|
|
|
|
# (class_name:),
|
|
|
|
# (hint:0),
|
|
|
|
# (hint_string:),
|
|
|
|
# (name:),
|
|
|
|
# (type:4),
|
|
|
|
# (usage:7)
|
|
|
|
# }]
|
|
|
|
# default_args []
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
var _utils = load("res://addons/gut/utils.gd").get_instance()
|
2022-06-02 04:13:43 +02:00
|
|
|
var _lgr = _utils.get_logger()
|
2023-01-20 23:34:39 +01:00
|
|
|
const PARAM_PREFIX = "p_"
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
# ------------------------------------------------------
|
|
|
|
# _supported_defaults
|
|
|
|
#
|
|
|
|
# This array contains all the data types that are supported for default values.
|
|
|
|
# If a value is supported it will contain either an empty string or a prefix
|
|
|
|
# that should be used when setting the parameter default value.
|
|
|
|
# For example int, real, bool do not need anything func(p1=1, p2=2.2, p3=false)
|
|
|
|
# but things like Vectors and Colors do since only the parameters to create a
|
|
|
|
# new Vector or Color are included in the metadata.
|
|
|
|
# ------------------------------------------------------
|
2023-01-20 23:34:39 +01:00
|
|
|
# TYPE_NIL = 0 — Variable is of type nil (only applied for null).
|
|
|
|
# TYPE_BOOL = 1 — Variable is of type bool.
|
|
|
|
# TYPE_INT = 2 — Variable is of type int.
|
|
|
|
# TYPE_FLOAT = 3 — Variable is of type float/real.
|
|
|
|
# TYPE_STRING = 4 — Variable is of type String.
|
|
|
|
# TYPE_VECTOR2 = 5 — Variable is of type Vector2.
|
|
|
|
# TYPE_RECT2 = 6 — Variable is of type Rect2.
|
|
|
|
# TYPE_VECTOR3 = 7 — Variable is of type Vector3.
|
|
|
|
# TYPE_COLOR = 14 — Variable is of type Color.
|
|
|
|
# TYPE_OBJECT = 17 — Variable is of type Object.
|
|
|
|
# TYPE_DICTIONARY = 18 — Variable is of type Dictionary.
|
|
|
|
# TYPE_ARRAY = 19 — Variable is of type Array.
|
|
|
|
# TYPE_PACKED_VECTOR2_ARRAY = 24 — Variable is of type PackedVector2Array.
|
|
|
|
# TYPE_TRANSFORM3D = 13 — Variable is of type Transform3D.
|
|
|
|
# TYPE_TRANSFORM2D = 8 — Variable is of type Transform2D.
|
|
|
|
# TYPE_RID = 16 — Variable is of type RID.
|
|
|
|
# TYPE_PACKED_INT32_ARRAY = 21 — Variable is of type PackedInt32Array.
|
|
|
|
# TYPE_PACKED_FLOAT32_ARRAY = 22 — Variable is of type PackedFloat32Array.
|
|
|
|
# TYPE_PACKED_STRING_ARRAY = 23 — Variable is of type PackedStringArray.
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
# TYPE_PLANE = 9 — Variable is of type Plane.
|
2022-11-09 21:57:46 +01:00
|
|
|
# TYPE_QUATERNION = 10 — Variable is of type Quaternion.
|
2022-06-02 04:13:43 +02:00
|
|
|
# TYPE_AABB = 11 — Variable is of type AABB.
|
|
|
|
# TYPE_BASIS = 12 — Variable is of type Basis.
|
|
|
|
# TYPE_NODE_PATH = 15 — Variable is of type NodePath.
|
2022-11-09 21:57:46 +01:00
|
|
|
# TYPE_PACKED_BYTE_ARRAY = 20 — Variable is of type PackedByteArray.
|
|
|
|
# TYPE_PACKED_VECTOR3_ARRAY = 25 — Variable is of type PackedVector3Array.
|
|
|
|
# TYPE_PACKED_COLOR_ARRAY = 26 — Variable is of type PackedColorArray.
|
2022-06-02 04:13:43 +02:00
|
|
|
# TYPE_MAX = 27 — Marker for end of type constants.
|
|
|
|
# ------------------------------------------------------
|
|
|
|
var _supported_defaults = []
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
|
2022-06-02 04:13:43 +02:00
|
|
|
func _init():
|
|
|
|
for _i in range(TYPE_MAX):
|
|
|
|
_supported_defaults.append(null)
|
|
|
|
|
|
|
|
# These types do not require a prefix for defaults
|
2023-01-20 23:34:39 +01:00
|
|
|
_supported_defaults[TYPE_NIL] = ""
|
|
|
|
_supported_defaults[TYPE_BOOL] = ""
|
|
|
|
_supported_defaults[TYPE_INT] = ""
|
|
|
|
_supported_defaults[TYPE_FLOAT] = ""
|
|
|
|
_supported_defaults[TYPE_OBJECT] = ""
|
|
|
|
_supported_defaults[TYPE_ARRAY] = ""
|
|
|
|
_supported_defaults[TYPE_STRING] = ""
|
|
|
|
_supported_defaults[TYPE_STRING_NAME] = ""
|
|
|
|
_supported_defaults[TYPE_DICTIONARY] = ""
|
|
|
|
_supported_defaults[TYPE_PACKED_VECTOR2_ARRAY] = ""
|
|
|
|
_supported_defaults[TYPE_RID] = ""
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
# These require a prefix for whatever default is provided
|
2023-01-20 23:34:39 +01:00
|
|
|
_supported_defaults[TYPE_VECTOR2] = "Vector2"
|
|
|
|
_supported_defaults[TYPE_VECTOR2I] = "Vector2i"
|
|
|
|
_supported_defaults[TYPE_RECT2] = "Rect2"
|
|
|
|
_supported_defaults[TYPE_RECT2I] = "Rect2i"
|
|
|
|
_supported_defaults[TYPE_VECTOR3] = "Vector3"
|
|
|
|
_supported_defaults[TYPE_COLOR] = "Color"
|
|
|
|
_supported_defaults[TYPE_TRANSFORM2D] = "Transform2D"
|
|
|
|
_supported_defaults[TYPE_TRANSFORM3D] = "Transform3D"
|
|
|
|
_supported_defaults[TYPE_PACKED_INT32_ARRAY] = "PackedInt32Array"
|
|
|
|
_supported_defaults[TYPE_PACKED_FLOAT32_ARRAY] = "PackedFloat32Array"
|
|
|
|
_supported_defaults[TYPE_PACKED_STRING_ARRAY] = "PackedStringArray"
|
|
|
|
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
# ###############
|
|
|
|
# Private
|
|
|
|
# ###############
|
2023-01-20 23:34:39 +01:00
|
|
|
var _func_text = _utils.get_file_as_text("res://addons/gut/double_templates/function_template.txt")
|
|
|
|
var _init_text = _utils.get_file_as_text("res://addons/gut/double_templates/init_template.txt")
|
|
|
|
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
func _is_supported_default(type_flag):
|
2023-01-20 23:34:39 +01:00
|
|
|
return (
|
|
|
|
type_flag >= 0
|
|
|
|
and type_flag < _supported_defaults.size()
|
|
|
|
and _supported_defaults[type_flag] != null
|
|
|
|
)
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
func _make_stub_default(method, index):
|
2023-01-20 23:34:39 +01:00
|
|
|
return str('__gutdbl.default_val("', method, '",', index, ")")
|
|
|
|
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
func _make_arg_array(method_meta, override_size):
|
|
|
|
var to_return = []
|
|
|
|
|
|
|
|
var has_unsupported_defaults = false
|
|
|
|
var dflt_start = method_meta.args.size() - method_meta.default_args.size()
|
|
|
|
|
|
|
|
for i in range(method_meta.args.size()):
|
|
|
|
var pname = method_meta.args[i].name
|
2023-01-07 20:26:17 +01:00
|
|
|
var dflt_text = _make_stub_default(method_meta.name, i)
|
2022-06-02 04:13:43 +02:00
|
|
|
to_return.append(CallParameters.new(PARAM_PREFIX + pname, dflt_text))
|
|
|
|
|
|
|
|
# Add in extra parameters from stub settings.
|
2023-01-20 23:34:39 +01:00
|
|
|
if override_size != null:
|
2022-06-02 04:13:43 +02:00
|
|
|
for i in range(method_meta.args.size(), override_size):
|
2023-01-20 23:34:39 +01:00
|
|
|
var pname = str(PARAM_PREFIX, "arg", i)
|
|
|
|
print("-------- ", i, " ", pname)
|
2022-06-02 04:13:43 +02:00
|
|
|
var dflt_text = _make_stub_default(method_meta.name, i)
|
|
|
|
to_return.append(CallParameters.new(pname, dflt_text))
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
return [has_unsupported_defaults, to_return]
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
# Creates a list of parameters with defaults of null unless a default value is
|
|
|
|
# found in the metadata. If a default is found in the meta then it is used if
|
|
|
|
# it is one we know how support.
|
|
|
|
#
|
|
|
|
# If a default is found that we don't know how to handle then this method will
|
|
|
|
# return null.
|
|
|
|
func _get_arg_text(arg_array):
|
2023-01-20 23:34:39 +01:00
|
|
|
var text = ""
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
for i in range(arg_array.size()):
|
2023-01-20 23:34:39 +01:00
|
|
|
text += str(arg_array[i].p_name, "=", arg_array[i].default)
|
|
|
|
if i != arg_array.size() - 1:
|
|
|
|
text += ", "
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
return text
|
|
|
|
|
|
|
|
|
|
|
|
# creates a call to the function in meta in the super's class.
|
2023-01-20 23:34:39 +01:00
|
|
|
func _get_super_call_text(method_name, args, super_name = ""):
|
|
|
|
var params = ""
|
2022-06-02 04:13:43 +02:00
|
|
|
for i in range(args.size()):
|
|
|
|
params += args[i].p_name
|
2023-01-20 23:34:39 +01:00
|
|
|
if i != args.size() - 1:
|
|
|
|
params += ", "
|
2022-06-02 04:13:43 +02:00
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
return str(super_name, "await super(", params, ")")
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
func _get_spy_call_parameters_text(args):
|
2023-01-20 23:34:39 +01:00
|
|
|
var called_with = "null"
|
2022-06-02 04:13:43 +02:00
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
if args.size() > 0:
|
|
|
|
called_with = "["
|
2022-06-02 04:13:43 +02:00
|
|
|
for i in range(args.size()):
|
|
|
|
called_with += args[i].p_name
|
2023-01-20 23:34:39 +01:00
|
|
|
if i < args.size() - 1:
|
|
|
|
called_with += ", "
|
|
|
|
called_with += "]"
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
return called_with
|
|
|
|
|
|
|
|
|
|
|
|
# ###############
|
|
|
|
# Public
|
|
|
|
# ###############
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
|
2023-01-07 20:26:17 +01:00
|
|
|
func _get_init_text(meta, args, method_params, param_array):
|
|
|
|
var text = null
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
var decleration = str("func ", meta.name, "(", method_params, ")")
|
|
|
|
var super_params = ""
|
|
|
|
if args.size() > 0:
|
2023-01-07 20:26:17 +01:00
|
|
|
for i in range(args.size()):
|
|
|
|
super_params += args[i].p_name
|
2023-01-20 23:34:39 +01:00
|
|
|
if i != args.size() - 1:
|
|
|
|
super_params += ", "
|
|
|
|
|
|
|
|
text = _init_text.format(
|
|
|
|
{
|
|
|
|
"func_decleration": decleration,
|
|
|
|
"super_params": super_params,
|
|
|
|
"param_array": param_array,
|
|
|
|
"method_name": meta.name
|
|
|
|
}
|
|
|
|
)
|
2023-01-07 20:26:17 +01:00
|
|
|
|
|
|
|
return text
|
|
|
|
|
2022-06-02 04:13:43 +02:00
|
|
|
|
2022-11-09 22:52:54 +01:00
|
|
|
# Creates a delceration for a function based off of function metadata. All
|
2022-06-02 04:13:43 +02:00
|
|
|
# types whose defaults are supported will have their values. If a datatype
|
|
|
|
# is not supported and the parameter has a default, a warning message will be
|
|
|
|
# printed and the declaration will return null.
|
2023-01-07 20:26:17 +01:00
|
|
|
#
|
|
|
|
# path is no longer used
|
2023-01-20 23:34:39 +01:00
|
|
|
func get_function_text(meta, path = null, override_size = null, super_name = ""):
|
|
|
|
var method_params = ""
|
2022-06-02 04:13:43 +02:00
|
|
|
var text = null
|
2023-01-20 23:34:39 +01:00
|
|
|
if override_size != null:
|
|
|
|
print("!!!!!! ", override_size)
|
2022-06-02 04:13:43 +02:00
|
|
|
var result = _make_arg_array(meta, override_size)
|
|
|
|
var has_unsupported = result[0]
|
|
|
|
var args = result[1]
|
|
|
|
|
|
|
|
var param_array = _get_spy_call_parameters_text(args)
|
2023-01-20 23:34:39 +01:00
|
|
|
if has_unsupported:
|
2022-06-02 04:13:43 +02:00
|
|
|
# This will cause a runtime error. This is the most convenient way to
|
|
|
|
# to stop running before the error gets more obscure. _make_arg_array
|
|
|
|
# generates a gut error when unsupported defaults are found.
|
|
|
|
method_params = null
|
|
|
|
else:
|
2023-01-20 23:34:39 +01:00
|
|
|
method_params = _get_arg_text(args)
|
2023-01-07 20:26:17 +01:00
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
if param_array == "null":
|
|
|
|
param_array = "[]"
|
2023-01-07 20:26:17 +01:00
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
if method_params != null:
|
|
|
|
if meta.name == "_init":
|
|
|
|
text = _get_init_text(meta, args, method_params, param_array)
|
2023-01-07 20:26:17 +01:00
|
|
|
else:
|
2023-01-20 23:34:39 +01:00
|
|
|
var decleration = str("func ", meta.name, "(", method_params, "):")
|
2023-01-07 20:26:17 +01:00
|
|
|
# decleration = str('# ', meta, "\n", decleration)
|
2023-01-20 23:34:39 +01:00
|
|
|
text = _func_text.format(
|
|
|
|
{
|
|
|
|
"func_decleration": decleration,
|
|
|
|
"method_name": meta.name,
|
|
|
|
"param_array": param_array,
|
|
|
|
"super_call": _get_super_call_text(meta.name, args, super_name)
|
|
|
|
}
|
|
|
|
)
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
return text
|
|
|
|
|
|
|
|
|
|
|
|
func get_logger():
|
|
|
|
return _lgr
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
|
2022-06-02 04:13:43 +02:00
|
|
|
func set_logger(logger):
|
|
|
|
_lgr = logger
|