94 lines
2.8 KiB
GDScript
94 lines
2.8 KiB
GDScript
extends Control
|
|
|
|
var dir_user := Directory.new()
|
|
const P_TESTDATA := 'user://test_data/'
|
|
# Shared state between tests
|
|
var save_slot_buffers = []
|
|
var save_slot_dicts = []
|
|
|
|
func test(label, input):
|
|
if input:
|
|
print('SUCCESS: ' + label)
|
|
else:
|
|
print('FAILURE: ' + label)
|
|
|
|
func load_snes_savefile(filename: String = 'res://test.srm'):
|
|
var save_file := File.new()
|
|
match save_file.open(filename, File.READ):
|
|
OK:
|
|
pass
|
|
var error:
|
|
print_debug('Failed to open test.srm for reading: %d' % error)
|
|
return
|
|
for i in 4:
|
|
self.save_slot_buffers.append(SaveLoader.get_save_slot(save_file, i))
|
|
self.save_slot_dicts.append(SaveLoader.deserialize_save_slot(self.save_slot_buffers[i].data_array))
|
|
print('Loaded test save file')
|
|
|
|
func generate_known_good_results():
|
|
load_snes_savefile()
|
|
if not self.save_slot_dicts:
|
|
return
|
|
match dir_user.make_dir_recursive(P_TESTDATA):
|
|
OK:
|
|
pass
|
|
var error:
|
|
print_debug('Failed to create "%s" with error code %d' % [P_TESTDATA, error])
|
|
return
|
|
var filename := P_TESTDATA + 'test.srm.json'
|
|
match Common.save_json(filename, self.save_slot_dicts):
|
|
OK:
|
|
pass
|
|
var error:
|
|
print_debug('Failed to save "%s" with error code %d' % [filename, error])
|
|
return
|
|
|
|
func test_save_loading() -> bool:
|
|
load_snes_savefile()
|
|
if not self.save_slot_dicts:
|
|
print_debug('Failed to load test savefile')
|
|
return false
|
|
var filename := P_TESTDATA + 'test.srm.json'
|
|
var known_good = Common.load_json(filename)
|
|
match typeof(known_good):
|
|
TYPE_ARRAY:
|
|
print_debug('Comparing known savefile results')
|
|
return Common.are_arrays_equal(self.save_slot_dicts, known_good)
|
|
TYPE_DICTIONARY:
|
|
print_debug('Known savefile results "%s" is a dict instead of an array. Did we change formats?' % filename)
|
|
return false
|
|
_:
|
|
print_debug('Failed to load known savefile results "%s"' % filename)
|
|
return false
|
|
|
|
func test_save_serialization() -> bool:
|
|
if not self.save_slot_dicts:
|
|
print_debug('test savefile not loaded')
|
|
return false
|
|
for i in 4:
|
|
var bytes = SaveLoader.serialize_save_slot(self.save_slot_dicts[i])
|
|
if bytes != self.save_slot_buffers[i].data_array:
|
|
print_debug('Slot %d failed to serialize correctly, rescanning' % i)
|
|
for j in 0x700:
|
|
var b1: int = bytes[j]
|
|
var b2: int = self.save_slot_buffers[i].data_array[j]
|
|
if b1 != b2:
|
|
print_debug('Mismatch occurs at byte %d: %d vs %d' % [j, b1, b2])
|
|
return false
|
|
return true
|
|
|
|
|
|
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready() -> void:
|
|
match dir_user.open('user://'):
|
|
OK:
|
|
pass
|
|
var error:
|
|
print_debug('Failed to open user directory')
|
|
# generate_known_good_results() # Uncomment this to get your sample on first run
|
|
test('SNES save file loaded to array of dictionaries', test_save_loading())
|
|
test('SNES save file slots serialized from dictionaries', test_save_serialization())
|
|
get_tree().quit()
|