2023-08-03 17:27:03 +09:30
|
|
|
extends Control
|
|
|
|
|
|
|
|
var dir_user := Directory.new()
|
|
|
|
const P_TESTDATA := 'user://test_data/'
|
2023-08-03 22:09:30 +09:30
|
|
|
const FILENAME_TEST_SAVE := 'res://test.srm'
|
2023-08-03 21:02:41 +09:30
|
|
|
# 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)
|
2023-08-03 17:27:03 +09:30
|
|
|
|
2023-08-03 22:09:30 +09:30
|
|
|
func load_snes_savefile(filename: String = FILENAME_TEST_SAVE):
|
2023-08-03 17:27:03 +09:30
|
|
|
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:
|
2023-08-03 21:02:41 +09:30
|
|
|
self.save_slot_buffers.append(SaveLoader.get_save_slot(save_file, i))
|
2023-08-03 22:09:30 +09:30
|
|
|
self.save_slot_dicts.append(SaveLoader.deserialize_save_slot(self.save_slot_buffers[i]))
|
2023-08-03 17:27:03 +09:30
|
|
|
print('Loaded test save file')
|
|
|
|
|
|
|
|
func generate_known_good_results():
|
2023-08-03 21:02:41 +09:30
|
|
|
load_snes_savefile()
|
|
|
|
if not self.save_slot_dicts:
|
2023-08-03 17:27:03 +09:30
|
|
|
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'
|
2023-08-03 21:02:41 +09:30
|
|
|
match Common.save_json(filename, self.save_slot_dicts):
|
2023-08-03 17:27:03 +09:30
|
|
|
OK:
|
|
|
|
pass
|
|
|
|
var error:
|
|
|
|
print_debug('Failed to save "%s" with error code %d' % [filename, error])
|
|
|
|
return
|
|
|
|
|
|
|
|
func test_save_loading() -> bool:
|
2023-08-03 21:02:41 +09:30
|
|
|
load_snes_savefile()
|
|
|
|
if not self.save_slot_dicts:
|
2023-08-03 17:27:03 +09:30
|
|
|
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')
|
2023-08-03 21:02:41 +09:30
|
|
|
return Common.are_arrays_equal(self.save_slot_dicts, known_good)
|
2023-08-03 17:27:03 +09:30
|
|
|
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
|
|
|
|
|
2023-08-03 22:09:30 +09:30
|
|
|
func test_save_slot_serialization() -> bool:
|
2023-08-03 21:02:41 +09:30
|
|
|
if not self.save_slot_dicts:
|
|
|
|
print_debug('test savefile not loaded')
|
|
|
|
return false
|
|
|
|
for i in 4:
|
2023-08-03 22:09:30 +09:30
|
|
|
var bytes = SaveLoader.serialize_save_slot(self.save_slot_dicts[i]).data_array
|
2023-08-03 21:02:41 +09:30
|
|
|
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:
|
2023-08-03 22:09:30 +09:30
|
|
|
print_debug('Mismatch occurs at byte $%04X (%d): $%02X (%d) vs $%02X (%d)' % [j, j, b1, b1, b2, b2])
|
2023-08-03 21:02:41 +09:30
|
|
|
return false
|
|
|
|
return true
|
|
|
|
|
2023-08-03 22:09:30 +09:30
|
|
|
func test_snes_save_serialization() -> bool:
|
|
|
|
if not self.save_slot_dicts:
|
|
|
|
print_debug('test savefile not loaded')
|
|
|
|
return false
|
|
|
|
var bytes := SaveLoader.make_snes_save_file(self.save_slot_dicts)
|
|
|
|
var file := File.new()
|
|
|
|
match file.open(FILENAME_TEST_SAVE, File.READ):
|
|
|
|
OK:
|
|
|
|
var orig_bytes := file.get_buffer(file.get_len())
|
|
|
|
if orig_bytes != bytes:
|
|
|
|
print_debug('SNES Save File failed to serialize correctly, rescanning')
|
|
|
|
for j in 0x2000:
|
|
|
|
var b1: int = bytes[j]
|
|
|
|
var b2: int = orig_bytes[j]
|
|
|
|
if b1 != b2:
|
|
|
|
print_debug('Mismatch occurs at byte $%04X (%d): $%02X (%d) vs $%02X (%d)' % [j, j, b1, b1, b2, b2])
|
|
|
|
return false
|
|
|
|
var error:
|
|
|
|
print_debug('Failed to open test.srm for reading: %d' % error)
|
|
|
|
return false
|
|
|
|
return true
|
2023-08-03 21:02:41 +09:30
|
|
|
|
2023-08-03 17:27:03 +09:30
|
|
|
|
|
|
|
# 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')
|
2023-08-03 17:30:41 +09:30
|
|
|
# generate_known_good_results() # Uncomment this to get your sample on first run
|
2023-08-03 21:02:41 +09:30
|
|
|
test('SNES save file loaded to array of dictionaries', test_save_loading())
|
2023-08-03 22:09:30 +09:30
|
|
|
test('SNES save file slots serialized from dictionaries', test_save_slot_serialization())
|
|
|
|
test('SNES save file serialized to original source bytes', test_snes_save_serialization())
|
2023-08-03 17:27:03 +09:30
|
|
|
get_tree().quit()
|