This repository has been archived on 2023-12-19. You can view files and clone it, but cannot push or open issues or pull requests.
MUR/scripts/game/local_storage.gd

58 lines
1.2 KiB
GDScript3
Raw Normal View History

2019-07-23 19:49:04 +02:00
extends Node
const FILE_PATH = "user://local_storage"
func _ready():
TranslationServer.set_locale(read_value("locale","en"))
func read_content():
var f = File.new()
var err = f.open(FILE_PATH, File.READ)
var content = {}
if err == OK:
content = parse_json(f.get_as_text())
f.close()
if content == null:
content = {}
return content
func write_content(content:Dictionary):
var f = File.new()
var err = f.open(FILE_PATH, File.WRITE)
#var err = f.open_encrypted_with_pass(FILE_PATH, File.WRITE, OS.get_unique_id())
f.store_string(to_json(content))
f.close()
func write_value(key:String, value):
var content = read_content()
content[key] = value
write_content(content)
func read_value(key:String, default = null):
var content = read_content()
if content.has(key) && content.get(key) != null:
return content.get(key)
else:
return default
func delete_value(key:String):
var content = read_content()
content.erase(key)
write_content(content)
func write_values(new_content:Dictionary):
var content = read_content()
for key in new_content:
content[key] = new_content[key]
write_content(content)
func read_values():
return read_content()