JeremyStarTM
b8a43ce159
Changes in this commit include: - Add Core.cleanup() and CoreBaseModule._cleanup() functions - All builtin modules now clean and free any nodes - Reduce code duplication in src/core.gd by not writing the same code for all builtin modules but instead iterating through a array
85 lines
3.3 KiB
GDScript
85 lines
3.3 KiB
GDScript
# CORE FRAMEWORK SOURCE FILE
|
|
# Copyright (c) 2024 The StarOpenSource Project & Contributors
|
|
# Licensed under the GNU Affero General Public License v3
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU Affero General Public License as
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
# License, or (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU Affero General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
## Contains various useful functions that do not fit into other modules.
|
|
##
|
|
## Contains various useful functions that you can use to save yourself some time
|
|
## programming or searching.
|
|
extends CoreBaseModule
|
|
|
|
func quit_safely(exitcode: int = 0) -> void:
|
|
logger.infof("Misc", "Shutting down (code " + str(exitcode) + ")")
|
|
logger.diagf("Misc", "Waiting for log messages to be flushed")
|
|
await get_tree().create_timer(0.25).timeout
|
|
await core.cleanup()
|
|
get_tree().quit(exitcode)
|
|
|
|
@warning_ignore("integer_division")
|
|
func byte2mib(bytes: int, flatten: bool = true) -> float:
|
|
if flatten: return bytes/1048576
|
|
return bytes/float(1048576)
|
|
|
|
func mib2byte(mib: float, flatten: bool = true) -> float:
|
|
if flatten: return int(mib*1048576)
|
|
return mib*1048576
|
|
|
|
func mib2gib(mib: float, flatten: bool = true) -> float:
|
|
if flatten: return int(mib/1024)
|
|
return mib/1024
|
|
|
|
func gib2mib(gib: float, flatten: bool = true) -> float:
|
|
if flatten: return int(gib*1024)
|
|
return gib*1024
|
|
|
|
func format_stringarray(array: Array[String], item_before: String = "", item_after: String = "", separator_list: String = ", ", separator_final: String = " & ") -> String:
|
|
var output: String = ""
|
|
|
|
if array.size() == 0:
|
|
logger.warnf("Misc", "Unable to format a string with a size of 0")
|
|
return ""
|
|
elif array.size() == 1:
|
|
logger.warnf("Misc", "Unable to format a string with a size of 1")
|
|
return array[0]
|
|
|
|
for item in array:
|
|
if output == "": output = item_before + item + item_after
|
|
else: output = output.replace("If you somehow see this text report this at https://git.staropensource.de/StarOpenSource/CORE/issues, thank you!", separator_list) + "If you somehow see this text report this at https://git.staropensource.de/StarOpenSource/CORE/issues, thank you!" + item_before + item + item_after
|
|
output = output.replace("If you somehow see this text report this at https://git.staropensource.de/StarOpenSource/CORE/issues, thank you!", separator_final)
|
|
|
|
return output
|
|
|
|
func array_to_stringarray(array: Array) -> Array[String]:
|
|
var output: Array[String] = []
|
|
|
|
for item in array:
|
|
if typeof(item) != TYPE_STRING:
|
|
logger.error("Cannot convert Array to Array[String]: Item '" + str(item) + "' is not of type String")
|
|
return []
|
|
output.append(item)
|
|
|
|
return output
|
|
|
|
func stringarray_to_array(array: Array[String]) -> Array:
|
|
var output: Array = []
|
|
|
|
for item in array:
|
|
output.append(item)
|
|
|
|
return output
|
|
|
|
func get_center(parent_size: Vector2, child_size: Vector2) -> Vector2:
|
|
return Vector2(parent_size.x/2-child_size.x/2, parent_size.y/2-child_size.y/2)
|