Added hash salt, added game data saving

This commit is contained in:
gotfishmakesticks 2023-11-11 17:50:14 +03:00
commit 11ea0f0d51
7 changed files with 43 additions and 51 deletions

View file

@ -8,50 +8,42 @@ enum AMMO_TYPE {NULL, LASER_ENERGY, ROCKETS}
const DEFAULT_ITEM = preload("res://items/test_item.tres")
const DEFAULT_WEAPON = preload("res://scenes/weapons/presets/SingleLaserMk1.tscn")
const salt = "2ndbeam"
const gameversion = "Ictar 1.1"
static var profile : Profile = Profile.new()
## Creates a new profile if it doesn't exist
static func profile_create(profile_name : String) -> void:
var path = "user://"+profile_name+".cosmic"
if FileAccess.file_exists(path):
return
var profile_meta = {
'hash' : {},
'meta' : {
'created_in_version' : gameversion,
'creation_date' : Time.get_datetime_string_from_system(),
'last_version' : gameversion,
'last_updated' : Time.get_datetime_string_from_system(),
'profile_name' : profile_name,
'legit' : true
}
}
var profile_meta_keys = profile_meta['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()
var file = FileAccess.open(path, FileAccess.WRITE)
var json_string = JSON.stringify(profile_meta, "\t")
file.store_string(json_string)
profile = Profile.new()
profile.profile_meta['meta']['profile_name'] = profile_name
profile_save_from_meta(profile.profile_meta)
## 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"
if not FileAccess.file_exists(path):
profile_create(profile_name)
return
var profile_meta = profile_load(profile_name)
profile_save_from_meta(profile_meta)
## Saves profile meta and includes game data if there is an active session.
static func profile_save(scene : Node) -> void:
if scene.name != "MainMenu":
profile.profile_meta['game'] = {
"ammo" : scene.ship.hull.ammunition,
"hp" : scene.ship.hull.hp,
"fuel" : scene.ship.hull.fuel,
"money" : scene.ship.money
}
if scene.ship.primary_slot.get_child_count() > 0:
profile.profile_meta['game']['primaryweapon'] = scene.ship.primary_slot.get_child(0).id
if scene.ship.secondary_slot.get_child_count() > 0:
profile.profile_meta['game']['secondaryweapon'] = scene.ship.secondary_slot.get_child(0).id
profile_save_from_meta(profile.profile_meta)
## 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()
for i in range(len(profile_meta_keys)):
profile_meta['hash'][i] = str(profile_meta['meta'][profile_meta_keys[i]]).sha256_buffer().hex_encode()
for key in profile_meta['meta']:
profile_meta['hash'][key] = (str(profile_meta['meta'][key]) + salt).sha256_buffer().hex_encode()
if 'game' in profile_meta:
for key in profile_meta['game']:
profile_meta['hash'][key] = (str(profile_meta['game'][key]) + salt).sha256_buffer().hex_encode()
var json_string = JSON.stringify(profile_meta, "\t")
var file = FileAccess.open(path, FileAccess.WRITE)
file.store_string(json_string)
@ -77,10 +69,13 @@ static func profile_delete(profile_name : String) -> void:
static func profile_legit_check(profile_meta : Dictionary) -> Dictionary:
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())
for key in profile_meta['meta']:
old_hash.append(profile_meta['hash'][key])
new_hash.append((str(profile_meta['meta'][key]) + salt).sha256_buffer().hex_encode())
if 'game' in profile_meta:
for key in profile_meta['game']:
old_hash.append(profile_meta['hash'][key])
new_hash.append((str(profile_meta['game'][key]) + salt).sha256_buffer().hex_encode())
old_hash.sort()
new_hash.sort()
if old_hash == new_hash and profile_meta['meta']['legit']:
@ -101,8 +96,7 @@ static func get_item(id : String) -> Item:
## 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 = "res://weapons/presets/{name}.tscn".format({"name": path_name})
var path = "res://scenes/weapons/presets/{name}.tscn".format({"name": id})
var res = load(path)
if res != null:
return res