From f58dc7f37ba5f0ea9fe98e12a7a2f81fa53f06e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Thu, 25 May 2023 19:46:42 +0300 Subject: [PATCH] add dotenv singleton --- .gitignore | 1 + Classes/DotEnv.gd | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 Classes/DotEnv.gd diff --git a/.gitignore b/.gitignore index 4709183..9f37484 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ # Godot 4+ specific ignores .godot/ +.env \ No newline at end of file diff --git a/Classes/DotEnv.gd b/Classes/DotEnv.gd new file mode 100644 index 0000000..f85bc30 --- /dev/null +++ b/Classes/DotEnv.gd @@ -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()