make NodeDB static (#90)

closes #88

Reviewed-on: https://codeberg.org/StreamGraph/StreamGraph/pulls/90
Co-authored-by: Lera Elvoé <yagich@poto.cafe>
Co-committed-by: Lera Elvoé <yagich@poto.cafe>
This commit is contained in:
Lera Elvoé 2024-02-29 19:04:21 +00:00 committed by yagich
parent d98d6f52bd
commit 6d3cc87fad
2 changed files with 47 additions and 48 deletions

View file

@ -1,16 +1,15 @@
# (c) 2023-present Eroax
# (c) 2023-present Yagich
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
extends Node
class_name NodeDB_
class_name NodeDB
## Filepath used for referencing all [DeckNode] Files from.
## Path to search for node files.
const BASE_NODE_PATH := "res://classes/deck/nodes/"
## Filepath where an Index of [NodeDB_.NodeDescriptor]s are saved to avoid reloading
## Filepath where the index of [NodeDB.NodeDescriptor]s are saved to avoid reloading
## everything each run.
## @experimental
const NODE_INDEX_CACHE_PATH := "user://nodes_index.json"
## Filepath where the the list of Favorite Nodes is stored.
## Filepath where the the list of favorite nodes is stored.
const FAVORITE_NODES_PATH := "user://favorite_nodes.json"
## A map of [code]snake_case[/code] category names for proper capitalization.
@ -18,17 +17,15 @@ const CATEGORY_CAPITALIZATION := {
"obs": "OBS"
}
## [Array] used for storing all the "Favorite" Nodes that were set in the [AddNodeMenu]
var favorite_nodes: Array[String]
## A list of nodes that the user marked favorite in the [AddNodeMenu].
static var favorite_nodes: Array[String]
# Dictionary[node_type, NodeDescriptor]
## [Dictionary] filled with node_type, or [String] keys. Correlating to
## [NodeDB_.NodeDescriptor]'s. Essentially where all the nodes are loaded in for data reference.
var nodes: Dictionary = {}
## The node index. Maps [member DeckNode.node_type]s to [NodeDB.NodeDescriptor].
static var nodes: Dictionary = {}
## Loads in all the [DeckNode]s from [member BASE_NODE_PATH]. Or if a working
## cache exists at [member NODE_INDEX_CACHE_PATH] that takes priority
func _init() -> void:
static func _static_init() -> void:
load_favorites()
#if load_node_index():
#return
@ -38,7 +35,8 @@ func _init() -> void:
save_node_index()
func create_descriptors(path: String) -> void:
## Fills the [member nodes] index.
static func create_descriptors(path: String) -> void:
var dir := DirAccess.open(path)
dir.list_dir_begin()
var current_file := dir.get_next()
@ -66,15 +64,16 @@ func create_descriptors(path: String) -> void:
current_file = dir.get_next()
## Helper Function that instances a [DeckNode] based off of it's [member DeckNode.node_type]
func instance_node(type: String) -> DeckNode:
if not nodes.has(type):
## Instantiates a [DeckNode] from a given [param node_type]. See [member DeckNode.node_type].
static func instance_node(node_type: String) -> DeckNode:
if not nodes.has(node_type):
return null
return load(nodes[type]["script_path"]).new()
return load(nodes[node_type]["script_path"]).new()
## Handles Saving the Index of all loaded nodes to [member NODE_INDEX_CACHE_PATH]
func save_node_index() -> void:
## Saves the index of all loaded nodes to [member NODE_INDEX_CACHE_PATH].
static func save_node_index() -> void:
var d := {}
for node_type in nodes:
var nd: NodeDescriptor = nodes[node_type] as NodeDescriptor
@ -84,12 +83,13 @@ func save_node_index() -> void:
var f := FileAccess.open(NODE_INDEX_CACHE_PATH, FileAccess.WRITE)
f.store_string(json)
## Loads the Node Index from [member NODE_INDEX_CACHE_PATH] adding all of the
## [NodeDB_.NodeDescriptor]s in it to the [member nodes] [Dictionary]
func load_node_index() -> bool:
## Loads the node index from [member NODE_INDEX_CACHE_PATH]. Returns [code]true[/code]
## if the index was found on the file system.
static func load_node_index() -> bool:
var f := FileAccess.open(NODE_INDEX_CACHE_PATH, FileAccess.READ)
if f == null:
DeckHolder.logger.log_system("node index file does not exist", Logger.LogType.ERROR)
DeckHolder.logger.log_system("node index file does not exist", Logger.LogType.WARN)
return false
var data: Dictionary = JSON.parse_string(f.get_as_text()) as Dictionary
@ -105,9 +105,10 @@ func load_node_index() -> bool:
DeckHolder.logger.log_system("node index file exists, loaded")
return true
## Sets a specific [member DeckNode.node_type] to be a "favorite" for use in
## [AddNodeMenu]. Then stores the updated list of favorites at [member FAVORITE_NODES_PATH]
func set_node_favorite(node_type: String, favorite: bool) -> void:
## Marks a [member DeckNode.node_type] as "favorite" for use in
## [AddNodeMenu].
static func set_node_favorite(node_type: String, favorite: bool) -> void:
if (favorite and node_type in favorite_nodes) or (not favorite and node_type not in favorite_nodes):
return
@ -119,8 +120,9 @@ func set_node_favorite(node_type: String, favorite: bool) -> void:
var f := FileAccess.open(FAVORITE_NODES_PATH, FileAccess.WRITE)
f.store_string(JSON.stringify(favorite_nodes, "\t"))
## Loads the list of Favorite [memeber DeckNode.node_type]s from [member FAVORITE_NODES_PATH]
func load_favorites() -> void:
## Loads the list of favorite [memeber DeckNode.node_type]s from [member FAVORITE_NODES_PATH].
static func load_favorites() -> void:
var f := FileAccess.open(FAVORITE_NODES_PATH, FileAccess.READ)
if not f:
return
@ -128,36 +130,35 @@ func load_favorites() -> void:
favorite_nodes.clear()
favorite_nodes.assign(data)
## Returns [code]true[/code] if the specified [member DeckNode.node_type] is marked Favorite
## Returns [code]true[/code] if the specified [member DeckNode.node_type] is marked favorite
## by the user.
func is_node_favorite(node_type: String) -> bool:
static func is_node_favorite(node_type: String) -> bool:
return node_type in favorite_nodes
## Returns a capitalized category string.
func get_category_capitalization(category: String) -> String:
static func get_category_capitalization(category: String) -> String:
return CATEGORY_CAPITALIZATION.get(category, category.capitalize())
## Used for storing the shorthand data of a [DeckNode].
##
## Allows for more simply storing [DeckNode]s properties without needing to
## keep an instance
class NodeDescriptor:
## Default Name of the [DeckNode] type this is storing properties of.
## Default name of the [DeckNode] type this is storing properties of.
var name: String
## The [member DeckNode.node_type] of the [DeckNode] this is based off of.
## The node type of the [DeckNode] reference. See [member DeckNode.node_type].
var type: String
## The description of the [DeckNode] reference [member DeckNode.description]
## The description of the [DeckNode] reference. See [member DeckNode.description].
var description: String
## The description of the [DeckNode] reference [member DeckNode.aliases]
## The aliases of the [DeckNode] reference. Stored as a flattened string of
## the [member DeckNode.aliases] array.
var aliases: String
## The description of the [DeckNode] reference [member DeckNode.category]
## The category of the [DeckNode] reference. See [member DeckNode.category].
var category: String
## The description of the [DeckNode] reference [member DeckNode.appears_in_search]
## Whether this [DeckNode] reference will appear when searching. See [member DeckNode.appears_in_search].
var appears_in_search: bool
## Stores the path to this nodes [Script] in res:// for later instantiation.
## Stores the path to this node's script for later instantiation.
var script_path: String
func _init(
@ -178,7 +179,8 @@ class NodeDescriptor:
category = p_category
appears_in_search = p_appears_in_search
## Converts all the properties in this [NodeDB_.NodeDescriptor] to a [Dictionary]. Format - {propery : value}
## Returns a [Dictionary] representation of this node descriptor.
func to_dictionary() -> Dictionary:
var d := {
"name": name,
@ -191,7 +193,8 @@ class NodeDescriptor:
}
return d
## Creates a [NodeDB_.NodeDescriptor] from a given [Dictionary] of properties.
## Creates a new [NodeDB.NodeDescriptor] from a given [Dictionary] of properties.
static func from_dictionary(data: Dictionary) -> NodeDescriptor:
var nd := NodeDescriptor.new(
data.get("script_path", ""),

View file

@ -19,10 +19,6 @@ config/auto_accept_quit=false
config/features=PackedStringArray("4.2", "Forward Plus")
config/icon="res://dist/logo-flattened.svg"
[autoload]
NodeDB="*res://classes/deck/node_db.gd"
[editor_plugins]
enabled=PackedStringArray("res://addons/no-obs-ws/plugin.cfg", "res://addons/no_twitch/plugin.cfg")