Created
May 3, 2021 02:42
-
-
Save xarses/7f815dc84781df79508586d93edfb15c to your computer and use it in GitHub Desktop.
Example gdscript (Godot) json file helpers
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
extends JSONFile | |
# Copyright (C) 2020: Andrew Woodward (xarses) | |
# This file is available under the MIT license | |
class_name Config | |
var meta = {"default": {}} | |
""" | |
data = { | |
default: { | |
key: value | |
} | |
} | |
meta = { | |
default: { | |
key: {default: null, type: "string", desc: ""} | |
} | |
} | |
""" | |
func _init(resource).(resource): | |
pass | |
#func _default_data(): | |
# return {debug = false, servers = [], instanceName = ""} | |
func add_config(key, default=null, type="string", desc="", space="default"): | |
# Declare a new config value, and metadata for rendering, duplicates are not allowed. | |
if not meta.has(space): | |
meta[space] = {} | |
var metaNameSpace = meta[space] | |
var myMeta = {default=default, type=type, desc=desc} | |
if not metaNameSpace.has(key): | |
metaNameSpace[key] = myMeta | |
var storedMeta = metaNameSpace[key] | |
assert(myMeta.hash() == storedMeta.hash(), | |
"Config declration (%s) can not be declared a second time with different metadata" % key) | |
var valueNameSpace = get_default(space, {}) | |
if not valueNameSpace.has(key): | |
valueNameSpace[key] = default | |
var val = valueNameSpace.get(key, default) | |
return val | |
func get(key, default=null, space="default"): | |
if not data.has(space): | |
data[space] = {} | |
return data[space].get(key, default) | |
func get_meta(key, space="default"): | |
if not meta.has(space): | |
meta[space] = {} | |
return meta[space].get(key, {default=null, type="string", desc="Udefined %s" % key}) | |
func get_combined(key, default=null, space="default"): | |
var ret = get_meta(key, space) | |
ret["value"] = get(key, default, space) | |
return ret | |
func get_sapces(): | |
return data.keys() | |
func set(key, value, space="default"): | |
if not data.has(space): | |
data[space] = {} | |
data[space][key] = value | |
func build_ui(key, space="default"): | |
pass | |
func migrate_v1(): | |
# TODO: | |
# We should be able to do backwards compatible migrations with v1 (UPDeck) | |
# This will ensure that normalization occurs when the data loaded | |
pass |
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
extends Reference | |
# Copyright (C) 2020: Andrew Woodward (xarses) | |
# This file is available under the MIT license | |
class_name JSONFile | |
var data := {} | |
var fileName := "" | |
func _init(resource=""): | |
if typeof(resource) == TYPE_STRING: | |
data = self.load(resource) | |
fileName=resource | |
elif typeof(resource) == TYPE_DICTIONARY: | |
data = resource | |
if not data: | |
data = _default_data() | |
# Apply migrations | |
migrate_v1() | |
func _default_data(): | |
# Overload this function if you want to have your own default data returned | |
# It seems to be the easiest way to deal with replacing a variable from | |
# the parent. | |
return {} | |
func get(key, default = null): | |
# Returns the value of the key if defined, otherwise return the default | |
# This DOES NOT alsu set the default, see get_default(key, default) for that | |
return data.get(key, default) | |
func set(key, value): | |
# Sets the key to value in the underlying data structure. | |
data[key] = value | |
func get_default(key, default): | |
# Returns the key if defined, if not it returns the default AND sets the key | |
# to said default value | |
if not data.has(key): | |
data[key] = default | |
return data[key] | |
func load(path): | |
var file = File.new() | |
if file.open(path, File.READ) == OK: | |
print("Opened %s from %s" % [path, file.get_path_absolute()]) | |
var result = JSON.parse(file.get_as_text()) | |
file.close() | |
return result.result | |
# There was an error, but we don't care we'll just init the data later | |
return {} | |
func save(): | |
var file = File.new() | |
var err = file.open(fileName, File.WRITE) | |
if err == OK: | |
file.store_string(to_json(data)) | |
file.close() | |
else: | |
print("WARNING: there was an error attempting to save %s" % fileName) | |
func migrate_v1(): | |
# TODO: | |
# We should be able to do backwards compatible migrations with v1 (UPDeck) | |
# This will ensure that normalization occurs when the data loaded | |
pass | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment