ResourceGetter.gd 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. class_name ResourceGetter
  2. extends Node
  3. var original_resource: Resource = null
  4. static var cache := {}
  5. func get_resource(resource: Resource) -> Resource:
  6. if resource == null:
  7. return null
  8. if original_resource == null:
  9. original_resource = resource
  10. if cache.has(original_resource.resource_path) and resource is not AtlasTexture:
  11. return cache.get(original_resource.resource_path)
  12. var path := ""
  13. if original_resource is AtlasTexture:
  14. path = get_resource_path(original_resource.atlas.resource_path)
  15. else:
  16. path = get_resource_path(original_resource.resource_path)
  17. if path == original_resource.resource_path:
  18. return original_resource
  19. if original_resource is Texture:
  20. var new_resource = null
  21. if path.contains("user://"):
  22. new_resource = ImageTexture.create_from_image(Image.load_from_file(path))
  23. else:
  24. new_resource = load(path)
  25. send_to_cache(original_resource.resource_path, new_resource)
  26. if original_resource is AtlasTexture:
  27. var atlas = AtlasTexture.new()
  28. atlas.atlas = new_resource
  29. atlas.region = original_resource.region
  30. return atlas
  31. return new_resource
  32. elif original_resource is AudioStream:
  33. if path.get_file().contains(".wav"):
  34. var new_resource = AudioStreamWAV.load_from_file(path)
  35. send_to_cache(original_resource.resource_path, new_resource)
  36. return new_resource
  37. elif path.get_file().contains(".mp3"):
  38. var new_resource = AudioStreamMP3.load_from_file(path)
  39. send_to_cache(original_resource.resource_path, new_resource)
  40. return new_resource
  41. elif original_resource is Font:
  42. var new_font = FontFile.new()
  43. new_font.load_bitmap_font(path)
  44. send_to_cache(original_resource.resource_path, new_font)
  45. return new_font
  46. send_to_cache(original_resource.resource_path, original_resource)
  47. return original_resource
  48. func send_to_cache(resource_path := "", resource_to_cache: Resource = null) -> void:
  49. if cache.has(resource_path) == false:
  50. cache.set(resource_path, resource_to_cache)
  51. func get_resource_path(resource_path := "") -> String:
  52. for i in Settings.file.visuals.resource_packs:
  53. var test = resource_path.replace("res://Assets/", "user://resource_packs/" + i + "/")
  54. if FileAccess.file_exists(test):
  55. return test
  56. return resource_path