2022-06-02 04:13:43 +02:00
|
|
|
# ##############################################################################
|
|
|
|
#(G)odot (U)nit (T)est class
|
|
|
|
#
|
|
|
|
# ##############################################################################
|
|
|
|
# The MIT License (MIT)
|
|
|
|
# =====================
|
|
|
|
#
|
|
|
|
# Copyright (c) 2020 Tom "Butch" Wesley
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be included in
|
|
|
|
# all copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
# THE SOFTWARE.
|
|
|
|
#
|
|
|
|
# ##############################################################################
|
|
|
|
# Description
|
|
|
|
# -----------
|
|
|
|
# This class is a PSUEDO SINGLETON. You should not make instances of it but use
|
|
|
|
# the get_instance static method.
|
|
|
|
# ##############################################################################
|
|
|
|
extends Node
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
|
2022-06-02 04:13:43 +02:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# The instance name as a function since you can't have static variables.
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
static func INSTANCE_NAME():
|
2023-01-20 23:34:39 +01:00
|
|
|
return "__GutUtilsInstName__"
|
|
|
|
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Gets the root node without having to be in the tree and pushing out an error
|
|
|
|
# if we don't have a main loop ready to go yet.
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
static func get_root_node():
|
|
|
|
var main_loop = Engine.get_main_loop()
|
2023-01-20 23:34:39 +01:00
|
|
|
if main_loop != null:
|
2022-06-02 04:13:43 +02:00
|
|
|
return main_loop.root
|
|
|
|
else:
|
2023-01-20 23:34:39 +01:00
|
|
|
push_error("No Main Loop Yet")
|
2022-06-02 04:13:43 +02:00
|
|
|
return null
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
|
2022-06-02 04:13:43 +02:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Get the ONE instance of utils
|
2023-01-07 20:26:17 +01:00
|
|
|
# Since we can't have static variables we have to store the instance in the
|
|
|
|
# tree. This means you have to wait a bit for the main loop to be up and
|
|
|
|
# running.
|
2022-06-02 04:13:43 +02:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
static func get_instance():
|
|
|
|
var the_root = get_root_node()
|
|
|
|
var inst = null
|
2023-01-20 23:34:39 +01:00
|
|
|
if the_root.has_node(INSTANCE_NAME()):
|
2022-06-02 04:13:43 +02:00
|
|
|
inst = the_root.get_node(INSTANCE_NAME())
|
|
|
|
else:
|
2023-01-20 23:34:39 +01:00
|
|
|
inst = load("res://addons/gut/utils.gd").new()
|
2022-06-02 04:13:43 +02:00
|
|
|
inst.set_name(INSTANCE_NAME())
|
|
|
|
the_root.add_child(inst)
|
|
|
|
return inst
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
|
|
|
|
var Logger = load("res://addons/gut/logger.gd") # everything should use get_logger
|
2022-06-02 04:13:43 +02:00
|
|
|
var _lgr = null
|
2023-01-07 20:26:17 +01:00
|
|
|
var json = JSON.new()
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
var _test_mode = false
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
var AutoFree = load("res://addons/gut/autofree.gd")
|
|
|
|
var Awaiter = load("res://addons/gut/awaiter.gd")
|
|
|
|
var Comparator = load("res://addons/gut/comparator.gd")
|
|
|
|
var CompareResult = load("res://addons/gut/compare_result.gd")
|
|
|
|
var DiffTool = load("res://addons/gut/diff_tool.gd")
|
|
|
|
var Doubler = load("res://addons/gut/doubler.gd")
|
|
|
|
var Gut = load("res://addons/gut/gut.gd")
|
|
|
|
var HookScript = load("res://addons/gut/hook_script.gd")
|
|
|
|
var InnerClassRegistry = load("res://addons/gut/inner_class_registry.gd")
|
2022-06-02 04:13:43 +02:00
|
|
|
var InputFactory = load("res://addons/gut/input_factory.gd")
|
|
|
|
var InputSender = load("res://addons/gut/input_sender.gd")
|
2023-01-20 23:34:39 +01:00
|
|
|
var JunitXmlExport = load("res://addons/gut/junit_xml_export.gd")
|
|
|
|
var MethodMaker = load("res://addons/gut/method_maker.gd")
|
|
|
|
var OneToMany = load("res://addons/gut/one_to_many.gd")
|
|
|
|
var OrphanCounter = load("res://addons/gut/orphan_counter.gd")
|
|
|
|
var ParameterFactory = load("res://addons/gut/parameter_factory.gd")
|
|
|
|
var ParameterHandler = load("res://addons/gut/parameter_handler.gd")
|
|
|
|
var Printers = load("res://addons/gut/printers.gd")
|
|
|
|
var ResultExporter = load("res://addons/gut/result_exporter.gd")
|
|
|
|
var ScriptCollector = load("res://addons/gut/script_parser.gd")
|
|
|
|
var Spy = load("res://addons/gut/spy.gd")
|
|
|
|
var Strutils = load("res://addons/gut/strutils.gd")
|
|
|
|
var Stubber = load("res://addons/gut/stubber.gd")
|
|
|
|
var StubParams = load("res://addons/gut/stub_params.gd")
|
|
|
|
var Summary = load("res://addons/gut/summary.gd")
|
|
|
|
var Test = load("res://addons/gut/test.gd")
|
|
|
|
var TestCollector = load("res://addons/gut/test_collector.gd")
|
|
|
|
var ThingCounter = load("res://addons/gut/thing_counter.gd")
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
# Source of truth for the GUT version
|
2023-01-20 23:34:39 +01:00
|
|
|
var version = "7.4.1"
|
2022-06-02 04:13:43 +02:00
|
|
|
# The required Godot version as an array.
|
|
|
|
var req_godot = [3, 2, 0]
|
2023-01-07 20:26:17 +01:00
|
|
|
|
2022-11-09 22:52:54 +01:00
|
|
|
# Online fetch of the latest version available on github
|
2022-06-02 04:13:43 +02:00
|
|
|
var latest_version = null
|
|
|
|
var should_display_latest_version = false
|
|
|
|
|
|
|
|
# These methods all call super implicitly. Stubbing them to call super causes
|
|
|
|
# super to be called twice.
|
|
|
|
var non_super_methods = [
|
|
|
|
"_init",
|
|
|
|
"_ready",
|
|
|
|
"_notification",
|
|
|
|
"_enter_world",
|
|
|
|
"_exit_world",
|
|
|
|
"_process",
|
|
|
|
"_physics_process",
|
|
|
|
"_exit_tree",
|
|
|
|
"_gui_input ",
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
func _ready() -> void:
|
|
|
|
_http_request_latest_version()
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
|
2022-06-02 04:13:43 +02:00
|
|
|
func _http_request_latest_version() -> void:
|
2023-01-07 20:26:17 +01:00
|
|
|
return
|
2022-06-02 04:13:43 +02:00
|
|
|
var http_request = HTTPRequest.new()
|
|
|
|
http_request.name = "http_request"
|
|
|
|
add_child(http_request)
|
2023-01-20 23:34:39 +01:00
|
|
|
http_request.connect(
|
|
|
|
"request_completed", Callable(self, "_on_http_request_latest_version_completed")
|
|
|
|
)
|
2022-06-02 04:13:43 +02:00
|
|
|
# Perform a GET request. The URL below returns JSON as of writing.
|
2023-01-07 20:26:17 +01:00
|
|
|
var __error = http_request.request("https://api.github.com/repos/bitwes/Gut/releases/latest")
|
2022-06-02 04:13:43 +02:00
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
|
2022-06-02 04:13:43 +02:00
|
|
|
func _on_http_request_latest_version_completed(result, response_code, headers, body):
|
|
|
|
if not result == HTTPRequest.RESULT_SUCCESS:
|
|
|
|
return
|
|
|
|
|
2022-11-09 21:57:46 +01:00
|
|
|
var test_json_conv = JSON.new()
|
|
|
|
test_json_conv.parse(body.get_string_from_utf8())
|
|
|
|
var response = test_json_conv.get_data()
|
2022-06-02 04:13:43 +02:00
|
|
|
# Will print the user agent string used by the HTTPRequest node (as recognized by httpbin.org).
|
|
|
|
if response:
|
|
|
|
if response.get("html_url"):
|
|
|
|
latest_version = Array(response.html_url.split("/")).pop_back().right(1)
|
|
|
|
if latest_version != version:
|
|
|
|
should_display_latest_version = true
|
|
|
|
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
const GUT_METADATA = "__gutdbl"
|
2022-06-02 04:13:43 +02:00
|
|
|
|
2023-01-07 20:26:17 +01:00
|
|
|
# Note, these cannot change since places are checking for TYPE_INT to determine
|
|
|
|
# how to process parameters.
|
2023-01-20 23:34:39 +01:00
|
|
|
enum DOUBLE_STRATEGY { SCRIPT_ONLY, INCLUDE_SUPER }
|
|
|
|
|
|
|
|
enum DIFF { DEEP, SHALLOW, SIMPLE }
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Blurb of text with GUT and Godot versions.
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
func get_version_text():
|
|
|
|
var v_info = Engine.get_version_info()
|
2023-01-20 23:34:39 +01:00
|
|
|
var gut_version_info = str("GUT version: ", version)
|
|
|
|
var godot_version_info = str(
|
|
|
|
"Godot version: ", v_info.major, ".", v_info.minor, ".", v_info.patch
|
|
|
|
)
|
2022-06-02 04:13:43 +02:00
|
|
|
return godot_version_info + "\n" + gut_version_info
|
|
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Returns a nice string for erroring out when we have a bad Godot version.
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
func get_bad_version_text():
|
2023-01-20 23:34:39 +01:00
|
|
|
var ver = ".".join(PackedStringArray(req_godot))
|
2022-06-02 04:13:43 +02:00
|
|
|
var info = Engine.get_version_info()
|
2023-01-20 23:34:39 +01:00
|
|
|
var gd_version = str(info.major, ".", info.minor, ".", info.patch)
|
|
|
|
return (
|
|
|
|
"GUT " + version + " requires Godot " + ver + " or greater. Godot version is " + gd_version
|
|
|
|
)
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Checks the Godot version against req_godot array.
|
|
|
|
# ------------------------------------------------------------------------------
|
2023-01-20 23:34:39 +01:00
|
|
|
func is_version_ok(engine_info = Engine.get_version_info(), required = req_godot):
|
2022-06-02 04:13:43 +02:00
|
|
|
var is_ok = null
|
|
|
|
var engine_array = [engine_info.major, engine_info.minor, engine_info.patch]
|
|
|
|
|
|
|
|
var idx = 0
|
2023-01-20 23:34:39 +01:00
|
|
|
while is_ok == null and idx < engine_array.size():
|
|
|
|
if engine_array[idx] > required[idx]:
|
2022-06-02 04:13:43 +02:00
|
|
|
is_ok = true
|
2023-01-20 23:34:39 +01:00
|
|
|
elif engine_array[idx] < required[idx]:
|
2022-06-02 04:13:43 +02:00
|
|
|
is_ok = false
|
|
|
|
|
|
|
|
idx += 1
|
|
|
|
|
|
|
|
# still null means each index was the same.
|
|
|
|
return nvl(is_ok, true)
|
|
|
|
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
func godot_version(engine_info = Engine.get_version_info()):
|
|
|
|
return str(engine_info.major, ".", engine_info.minor, ".", engine_info.patch)
|
2023-01-07 20:26:17 +01:00
|
|
|
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
func is_godot_version(expected, engine_info = Engine.get_version_info()):
|
2023-01-07 20:26:17 +01:00
|
|
|
var engine_array = [engine_info.major, engine_info.minor, engine_info.patch]
|
2023-01-20 23:34:39 +01:00
|
|
|
var expected_array = expected.split(".")
|
2023-01-07 20:26:17 +01:00
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
if expected_array.size() > engine_array.size():
|
2023-01-07 20:26:17 +01:00
|
|
|
return false
|
|
|
|
|
|
|
|
var is_version = true
|
|
|
|
var i = 0
|
2023-01-20 23:34:39 +01:00
|
|
|
while i < expected_array.size() and i < engine_array.size() and is_version:
|
|
|
|
if expected_array[i] == str(engine_array[i]):
|
2023-01-07 20:26:17 +01:00
|
|
|
i += 1
|
|
|
|
else:
|
|
|
|
is_version = false
|
|
|
|
|
|
|
|
return is_version
|
|
|
|
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
func is_godot_version_gte(expected, engine_info = Engine.get_version_info()):
|
|
|
|
return is_version_ok(engine_info, expected.split("."))
|
2023-01-07 20:26:17 +01:00
|
|
|
|
|
|
|
|
2022-06-02 04:13:43 +02:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Everything should get a logger through this.
|
|
|
|
#
|
|
|
|
# When running in test mode this will always return a new logger so that errors
|
|
|
|
# are not caused by getting bad warn/error/etc counts.
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
func get_logger():
|
2023-01-20 23:34:39 +01:00
|
|
|
if _test_mode:
|
2022-06-02 04:13:43 +02:00
|
|
|
return Logger.new()
|
|
|
|
else:
|
2023-01-20 23:34:39 +01:00
|
|
|
if _lgr == null:
|
2022-06-02 04:13:43 +02:00
|
|
|
_lgr = Logger.new()
|
|
|
|
return _lgr
|
|
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# return if_null if value is null otherwise return value
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
func nvl(value, if_null):
|
2023-01-20 23:34:39 +01:00
|
|
|
if value == null:
|
2022-06-02 04:13:43 +02:00
|
|
|
return if_null
|
|
|
|
else:
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# returns true if the object has been freed, false if not
|
|
|
|
#
|
|
|
|
# From what i've read, the weakref approach should work. It seems to work most
|
|
|
|
# of the time but sometimes it does not catch it. The str comparison seems to
|
|
|
|
# fill in the gaps. I've not seen any errors after adding that check.
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
func is_freed(obj):
|
|
|
|
var wr = weakref(obj)
|
2023-01-20 23:34:39 +01:00
|
|
|
return !(wr.get_ref() and str(obj) != "<Freed Object>")
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Pretty self explanitory.
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
func is_not_freed(obj):
|
|
|
|
return !is_freed(obj)
|
|
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Checks if the passed in object is a GUT Double or Partial Double.
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
func is_double(obj):
|
|
|
|
var to_return = false
|
2023-01-20 23:34:39 +01:00
|
|
|
if typeof(obj) == TYPE_OBJECT and is_instance_valid(obj):
|
|
|
|
to_return = obj.has_method("__gutdbl_check_method__")
|
2022-06-02 04:13:43 +02:00
|
|
|
return to_return
|
|
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Checks if the passed in is an instance of a class
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
func is_instance(obj):
|
2023-01-20 23:34:39 +01:00
|
|
|
return (
|
|
|
|
typeof(obj) == TYPE_OBJECT
|
|
|
|
and !is_native_class(obj)
|
|
|
|
and !obj.has_method("new")
|
|
|
|
and !obj.has_method("instantiate")
|
|
|
|
)
|
|
|
|
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Checks if the passed in is a GDScript
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
func is_gdscript(obj):
|
2023-01-20 23:34:39 +01:00
|
|
|
return typeof(obj) == TYPE_OBJECT and str(obj).begins_with("<GDScript#")
|
2023-01-07 20:26:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Checks if the passed in is an inner class
|
|
|
|
#
|
|
|
|
# Looks like the resource_path will be populated for gdscripts, and not populated
|
|
|
|
# for gdscripts inside a gdscript.
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
func is_inner_class(obj):
|
2023-01-20 23:34:39 +01:00
|
|
|
return is_gdscript(obj) and obj.resource_path == ""
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
2022-11-09 22:52:54 +01:00
|
|
|
# Returns an array of values by calling get(property) on each element in source
|
2022-06-02 04:13:43 +02:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
func extract_property_from_array(source, property):
|
|
|
|
var to_return = []
|
2023-01-20 23:34:39 +01:00
|
|
|
for i in source.size():
|
2022-06-02 04:13:43 +02:00
|
|
|
to_return.append(source[i].get(property))
|
|
|
|
return to_return
|
|
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# true if file exists, false if not.
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
func file_exists(path):
|
2023-01-07 20:26:17 +01:00
|
|
|
return FileAccess.file_exists(path)
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Write a file.
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
func write_file(path, content):
|
2023-01-07 20:26:17 +01:00
|
|
|
var f = FileAccess.open(path, FileAccess.WRITE)
|
2023-01-20 23:34:39 +01:00
|
|
|
if f != null:
|
2022-06-02 04:13:43 +02:00
|
|
|
f.store_string(content)
|
2023-01-20 23:34:39 +01:00
|
|
|
f = null
|
2022-06-02 04:13:43 +02:00
|
|
|
|
2023-01-07 20:26:17 +01:00
|
|
|
return FileAccess.get_open_error()
|
2022-06-02 04:13:43 +02:00
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
|
2022-06-02 04:13:43 +02:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# true if what is passed in is null or an empty string.
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
func is_null_or_empty(text):
|
2023-01-20 23:34:39 +01:00
|
|
|
return text == null or text == ""
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Get the name of a native class or null if the object passed in is not a
|
|
|
|
# native class.
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
func get_native_class_name(thing):
|
|
|
|
var to_return = null
|
2023-01-20 23:34:39 +01:00
|
|
|
if is_native_class(thing):
|
2022-06-02 04:13:43 +02:00
|
|
|
var newone = thing.new()
|
|
|
|
to_return = newone.get_class()
|
2023-01-20 23:34:39 +01:00
|
|
|
if !newone is RefCounted:
|
2022-06-02 04:13:43 +02:00
|
|
|
newone.free()
|
|
|
|
return to_return
|
|
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Checks an object to see if it is a GDScriptNativeClass
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
func is_native_class(thing):
|
|
|
|
var it_is = false
|
2023-01-20 23:34:39 +01:00
|
|
|
if typeof(thing) == TYPE_OBJECT:
|
2023-01-07 20:26:17 +01:00
|
|
|
it_is = str(thing).begins_with("<GDScriptNativeClass#")
|
2022-06-02 04:13:43 +02:00
|
|
|
return it_is
|
|
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Returns the text of a file or an empty string if the file could not be opened.
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
func get_file_as_text(path):
|
2023-01-20 23:34:39 +01:00
|
|
|
var to_return = ""
|
2023-01-07 20:26:17 +01:00
|
|
|
var f = FileAccess.open(path, FileAccess.READ)
|
2023-01-20 23:34:39 +01:00
|
|
|
if f != null:
|
2022-06-02 04:13:43 +02:00
|
|
|
to_return = f.get_as_text()
|
2023-01-07 20:26:17 +01:00
|
|
|
f = null
|
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
|
|
|
# ------------------------------------------------------------------------------
|
2022-11-09 22:52:54 +01:00
|
|
|
# Loops through an array of things and calls a method or checks a property on
|
2023-01-07 20:26:17 +01:00
|
|
|
# each element until it finds the returned value. -1 is returned if not found
|
|
|
|
# or the index is returned if found.
|
2022-06-02 04:13:43 +02:00
|
|
|
# ------------------------------------------------------------------------------
|
2023-01-07 20:26:17 +01:00
|
|
|
func search_array_idx(ar, prop_method, value):
|
2022-06-02 04:13:43 +02:00
|
|
|
var found = false
|
|
|
|
var idx = 0
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
while idx < ar.size() and !found:
|
2022-06-02 04:13:43 +02:00
|
|
|
var item = ar[idx]
|
2023-01-07 20:26:17 +01:00
|
|
|
var prop = item.get(prop_method)
|
2023-01-20 23:34:39 +01:00
|
|
|
if !(prop is Callable):
|
|
|
|
if item.get(prop_method) == value:
|
2022-06-02 04:13:43 +02:00
|
|
|
found = true
|
2023-01-20 23:34:39 +01:00
|
|
|
elif prop != null:
|
2023-01-07 20:26:17 +01:00
|
|
|
var called_val = prop.call()
|
2023-01-20 23:34:39 +01:00
|
|
|
if called_val == value:
|
2022-06-02 04:13:43 +02:00
|
|
|
found = true
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
if !found:
|
2022-06-02 04:13:43 +02:00
|
|
|
idx += 1
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
if found:
|
2023-01-07 20:26:17 +01:00
|
|
|
return idx
|
|
|
|
else:
|
|
|
|
return -1
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
|
2023-01-07 20:26:17 +01:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Loops through an array of things and calls a method or checks a property on
|
|
|
|
# each element until it finds the returned value. The item in the array is
|
|
|
|
# returned or null if it is not found (this method originally came first).
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
func search_array(ar, prop_method, value):
|
|
|
|
var idx = search_array_idx(ar, prop_method, value)
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
if idx != -1:
|
2022-06-02 04:13:43 +02:00
|
|
|
return ar[idx]
|
|
|
|
else:
|
|
|
|
return null
|
|
|
|
|
|
|
|
|
|
|
|
func are_datatypes_same(got, expected):
|
|
|
|
return !(typeof(got) != typeof(expected) and got != null and expected != null)
|
|
|
|
|
|
|
|
|
|
|
|
func pretty_print(dict):
|
2023-01-20 23:34:39 +01:00
|
|
|
print(json.stringify(dict, " "))
|
2022-06-02 04:13:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
func get_script_text(obj):
|
|
|
|
return obj.get_script().get_source_code()
|
|
|
|
|
|
|
|
|
|
|
|
func get_singleton_by_name(name):
|
|
|
|
var source = str("var singleton = ", name)
|
|
|
|
var script = GDScript.new()
|
|
|
|
script.set_source_code(source)
|
|
|
|
script.reload()
|
|
|
|
return script.new().singleton
|
2023-01-07 20:26:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
func dec2bistr(decimal_value, max_bits = 31):
|
|
|
|
var binary_string = ""
|
|
|
|
var temp
|
|
|
|
var count = max_bits
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
while count >= 0:
|
2023-01-07 20:26:17 +01:00
|
|
|
temp = decimal_value >> count
|
2023-01-20 23:34:39 +01:00
|
|
|
if temp & 1:
|
2023-01-07 20:26:17 +01:00
|
|
|
binary_string = binary_string + "1"
|
|
|
|
else:
|
|
|
|
binary_string = binary_string + "0"
|
|
|
|
count -= 1
|
|
|
|
|
|
|
|
return binary_string
|
|
|
|
|
|
|
|
|
|
|
|
func add_line_numbers(contents):
|
2023-01-20 23:34:39 +01:00
|
|
|
if contents == null:
|
|
|
|
return ""
|
2023-01-07 20:26:17 +01:00
|
|
|
|
|
|
|
var to_return = ""
|
|
|
|
var lines = contents.split("\n")
|
|
|
|
var line_num = 1
|
|
|
|
for line in lines:
|
2023-01-20 23:34:39 +01:00
|
|
|
var line_str = str(line_num).lpad(6, " ")
|
|
|
|
to_return += str(line_str, " |", line, "\n")
|
2023-01-07 20:26:17 +01:00
|
|
|
line_num += 1
|
|
|
|
return to_return
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
|
|
|
|
func pp(dict, indent = ""):
|
|
|
|
var text = json.stringify(dict, " ")
|
2023-01-07 20:26:17 +01:00
|
|
|
print(text)
|
|
|
|
|
|
|
|
|
|
|
|
var _created_script_count = 0
|
2023-01-20 23:34:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
func create_script_from_source(source, override_path = null):
|
2023-01-07 20:26:17 +01:00
|
|
|
_created_script_count += 1
|
2023-01-20 23:34:39 +01:00
|
|
|
var r_path = "" #str('workaround for godot issue #65263 (', _created_script_count, ')')
|
|
|
|
if override_path != null:
|
2023-01-07 20:26:17 +01:00
|
|
|
r_path = override_path
|
|
|
|
|
|
|
|
var DynamicScript = GDScript.new()
|
|
|
|
DynamicScript.source_code = source
|
|
|
|
# The resource_path must be unique or Godot thinks it is trying
|
|
|
|
# to load something it has already loaded and generates an error like
|
|
|
|
# ERROR: Another resource is loaded from path 'workaround for godot issue #65263' (possible cyclic resource inclusion).
|
|
|
|
DynamicScript.resource_path = r_path
|
|
|
|
var result = DynamicScript.reload()
|
|
|
|
|
|
|
|
return DynamicScript
|
|
|
|
|
|
|
|
|
|
|
|
func get_scene_script_object(scene):
|
|
|
|
var state = scene.get_state()
|
|
|
|
var to_return = null
|
|
|
|
var root_node_path = NodePath(".")
|
|
|
|
var node_idx = 0
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
while node_idx < state.get_node_count() and to_return == null:
|
2023-01-07 20:26:17 +01:00
|
|
|
# Assumes that the first node we encounter that has a root node path, one
|
|
|
|
# property, and that property is named 'script' is the GDScript for the
|
|
|
|
# scene. This could be flawed.
|
2023-01-20 23:34:39 +01:00
|
|
|
if (
|
|
|
|
state.get_node_path(node_idx) == root_node_path
|
|
|
|
and state.get_node_property_count(node_idx) == 1
|
|
|
|
):
|
|
|
|
if state.get_node_property_name(node_idx, 0) == "script":
|
2023-01-07 20:26:17 +01:00
|
|
|
to_return = state.get_node_property_value(node_idx, 0)
|
|
|
|
|
|
|
|
node_idx += 1
|
|
|
|
|
2023-01-20 23:34:39 +01:00
|
|
|
return to_return
|