From 6ec484b679a1a6be305a241b44d611e732d41906 Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Fri, 4 Aug 2023 14:18:17 +0930 Subject: [PATCH] move struct tsv parsing to struct.gd --- scripts/loaders/save_loader.gd | 23 +---------------------- scripts/struct.gd | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/scripts/loaders/save_loader.gd b/scripts/loaders/save_loader.gd index 547c357..4fab5e1 100644 --- a/scripts/loaders/save_loader.gd +++ b/scripts/loaders/save_loader.gd @@ -126,25 +126,4 @@ func delete_save_slot(sram: File, slot_id: int): sram.store_16(0) func _ready(): - var file := File.new() - var error = file.open('res://data/SNES_save.tsv', File.READ) - if error == OK: - var current_struct: STRUCT.Struct - var line_num := 0 # Currently only used for step-through debugging - while file.get_position() < file.get_len(): - var line := file.get_csv_line('\t') - line_num += 1 - var size = line.size() - if size < 2: - continue - # Size is at least 2 - var type := line[0] - var label := line[1] - if type == 'struct': - # New struct declaration - current_struct = STRUCT.Struct.new() - struct_types[label] = current_struct - elif type and label: - # TODO: Maybe store the trailing comments somewhere? - current_struct.members.append([label, STRUCT.get_structarraytype(type, struct_types)]) - file.close() + STRUCT.parse_struct_definitions_from_tsv_filename('res://data/SNES_save.tsv', struct_types) diff --git a/scripts/struct.gd b/scripts/struct.gd index e1a67d9..f75da2e 100644 --- a/scripts/struct.gd +++ b/scripts/struct.gd @@ -187,3 +187,32 @@ static func get_structarraytype(type: String, existing_structs: Dictionary): print_debug('Invalid keyword used in type designator: "%s"' % k) return return inner_type + +static func parse_struct_definitions_from_tsv_file(tsv_file: File, existing_structs: Dictionary) -> void: + var current_struct: Struct + var line_num := 0 # Currently only used for step-through debugging + while tsv_file.get_position() < tsv_file.get_len(): + var line := tsv_file.get_csv_line('\t') + line_num += 1 + var size = line.size() + if size < 2: + continue + # Size is at least 2 + var type := line[0] + var label := line[1] + if type == 'struct': + # New struct declaration + current_struct = Struct.new() + existing_structs[label] = current_struct + elif type and label: + # TODO: Maybe store the trailing comments somewhere? + current_struct.members.append([label, get_structarraytype(type, existing_structs)]) + +static func parse_struct_definitions_from_tsv_filename(filename: String, existing_structs: Dictionary) -> int: + var file := File.new() + match file.open(filename, File.READ): + OK: + parse_struct_definitions_from_tsv_file(file, existing_structs) + return OK + var error: + return error