Conf.gd 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. extends ServiceBase
  2. class_name Conf
  3. #
  4. enum Type
  5. {
  6. NONE = -1,
  7. SETTINGS = 0,
  8. USERSETTINGS,
  9. COUNT
  10. }
  11. static var confFiles : Array = []
  12. static var cache : Dictionary = {}
  13. #
  14. static func GetCacheID(section : String, key : String, type : Type) -> String:
  15. return "%s%s%d" % [section, key, type]
  16. static func GetVariant(section : String, key : String, type : Type, default = null):
  17. if not confFiles[type] or type >= Type.COUNT:
  18. assert(false, "Config type is not valid, returning default value")
  19. return default
  20. var value = default
  21. var cacheID = GetCacheID(section, key, type)
  22. if cacheID in cache:
  23. value = cache[cacheID]
  24. elif confFiles[type].has_section_key(section, key):
  25. value = confFiles[type].get_value(section, key, default)
  26. cache[cacheID] = value
  27. return value
  28. static func GetBool(section : String, key : String, type : Type = Type.NONE) -> bool:
  29. return GetVariant(section, key, type, false)
  30. static func GetInt(section : String, key : String, type : Type = Type.NONE) -> int:
  31. return GetVariant(section, key, type, 0)
  32. static func GetFloat(section : String, key : String, type : Type = Type.NONE) -> float:
  33. return GetVariant(section, key, type, 0.0)
  34. static func GetVector2(section : String, key : String, type : Type = Type.NONE) -> Vector2:
  35. return GetVariant(section, key, type, Vector2.ZERO)
  36. static func GetVector2i(section : String, key : String, type : Type = Type.NONE) -> Vector2i:
  37. return GetVariant(section, key, type, Vector2i.ZERO)
  38. static func GetString(section : String, key : String, type : Type = Type.NONE) -> String:
  39. return GetVariant(section, key, type, "")
  40. static func SetValue(section : String, key : String, type : Type, value):
  41. assert(type < Type.COUNT and confFiles[type] != null, "Can't find %s within our loaded conf files")
  42. if type >= Type.COUNT or not confFiles[type]:
  43. return
  44. confFiles[type].set_value(section, key, value)
  45. var cacheID = GetCacheID(section, key, type)
  46. if cacheID in cache:
  47. cache[cacheID] = value
  48. static func HasSection(section : String, type : Type) -> bool:
  49. assert(type < Type.COUNT, "Can't find %s within our loaded conf files")
  50. return type < Type.COUNT and confFiles[type].has_section(section)
  51. static func HasSectionKey(section : String, key : String, type : Type) -> bool:
  52. assert(type < Type.COUNT, "Can't find %s within our loaded conf files")
  53. return type < Type.COUNT and confFiles[type].has_section_key(section, key)
  54. static func SaveType(fileName : String, type : Type):
  55. assert(type < Type.COUNT, "Can't find %s within our loaded conf files")
  56. if type < Type.COUNT:
  57. FileSystem.SaveConfig(fileName, confFiles[type])
  58. #
  59. static func Init():
  60. confFiles.append(FileSystem.LoadConfig("settings"))
  61. confFiles.append(FileSystem.LoadConfig("settings", true))
  62. assert(confFiles.size() == Type.COUNT, "Config files count mismatch")