ChrisTutorials

gb asset resolver 5.0.5 hotfix

May 3rd, 2026
83
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.37 KB | None | 0 0
  1. ## Centralized asset resolver for loading Grid Building resources from folders.
  2. ##
  3. ## Provides unified loading functionality for placeables, category tags, and other
  4. ## Grid Building assets. Supports both folder-based loading and individual asset arrays.
  5. ## Handles resource validation and provides consistent error handling across the plugin.
  6. class_name GBAssetResolver
  7. extends RefCounted
  8.  
  9. ## Result structure for asset loading operations
  10. class LoadResult:
  11. var assets: Array = []
  12. var errors: Array[String] = []
  13. var warnings: Array[String] = []
  14.  
  15. func is_successful() -> bool:
  16. return errors.is_empty()
  17.  
  18. func has_warnings() -> bool:
  19. return not warnings.is_empty()
  20.  
  21. func get_summary() -> String:
  22. var parts: Array[String] = []
  23. parts.append("Loaded: %d assets" % assets.size())
  24. if not errors.is_empty():
  25. parts.append("Errors: %d" % errors.size())
  26. if not warnings.is_empty():
  27. parts.append("Warnings: %d" % warnings.size())
  28. return "[%s]" % ", ".join(parts)
  29.  
  30. ## Loads placeables from a folder path, maintaining backward compatibility with PlaceableLoader
  31. static func load_placeables(folder_path: String, individual_placeables: Array[Placeable] = []) -> Array[Placeable]:
  32. var result := load_assets_of_type(folder_path, Placeable, individual_placeables)
  33. if not result.is_successful():
  34. push_error("Failed to load placeables from '%s': %s" % [folder_path, str(result.errors)])
  35.  
  36. # Convert generic array to typed array
  37. var typed_result: Array[Placeable] = []
  38. for asset in result.assets:
  39. if asset is Placeable:
  40. typed_result.append(asset as Placeable)
  41. return typed_result
  42.  
  43. ## Loads placeables and returns detailed LoadResult for advanced error handling
  44. static func load_placeables_with_result(folder_path: String, individual_placeables: Array[Placeable] = []) -> LoadResult:
  45. return load_assets_of_type(folder_path, Placeable, individual_placeables)
  46.  
  47. ## Loads category tags from a folder path
  48. static func load_category_tags(folder_path: String, individual_tags: Array[CategoricalTag] = []) -> Array[CategoricalTag]:
  49. var result := load_assets_of_type(folder_path, CategoricalTag, individual_tags)
  50. if not result.is_successful():
  51. push_error("Failed to load category tags from '%s': %s" % [folder_path, str(result.errors)])
  52.  
  53. # Convert generic array to typed array
  54. var typed_result: Array[CategoricalTag] = []
  55. for asset in result.assets:
  56. if asset is CategoricalTag:
  57. typed_result.append(asset as CategoricalTag)
  58. return typed_result
  59.  
  60. ## Loads category tags and returns detailed LoadResult for advanced error handling
  61. static func load_category_tags_with_result(folder_path: String, individual_tags: Array[CategoricalTag] = []) -> LoadResult:
  62. return load_assets_of_type(folder_path, CategoricalTag, individual_tags)
  63.  
  64. ## Loads placeable sequences from a folder path
  65. static func load_placeable_sequences(folder_path: String, individual_sequences: Array[PlaceableSequence] = []) -> Array[PlaceableSequence]:
  66. var result := load_assets_of_type(folder_path, PlaceableSequence, individual_sequences)
  67. if not result.is_successful():
  68. push_error("Failed to load placeable sequences from '%s': %s" % [folder_path, str(result.errors)])
  69.  
  70. # Convert generic array to typed array
  71. var typed_result: Array[PlaceableSequence] = []
  72. for asset in result.assets:
  73. if asset is PlaceableSequence:
  74. typed_result.append(asset as PlaceableSequence)
  75. return typed_result
  76.  
  77. ## Loads placeable sequences and returns detailed LoadResult for advanced error handling
  78. static func load_placeable_sequences_with_result(folder_path: String, individual_sequences: Array[PlaceableSequence] = []) -> LoadResult:
  79. return load_assets_of_type(folder_path, PlaceableSequence, individual_sequences)
  80.  
  81. ## Generic asset loading function that can load any resource type from a folder
  82. static func load_assets_of_type(folder_path: String, asset_type: GDScript, individual_assets: Array = []) -> LoadResult:
  83. var result := LoadResult.new()
  84.  
  85. # Start with individual assets if provided
  86. for asset in individual_assets:
  87. if asset != null and asset.get_script() == asset_type:
  88. result.assets.append(asset)
  89. elif asset != null:
  90. result.warnings.append("Individual asset type mismatch: expected %s, got %s" % [asset_type.get_global_name(), asset.get_class()])
  91. else:
  92. result.warnings.append("Individual asset is null")
  93.  
  94. # Load from folder if path is provided
  95. if folder_path != null and folder_path != "":
  96. var folder_result := _load_from_folder(folder_path, asset_type)
  97. result.assets.append_array(folder_result.assets)
  98. result.errors.append_array(folder_result.errors)
  99. result.warnings.append_array(folder_result.warnings)
  100.  
  101. return result
  102.  
  103. ## Internal helper to load assets from a folder.
  104. ## Uses ResourceLoader.list_directory() which works correctly in exported projects
  105. ## (including web/PCK) where DirAccess methods cannot enumerate .tres resource files.
  106. static func _load_from_folder(folder_path: String, asset_type: GDScript) -> LoadResult:
  107. var result := LoadResult.new()
  108.  
  109. if folder_path == null or folder_path == "":
  110. result.errors.append("Folder path is empty")
  111. return result
  112.  
  113. if not folder_path.begins_with("res://") and not folder_path.begins_with("user://"):
  114. result.errors.append("Path '%s' is not a valid resource path. Use 'res://' or 'user://' paths." % folder_path)
  115. return result
  116.  
  117. var entries := ResourceLoader.list_directory(folder_path)
  118. if entries.is_empty():
  119. return result
  120.  
  121. for entry in entries:
  122. if entry.ends_with("/"):
  123. # Recursively scan sub folders for assets
  124. var sub_path := folder_path.path_join(entry)
  125. var sub_result := _load_from_folder(sub_path, asset_type)
  126. result.assets.append_array(sub_result.assets)
  127. result.errors.append_array(sub_result.errors)
  128. result.warnings.append_array(sub_result.warnings)
  129. elif entry.ends_with(".tres"):
  130. var full_path := folder_path.path_join(entry)
  131. var load_result := _load_single_asset(full_path, asset_type, true)
  132.  
  133. if load_result.asset != null:
  134. result.assets.append(load_result.asset)
  135. elif not load_result.errors.is_empty():
  136. for error in load_result.errors:
  137. if not error.begins_with("Resource type mismatch"):
  138. result.errors.append(error)
  139.  
  140. return result
  141.  
  142. ## Result structure for single asset loading
  143. class SingleAssetResult:
  144. var asset: Resource = null
  145. var errors: Array[String] = []
  146.  
  147. ## Internal helper to load a single asset file with tolerance for type mismatches
  148. static func _load_single_asset(file_path: String, expected_type: GDScript, tolerate_type_mismatch: bool = false) -> SingleAssetResult:
  149. var result := SingleAssetResult.new()
  150.  
  151. if not ResourceLoader.exists(file_path):
  152. result.errors.append("Resource file does not exist: %s" % file_path)
  153. return result
  154.  
  155. var resource := ResourceLoader.load(file_path)
  156. if resource == null:
  157. result.warnings.append("Skipped '%s': ResourceLoader.load returned null (may have missing dependencies)" % file_path)
  158. return result
  159.  
  160. # Check if the resource is of the expected type
  161. var is_correct_type := false
  162.  
  163. # Check if resource has the expected script type
  164. if resource.get_script() == expected_type:
  165. is_correct_type = true
  166. # Check if resource inherits from expected class (for built-in types)
  167. elif expected_type.get_global_name() != "":
  168. var expected_class_name := expected_type.get_global_name()
  169. is_correct_type = resource.get_class() == expected_class_name or resource.has_method("get_class") and resource.get_class() == expected_class_name
  170.  
  171. if is_correct_type:
  172. result.asset = resource
  173. else:
  174. var expected_name := expected_type.get_global_name() if expected_type.get_global_name() != "" else str(expected_type)
  175. var message := "Resource type mismatch in '%s': expected %s, got %s" % [file_path, expected_name, resource.get_class()]
  176. if tolerate_type_mismatch:
  177. # For folder loading, type mismatches are just warnings - skip the file
  178. # This is expected when a folder contains mixed resource types
  179. pass # Just skip - don't even warn since this is normal
  180. else:
  181. result.errors.append(message)
  182.  
  183. return result
  184.  
  185. ## Validates that a folder path is accessible.
  186. ## Uses ResourceLoader.list_directory() for export-compatible checking.
  187. static func validate_folder_path(folder_path: String) -> bool:
  188. if folder_path == null or folder_path == "":
  189. return false
  190.  
  191. if not folder_path.begins_with("res://") and not folder_path.begins_with("user://"):
  192. return false
  193.  
  194. return not ResourceLoader.list_directory(folder_path).is_empty()
  195.  
  196. ## Gets diagnostic information about a folder's contents.
  197. ## Uses ResourceLoader.list_directory() for export-compatible resource enumeration.
  198. static func get_folder_diagnostics(folder_path: String) -> Dictionary:
  199. var diagnostics := {
  200. "exists": false,
  201. "total_files": 0,
  202. "tres_files": 0,
  203. "subdirectories": 0,
  204. "sample_files": []
  205. }
  206.  
  207. if folder_path == null or folder_path == "":
  208. return diagnostics
  209.  
  210. if not folder_path.begins_with("res://") and not folder_path.begins_with("user://"):
  211. return diagnostics
  212.  
  213. var entries := ResourceLoader.list_directory(folder_path)
  214. if entries.is_empty():
  215. return diagnostics
  216.  
  217. diagnostics.exists = true
  218.  
  219. for entry in entries:
  220. if entry.ends_with("/"):
  221. diagnostics.subdirectories += 1
  222. else:
  223. diagnostics.total_files += 1
  224. if entry.ends_with(".tres"):
  225. diagnostics.tres_files += 1
  226. if diagnostics.sample_files.size() < 5:
  227. diagnostics.sample_files.append(entry)
  228.  
  229. return diagnostics
Advertisement
Comments
  • ChrisTutorials
    200 days
    # text 0.13 KB | 0 0
    1. Copy paste to addons\grid_building\utils\gb_asset_resolver.gd replacing the old one to add nested subdirectory placeable loading
Add Comment
Please, Sign In to add comment