2024-02-12 19:47:28 +01:00
# CORE FRAMEWORK SOURCE FILE
# Copyright (c) 2024 The StarOpenSource Project & Contributors
2024-03-03 18:53:09 +01:00
# Licensed under the GNU Affero General Public License v3
2024-02-12 19:47:28 +01:00
#
# This program is free software: you can redistribute it and/or modify
2024-03-03 18:53:09 +01:00
# 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.
2024-02-12 19:47:28 +01:00
#
# 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
2024-03-03 18:53:09 +01:00
# GNU Affero General Public License for more details.
2024-02-12 19:47:28 +01:00
#
2024-03-03 18:53:09 +01:00
# You should have received a copy of the GNU Affero General Public License
2024-02-12 19:47:28 +01:00
# along with this program. If not, see <https://www.gnu.org/licenses/>.
2024-04-08 20:14:38 +02:00
## Contains various utility functions.
2024-02-12 19:47:28 +01:00
##
2024-04-08 20:14:38 +02:00
## This module contains many methods that don't fit into any other module
## and generally make your life as a developer easier.
2024-02-04 21:36:30 +01:00
extends CoreBaseModule
2024-04-08 20:14:38 +02:00
# +++ data type conversion +++
## Converts a number of bytes into mebibytes.[br]
## [br]
## If [code]flatten[/code] is set to [code]true[/code], the decimal part will be discarded.
2024-02-09 15:28:42 +01:00
@ warning_ignore ( " integer_division " )
func byte2mib ( bytes : int , flatten : bool = true ) - > float :
if flatten : return bytes / 1048576
return bytes / float ( 1048576 )
2024-04-08 20:14:38 +02:00
## Converts a number of mebibytes into bytes.[br]
## [br]
## If [code]flatten[/code] is set to [code]true[/code], the decimal part will be discarded.
2024-02-10 18:48:37 +01:00
func mib2byte ( mib : float , flatten : bool = true ) - > float :
if flatten : return int ( mib * 1048576 )
return mib * 1048576
2024-02-09 15:28:42 +01:00
2024-04-08 20:14:38 +02:00
## Converts a number of mebibytes into gibibytes.[br]
## [br]
## If [code]flatten[/code] is set to [code]true[/code], the decimal part will be discarded.
2024-02-09 15:28:42 +01:00
func mib2gib ( mib : float , flatten : bool = true ) - > float :
2024-02-10 18:48:37 +01:00
if flatten : return int ( mib / 1024 )
return mib / 1024
2024-02-09 15:28:42 +01:00
2024-04-08 20:14:38 +02:00
## Converts a number of gebibytes into mebibytes.[br]
## [br]
## If [code]flatten[/code] is set to [code]true[/code], the decimal part will be discarded.
2024-02-10 18:48:37 +01:00
func gib2mib ( gib : float , flatten : bool = true ) - > float :
if flatten : return int ( gib * 1024 )
return gib * 1024
2024-03-24 15:42:38 +01:00
2024-04-08 20:14:38 +02:00
# +++ type formatting +++
## Converts a string array into a normal, nicely formatted string.[br]
## [br]
## With [code]item_before[/code] and [code]item_after[/code] you can customize the lists apperance. Here's an example on how to format every item bold:
## [codeblock]
## extends Node
##
## var core: Core = get_node("/root/CORE")
## var misc: CoreBaseModule = core.misc
## var logger: CoreLoggerInstance = core.logger.get_instance("some/script.gd")
##
## func _ready() -> void:
2024-04-08 21:19:24 +02:00
## var array: Array[String] = ["Apples", "Bananas", "Oranges"]
##
## logger.info(misc.format_stringarray(array))
## logger.info(misc.format_stringarray(array, "[b]", "[/b]"))
2024-04-08 20:14:38 +02:00
## [/codeblock]
2024-03-24 15:42:38 +01:00
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 :
2024-03-31 18:01:57 +02:00
logger . warnf ( " misc " , " Unable to format a string with a size of 0 " )
2024-03-24 15:42:38 +01:00
return " "
elif array . size ( ) == 1 :
2024-03-31 18:01:57 +02:00
logger . warnf ( " misc " , " Unable to format a string with a size of 1 " )
2024-03-24 15:42:38 +01:00
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
2024-03-24 15:56:45 +01:00
2024-04-08 20:14:38 +02:00
# +++ array conversion +++
## Converts an array into a string array.[br]
## [br]
## If an item is found that is not of type [code]String[/code], an empty array is returned.
2024-03-24 15:56:45 +01:00
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
2024-03-24 22:59:57 +01:00
2024-04-08 20:14:38 +02:00
## Converts a string array into an array.
2024-03-24 22:59:57 +01:00
func stringarray_to_array ( array : Array [ String ] ) - > Array :
var output : Array = [ ]
for item in array :
output . append ( item )
return output
2024-03-25 17:43:13 +01:00
2024-04-08 20:14:38 +02:00
# +++ etc +++
## Calculates the center of the child in the area of the parent.[br]
## [br]
## Example:
## [codeblock]
## extends Control
##
## var core: Core = get_node("/root/CORE")
## var misc: CoreBaseModule = core.misc
##
## func _ready() -> void:
2024-04-08 21:19:24 +02:00
## position = misc.center_object(get_parent().size, size)
2024-04-08 20:14:38 +02:00
## [/codeblock]
2024-03-25 21:10:59 +01:00
func get_center ( parent_size : Vector2 , child_size : Vector2 ) - > Vector2 :
2024-03-25 17:43:13 +01:00
return Vector2 ( parent_size . x / 2 - child_size . x / 2 , parent_size . y / 2 - child_size . y / 2 )
2024-04-08 20:14:38 +02:00
2024-04-08 21:19:24 +02:00
## Makes sure for all log messages to be flushed and that CORE is correctly cleaned up.[br]
## Using [method SceneTree.quit] directly may cause various issues.[br]
## [b]Note: [i]Using the [code]await[/code] keyword is required for this function.[/i][/b]
2024-04-08 20:14:38 +02:00
func quit_safely ( exitcode : int = 0 ) - > void :
logger . infof ( " misc " , " Shutting down (code " + str ( exitcode ) + " ) " )
await get_tree ( ) . create_timer ( 0.25 ) . timeout
await core . cleanup ( )
get_tree ( ) . quit ( exitcode )