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

@ -171,7 +171,7 @@ offset_left = 312.0
offset_top = 378.0
offset_right = 983.0
offset_bottom = 492.0
text = "Quit to main menu"
text = "Save and quit to main menu"
[node name="Label" type="Label" parent="GUI/Interface/PauseController"]
layout_mode = 0

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

View file

@ -49,19 +49,14 @@ func bought_action():
var primary_slot = ship.get_node("PrimaryWeapon")
var secondary_slot = ship.get_node("SecondaryWeapon")
var bought_weapon = get_tree().current_scene.bought_weapon
var weapon_dict = get_tree().current_scene.weapon_dict
var slot = primary_slot if data["slot"] == "primary" else secondary_slot
if !bought_weapon[data["weapon"]]:
bought_weapon[data["weapon"]] = true
else:
ship.money += price
if slot.get_child_count() == 0:
var weapon_inst = load(weapon_dict[data["weapon"]]).instantiate()
slot.add_child(weapon_inst)
slot.position = data["position"]
else:
for node in slot.get_children():
node.queue_free()
var weapon_inst = load(weapon_dict[data["weapon"]]).instantiate()
var weapon_inst = Game.get_weapon(data['weapon']).instantiate()
weapon_inst.id = data['weapon']
weapon_inst.position = data["position"]
slot.add_child(weapon_inst)
slot.position = data["position"]

View file

@ -8,4 +8,5 @@ func _on_unpause_button_button_up():
func _on_exit_button_button_up():
get_tree().current_scene.unpause()
Game.profile_save(get_tree().current_scene)
get_tree().change_scene_to_file("res://scenes/MainMenu.tscn")

View file

@ -1,6 +1,6 @@
extends Label
func _ready():
text = "GammaCosmicRays Version {version}"
text = "GammaCosmicRays version {version}".format({"version" : Game.gameversion})
if OS.has_feature("editor"):
text += " uncompiled\nThis is a debug/prerelease version."

View file

@ -8,6 +8,8 @@ class_name MainShip
@onready var pause_controller = $GUI/Interface/PauseController
@onready var minimap = $CanvasLayer/Control/Minimap
@onready var camera = $Camera
@onready var primary_slot = $PrimaryWeapon
@onready var secondary_slot = $SecondaryWeapon
var allow_shooting = true
var faction = "player"

View file

@ -9,7 +9,7 @@ class_name Weapon
@export var shoot_timer : Timer
@export var shoot_action : String = ""
@export var spawner_points : Array[Node2D]
var id : String = "SingleLaserMk1"
@onready var ship = $"../.."
@onready var slot = $".."