-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_functions.gd
97 lines (80 loc) · 3.24 KB
/
helper_functions.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
extends Node
## Print the given string along with the Debug Instance (always 0 for server)
## and the multiplayer ID (always 1 for the server)
## The text MUST be a string. Use str() to format non-strings or multiple strings
## The color is optional and can be used to colorize the text.
func log_print(text: String, color: String = "white") -> void:
if Globals.is_server or OS.is_debug_build():
var unique_id: int = -1
if multiplayer.has_multiplayer_peer():
unique_id = multiplayer.get_unique_id()
var local_debug_instance_number_text: String = ""
if Globals.local_debug_instance_number > -1:
local_debug_instance_number_text = str(Globals.local_debug_instance_number, " ")
print_rich(
"[color=",
color,
"]",
local_debug_instance_number_text,
unique_id,
" ",
text,
"[/color]"
)
func generate_random_string(length: int) -> String:
var crypto: Crypto = Crypto.new()
var random_bytes: PackedByteArray = crypto.generate_random_bytes(length)
var characters: String = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_"
var word: String = ""
# Use randi()
# for i in range(length):
# word += characters[randi() % len(characters)]
# Use crypto
var cursor: int = 0
for byte: int in random_bytes:
cursor += byte
word += characters[cursor % len(characters)]
return word
func save_data_to_file(file_name: String, content: String) -> void:
var file: FileAccess = FileAccess.open(file_name, FileAccess.WRITE)
file.store_string(content)
func load_data_from_file(file_name: String) -> String:
var content: String = ""
var file: FileAccess = FileAccess.open(file_name, FileAccess.READ)
if file:
content = file.get_as_text()
return content
## Saves player data on the server side for use when the same player joins again later.
func save_server_player_save_data_to_file() -> void:
# Save config back out to file, even if we imported it from the file.
save_data_to_file(
Globals.server_player_save_data_file_name, JSON.stringify(Globals.player_save_data)
)
func parse_thing_name(node_name: String) -> Dictionary:
var parsed_thing_name: Dictionary = {}
# Use the given Node name to determine the thing name and ID
var split_node_name: PackedStringArray = node_name.split("-")
if not split_node_name or split_node_name.size() != 2:
printerr("Invalid thing grabbed: ", node_name)
return parsed_thing_name
parsed_thing_name.name = split_node_name[0]
parsed_thing_name.id = int(split_node_name[1])
return parsed_thing_name
func quit_gracefully() -> void:
# Quitting in Web just stops the game but leaves it stalled in the browser window, so it really should never happen.
if !Globals.shutdown_in_progress and OS.get_name() != "Web":
Globals.shutdown_in_progress = true
if Globals.is_server:
print_rich(
"[color=orange]Disconnecting clients and saving data before shutting down server...[/color]"
)
var server_label: Node = get_node_or_null(
"/root/Main/Map/ServerCamera/CanvasLayer/Control/Label"
)
if server_label:
server_label.text = "Disconnecting clients and shutting down server..."
Network.shutdown_server()
while Network.peers.size() > 0:
print_rich("[color=orange]...server still clearing clients...[/color]")
await get_tree().create_timer(0.5).timeout
get_tree().quit()