2023-06-24 05:39:50 +02:00
|
|
|
extends Node
|
|
|
|
|
|
|
|
const BASE_NODE_PATH := "res://classes/deck/nodes/"
|
2023-07-21 08:39:38 +02:00
|
|
|
const NODE_INDEX_CACHE_PATH := "user://nodes_index.json"
|
2023-11-23 07:38:10 +01:00
|
|
|
const FAVORITE_NODES_PATH := "user://favorite_nodes.json"
|
|
|
|
|
|
|
|
var favorite_nodes: Array[String]
|
2023-07-21 07:30:12 +02:00
|
|
|
|
|
|
|
# Dictionary[node_type, NodeDescriptor]
|
2023-06-24 05:39:50 +02:00
|
|
|
var nodes: Dictionary = {}
|
|
|
|
|
|
|
|
|
|
|
|
func _init() -> void:
|
2023-11-23 07:38:10 +01:00
|
|
|
load_favorites()
|
2023-11-22 05:26:11 +01:00
|
|
|
#if load_node_index():
|
|
|
|
#return
|
2023-07-21 08:39:38 +02:00
|
|
|
|
2023-06-24 05:39:50 +02:00
|
|
|
var dir := DirAccess.open(BASE_NODE_PATH)
|
|
|
|
dir.list_dir_begin()
|
|
|
|
var current_file := dir.get_next()
|
|
|
|
while current_file != "":
|
|
|
|
print(current_file)
|
|
|
|
if current_file.ends_with(".gd"):
|
2023-07-21 07:30:12 +02:00
|
|
|
var script_path := BASE_NODE_PATH.path_join(current_file)
|
|
|
|
var node: DeckNode = load(script_path).new() as DeckNode
|
2023-07-21 08:39:38 +02:00
|
|
|
var aliases: String = node.aliases.reduce(
|
|
|
|
func(accum, el) -> void:
|
|
|
|
accum += el
|
|
|
|
, "")
|
2023-11-23 07:38:10 +01:00
|
|
|
var descriptor := NodeDescriptor.new(
|
|
|
|
script_path,
|
|
|
|
node.name,
|
|
|
|
node.node_type,
|
|
|
|
node.description,
|
|
|
|
aliases,
|
|
|
|
node.category,
|
|
|
|
node.appears_in_search,
|
|
|
|
)
|
2023-07-21 07:30:12 +02:00
|
|
|
nodes[node.node_type] = descriptor
|
2023-06-24 05:39:50 +02:00
|
|
|
current_file = dir.get_next()
|
|
|
|
|
2023-07-21 08:39:38 +02:00
|
|
|
save_node_index()
|
|
|
|
|
2023-06-24 05:39:50 +02:00
|
|
|
|
|
|
|
func instance_node(type: String) -> DeckNode:
|
|
|
|
if !nodes.has(type):
|
|
|
|
return null
|
|
|
|
|
2023-07-21 07:30:12 +02:00
|
|
|
return load(nodes[type]["script_path"]).new()
|
|
|
|
|
|
|
|
|
2023-07-21 08:39:38 +02:00
|
|
|
func save_node_index() -> void:
|
|
|
|
var d := {}
|
|
|
|
for node_type in nodes:
|
|
|
|
var nd: NodeDescriptor = nodes[node_type] as NodeDescriptor
|
|
|
|
d[node_type] = nd.to_dictionary()
|
|
|
|
|
|
|
|
var json := JSON.stringify(d, "\t")
|
|
|
|
var f := FileAccess.open(NODE_INDEX_CACHE_PATH, FileAccess.WRITE)
|
|
|
|
f.store_string(json)
|
|
|
|
|
|
|
|
|
|
|
|
func load_node_index() -> bool:
|
|
|
|
var f := FileAccess.open(NODE_INDEX_CACHE_PATH, FileAccess.READ)
|
|
|
|
if f == null:
|
|
|
|
print("node index file does not exist")
|
|
|
|
return false
|
|
|
|
|
|
|
|
var data: Dictionary = JSON.parse_string(f.get_as_text()) as Dictionary
|
|
|
|
if data.is_empty():
|
|
|
|
print("node index file exists, but is empty")
|
|
|
|
return false
|
|
|
|
|
|
|
|
for node_type in data:
|
|
|
|
var nd_dict: Dictionary = data[node_type]
|
|
|
|
var nd := NodeDescriptor.from_dictionary(nd_dict)
|
|
|
|
nodes[node_type] = nd
|
|
|
|
|
|
|
|
print("node index file exists, loaded")
|
|
|
|
return true
|
|
|
|
|
|
|
|
|
2023-11-23 07:38:10 +01:00
|
|
|
func set_node_favorite(node_type: String, favorite: bool) -> void:
|
|
|
|
if (favorite && node_type in favorite_nodes) || (!favorite && !(node_type in favorite_nodes)):
|
|
|
|
return
|
|
|
|
|
|
|
|
if favorite:
|
|
|
|
favorite_nodes.append(node_type)
|
|
|
|
else:
|
|
|
|
favorite_nodes.erase(node_type)
|
|
|
|
|
|
|
|
var f := FileAccess.open(FAVORITE_NODES_PATH, FileAccess.WRITE)
|
|
|
|
f.store_string(JSON.stringify(favorite_nodes, "\t"))
|
|
|
|
|
|
|
|
|
|
|
|
func load_favorites() -> void:
|
|
|
|
var f := FileAccess.open(FAVORITE_NODES_PATH, FileAccess.READ)
|
|
|
|
if !f:
|
|
|
|
return
|
|
|
|
var data: Array = JSON.parse_string(f.get_as_text())
|
|
|
|
favorite_nodes.clear()
|
|
|
|
favorite_nodes.assign(data)
|
|
|
|
|
|
|
|
|
|
|
|
func is_node_favorite(node_type: String) -> bool:
|
|
|
|
return node_type in favorite_nodes
|
|
|
|
|
|
|
|
|
2023-07-21 07:30:12 +02:00
|
|
|
class NodeDescriptor:
|
|
|
|
var name: String
|
2023-11-23 07:38:10 +01:00
|
|
|
var type: String
|
2023-07-21 07:30:12 +02:00
|
|
|
var description: String
|
|
|
|
var aliases: String
|
2023-11-23 07:38:10 +01:00
|
|
|
var category: String
|
|
|
|
var appears_in_search: bool
|
2023-07-21 07:30:12 +02:00
|
|
|
|
|
|
|
var script_path: String
|
|
|
|
|
|
|
|
|
2023-11-23 07:38:10 +01:00
|
|
|
func _init(
|
|
|
|
p_script_path: String,
|
|
|
|
p_name: String,
|
|
|
|
p_type: String,
|
|
|
|
p_description: String,
|
|
|
|
p_aliases: String,
|
|
|
|
p_category: String,
|
|
|
|
p_appears_in_search: bool,
|
|
|
|
) -> void:
|
|
|
|
script_path = p_script_path
|
2023-07-21 07:30:12 +02:00
|
|
|
|
2023-11-23 07:38:10 +01:00
|
|
|
name = p_name
|
|
|
|
type = p_type
|
|
|
|
description = p_description
|
|
|
|
aliases = p_aliases
|
|
|
|
category = p_category
|
|
|
|
appears_in_search = p_appears_in_search
|
2023-07-21 08:39:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
func to_dictionary() -> Dictionary:
|
|
|
|
var d := {
|
|
|
|
"name": name,
|
2023-11-23 07:38:10 +01:00
|
|
|
"type": type,
|
2023-07-21 08:39:38 +02:00
|
|
|
"description": description,
|
|
|
|
"aliases": aliases,
|
2023-11-23 07:38:10 +01:00
|
|
|
"script_path": script_path,
|
|
|
|
"category": category,
|
|
|
|
"appears_in_search": appears_in_search,
|
2023-07-21 08:39:38 +02:00
|
|
|
}
|
|
|
|
return d
|
|
|
|
|
|
|
|
|
|
|
|
static func from_dictionary(data: Dictionary) -> NodeDescriptor:
|
|
|
|
var nd := NodeDescriptor.new(
|
|
|
|
data.get("script_path", ""),
|
|
|
|
data.get("name", ""),
|
2023-11-23 07:38:10 +01:00
|
|
|
data.get("type", ""),
|
2023-07-21 08:39:38 +02:00
|
|
|
data.get("description", ""),
|
2023-11-23 07:38:10 +01:00
|
|
|
data.get("aliases", ""),
|
|
|
|
data.get("category", ""),
|
|
|
|
data.get("appears_in_search", false),
|
|
|
|
)
|
2023-07-21 08:39:38 +02:00
|
|
|
|
|
|
|
return nd
|