110 lines
4.1 KiB
GDScript
110 lines
4.1 KiB
GDScript
extends Node
|
|
|
|
class_name Game
|
|
|
|
enum ITEM_TYPE {VALUABLE, WEAPON, MODULE, AMMUNITION}
|
|
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 gameversion = "Ictar 1.1"
|
|
|
|
## Creates new profile or resaves it if it exists
|
|
static func profile_create(profile_name : String) -> void:
|
|
var path = "user://"+profile_name+".cosmic"
|
|
if FileAccess.file_exists(path):
|
|
profile_save(profile_name, "menu")
|
|
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)
|
|
|
|
## 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 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()
|
|
var file = FileAccess.open(path, FileAccess.WRITE)
|
|
var json_string = JSON.stringify(profile_meta, "\t")
|
|
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)
|
|
for meta in profile_meta:
|
|
print(meta, ": ", profile_meta[meta])
|
|
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.
|
|
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 = "res://items/{name}.tres".format({"name": path_name})
|
|
var res = load(path)
|
|
if res != null:
|
|
return res.duplicate()
|
|
else:
|
|
return DEFAULT_ITEM.duplicate()
|
|
|
|
## 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 res = load(path)
|
|
if res != null:
|
|
return res
|
|
else:
|
|
return DEFAULT_WEAPON
|