DB.gd 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. extends Object
  2. class_name DB
  3. static var MapsDB : Dictionary[int, FileData] = {}
  4. static var MusicsDB : Dictionary[int, FileData] = {}
  5. static var RacesDB : Dictionary[int, RaceData] = {}
  6. static var HairstylesDB : Dictionary[int, FileData] = {}
  7. static var PalettesDB : Array[Dictionary] = []
  8. static var EntitiesDB : Dictionary[int, EntityData] = {}
  9. static var EmotesDB : Dictionary[int, BaseCell] = {}
  10. static var ItemsDB : Dictionary[int, ItemCell] = {}
  11. static var SkillsDB : Dictionary[int, SkillCell] = {}
  12. static var QuestsDB : Dictionary[int, QuestData] = {}
  13. static var hashDB : Dictionary = {}
  14. const UnknownHash : int = -1
  15. static var PlayerHash : int = "Player".hash()
  16. static var ShipHash : int = "Ship".hash()
  17. static var OceanHash : int = "Ocean".hash()
  18. enum Palette
  19. {
  20. HAIR = 0,
  21. SKIN,
  22. EQUIPMENT,
  23. COUNT
  24. }
  25. #
  26. static func ParseMapsDB():
  27. var result = FileSystem.LoadDB("maps.json")
  28. if not result.is_empty():
  29. for key in result:
  30. var data : FileData = FileData.Create(key, result[key].Path)
  31. MapsDB[data._id] = data
  32. static func ParseMusicsDB():
  33. var result = FileSystem.LoadDB("musics.json")
  34. if not result.is_empty():
  35. for key in result:
  36. var data : FileData = FileData.Create(key, result[key].Path)
  37. MusicsDB[data._id] = data
  38. static func ParseRacesDB():
  39. var result = FileSystem.LoadDB("races.json")
  40. if not result.is_empty():
  41. for key in result:
  42. var id = SetCellHash(key)
  43. assert(id not in RacesDB, "Duplicated cell in RacesDB")
  44. RacesDB[id] = RaceData.Create(key, result[key])
  45. static func ParseHairstylesDB():
  46. var result : Dictionary = FileSystem.LoadDB("hairstyles.json")
  47. if not result.is_empty():
  48. for key in result:
  49. var data : FileData = FileData.Create(key, result[key])
  50. HairstylesDB[data._id] = data
  51. static func ParsePalettesDB():
  52. PalettesDB.resize(Palette.COUNT)
  53. var result : Dictionary = FileSystem.LoadDB("palettes.json")
  54. if not result.is_empty():
  55. for categoryKey in result:
  56. var category : Dictionary = result[categoryKey]
  57. var categoryIdx : int = int(categoryKey)
  58. for key in category:
  59. var id = SetCellHash(key)
  60. assert(id not in PalettesDB[categoryIdx], "Duplicated cell in PalettesDB")
  61. PalettesDB[categoryIdx][id] = FileData.Create(key, category[key])
  62. static func ParseEntitiesDB():
  63. var result = FileSystem.LoadDB("entities.json")
  64. if not result.is_empty():
  65. for key in result:
  66. var entity : EntityData = EntityData.Create(result[key])
  67. EntitiesDB[entity._id] = entity
  68. static func ParseEmotesDB():
  69. var result = FileSystem.LoadDB("emotes.json")
  70. if not result.is_empty():
  71. for key in result:
  72. var cell : BaseCell = FileSystem.LoadCell(Path.EmotePst + result[key].Path + Path.RscExt)
  73. cell.id = SetCellHash(cell.name)
  74. assert(EmotesDB.has(cell.id) == false, "Duplicated cell in EmotesDB")
  75. EmotesDB[cell.id] = cell
  76. static func ParseItemsDB():
  77. var result = FileSystem.LoadDB("items.json")
  78. if not result.is_empty():
  79. for key in result:
  80. var cell : ItemCell = FileSystem.LoadCell(Path.ItemPst + result[key].Path + Path.RscExt)
  81. cell.id = SetCellHash(cell.name)
  82. assert(ItemsDB.has(cell.id) == false, "Duplicated cell in ItemsDB")
  83. ItemsDB[cell.id] = cell
  84. static func ParseSkillsDB():
  85. var result = FileSystem.LoadDB("skills.json")
  86. if not result.is_empty():
  87. for key in result:
  88. var cell : SkillCell = FileSystem.LoadCell(Path.SkillPst + result[key].Path + Path.RscExt)
  89. cell.Instantiate()
  90. cell.id = SetCellHash(cell.name)
  91. assert(SkillsDB.has(cell.id) == false, "Duplicated cell in SkillsDB")
  92. SkillsDB[cell.id] = cell
  93. static func ParseQuestsDB():
  94. var result = FileSystem.LoadDB("quests.json")
  95. if not result.is_empty():
  96. for key in result:
  97. var quest : QuestData = FileSystem.LoadQuest(Path.QuestPst + result[key].Path + Path.RscExt)
  98. assert(QuestsDB.has(quest.id) == false, "Duplicated quest in QuestsDB")
  99. QuestsDB[quest.id] = quest
  100. #
  101. static func HasCellHash(cellname : StringName) -> bool:
  102. return hashDB.has(cellname)
  103. static func SetCellHash(cellname : StringName) -> int:
  104. var cellHash : int = UnknownHash
  105. var hasHash : bool = HasCellHash(cellname)
  106. assert(not hasHash, "Cell hash %d already exists for %s" % [cellHash, cellname])
  107. if not hasHash:
  108. cellHash = cellname.hash()
  109. hashDB[cellname] = cellHash
  110. return cellHash
  111. static func GetCellHash(cellname : StringName) -> int:
  112. var hasHash : bool = HasCellHash(cellname)
  113. assert(hasHash, "Cell hash doesn't exist for " + cellname)
  114. return hashDB[cellname] if hasHash else UnknownHash
  115. #
  116. static func GetItem(cellHash : int, customfield : String = "") -> ItemCell:
  117. var cell : ItemCell = ItemsDB.get(cellHash, null)
  118. assert(cell != null, "Could not find the identifier %s in ItemsDB" % [cellHash])
  119. if cell and customfield != cell.customfield:
  120. var customCell = cell.duplicate()
  121. customCell.customfield = customfield
  122. if HasCellHash(customfield):
  123. var paletteHash : int = GetCellHash(customfield)
  124. if paletteHash in PalettesDB[Palette.EQUIPMENT]:
  125. var paletteData : FileData = DB.GetPalette(DB.Palette.EQUIPMENT, paletteHash)
  126. if paletteData:
  127. customCell.shader = FileSystem.LoadPalette(paletteData._path)
  128. return customCell
  129. else:
  130. return cell
  131. static func GetEntity(entityHash : int) -> EntityData:
  132. var data : EntityData = EntitiesDB.get(entityHash, null)
  133. assert(data != null, "Could not find the identifier %s in EntitiesDB" % [entityHash])
  134. return data
  135. static func GetEmote(cellHash : int) -> BaseCell:
  136. var data : BaseCell = EmotesDB.get(cellHash, null)
  137. assert(data != null, "Could not find the identifier %s in EmotesDB" % [cellHash])
  138. return data
  139. static func GetSkill(cellHash : int) -> SkillCell:
  140. var data : SkillCell = SkillsDB.get(cellHash, null)
  141. assert(data != null, "Could not find the identifier %s in SkillsDB" % [cellHash])
  142. return data
  143. static func GetRace(cellHash : int) -> RaceData:
  144. var data : RaceData = RacesDB.get(cellHash, null)
  145. assert(data != null, "Could not find the identifier %s in RacesDB" % [cellHash])
  146. return data
  147. static func GetHairstyle(cellHash : int) -> FileData:
  148. var data : FileData = HairstylesDB.get(cellHash, null)
  149. assert(data != null, "Could not find the identifier %s in HairstylesDB" % [cellHash])
  150. return data
  151. static func GetPalette(type : Palette, cellHash : int) -> FileData:
  152. var data : FileData = PalettesDB[type].get(cellHash, null)
  153. assert(data != null, "Could not find the identifier %s in PalettesDB" % [cellHash])
  154. return data
  155. static func GetQuest(questID : int) -> QuestData:
  156. var data : QuestData = QuestsDB.get(questID, null)
  157. assert(data != null, "Could not find the identifier %s in QuestsDB" % [questID])
  158. return data
  159. #
  160. static func Init():
  161. ParseMapsDB()
  162. ParseMusicsDB()
  163. ParsePalettesDB()
  164. ParseRacesDB()
  165. ParseHairstylesDB()
  166. ParseEmotesDB()
  167. ParseItemsDB()
  168. ParseSkillsDB()
  169. ParseEntitiesDB()
  170. ParseQuestsDB()