add dotenv singleton

This commit is contained in:
Lera Elvoé 2023-05-25 19:46:42 +03:00
parent 9173250580
commit f58dc7f37b
Signed by: yagich
SSH Key Fingerprint: SHA256:6xjGb6uA7lAVcULa7byPEN//rQ0wPoG+UzYVMfZnbvc
2 changed files with 23 additions and 0 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
# Godot 4+ specific ignores # Godot 4+ specific ignores
.godot/ .godot/
.env

22
Classes/DotEnv.gd Normal file
View File

@ -0,0 +1,22 @@
extends Node
func _init() -> void:
var path = ""
if OS.has_feature("editor"):
path = ProjectSettings.globalize_path("res://.env")
else:
path = OS.get_executable_path().get_base_dir().path_join(".env")
if !FileAccess.file_exists(path):
return
var f := FileAccess.open(path, FileAccess.READ)
while !f.eof_reached():
var line := f.get_line()
var split := line.split("=")
OS.set_environment(split[0].strip_edges(), split[1].strip_edges())
func _ready() -> void:
queue_free()