2022-06-02 04:13:43 +02:00
|
|
|
# {
|
|
|
|
# instance_id_or_path1:{
|
|
|
|
# method1:[ [p1, p2], [p1, p2] ],
|
|
|
|
# method2:[ [p1, p2], [p1, p2] ]
|
|
|
|
# },
|
|
|
|
# instance_id_or_path1:{
|
|
|
|
# method1:[ [p1, p2], [p1, p2] ],
|
|
|
|
# method2:[ [p1, p2], [p1, p2] ]
|
|
|
|
# },
|
|
|
|
# }
|
|
|
|
var _calls = {}
|
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()
|
|
|
|
var _compare = _utils.Comparator.new()
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
|
2022-06-02 04:13:43 +02:00
|
|
|
func _find_parameters(call_params, params_to_find):
|
|
|
|
var found = false
|
|
|
|
var idx = 0
|
2023-01-20 23:34:39 +01:00
|
|
|
while idx < call_params.size() and !found:
|
2022-06-02 04:13:43 +02:00
|
|
|
var result = _compare.deep(call_params[idx], params_to_find)
|
2023-01-20 23:34:39 +01:00
|
|
|
if result.are_equal:
|
2022-06-02 04:13:43 +02:00
|
|
|
found = true
|
|
|
|
else:
|
|
|
|
idx += 1
|
|
|
|
return found
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
|
2022-06-02 04:13:43 +02:00
|
|
|
func _get_params_as_string(params):
|
2023-01-20 23:34:39 +01:00
|
|
|
var to_return = ""
|
|
|
|
if params == null:
|
|
|
|
return ""
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
for i in range(params.size()):
|
2023-01-20 23:34:39 +01:00
|
|
|
if params[i] == null:
|
|
|
|
to_return += "null"
|
2022-06-02 04:13:43 +02:00
|
|
|
else:
|
2023-01-20 23:34:39 +01:00
|
|
|
if typeof(params[i]) == TYPE_STRING:
|
2022-06-02 04:13:43 +02:00
|
|
|
to_return += str('"', params[i], '"')
|
|
|
|
else:
|
|
|
|
to_return += str(params[i])
|
2023-01-20 23:34:39 +01:00
|
|
|
if i != params.size() - 1:
|
|
|
|
to_return += ", "
|
2022-06-02 04:13:43 +02:00
|
|
|
return to_return
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
|
|
|
|
func add_call(variant, method_name, parameters = null):
|
|
|
|
if !_calls.has(variant):
|
2022-06-02 04:13:43 +02:00
|
|
|
_calls[variant] = {}
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
if !_calls[variant].has(method_name):
|
2022-06-02 04:13:43 +02:00
|
|
|
_calls[variant][method_name] = []
|
|
|
|
|
|
|
|
_calls[variant][method_name].append(parameters)
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
|
|
|
|
func was_called(variant, method_name, parameters = null):
|
2022-06-02 04:13:43 +02:00
|
|
|
var to_return = false
|
2023-01-20 23:34:39 +01:00
|
|
|
if _calls.has(variant) and _calls[variant].has(method_name):
|
|
|
|
if parameters:
|
2022-06-02 04:13:43 +02:00
|
|
|
to_return = _find_parameters(_calls[variant][method_name], parameters)
|
|
|
|
else:
|
|
|
|
to_return = true
|
|
|
|
return to_return
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
|
|
|
|
func get_call_parameters(variant, method_name, index = -1):
|
2022-06-02 04:13:43 +02:00
|
|
|
var to_return = null
|
|
|
|
var get_index = -1
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
if _calls.has(variant) and _calls[variant].has(method_name):
|
2022-06-02 04:13:43 +02:00
|
|
|
var call_size = _calls[variant][method_name].size()
|
2023-01-20 23:34:39 +01:00
|
|
|
if index == -1:
|
2022-06-02 04:13:43 +02:00
|
|
|
# get the most recent call by default
|
2023-01-20 23:34:39 +01:00
|
|
|
get_index = call_size - 1
|
2022-06-02 04:13:43 +02:00
|
|
|
else:
|
|
|
|
get_index = index
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
if get_index < call_size:
|
2022-06-02 04:13:43 +02:00
|
|
|
to_return = _calls[variant][method_name][get_index]
|
|
|
|
else:
|
2023-01-20 23:34:39 +01:00
|
|
|
_lgr.error(
|
|
|
|
str(
|
|
|
|
"Specified index ",
|
|
|
|
index,
|
|
|
|
" is outside range of the number of registered calls: ",
|
|
|
|
call_size
|
|
|
|
)
|
|
|
|
)
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
return to_return
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
|
|
|
|
func call_count(instance, method_name, parameters = null):
|
2022-06-02 04:13:43 +02:00
|
|
|
var to_return = 0
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
if was_called(instance, method_name):
|
|
|
|
if parameters:
|
2022-06-02 04:13:43 +02:00
|
|
|
for i in range(_calls[instance][method_name].size()):
|
2023-01-20 23:34:39 +01:00
|
|
|
if _calls[instance][method_name][i] == parameters:
|
2022-06-02 04:13:43 +02:00
|
|
|
to_return += 1
|
|
|
|
else:
|
|
|
|
to_return = _calls[instance][method_name].size()
|
|
|
|
return to_return
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
|
2022-06-02 04:13:43 +02:00
|
|
|
func clear():
|
|
|
|
_calls = {}
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
|
2022-06-02 04:13:43 +02:00
|
|
|
func get_call_list_as_string(instance):
|
2023-01-20 23:34:39 +01:00
|
|
|
var to_return = ""
|
|
|
|
if _calls.has(instance):
|
2022-06-02 04:13:43 +02:00
|
|
|
for method in _calls[instance]:
|
|
|
|
for i in range(_calls[instance][method].size()):
|
2023-01-20 23:34:39 +01:00
|
|
|
to_return += str(
|
|
|
|
method, "(", _get_params_as_string(_calls[instance][method][i]), ")\n"
|
|
|
|
)
|
2022-06-02 04:13:43 +02:00
|
|
|
return to_return
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
|
2022-06-02 04:13:43 +02:00
|
|
|
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
|