Completed profile meta save/load + legit check by hash
This commit is contained in:
parent
b13764f9a5
commit
94ee27472c
3 changed files with 52 additions and 19 deletions
|
|
@ -8,9 +8,10 @@ enum AMMO_TYPE {NULL, LASER_ENERGY, ROCKETS}
|
||||||
const DEFAULT_ITEM = preload("res://items/test_item.tres")
|
const DEFAULT_ITEM = preload("res://items/test_item.tres")
|
||||||
const DEFAULT_WEAPON = preload("res://scenes/weapons/presets/SingleLaserMk1.tscn")
|
const DEFAULT_WEAPON = preload("res://scenes/weapons/presets/SingleLaserMk1.tscn")
|
||||||
|
|
||||||
static var gameversion = "Ictar 1.1"
|
const gameversion = "Ictar 1.1"
|
||||||
|
|
||||||
static func profile_create(profile_name):
|
## Creates new profile or resaves it if it exists
|
||||||
|
static func profile_create(profile_name : String) -> void:
|
||||||
var path = "user://"+profile_name+".cosmic"
|
var path = "user://"+profile_name+".cosmic"
|
||||||
if FileAccess.file_exists(path):
|
if FileAccess.file_exists(path):
|
||||||
profile_save(profile_name, "menu")
|
profile_save(profile_name, "menu")
|
||||||
|
|
@ -27,47 +28,69 @@ static func profile_create(profile_name):
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var profile_meta_keys = profile_meta['meta'].keys()
|
var profile_meta_keys = profile_meta['meta'].keys()
|
||||||
for i in range(len(profile_meta_keys)):
|
|
||||||
if profile_meta_keys[i][0] == "_":
|
|
||||||
profile_meta_keys.remove_at(i)
|
|
||||||
for i in range(len(profile_meta_keys)):
|
for i in range(len(profile_meta_keys)):
|
||||||
profile_meta['hash'][i] = str(profile_meta['meta'][profile_meta_keys[i]]).sha256_buffer().hex_encode()
|
profile_meta['hash'][i] = str(profile_meta['meta'][profile_meta_keys[i]]).sha256_buffer().hex_encode()
|
||||||
var file = FileAccess.open(path, FileAccess.WRITE)
|
var file = FileAccess.open(path, FileAccess.WRITE)
|
||||||
var json_string = JSON.stringify(profile_meta, "\t")
|
var json_string = JSON.stringify(profile_meta, "\t")
|
||||||
file.store_string(json_string)
|
file.store_string(json_string)
|
||||||
|
|
||||||
static func profile_save(profile_name, _gamestate):
|
## Saves profile with new last_time and last_version keys or creates one if it doesn't exist
|
||||||
|
static func profile_save(profile_name : String, _gamestate : String) -> void:
|
||||||
var path = "user://"+profile_name+".cosmic"
|
var path = "user://"+profile_name+".cosmic"
|
||||||
if not FileAccess.file_exists(path):
|
if not FileAccess.file_exists(path):
|
||||||
profile_create(profile_name)
|
profile_create(profile_name)
|
||||||
return
|
return
|
||||||
var profile_meta = profile_load(profile_name)
|
var profile_meta = profile_load(profile_name)
|
||||||
profile_meta['meta']['lastversion'] = gameversion
|
profile_save_from_meta(profile_meta)
|
||||||
profile_meta['meta']['lastupdated'] = Time.get_datetime_string_from_system()
|
|
||||||
|
## Saves profile with provided profile meta
|
||||||
|
static func profile_save_from_meta(profile_meta : Dictionary) -> void:
|
||||||
|
var path = "user://"+profile_meta['meta']['profile_name']+".cosmic"
|
||||||
|
profile_meta['meta']['last_version'] = gameversion
|
||||||
|
profile_meta['meta']['last_updated'] = Time.get_datetime_string_from_system()
|
||||||
var profile_meta_keys = profile_meta['meta'].keys()
|
var profile_meta_keys = profile_meta['meta'].keys()
|
||||||
for i in range(len(profile_meta_keys)):
|
|
||||||
if profile_meta_keys[i][0] == "_":
|
|
||||||
profile_meta_keys.remove_at(i)
|
|
||||||
for i in range(len(profile_meta_keys)):
|
for i in range(len(profile_meta_keys)):
|
||||||
profile_meta['hash'][i] = str(profile_meta['meta'][profile_meta_keys[i]]).sha256_buffer().hex_encode()
|
profile_meta['hash'][i] = str(profile_meta['meta'][profile_meta_keys[i]]).sha256_buffer().hex_encode()
|
||||||
var file = FileAccess.open(path, FileAccess.WRITE)
|
var file = FileAccess.open(path, FileAccess.WRITE)
|
||||||
var json_string = JSON.stringify(profile_meta, "\t")
|
var json_string = JSON.stringify(profile_meta, "\t")
|
||||||
file.store_string(json_string)
|
file.store_string(json_string)
|
||||||
|
|
||||||
|
|
||||||
static func profile_load(profile_name):
|
## Returns profile meta of profile if it exists
|
||||||
|
static func profile_load(profile_name : String) -> Dictionary:
|
||||||
var path = "user://"+profile_name+".cosmic"
|
var path = "user://"+profile_name+".cosmic"
|
||||||
if not FileAccess.file_exists(path):
|
if not FileAccess.file_exists(path):
|
||||||
return
|
return {}
|
||||||
var file = FileAccess.open(path, FileAccess.READ)
|
var file = FileAccess.open(path, FileAccess.READ)
|
||||||
var string = file.get_as_text()
|
var string = file.get_as_text()
|
||||||
var profile_meta = JSON.parse_string(string)
|
var profile_meta = JSON.parse_string(string)
|
||||||
for meta in profile_meta:
|
for meta in profile_meta:
|
||||||
print(meta, ": ", profile_meta[meta])
|
print(meta, ": ", profile_meta[meta])
|
||||||
print(profile_meta[meta].keys())
|
|
||||||
return profile_meta
|
return profile_meta
|
||||||
|
|
||||||
static func get_item(id : String):
|
## Deletes profile file if it exists
|
||||||
|
static func profile_delete(profile_name : String) -> void:
|
||||||
|
var path = "user://{profile}.cosmic".format({"profile": profile_name})
|
||||||
|
if FileAccess.file_exists(path):
|
||||||
|
var dir = DirAccess.open("user://")
|
||||||
|
dir.remove(path)
|
||||||
|
|
||||||
|
## Compares existing hash and calculated hash of file and if they are not equal resaves file with legit = false.
|
||||||
|
static func profile_legit_check(profile_meta : Dictionary) -> void:
|
||||||
|
var old_hash = []
|
||||||
|
var new_hash = []
|
||||||
|
var profile_meta_keys = profile_meta['meta'].keys()
|
||||||
|
for i in range(len(profile_meta_keys)):
|
||||||
|
old_hash.append(profile_meta['hash'][str(i)])
|
||||||
|
new_hash.append(str(profile_meta['meta'][profile_meta_keys[i]]).sha256_buffer().hex_encode())
|
||||||
|
old_hash.sort()
|
||||||
|
new_hash.sort()
|
||||||
|
if old_hash == new_hash and profile_meta['meta']['legit']:
|
||||||
|
return
|
||||||
|
profile_meta['meta']['legit'] = false
|
||||||
|
profile_save_from_meta(profile_meta)
|
||||||
|
|
||||||
|
## Returns copy of loaded item resource in directory if it exists or DEFAULT_ITEM instead.
|
||||||
|
static func get_item(id : String) -> Item:
|
||||||
var path_name = id.to_lower().replace(" ", "_")
|
var path_name = id.to_lower().replace(" ", "_")
|
||||||
var path = "res://items/{name}.tres".format({"name": path_name})
|
var path = "res://items/{name}.tres".format({"name": path_name})
|
||||||
var res = load(path)
|
var res = load(path)
|
||||||
|
|
@ -76,7 +99,8 @@ static func get_item(id : String):
|
||||||
else:
|
else:
|
||||||
return DEFAULT_ITEM.duplicate()
|
return DEFAULT_ITEM.duplicate()
|
||||||
|
|
||||||
static func get_weapon(id : String):
|
## Returns weapon packed scene if it exists or DEFAULT_WEAPON instead
|
||||||
|
static func get_weapon(id : String) -> PackedScene:
|
||||||
var path_name = id.replace(" ", "")
|
var path_name = id.replace(" ", "")
|
||||||
var path = "res://weapons/presets/{name}.tscn".format({"name": path_name})
|
var path = "res://weapons/presets/{name}.tscn".format({"name": path_name})
|
||||||
var res = load(path)
|
var res = load(path)
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ extends Node2D
|
||||||
|
|
||||||
var map_width = 1280
|
var map_width = 1280
|
||||||
var map_height = 720
|
var map_height = 720
|
||||||
|
var menu_id = 0
|
||||||
@onready var b1 = $Control/MenuButton1
|
@onready var b1 = $Control/MenuButton1
|
||||||
@onready var b2 = $Control/MenuButton2
|
@onready var b2 = $Control/MenuButton2
|
||||||
@onready var b3 = $Control/MenuButton3
|
@onready var b3 = $Control/MenuButton3
|
||||||
|
|
@ -36,3 +37,4 @@ func change_menu(id):
|
||||||
b3.change_name()
|
b3.change_name()
|
||||||
b4.change_name()
|
b4.change_name()
|
||||||
b5.change_name()
|
b5.change_name()
|
||||||
|
menu_id = id
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ extends Button
|
||||||
|
|
||||||
@export var id = "Null"
|
@export var id = "Null"
|
||||||
|
|
||||||
@onready var сontroller = $"../.."
|
@onready var controller = $"../.."
|
||||||
|
|
||||||
func _ready():
|
func _ready():
|
||||||
button_up.connect(_on_button_up)
|
button_up.connect(_on_button_up)
|
||||||
|
|
@ -15,9 +15,16 @@ func _on_button_up():
|
||||||
"CreateProfile":
|
"CreateProfile":
|
||||||
Game.profile_create("1")
|
Game.profile_create("1")
|
||||||
"LoadProfile":
|
"LoadProfile":
|
||||||
Game.profile_load("1")
|
Game.profile_legit_check(Game.profile_load("1"))
|
||||||
|
"DeleteProfile":
|
||||||
|
Game.profile_delete("1")
|
||||||
"ExitGame":
|
"ExitGame":
|
||||||
get_tree().quit()
|
get_tree().quit()
|
||||||
|
"Profiles":
|
||||||
|
controller.change_menu(1)
|
||||||
|
"Back":
|
||||||
|
var new_menu_id = 0
|
||||||
|
controller.change_menu(new_menu_id)
|
||||||
|
|
||||||
func change_name():
|
func change_name():
|
||||||
visible = true
|
visible = true
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue