From 7c7cfdaa0a11fdfdd7f3129396a12f438c07a0c2 Mon Sep 17 00:00:00 2001 From: RodZill4 Date: Thu, 13 Feb 2020 08:37:42 +0100 Subject: [PATCH] optimised loader by caching predefined nodes --- addons/material_maker/engine/loader.gd | 37 ++++++++++++++++---------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/addons/material_maker/engine/loader.gd b/addons/material_maker/engine/loader.gd index 603da7f..b60a4bb 100644 --- a/addons/material_maker/engine/loader.gd +++ b/addons/material_maker/engine/loader.gd @@ -3,6 +3,28 @@ extends Node const STD_GENDEF_PATH = "res://addons/material_maker/nodes" +var generators = {} + +func _ready()-> void: + var gen_count = 0 + for path in [ STD_GENDEF_PATH, OS.get_executable_path().get_base_dir()+"/generators" ]: + var dir = Directory.new() + if dir.open(path) == OK: + dir.list_dir_begin() + var file_name = dir.get_next() + while file_name != "": + if !dir.current_is_dir() and file_name.get_extension() == "mmg": + var file : File = File.new() + if file.open(path+"/"+file_name, File.READ) == OK: + if !generators.has(file_name.get_basename()): + gen_count += 1 + generators[file_name.get_basename()] = parse_json(file.get_as_text()) + file.close() + file_name = dir.get_next() + else: + print("An error occurred when trying to access the path.") + print(gen_count) + func generator_name_from_path(path : String) -> String: for p in [ STD_GENDEF_PATH, OS.get_executable_path().get_base_dir()+"/generators" ]: print(p) @@ -63,20 +85,7 @@ func create_gen(data) -> MMGenBase: if types.has(data.type): generator = types[data.type].new() else: - var file = File.new() - var gen_paths = [ STD_GENDEF_PATH, OS.get_executable_path().get_base_dir()+"/generators" ] - for p in gen_paths: - if file.open(p+"/"+data.type+".mmg", File.READ) == OK: - generator = create_gen(parse_json(file.get_as_text())) - generator.model = data.type - file.close() - break - elif file.open(p+"/"+data.type+".mmn", File.READ) == OK: - generator = MMGenShader.new() - generator.model = data.type - generator.set_shader_model(parse_json(file.get_as_text())) - file.close() - break + generator = create_gen(generators[data.type]) if generator == null: print("Cannot find description for "+data.type) if generator != null: