Skip to content

Commit

Permalink
+ scp extractor with calling unlua.jar
Browse files Browse the repository at this point in the history
  • Loading branch information
redsuns-chan committed Jul 22, 2022
1 parent 7f07209 commit d673ed0
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 112 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ __pycache__/
*.py[cod]
*$py.class

# Visual Studio Code Config
.vscode

# C extensions
*.so

Expand Down
113 changes: 57 additions & 56 deletions sdgo-mod-extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,67 @@
import codecs
import json

sdgo_data_path = "D:/Project/SDGO/data/"

def extract_header(f):
read_zoa = 0
header_pointer = 0
max_header_length = 24
while read_zoa == 0 or header_pointer >= max_header_length:
try:
byte = f.read(4)
byte = byte.hex()
if (byte == "5a4f4100"): # 'ZOA'
read_zoa = 1
header_pointer += 4
except UnicodeDecodeError:
print("Error while reading file bytes")
section1 = f.read(1)
texture_count = int(f.read(1).hex(), 16)
f.read(2)
f.read(1)
object_count = int(f.read(1).hex(), 16)
f.read(2)
section3 = f.read(4)
section4 = f.read(4)
return {
"texture_count": texture_count,
"bone_count": object_count
}
read_zoa = 0
header_pointer = 0
max_header_length = 24
while read_zoa == 0 or header_pointer >= max_header_length:
try:
byte = f.read(4)
byte = byte.hex()
if (byte == "5a4f4100"): # 'ZOA'
read_zoa = 1
header_pointer += 4
except UnicodeDecodeError:
print("Error while reading file bytes")
section1 = f.read(1)
texture_count = int(f.read(1).hex(), 16)
f.read(2)
f.read(1)
object_count = int(f.read(1).hex(), 16)
f.read(2)
section3 = f.read(4)
section4 = f.read(4)
return {
"texture_count": texture_count,
"bone_count": object_count
}

def extract_bone_names(f, bone_count):
section_length = 256 // 2
byte = ""
bones = []
for i in range(bone_count):
bone_str = ""
for x in range(section_length):
byte_hex = f.read(2).hex()
try:
if byte_hex == "0000":
bone_str += " "
else:
letter = byte_hex[2:4]
letter_bytes = bytes(letter, encoding="utf-8")
bone_str += str(codecs.decode(letter_bytes, "hex"), "utf-8")
except UnicodeDecodeError:
bone_str += ""
bones.append(bone_str.strip())
bone_str = ""
return bones
section_length = 256 // 2
byte = ""
bones = []
for i in range(bone_count):
bone_str = ""
for x in range(section_length):
byte_hex = f.read(2).hex()
try:
if byte_hex == "0000":
bone_str += " "
else:
letter = byte_hex[2:4]
letter_bytes = bytes(letter, encoding="utf-8")
bone_str += str(codecs.decode(letter_bytes, "hex"), "utf-8")
except UnicodeDecodeError:
bone_str += ""
bones.append(bone_str.strip())
bone_str = ""
return bones

def main(index):
try:
f = open('./mdrs/' + index + '.mod', 'rb')
try:
file_info = extract_header(f)
file_info["bone_names"] = extract_bone_names(f, file_info["bone_count"])

f.close()
#print(json.dumps(file_info))
except FileExistsError:
print("unable to close file")
except FileNotFoundError:
print('model file not exists')
try:
f = open(sdgo_data_path + 'mdrs/' + index + '.mod', 'rb')
try:
file_info = extract_header(f)
file_info["bone_names"] = extract_bone_names(f, file_info["bone_count"])
f.close()
print(json.dumps(file_info))
except FileExistsError:
print("unable to close file")
except FileNotFoundError:
print('model file not exists')

main(sys.argv[1])
24 changes: 24 additions & 0 deletions sdgo-script-extractor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

import os
from pathlib import Path
import shutil

sdgo_data_path = "D:/Project/SDGO/data/"
target_path = sdgo_data_path + "scp/decoded/"

command = "java -jar ./unlua.jar \"" + sdgo_data_path + "scp/{{filename}}.scp" + "\" > \"" + target_path + "{{filename}}.scp\""

def main():
print("sdgo script extractor")
Path(target_path).mkdir(parents=True, exist_ok=True)
file_list = os.listdir(sdgo_data_path + "scp")
for file_name in file_list:
if file_name.endswith(".scp"):
print("decompiling script " + file_name)
file_name = file_name.replace(".scp", "")
os.system("cmd /c " + command.replace("{{filename}}", file_name))
if os.stat(target_path + file_name + ".scp").st_size == 0:
os.remove(target_path + file_name + ".scp")
shutil.copyfile(sdgo_data_path + "scp/" + file_name + ".scp", target_path + file_name + ".lua")

main()
112 changes: 56 additions & 56 deletions sdgo-texture-extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,65 @@
import os
import pathlib

sdgo_data_path = "/Users/redsunschan/Documents/sdgo-data/txrs/"
target_path = "/Users/redsunschan/Documents/sdgo-data/txrs/decoded/"
sdgo_data_path = "D:/Project/SDGO/data/"
target_path = sdgo_data_path + "txrs/decoded/"

def extract_texture(file_name):
f = open(sdgo_data_path + file_name, "rb")
file_size = os.path.getsize(sdgo_data_path + file_name)
found_type = ""
fhex = f.read(file_size).hex().upper()
# remove zoa header
if fhex.startswith("5A4F41544558310000000000"):
fhex = fhex.replace("5A4F41544558310000000000", "")
# detect type
if fhex.startswith("424D"):
found_type = "bmp"
elif fhex.endswith("89504E47"):
found_type = "png"
elif fhex.endswith("54525545564953494F4E2D5846494C452E00"):
found_type = "tga"
else: # if cannot find any recognized header, assume the file is dds, may need to improve this logic
found_type = "dds"
new_file = open(target_path + file_name.replace(".txr", "." + found_type), "wb")
new_file.write(bytes.fromhex(fhex))
f.close()
new_file.close()
return found_type
f = open(sdgo_data_path + file_name, "rb")
file_size = os.path.getsize(sdgo_data_path + file_name)
found_type = ""
fhex = f.read(file_size).hex().upper()
# remove zoa header
if fhex.startswith("5A4F41544558310000000000"):
fhex = fhex.replace("5A4F41544558310000000000", "")
# detect type
if fhex.startswith("424D"):
found_type = "bmp"
elif fhex.endswith("89504E47"):
found_type = "png"
elif fhex.endswith("54525545564953494F4E2D5846494C452E00"):
found_type = "tga"
else: # if cannot find any recognized header, assume the file is dds, may need to improve this logic
found_type = "dds"
new_file = open(target_path + file_name.replace(".txr", "." + found_type), "wb")
new_file.write(bytes.fromhex(fhex))
f.close()
new_file.close()
return found_type

def main():
print("sdgo texture extractor")
pathlib.Path(target_path).mkdir(parents=True, exist_ok=True)
file_list = os.listdir(sdgo_data_path)
txr_count = 0
tga_count = 0
png_count = 0
bmp_count = 0
dds_count = 0
unknown_count = 0
file_list.sort()
for file_name in file_list:
if (file_name.endswith(".txr")):
txr_count += 1
print("processing " + file_name)
file_type = extract_texture(file_name)
if file_type == "tga":
tga_count += 1
elif file_type == "png":
png_count += 1
elif file_type == "bmp":
bmp_count += 1
elif file_type == "dds":
dds_count += 1
else:
unknown_count += 1
print("converted textures: " + str(txr_count))
print("converted tga: " + str(tga_count))
print("converted png: " + str(png_count))
print("converted bmp: " + str(bmp_count))
print("converted dds: " + str(dds_count))
print("unknown files: " + str(unknown_count))
print("sdgo texture extractor")
pathlib.Path(target_path).mkdir(parents=True, exist_ok=True)
file_list = os.listdir(sdgo_data_path)
txr_count = 0
tga_count = 0
png_count = 0
bmp_count = 0
dds_count = 0
unknown_count = 0
file_list.sort()
for file_name in file_list:
if (file_name.endswith(".txr")):
txr_count += 1
print("processing " + file_name)
file_type = extract_texture(file_name)
if file_type == "tga":
tga_count += 1
elif file_type == "png":
png_count += 1
elif file_type == "bmp":
bmp_count += 1
elif file_type == "dds":
dds_count += 1
else:
unknown_count += 1
print("converted textures: " + str(txr_count))
print("converted tga: " + str(tga_count))
print("converted png: " + str(png_count))
print("converted bmp: " + str(bmp_count))
print("converted dds: " + str(dds_count))
print("unknown files: " + str(unknown_count))

main()
Binary file added unlua.jar
Binary file not shown.

0 comments on commit d673ed0

Please sign in to comment.