From 494452c3b407d64495f8282fb4abcc4d6085e395 Mon Sep 17 00:00:00 2001 From: Joshua Anderson Date: Tue, 31 Aug 2021 16:21:35 -0600 Subject: [PATCH] Auto add to applications on Linux --- Viewer.gd | 1 + project.godot | 4 ++++ shortcut_creator.gd | 50 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 shortcut_creator.gd diff --git a/Viewer.gd b/Viewer.gd index e406c92..ec35645 100644 --- a/Viewer.gd +++ b/Viewer.gd @@ -14,3 +14,4 @@ func _on_files_dropped(files, _screen): $Control/Label.text = path $WorldEnvironment.environment.background_sky.panorama = texture $TrackballCamera.reset_rotation() + diff --git a/project.godot b/project.godot index c4d6e57..c7ac40b 100644 --- a/project.godot +++ b/project.godot @@ -19,6 +19,10 @@ boot_splash/bg_color=Color( 0.298039, 0.529412, 0.866667, 1 ) config/icon="res://icon.png" config/windows_native_icon="res://icon.ico" +[autoload] + +ShortcutCreator="*res://shortcut_creator.gd" + [editor_plugins] enabled=PoolStringArray( "res://addons/goutte.camera.trackball/plugin.cfg" ) diff --git a/shortcut_creator.gd b/shortcut_creator.gd new file mode 100644 index 0000000..2053333 --- /dev/null +++ b/shortcut_creator.gd @@ -0,0 +1,50 @@ +extends Node + + + +# Called when the node enters the scene tree for the first time. +func _ready() -> void: + if !OS.is_debug_build() and OS.get_name() == "X11": + print(Engine.editor_hint) + create_shortcut() +# get_tree().quit() + +func create_shortcut() -> void: + # Get username + var output := [] + var _error = OS.execute("whoami", [], true, output) + var username: String = output[0].substr(0, output[0].length() - 1) # Trim junk + + # Get other useful data. + var project_name: String = ProjectSettings.get_setting("application/config/name") + var description: String = ProjectSettings.get_setting("application/config/description") + + # Prepare paths. + var user_folder_path := "/home/" + username + "/.local/share/godot/app_userdata/" + project_name + "/" + var desktop_file_path := user_folder_path + project_name + ".desktop" + var exec_path := OS.get_executable_path() + + # Save game icon to storage. + var icon: Texture = load("res://icon.png") + var icon_data := icon.get_data() + _error = icon_data.save_png("user://shortcut.png") + + # Write out file with all the necessary info. + var file := File.new() + _error = file.open(desktop_file_path, File.WRITE) + file.store_string("[Desktop Entry]\n") + file.store_string("Encoding=UTF-8\n") + file.store_string("Name=" + project_name + "\n") + file.store_string("Comment=" + description + "\n") + file.store_string("Exec=\"" + exec_path + "\"\n") + file.store_string("Icon=" + user_folder_path + "shortcut.png\n") + file.store_string("Terminal=false\n") + file.store_string("Type=Application\n") + file.store_string("Categories=Game;") + file.close() + + # Copies the desktop file to the right places and allows it to be registered. + _error = OS.execute("cp", [desktop_file_path, "/home/" + username + "/.local/share/applications"], true, output) + _error = OS.execute("cp", [desktop_file_path, "/usr/share/applications"], true, output) + + _error = OS.execute("chmod", ["+x", desktop_file_path], true, output)