Add Global and SaveManager scripts with localization and save/load functionality
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
# SaveManager.gd (добавить в AutoLoad)
|
||||
extends Node
|
||||
|
||||
# ~/Library/Application Support/Godot/app_userdata/Owniverse Adventure/savegame.cfg
|
||||
const SETTING_SECTION := "Settings"
|
||||
const SAVE_PATH = "user://settings.cfg"
|
||||
|
||||
#region Save Load routines
|
||||
|
||||
|
||||
func save_data(filename: String, password: String, data: Dictionary) -> void:
|
||||
filename = "user://" + filename + ".adventure"
|
||||
var file := FileAccess.open_encrypted_with_pass(filename, FileAccess.WRITE, password)
|
||||
if file == null:
|
||||
push_error("Can't write to the file: ", filename)
|
||||
return
|
||||
|
||||
file.store_var(data) # бинарно + быстрее + сериализация
|
||||
file.close()
|
||||
|
||||
|
||||
func load_data(filename: String, password: String) -> Dictionary:
|
||||
filename = "user://" + filename + ".adventure"
|
||||
if not FileAccess.file_exists(filename):
|
||||
return {}
|
||||
|
||||
var file := FileAccess.open_encrypted_with_pass(filename, FileAccess.READ, password)
|
||||
if file == null:
|
||||
push_error(filename, " couldn't be decrypted.")
|
||||
return {}
|
||||
|
||||
var data: Variant = file.get_var()
|
||||
file.close()
|
||||
|
||||
return data if data is Dictionary else {}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region Settings
|
||||
func save_settings():
|
||||
var config = ConfigFile.new()
|
||||
|
||||
for item in Global.Settings:
|
||||
config.set_value(SETTING_SECTION, item, Global.Settings[item])
|
||||
config.save(SAVE_PATH)
|
||||
|
||||
func load_settings():
|
||||
var config = ConfigFile.new()
|
||||
var err = config.load(SAVE_PATH)
|
||||
if err != OK:
|
||||
return false
|
||||
for item in Global.Settings:
|
||||
Global.Settings[item] = config.get_value(SETTING_SECTION, item, Global.Settings[item])
|
||||
return true
|
||||
#endregion
|
||||
Reference in New Issue
Block a user