@tool
extends Node3D

@export var refresh_animation_lib : bool :
	get:return false
	set(_value):
		Util.refresh_animation_lib()

@export var refresh_mesh_lib : bool :
	get:return false
	set(_value):
		process_mesh_lib("res://resource/mesh_level")
		print("done.")
		
@export var refresh_uid : bool :
	get:return false
	set(_value):
		process_dir("res://")
		print("done.")

func process_mesh_lib(dir_name: String) -> void:
	var dir := DirAccess.open(dir_name)
	if not dir:
		print("An error occurred when trying to access the path: ", dir_name)
		return
	var path_list = []
	dir.list_dir_begin()
	for file_name in dir.get_files():
		if file_name.ends_with(".vox"):
			path_list.append(dir_name + "/" + file_name)
	Util.refresh_mesh_library(path_list)

func process_dir(dir_name: String) -> void:
	var dir := DirAccess.open(dir_name)
	if dir:
		dir.list_dir_begin()
		var file_name = dir.get_next()
		if dir_name.ends_with("/"):
			dir_name = dir_name.trim_suffix("/")
		while file_name != "":
			if dir.current_is_dir():
				print("-----" + dir_name + "/" + file_name + " -----")
				process_dir(dir_name + "/" + file_name)
			elif file_name.get_extension() in [ "tres", "res" ]:
				print(dir_name + "/" + file_name)
				var res := ResourceLoader.load(dir_name + "/" + file_name)
				ResourceSaver.save(res)
				
			file_name = dir.get_next()
	else:
		print("An error occurred when trying to access the path: ", dir_name)
