Created
February 11, 2023 06:47
-
-
Save boruok/6293137b016f4f1c3e94fb6d287ad4c6 to your computer and use it in GitHub Desktop.
makeuse config files databases
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class_name Database extends Resource | |
export var directory := "res://data/" | |
export var extension := "cfg" | |
export var include_classes := true | |
var _tables := {} | |
func add_table(table_name: String, data_name: String) -> void: | |
if _tables.has(table_name): | |
Logger.warn(self, "failed to add new table: name already exist") | |
return | |
_tables[table_name] = Table.new(data_name) | |
func get_table(table_name: String) -> Table: | |
return _tables.get(table_name, null) | |
func has_table(table_name: String) -> bool: | |
return _tables.has(table_name) | |
func _init() -> void: | |
# load config data | |
var filedict := Utils.get_file_dict(directory, [extension]) | |
for k in filedict: | |
add_table(k, filedict[k]) | |
# load builtin class data | |
if include_classes: | |
add_table("classes", "") | |
# values are stored as list, so we have to iterate and manually add em into table | |
var classes := get_table("classes") | |
for i in ProjectSettings.get_setting("_global_script_classes"): | |
classes.set_value(i["class"], "path", i.path) | |
func get_class() -> String: | |
return "Database" | |
class Table: | |
var _base := "_base" | |
var _cfg : ConfigFile | |
func _init(filepath:="") -> void: | |
_cfg = ConfigFile.new() | |
if !filepath.empty(): | |
var err := _cfg.load(filepath) | |
var err_text := "" | |
match err: | |
7: | |
err_text += "file not found" | |
if err: | |
print_debug("%s failed to load config file: %s" % [get_class(), err_text]) | |
func set_value(section: String, key: String, value=null) -> void: | |
_cfg.set_value(section, key, value) | |
func get_value(section: String, key: String, default=null): | |
return _get_value(section, key, default) | |
func has_section(section: String) -> bool: | |
return _cfg.has_section(section) | |
func get_section(section: String) -> Dictionary: | |
var result := {} | |
for key in _cfg.get_section_keys(section): | |
result[key] = _cfg.get_value(section, key) | |
if key == _base: | |
result = get_section(_cfg.get_value(section, key)) | |
return result | |
func get_section_keys(section: String) -> PoolStringArray: | |
return _cfg.get_section_keys(section) | |
func get_sections() -> PoolStringArray: | |
var r : PoolStringArray | |
for s in _cfg.get_sections(): | |
if _cfg.get_value(s, "_base", ""): | |
r.append(s) | |
return r | |
func get_class() -> String: | |
return "Table" | |
func _get_value(section: String, key: String, default=null): | |
if _cfg.has_section_key(section, key): | |
return _cfg.get_value(section, key) | |
else: | |
if _cfg.has_section_key(section, _base): | |
return _get_value(_cfg.get_value(section, _base), key, default) | |
return default |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment