forked from godot-extended-libraries/godot-next
-
Notifications
You must be signed in to change notification settings - Fork 0
/
singletons.gd
91 lines (75 loc) · 3.08 KB
/
singletons.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
# author: xdgamestudios
# license: MIT
# description: An API for accessing singletons
# deps:
# - singleton_cache.tres
tool
extends Reference
class_name Singletons
##### CONSTANTS #####
const SINGLETON_CACHE = preload("res://addons/godot-next/data/singleton_cache.tres")
##### PUBLIC METHODS #####
# Look up a singleton by its script. If it doesn't exist yet, make it. If it's a Resource with a persistent file path, load it in from memory.
static func fetch(p_script: Script) -> Object:
var cache: Dictionary = SINGLETON_CACHE.get_cache()
if not cache.has(p_script):
if p_script is Resource:
var path = _get_persistent_path(p_script)
if path:
var paths: Dictionary = SINGLETON_CACHE.get_paths()
cache[p_script] = ResourceLoader.load(path) if ResourceLoader.exists(path) else p_script.new()
paths[p_script] = path
else:
cache[p_script] = p_script.new()
else:
cache[p_script] = p_script.new()
return cache[p_script]
# Returns a singleton by its class_name as a String
static func fetchs(p_name: String) -> Object:
var ct = ClassType.new(p_name)
if ct.res:
return fetch(ct.res)
return null
# Returns an editor-only singleton by its class name
static func fetch_editor(p_class: GDScriptNativeClass) -> Object:
if not Engine.editor_hint:
push_warning("Cannot access '%s' (editor-only class) at runtime." % p_class.get_class())
return null
var cache: Dictionary = SINGLETON_CACHE.get_cache()
if cache.has(p_class):
return cache[p_class]
return null
# Remove a singleton from the cache and any paths associated with it.
static func erase(p_script: Script) -> bool:
var cache: Dictionary = SINGLETON_CACHE.get_cache()
var paths: Dictionary = SINGLETON_CACHE.get_paths()
var erased = cache.erase(p_script)
#warning-ignore:return_value_discarded
paths.erase(p_script)
return erased
static func save(p_script: Script) -> void:
var paths: Dictionary = SINGLETON_CACHE.get_paths()
if not paths.has(p_script):
return
var cache: Dictionary = SINGLETON_CACHE.get_cache()
#warning-ignore:return_value_discarded
ResourceSaver.save(paths[p_script], cache[p_script])
static func save_all() -> void:
var cache: Dictionary = SINGLETON_CACHE.get_cache()
var paths: Dictionary = SINGLETON_CACHE.get_paths()
for a_script in paths:
#warning-ignore:return_value_discarded
ResourceSaver.save(paths[a_script], cache[a_script])
##### PRIVATE METHODS #####
static func _get_persistent_path(p_script: Script):
return p_script.get("SELF_RESOURCE")
# Register all editor-only singletons
static func _register_editor_singletons(plugin: EditorPlugin):
var cache: Dictionary = SINGLETON_CACHE.get_cache()
cache[UndoRedo] = plugin.get_undo_redo()
cache[EditorInterface] = plugin.get_editor_interface()
cache[ScriptEditor] = plugin.get_editor_interface().get_script_editor()
cache[EditorSelection] = plugin.get_editor_interface().get_selection()
cache[EditorSettings] = plugin.get_editor_interface().get_editor_settings()
cache[EditorFileSystem] = plugin.get_editor_interface().get_resource_filesystem()
cache[EditorResourcePreview] = plugin.get_editor_interface().get_resource_previewer()