Legend of the Gold Box... A game written for the LOWREZJAM 2018 game jam
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

159 lines
5.0KB

  1. import os, sys
  2. import json
  3. import weakref
  4. import pygame
  5. from .resourceLoaders import *
  6. class ResourceError(Exception):
  7. pass
  8. _GAME_PATH=calculate_real_path(os.path.dirname(sys.argv[0]))
  9. _RESOURCES={}
  10. def define_resource_type(rtype, sub_path, loader_fn):
  11. global _RESOURCES, _GAME_PATH, join_path
  12. if rtype in _RESOURCES:
  13. raise ResourceError("Resource '{}' already defined.".format(rtype))
  14. fullpath = join_path(_GAME_PATH, sub_path)
  15. if not os.path.isdir(fullpath):
  16. print("'{}' | '{}'".format(_GAME_PATH, fullpath))
  17. raise ResourceError("'{}' is not a valid directory.".format(sub_path))
  18. if not callable(loader_fn):
  19. raise ResourceError("Expected a callable as the resource loader.")
  20. _RESOURCES[rtype]={"r":[], "loader":loader_fn, "path":fullpath}
  21. def configure(conf):
  22. global _RESOURCES, join_path
  23. if not isinstance(conf, dict):
  24. raise TypeError("Expected a dictionary.")
  25. for key in conf:
  26. if key in _RESOURCES:
  27. fullpath = join_path(_GAME_PATH, conf[key])
  28. if not os.path.isdir(fullpath):
  29. raise ResourceError("'{}' is not a valid directory.".format(conf[key]))
  30. _RESOURCES[key]["path"] = fullpath
  31. _RESOURCES[key]["r"] = [] # Completely drop old list.
  32. class Manager:
  33. def __init__(self):
  34. pass
  35. @property
  36. def game_path(self):
  37. global _GAME_PATH
  38. return _GAME_PATH
  39. @property
  40. def resource_types(self):
  41. global _RESOURCES
  42. rtypes = []
  43. for key in _RESOURCES:
  44. rtypes.append(key)
  45. return rtypes
  46. def _getResourceDict(self, rtype, src):
  47. global _RESOURCES
  48. if rtype in _RESOURCES:
  49. for r in _RESOURCES[rtype]["r"]:
  50. if r["src"] == src:
  51. return r
  52. return None
  53. def has(self, rtype, src):
  54. return (self._getResourceDict(rtype, src) is not None)
  55. def store(self, rtype, src):
  56. global _RESOURCES
  57. if type not in _RESOURCES:
  58. raise ResourceError("Unknown resource type '{}'.".format(rtype))
  59. if self._getResourceDict(rtype, src) == None:
  60. _RESOURCES[rtype]["r"].append({"src":src, "instance":None, "locked":False})
  61. return self
  62. def remove(self, rtype, src):
  63. global _RESOURCES
  64. d = self._getResourceDict(rtype, src)
  65. if d is None:
  66. raise ResourceError("No '{}' resource '{}' stored.".format(rtype, src))
  67. _RESOURCES[rtype]["r"].remove(d)
  68. return self
  69. def clear(self, rtype, src, ignore_lock):
  70. d = self._getResourceDict(rtype, src)
  71. if d is None:
  72. raise ResourceError("No '{}' resource '{}' stored.".format(rtype, src))
  73. if d["locked"] == False or ignore_lock == True:
  74. d["instance"] = None
  75. return self
  76. def get(self, rtype, src):
  77. global _RESOURCES
  78. if rtype not in _RESOURCES:
  79. raise ResourceError("Unknown resource type '{}'.".format(rtype))
  80. d = self._getResourceDict(rtype, src)
  81. if d is None:
  82. raise ResourceError("No '{}' resource '{}' stored.".format(rtype, src))
  83. if d["instance"] is None:
  84. loader = _RESOURCES[rtype]["loader"]
  85. filename = join_path(self.data_path, src)
  86. try:
  87. d["instance"] = loader(filename)
  88. except Exception as e:
  89. raise e
  90. return weakref.ref(d["instance"])
  91. def lock(self, rtype, src, lock=True):
  92. d = self._getResourceDict(rtype, src)
  93. if d is None:
  94. raise ResourceError("No '{}' resource '{}' stored.".format(rtype, src))
  95. d["locked"]=lock
  96. return self
  97. def is_locked(self, rtype, src):
  98. d = self._getResourceDict(rtype, src)
  99. if d is not None and d["src"] == src:
  100. return d["locked"]
  101. return False
  102. def clear_resource_type(self, rtype, ignore_lock=False):
  103. global _RESOURCES
  104. if rtype in _RESOURCES:
  105. for r in _RESOURCES[rtype]["r"]:
  106. if r["locked"] == False or ignore_lock == True:
  107. r["instance"] = None
  108. return self
  109. def clear_resources(self, ignore_lock=False):
  110. global _RESOURCES
  111. for key in _RESOURCES:
  112. self.clear_resource_type(key, ignore_lock)
  113. return self
  114. def remove_resource_type(self, rtype):
  115. global _RESOURCES
  116. if rtype not in _RESOURCES:
  117. raise ResourceError("Unknown resource type '{}'.".format(rtype))
  118. _RESOURCES[rtype]["r"] = []
  119. return self
  120. def remove_resources(self):
  121. global _RESOURCES
  122. for key in _RESOURCES:
  123. _RESOURCES[key]["r"] = []
  124. return self
  125. # ---------------------------------------------------------------
  126. # Defining the built-in loaders located in resourceLoaders.py
  127. # ---------------------------------------------------------------
  128. define_resource_type("graphic", "graphics/", load_image)
  129. define_resource_type("audio", "audio/", load_audio)
  130. define_resource_type("json", "data/json/", load_JSON)