1- class_name VMFInstanceManager extends RefCounted ;
1+ class_name VMFInstanceManager extends RefCounted
22
33static var instances_cache = {};
44static var last_cache_changed = 0 ;
55
66static func get_instance_path (entity : Dictionary ) -> String :
7- var instance_path = entity .file .get_file ().get_basename () + '.vmf' ;
7+ var instance_path = entity .file .replace (".vmf" , "" ) + '.vmf' ; # Ensure the path ends with .vmf
8+ var intance_filename = instance_path .get_file ();
89 var map_base_folder = entity .vmf .get_base_dir () if "vmf" in entity else "" ;
910 var maps_folder := str (VMFConfig .gameinfo_path ).path_join ('maps' );
1011 var mapsrc_folder := str (VMFConfig .gameinfo_path ).path_join ('mapsrc' );
1112
1213 var instance_paths := [
14+ map_base_folder .path_join ('instances' ).path_join (intance_filename ),
15+ map_base_folder .path_join (intance_filename ),
1316 map_base_folder .path_join ('instances' ).path_join (instance_path ),
1417 map_base_folder .path_join (instance_path ),
1518 maps_folder .path_join ('instances' ).path_join (instance_path ),
@@ -41,58 +44,41 @@ static func get_subinstances(structure: Dictionary, entity_source: Dictionary) -
4144
4245 return subinstances ;
4346
44- static func get_imported_instance_path (instance_name : String ):
45- var folder : String = VMFConfig .import .instances_folder ;
46- var dir := ProjectSettings .globalize_path (folder );
47- return dir + "/" + instance_name + ".scn" ;
48-
49- static func load_instance (instance_name : String ):
50- if not is_instance_exists (instance_name ):
51- VMFLogger .error ("Failed to find instance file: %s " % instance_name );
47+ static func load_instance (instance_path : String ):
48+ if not ResourceLoader .exists (instance_path ):
49+ VMFLogger .error ("Failed to find instance file: %s " % instance_path );
5250 return ;
5351
54- if last_cache_changed == null :
55- last_cache_changed = 0 ;
56-
57- if Time .get_ticks_msec () - last_cache_changed > 10000 :
58- instances_cache = {};
59-
60- if instance_name in instances_cache :
61- return instances_cache [instance_name ];
52+ var cached_value := VMFCache .get_cached (instance_path );
53+ if cached_value : return cached_value ;
6254
63- var path = get_imported_instance_path (instance_name );
64- var scn := ResourceLoader .load (path );
55+ var scn := ResourceLoader .load (instance_path );
6556
66- instances_cache [instance_name ] = scn ;
67- last_cache_changed = Time .get_ticks_msec ();
57+ VMFCache .add_cached (instance_path , scn );
6858
6959 if not scn :
70- VMFLogger .error ("Failed to load instance resource: %s " % path );
60+ VMFLogger .error ("Failed to load instance resource: %s " % instance_path );
7161 return ;
7262
7363 return scn ;
74-
75- static func is_instance_exists (instance_name : String ):
76- var path = get_imported_instance_path (instance_name );
77-
78- return FileAccess .file_exists (path );
7964
8065static func import_instance (entity : Dictionary ):
8166 var instances_folder : String = VMFConfig .import .instances_folder ;
82- var file = get_instance_path (entity );
67+ var instance_vmf_file = get_instance_path (entity );
8368
84- if file == '' :
69+ if instance_vmf_file == '' :
8570 VMFLogger .error ("Failed to find instance file for entity: %s " % entity .file );
8671 return ;
8772
88- var filename := file .get_file ().get_basename ( );
89- var dir := ProjectSettings . globalize_path ( instances_folder ) ;
90- var path := dir + "/" + filename + ".scn" ;
91- var is_instance_already_imported := FileAccess . file_exists ( path );
73+ var instance_name = instance_vmf_file .get_file ().replace ( ".vmf" , "" );
74+ var map_path = entity . vmf . get_base_dir () if "vmf" in entity else "unknown_map" ;
75+ var relative_instance_path := instance_vmf_file . replace ( ".vmf" , ".scn" ). replace ( map_path + "/" , "" ). replace ( "instances/" , "" ) as String ;
76+ var instance_scene_path := VMFConfig . import . instances_folder . path_join ( relative_instance_path );
9277
93- if is_instance_already_imported : return load_instance (filename );
78+ var is_instance_already_imported := FileAccess .file_exists (instance_scene_path );
79+ if is_instance_already_imported : return load_instance (instance_scene_path );
9480
95- var structure = VDFParser .parse (file );
81+ var structure = VDFParser .parse (instance_vmf_file );
9682 var subinstances = get_subinstances (structure , entity );
9783
9884 if subinstances .size () > 0 :
@@ -103,22 +89,23 @@ static func import_instance(entity: Dictionary):
10389 var node := VMFNode .new ();
10490
10591 node .set_meta ("instance" , true );
106- node .vmf = file ;
107- node .name = filename + '_instance' ;
92+ node .vmf = instance_vmf_file ;
93+ node .name = instance_name + '_instance' ;
10894 node .save_geometry = false ;
10995 node .save_collision = false ;
11096 node .import_map ();
11197
11298 scn .pack (node );
11399
100+ var dir := instance_scene_path .get_base_dir ();
114101 if not DirAccess .dir_exists_absolute (dir ):
115102 DirAccess .make_dir_recursive_absolute (dir );
116103
117- var err := ResourceSaver .save (scn , path , ResourceSaver .FLAG_COMPRESS );
104+ var err := ResourceSaver .save (scn , instance_scene_path , ResourceSaver .FLAG_COMPRESS );
118105 if err :
119106 VMFLogger .error ("Failed to save instance resource: %s " % err );
120107 return ;
121108
122109 node .queue_free ();
123110
124- return load_instance (filename );
111+ return load_instance (instance_scene_path );
0 commit comments