extends Node class_name Game ## TODO: rewrite item system enum AmmoType {None, LaserEnergy, Rockets} enum BaseType {Power, Mining, Food, Trading, Module} enum Faction {None, Player, Peaceful, Neutral, Aggressive} #const DEFAULT_ITEM = preload("res://items/test_item.tres") #const DEFAULT_WEAPON = preload("res://scenes/weapons/presets/SingleLaserMk1.tscn") #const DEFAULT_HULL = preload("res://scenes/hulls/starterhull.tscn") #const DEFAULT_ENGINE = preload("res://scenes/engines/starterengine.tscn") #const DEFAULT_SHIELD = preload("res://scenes/shields/startershield.tscn") const salt = "2ndbeam" const gameversion = "Ifre 1.0" const beta = false static var profile : Profile = Profile.new() ## Creates a new profile if it doesn't exist static func profile_create(profile_name : String) -> void: profile = Profile.new() profile.profile_meta['meta']['profile_name'] = profile_name profile_save_from_meta(profile.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, "cargo" : scene.ship.hull.cargo, "hulls" : scene.ship.hulls, "engines" : scene.ship.engines, "shields" : scene.ship.shields, "weapons" : scene.ship.weapons } if !scene.ship.quest.new or scene.ship.quest_completed: profile.profile_meta['game']['quest'] = { 'type' : scene.ship.quest.type, 'progress_max' : scene.ship.quest.progress_max, 'reward_money' : scene.ship.quest.reward_money, 'restrictions' : scene.ship.quest.restrictions, 'data' : scene.ship.quest.data, 'got_reward' : scene.ship.quest_completed } profile.profile_meta['game']['ship_equipment'] = { 'hull' : scene.ship.hull.id, 'engine' : scene.ship.engine.id, 'shield' : scene.ship.shield.id, 'primary_weapon' : scene.ship.primary_slot.get_child(0).id, 'secondary_weapon' : 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() 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) ## Returns profile meta of profile if it exists static func profile_load(profile_name : String) -> Dictionary: var path = "user://"+profile_name+".cosmic" if not FileAccess.file_exists(path): return {} var file = FileAccess.open(path, FileAccess.READ) var string = file.get_as_text() var profile_meta = JSON.parse_string(string) return profile_meta ## 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. Either way it returns profile meta. static func profile_legit_check(profile_meta : Dictionary) -> Dictionary: var old_hash = [] var new_hash = [] if profile_meta == {}: return {} 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']: return profile_meta profile_meta['meta']['legit'] = false profile_save_from_meta(profile_meta) return 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 = "res://items/{name}.tres".format({"name": path_name}) if ResourceLoader.exists(path): var res = load(path) return res.duplicate() return null ## Returns weapon packed scene if it exists or DEFAULT_WEAPON instead static func get_weapon(id : String) -> PackedScene: var path = "res://scenes/weapons/presets/{name}.tscn".format({"name": id}) var res = load(path) return res static func get_module(id : String, type : String) -> PackedScene: var path = "res://scenes/{type}s/{name}.tscn".format({"name": id, "type": type}) var res = load(path) return res