73 lines
1.8 KiB
GDScript
73 lines
1.8 KiB
GDScript
# UltraFlare
|
|
# Store.gd is a set of functions for storing settings/data using JSON files
|
|
|
|
extends Node
|
|
|
|
# Create a new file object for the config
|
|
var store_dir = Directory.new()
|
|
var store_file = File.new()
|
|
|
|
# Define a place to store places to look for the date
|
|
var store_paths = [OS.get_config_dir()+"/UltraFlare/"]
|
|
var store_path = null
|
|
|
|
|
|
# When script/game is loaded,
|
|
func _ready():
|
|
# For each path,
|
|
for path in store_paths:
|
|
# If the path exists,
|
|
if store_dir.dir_exists(path):
|
|
if store_file.file_exists(path):
|
|
# Set the data_path to that path
|
|
store_path = path
|
|
# Break out of the loop
|
|
break
|
|
|
|
# If the for loop finished with no results,
|
|
if store_path == null:
|
|
# Set the data path to the first entry in the data_paths array
|
|
store_path = store_paths[0]
|
|
store_dir.make_dir_recursive(store_path)
|
|
store_file.open(store_path+"/store.json", 3)
|
|
|
|
# get_store()
|
|
# Used for getting all data in storage
|
|
func get_store():
|
|
var data = JSONParseResult.new()
|
|
store_file.open(store_path+"/store.json", 1)
|
|
|
|
# Read the data from the stores
|
|
if store_file.get_as_text() != "":
|
|
data = JSON.parse(store_file.get_as_text())
|
|
|
|
store_file.close()
|
|
|
|
# If no data was found,
|
|
if data.error or data == null or data.result == null:
|
|
return {}
|
|
# If data was found,
|
|
return data.result
|
|
|
|
|
|
# store()
|
|
# Used for storing data to the storage
|
|
func write_store(key, value):
|
|
# Read the data from the store
|
|
var data = get_store()
|
|
|
|
# If the value is null,
|
|
if value == null:
|
|
# Remove the key-value pair from the dictionary
|
|
data.erase(key)
|
|
else:
|
|
# Store the key-value pair
|
|
data[key] = value
|
|
|
|
store_file.open(store_path+"/store.json", 2)
|
|
|
|
# Translate the dictionary back to JSON and store it
|
|
store_file.store_string(JSON.print(data, " "))
|
|
# Close the file
|
|
store_file.close()
|