-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.gd
90 lines (75 loc) · 2.9 KB
/
plugin.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
@tool
extends EditorPlugin
const THEME_SETTINGS_PATH := "res://theme.json"
const ADDON_SETTINGS_PATH := "res://addons/retrohub_theme_helper/config.json"
var dock
var singleton
var theme_settings := {}
var addon_settings := {}
func _ready():
add_singletons()
add_dock()
load_settings()
func _exit_tree():
remove_dock()
remove_singletons()
func _disable_plugin():
save_settings()
func build():
save_settings()
return true
func _save_external_data():
save_settings()
func add_singletons():
add_autoload_singleton("JSONUtils", "res://addons/retrohub_theme_helper/utils/JSONUtils.gd")
add_autoload_singleton("FileUtils", "res://addons/retrohub_theme_helper/utils/FileUtils.gd")
add_autoload_singleton("RegionUtils", "res://addons/retrohub_theme_helper/utils/RegionUtils.gd")
add_autoload_singleton("RetroHubConfig", "res://addons/retrohub_theme_helper/singletons/Config.gd")
add_autoload_singleton("RetroHubUI", "res://addons/retrohub_theme_helper/singletons/UI.gd")
add_autoload_singleton("RetroHub", "res://addons/retrohub_theme_helper/singletons/RetroHub.gd")
add_autoload_singleton("RetroHubMedia", "res://addons/retrohub_theme_helper/singletons/Media.gd")
func add_dock():
dock = preload("res://addons/retrohub_theme_helper/dock/Dock.tscn").instantiate()
add_control_to_dock(EditorPlugin.DOCK_SLOT_LEFT_BR, dock)
func load_settings():
theme_settings = load_json(THEME_SETTINGS_PATH)
addon_settings = load_json(ADDON_SETTINGS_PATH)
var editor_control = get_editor_interface().get_base_control()
dock.setup_popup(editor_control)
dock.load_settings(theme_settings, addon_settings)
func load_json(path: String):
if FileAccess.file_exists(path):
var file := FileAccess.open(path, FileAccess.READ)
if not file:
push_warning("Error when opening %s, settings will be reset..." % path)
return {}
var json := JSON.new()
if json.parse(file.get_as_text()):
print("Error")
#push_warning("Error when parsing JSON for %s at line %d: %s. Settings will be reset..." % [path, json.get_error_line(), json.get_error_string()])
return {}
return json.data
return {}
func remove_singletons():
remove_autoload_singleton("RetroHubMedia")
remove_autoload_singleton("RetroHub")
remove_autoload_singleton("RetroHubUI")
remove_autoload_singleton("RetroHubConfig")
remove_autoload_singleton("RegionUtils")
remove_autoload_singleton("FileUtils")
remove_autoload_singleton("JSONUtils")
func remove_dock():
remove_control_from_docks(dock)
dock.queue_free()
func save_settings():
theme_settings = dock.get_theme_settings()
addon_settings = dock.get_addon_settings()
save_json(theme_settings, THEME_SETTINGS_PATH)
save_json(addon_settings, ADDON_SETTINGS_PATH)
func save_json(dict: Dictionary, path: String):
var file := FileAccess.open(path, FileAccess.WRITE)
if not file:
push_warning("Error when opening %s, settings will not be saved." % path)
return
file.store_string(JSON.stringify(dict, "\t", false))
file.close()