summaryrefslogtreecommitdiff
path: root/extractor/grf.gd
blob: 7e7e93cb50a653eabe05aac26966918750f7794b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
## see https://ragnarokresearchlab.github.io/file-formats/grf
class_name GRF


var header: Header

class Header:
	const BYTE_LENGTH := 46
	
	## Byte Length: 15
	## Master of Magic
	var signature: String = "Master of Magic"
	
	## Byte Length: 15
	var encryption: PackedByteArray
	
	## Byte Type: u32
	## Byte Length: 4
	var file_table_offset: int
	
	## Byte Type: u32
	## Byte Length: 4
	var scrambling_seed: int
	
	## Byte Type: u32
	## Byte Length: 4
	var scrambled_file_count: int
	
	## Byte Type: u32
	## Byte Length: 4
	var version: int
	
	
	func get_file_count() -> int:
		return scrambled_file_count - scrambling_seed - 7
	
	
	static func from_bytes(bytes: PackedByteArray):
		var header = Header.new()
		
		header.signature = bytes.slice(0, 15).get_string_from_utf8()
		header.encryption = bytes.slice(15, 15 + 15)
		header.file_table_offset = bytes.decode_u32(30)
		header.scrambling_seed = bytes.decode_u32(34)
		header.scrambled_file_count = bytes.decode_u32(38)
		header.version = bytes.decode_u32(42)
		
		return header


var file_table: FileTable

class FileTable:
	const BYTE_LENGTH := 8
	
	## Byte Type: u32
	## Byte Length: 4
	var compressed_size: int
	
	## Byte Type: u32
	## Byte Length: 4
	var decompressed_size: int
	
	var compressed_record_headers: PackedByteArray
	var decompressed_record_headers: PackedByteArray
	
	
	func generate_compressed_record_headers(grf_file: FileAccess):
		compressed_record_headers = grf_file.get_buffer(compressed_size)
	
	
	func decompress():
		decompressed_record_headers = compressed_record_headers.decompress(
			decompressed_size,
			FileAccess.CompressionMode.COMPRESSION_DEFLATE
		)
	
	
	static func from_bytes(bytes: PackedByteArray):
		var file_table = FileTable.new()
		
		file_table.compressed_size = bytes.decode_u32(0)
		file_table.decompressed_size = bytes.decode_u32(4)
		
		return file_table


var file_entries: Array[FileEntry]

class FileEntry:
	const BYTE_LENGTH := 17
	
	var file_name: String
	
	## Byte Type: u32
	## Byte Length: 4
	var compressed_size: int
	
	## Byte Type: u32
	## Byte Length: 4
	var byte_aligned_size: int
	
	## Byte Type: u32
	## Byte Length: 4
	var decompressed_size: int
	
	## Byte Type: u8
	## Byte Length: 1
	var file_type: int
	
	## Byte Type: u32
	## Byte Length: 4
	var offset: int
	
	
	func get_byte_length() -> int:
		return BYTE_LENGTH #+ file_name.to_ascii_buffer().size()
	
	
	func get_file_path() -> String:
		var parts = file_name.split("\\")
		
		return "/".join(parts)
	
	
	func get_contents(grf_file: FileAccess):
		var previous_position = grf_file.get_position()
		
		grf_file.seek(Header.BYTE_LENGTH + offset)
		var buffer = grf_file.get_buffer(byte_aligned_size)
		var contents = buffer.decompress(
			decompressed_size,
			FileAccess.CompressionMode.COMPRESSION_DEFLATE
		)
		
		grf_file.seek(previous_position)
		
		return contents
	
	
	@warning_ignore("shadowed_variable")
	static func from_bytes_with_filename(bytes: PackedByteArray, file_name: String):
		print(file_name)
		var file_entry = FileEntry.new()
		
		file_entry.file_name = file_name
		file_entry.compressed_size = bytes.decode_u32(0)
		file_entry.byte_aligned_size = bytes.decode_u32(4)
		file_entry.decompressed_size = bytes.decode_u32(8)
		file_entry.file_type = bytes.decode_u8(12)
		file_entry.offset = bytes.decode_u32(13)
		
		return file_entry


var file_access: FileAccess


static func open(path: String):
	var grf = GRF.new()
	
	grf.file_access = FileAccess.open(path, FileAccess.ModeFlags.READ)
	
	grf.header = GRF.Header.from_bytes(grf.file_access.get_buffer(Header.BYTE_LENGTH))
	
	grf.file_access.seek(Header.BYTE_LENGTH + grf.header.file_table_offset)
	grf.file_table = FileTable.from_bytes(grf.file_access.get_buffer(8))
	
	grf.file_table.generate_compressed_record_headers(grf.file_access)
	grf.file_table.decompress()
	
	grf.file_entries = [] as Array[FileEntry]
	var file_entry_offset = 0
	while file_entry_offset < grf.file_table.decompressed_record_headers.size():
		var file_name_size = grf.file_table.decompressed_record_headers.find(0, file_entry_offset) + 1 - file_entry_offset
		var file_entry = FileEntry.from_bytes_with_filename(
			grf.file_table.decompressed_record_headers.slice(
				file_entry_offset + file_name_size,
				file_entry_offset + file_name_size + FileEntry.BYTE_LENGTH
			),
			GRF.decode_string(grf.file_table.decompressed_record_headers.slice(
				file_entry_offset,
				file_entry_offset + file_name_size
			))
		)
		
		grf.file_entries.append(file_entry)
		file_entry_offset += file_entry.get_byte_length() + file_name_size
	
	return grf


func extract(destination: String = "res://data"):
	for file_entry in file_entries:
		var file_path: String = file_entry.get_file_path()
		
		var base_directory = DirAccess.open(destination)
		base_directory.make_dir_recursive("extracted/" + file_path.get_base_dir())
		
		var file = FileAccess.open("%s/extracted/%s" % [destination, file_path], FileAccess.WRITE_READ)
		file.store_buffer(file_entry.get_contents(file_access))
		
		# TODO: write pngs for sprites (and .tres files maybe if necessary)
		# TODO: (also maybe write .tres files for action data files(whatever they are))



static func decode_string(bytes: PackedByteArray):
	return bytes.get_string_from_ascii()
	# TODO: check unicode codepoints and parse accordingly
	var string = bytes.get_string_from_utf32()
	if string == "":
		string = bytes.get_string_from_utf16()
		if string == "":
			string = bytes.get_string_from_utf8()
			if string == "":
				string = bytes.get_string_from_ascii()
	
	return string