cache node index and load it

This commit is contained in:
Lera Elvoé 2023-07-21 09:39:38 +03:00
parent f9069db8be
commit 97aa95bf6b
No known key found for this signature in database
2 changed files with 66 additions and 7 deletions

View file

@ -1,25 +1,34 @@
extends Node extends Node
const BASE_NODE_PATH := "res://classes/deck/nodes/" const BASE_NODE_PATH := "res://classes/deck/nodes/"
const NODE_INDEX_CACHE_PATH := "user://nodes_index.json"
# Dictionary[node_type, NodeDescriptor] # Dictionary[node_type, NodeDescriptor]
var nodes: Dictionary = {} var nodes: Dictionary = {}
func _init() -> void: func _init() -> void:
if load_node_index():
return
var dir := DirAccess.open(BASE_NODE_PATH) var dir := DirAccess.open(BASE_NODE_PATH)
dir.list_dir_begin() dir.list_dir_begin()
var current_file := dir.get_next() var current_file := dir.get_next()
while current_file != "": while current_file != "":
print(current_file) print(current_file)
if current_file.ends_with(".gd"): if current_file.ends_with(".gd"):
# var f := FileAccess.open(BASE_NODE_PATH.path_join(current_file), FileAccess.READ)
var script_path := BASE_NODE_PATH.path_join(current_file) var script_path := BASE_NODE_PATH.path_join(current_file)
var node: DeckNode = load(script_path).new() as DeckNode var node: DeckNode = load(script_path).new() as DeckNode
var descriptor := NodeDescriptor.new(script_path, node.name, node.description, node.aliases) var aliases: String = node.aliases.reduce(
func(accum, el) -> void:
accum += el
, "")
var descriptor := NodeDescriptor.new(script_path, node.name, node.description, aliases)
nodes[node.node_type] = descriptor nodes[node.node_type] = descriptor
current_file = dir.get_next() current_file = dir.get_next()
save_node_index()
func instance_node(type: String) -> DeckNode: func instance_node(type: String) -> DeckNode:
if !nodes.has(type): if !nodes.has(type):
@ -28,6 +37,37 @@ func instance_node(type: String) -> DeckNode:
return load(nodes[type]["script_path"]).new() return load(nodes[type]["script_path"]).new()
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
class NodeDescriptor: class NodeDescriptor:
var name: String var name: String
var description: String var description: String
@ -36,12 +76,29 @@ class NodeDescriptor:
var script_path: String var script_path: String
func _init(p_script_path: String, p_name: String, p_description: String, p_aliases: Array[String]) -> void: func _init(p_script_path: String, p_name: String, p_description: String, p_aliases: String) -> void:
script_path = p_script_path script_path = p_script_path
name = p_name name = p_name
description = p_description description = p_description
aliases = p_aliases.reduce( aliases = p_aliases
func(accum, el) -> void:
accum += el
, "") func to_dictionary() -> Dictionary:
var d := {
"name": name,
"description": description,
"aliases": aliases,
"script_path": script_path
}
return d
static func from_dictionary(data: Dictionary) -> NodeDescriptor:
var nd := NodeDescriptor.new(
data.get("script_path", ""),
data.get("name", ""),
data.get("description", ""),
data.get("aliases", ""))
return nd

View file

@ -13,6 +13,8 @@ config_version=5
config/name="Re-DotDeck" config/name="Re-DotDeck"
config/tags=PackedStringArray("dot_deck") config/tags=PackedStringArray("dot_deck")
run/main_scene="res://graph_node_renderer/deck_renderer_graph_edit.tscn" run/main_scene="res://graph_node_renderer/deck_renderer_graph_edit.tscn"
config/use_custom_user_dir=true
config/custom_user_dir_name="dotdeck"
config/features=PackedStringArray("4.1", "Forward Plus") config/features=PackedStringArray("4.1", "Forward Plus")
run/low_processor_mode=true run/low_processor_mode=true
config/icon="res://icon.svg" config/icon="res://icon.svg"