Last active
March 11, 2017 02:58
-
-
Save Yepoleb/ce56be1f52322ae69887c2ed0736983f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import struct | |
import io | |
class ByteReader(object): | |
def __init__(self, stream): | |
self.stream = stream | |
def read(self, *args): | |
return self.stream.read(*args) | |
def unpack(self, fmt): | |
fmt = "=" + fmt | |
fmt_size = struct.calcsize(fmt) | |
return struct.unpack(fmt, self.stream.read(fmt_size)) | |
def unpack_one(self, fmt): | |
values = self.unpack(fmt) | |
assert len(values) == 1 | |
return values[0] | |
def read_char(self): | |
return self.unpack_one("c") | |
def read_int8(self): | |
return self.unpack_one("b") | |
def read_uint8(self): | |
return self.unpack_one("B") | |
def read_int16(self): | |
return self.unpack_one("h") | |
def read_uint16(self): | |
return self.unpack_one("H") | |
def read_int32(self): | |
return self.unpack_one("l") | |
def read_uint32(self): | |
return self.unpack_one("L") | |
def read_int64(self): | |
return self.unpack_one("q") | |
def read_uint64(self): | |
return self.unpack_one("Q") | |
def read_float(self): | |
return self.unpack_one("f") | |
def read_double(self): | |
return self.unpack_one("d") | |
def read_cstring(self): | |
string = b"" | |
while True: | |
c = self.read(1) | |
if c == b"\x00": | |
break | |
else: | |
string += c | |
return string | |
def read_vec3(self): | |
return self.unpack("4f") | |
def read_vec4(self): | |
return self.unpack("4f") | |
def read_vec3x3(self): | |
return (self.read_vec3() for i in range(3)) | |
def read_vec4x4(self): | |
return (self.read_vec4() for i in range(4)) | |
def read_bool(self): | |
return (self.unpack("?")) | |
def align(self, num): | |
current_pos = self.stream.tell() | |
align_bytes = num - (current_pos % num) | |
if align_bytes != num: | |
self.stream.seek(align_bytes, io.SEEK_CUR) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from bytereader import ByteReader | |
from pprint import pprint | |
import xml.etree.ElementTree | |
import xmltodict # external module | |
TEXTURE_TYPE = "ir_at_bbsimple3" | |
with open("shaders/ir_at_bbsimple3_bs.fxinfo_pc", "rb") as fxinfo_file: | |
fxinfo = xmltodict.parse(fxinfo_file)["root"] | |
#pprint(fxinfo) | |
binary = open("SMG_Pistol_A_high.matlib_pc", "rb") | |
reader = ByteReader(binary) | |
matlib = {} | |
matlib["signature"] = reader.read(4) | |
matlib["strings_size"] = reader.read_uint64() | |
matlib["strings_count"] = reader.read_uint64() | |
reader.align(16) | |
matlib["strings1"] = [] | |
for i in range(matlib["strings_count"]): | |
string = reader.read_cstring() | |
matlib["strings1"].append(string) | |
reader.align(16) | |
matlib["unknown1"] = reader.read(56) | |
matlib["materials_count"] = reader.read_uint64() | |
matlib["unknown2"] = reader.read(32) | |
typehandlers = { | |
"float": (4, lambda r: r.read_float()), | |
"float3": (12, lambda r: r.read_vec3()), | |
"float4": (16, lambda r: r.read_vec4()) | |
} | |
matlib["materials"] = [] | |
for i in range(matlib["materials_count"]): | |
material = {} | |
material["size"] = reader.read_uint64() | |
material["unknown1"] = reader.read(8) # cf3f80b4 | |
material["flags"] = reader.read_uint32() # 0x42 66 | |
material["unknown2"] = reader.read(4) # 0300090b | |
material["unknown3"] = reader.read(0x24) # 00 | |
material["unknown4"] = reader.read(4) # 918fb469 | |
material["unknown5"] = reader.read(4) # 00 | |
if TEXTURE_TYPE == "ir_at_bbsimple1": | |
material["unknown6"] = reader.read_uint32() # 0x14 | |
material["unknown7"] = reader.read(4) # 90eb0828 | |
material["unknown8"] = reader.read_uint32() # 1 string index? | |
material["unknown9"] = reader.read_uint32() # 0x28 40 | |
material["unknown10"] = reader.read(4) # cac948e8 | |
material["unknown11"] = reader.read_uint32() # 2 string index? | |
material["unknown12"] = reader.read(0x1c) # ef10cc08 | |
material["unknown13"] = reader.read(12) | |
for param in fxinfo["params"]["param"]: | |
print(param) | |
p_type = param["type"] | |
size, readfunc = typehandlers[p_type] | |
material[param["name"]] = readfunc(reader) | |
reader.align(16) | |
material["unknown32"] = reader.read(32) | |
matlib["materials"].append(material) | |
matlib["strings2"] = [] | |
for i in range(matlib["strings_count"]): | |
string = reader.read_cstring() | |
reader.align(2) | |
matlib["strings2"].append(string) | |
pprint(matlib) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment