100 lines
3.2 KiB
GDScript3
100 lines
3.2 KiB
GDScript3
|
class_name PakAssetLoader
|
||
|
|
||
|
static var key = "a8Yb5$IpVobR,1Xph!k(#!B9h$V[o[r-I'x.37Q%E;lt2wGgk)hNF_p_!mF?Ds54.*8ErbCK,30".to_utf8_buffer()
|
||
|
|
||
|
#loads a file from a .pak file.
|
||
|
static func load_file(path: String):
|
||
|
var split = path.split(".pak/")
|
||
|
var pak = split[0]
|
||
|
var filename = split[1]
|
||
|
|
||
|
var zip_reader = ZIPReader.new()
|
||
|
var err = zip_reader.open(pak + ".pak")
|
||
|
if err != OK:
|
||
|
report_errors(err, pak + ".pak")
|
||
|
zip_reader.close()
|
||
|
var bytes = zip_reader.read_file(filename)
|
||
|
zip_reader.close()
|
||
|
if bytes.size() == 0:
|
||
|
report_errors(ERR_FILE_NOT_FOUND, filename)
|
||
|
|
||
|
if filename.ends_with(".dat"):
|
||
|
var magic = bytes[0]+bytes[1]+bytes[2]+bytes[3]
|
||
|
if magic == 0x25 + 0x7C + 0x0A + 0x42: #XOR'ed DDS file
|
||
|
unXOR(bytes)
|
||
|
magic = bytes[0]+bytes[1]+bytes[2]+bytes[3]
|
||
|
|
||
|
if magic == 0x44 + 0x44 + 0x53 + 0x20: #DDS file
|
||
|
var header = bytes.slice(0, 0x80)
|
||
|
bytes = bytes.slice(0x80)
|
||
|
var height = header[0xc] + (header[0xd] * 256)
|
||
|
var width = header[0x10] + (header[0x11] * 256)
|
||
|
var format = header[0x54]+header[0x55]+header[0x56]+header[0x57]
|
||
|
if format == 0:
|
||
|
fix_BGRA(bytes)
|
||
|
var img = Image.create_from_data(width, height, false, Image.FORMAT_RGBA8, bytes)
|
||
|
return ImageTexture.create_from_image(img)
|
||
|
elif format == 0x44 + 0x58 + 0x54 + 0x35:
|
||
|
var img = Image.create_from_data(width, height, false, Image.FORMAT_DXT5, bytes)
|
||
|
return ImageTexture.create_from_image(img)
|
||
|
else:
|
||
|
report_errors(ERR_FILE_UNRECOGNIZED, filename)
|
||
|
return
|
||
|
else:
|
||
|
report_errors(ERR_FILE_UNRECOGNIZED, filename)
|
||
|
return
|
||
|
elif filename.ends_with(".ogg"):
|
||
|
var magic = bytes[0]+bytes[1]+bytes[2]+bytes[3]
|
||
|
|
||
|
if magic == 0x4F + 0x67 + 0x67 + 0x53:
|
||
|
var stream = AudioStreamOggVorbis.load_from_buffer(bytes)
|
||
|
stream.loop = true
|
||
|
stream.loop_offset = get_loop_off(filename)
|
||
|
return stream
|
||
|
else:
|
||
|
report_errors(ERR_FILE_UNRECOGNIZED, filename)
|
||
|
elif filename.ends_with(".fld"):
|
||
|
return bytes
|
||
|
|
||
|
|
||
|
static func fix_BGRA(bytes: PackedByteArray):
|
||
|
for i in range(0, bytes.size(), 4):
|
||
|
var tmp = bytes[i]
|
||
|
bytes[i] = bytes[i+2]
|
||
|
bytes[i+2] = tmp
|
||
|
|
||
|
static func unXOR(bytes: PackedByteArray):
|
||
|
for i in range(bytes.size()):
|
||
|
bytes[i] ^= key[i % key.size()]
|
||
|
|
||
|
|
||
|
static func get_loop_off(filename: String):
|
||
|
match filename:
|
||
|
"bgm/0.ogg":
|
||
|
return 1.72
|
||
|
"bgm/1.ogg":
|
||
|
return 2
|
||
|
|
||
|
|
||
|
static func report_errors(err: int, filepath):
|
||
|
# See: https://docs.godotengine.org/en/latest/classes/class_@globalscope.html#enum-globalscope-error
|
||
|
var result_hash = {
|
||
|
ERR_FILE_NOT_FOUND: "File: not found",
|
||
|
ERR_FILE_BAD_DRIVE: "File: Bad drive error",
|
||
|
ERR_FILE_BAD_PATH: "File: Bad path error.",
|
||
|
ERR_FILE_NO_PERMISSION: "File: No permission error.",
|
||
|
ERR_FILE_ALREADY_IN_USE: "File: Already in use error.",
|
||
|
ERR_FILE_CANT_OPEN: "File: Can't open error.",
|
||
|
ERR_FILE_CANT_WRITE: "File: Can't write error.",
|
||
|
ERR_FILE_CANT_READ: "File: Can't read error.",
|
||
|
ERR_FILE_UNRECOGNIZED: "File: Unrecognized error.",
|
||
|
ERR_FILE_CORRUPT: "File: Corrupt error.",
|
||
|
ERR_FILE_MISSING_DEPENDENCIES: "File: Missing dependencies error.",
|
||
|
ERR_FILE_EOF: "File: End of file (EOF) error."
|
||
|
}
|
||
|
if err in result_hash:
|
||
|
print("Error: ", result_hash[err], " ", filepath)
|
||
|
else:
|
||
|
print("Unknown error with file ", filepath, " error code: ", err)
|
||
|
|