LDD/reader/lif_reader.py

95 lines
2.7 KiB
Python

import os
import struct
LDD_INSTALL_PATH = r'C:\Program Files (x86)\LEGO Company\LEGO Digital Designer'
LDD_ASSET_PATH = os.path.join(LDD_INSTALL_PATH, 'Assets.lif')
def read():
def uint32(fd, offset):
fd.seek(offset)
buff = fd.read(4)
if bytesAre == 'str':
return (ord(buff[0]) * 16777216) + \
(ord(buff[1]) * 65536) + \
(ord(buff[2]) * 256) + \
ord(buff[3])
else:
return (buff[0] * 16777216) + \
(buff[1] * 65536) + \
(buff[2] * 256) + \
buff[3]
def uint16(fd, offset):
fd.seek(offset)
buff = fd.read(2)
if bytesAre == "str":
return (ord(buff[0]) * 256) + \
ord(buff[1])
else:
return (fd.read(1) * 256) + \
fd.read(1)
def recurse(fd, prefix, offset):
if prefix == "":
offset += 36
else:
folder_list.extend([prefix])
offset += 4
for i in range(uint32(fd, offset)):
offset += 4
entryType = uint16(fd, offset) # 1 = directory, 2 = file
print 'xxx', entryType, offset
offset += 6
# Build the name.offset
entryName = os.sep
while True:
fd.seek(offset)
read_chr = ''.join(struct.unpack('<s', fd.read(1)))
print '---', offset, read_chr
if not (read_chr != 0 and read_chr != b'\x00'):
break
if bytesAre == 'str':
entryName += read_chr
else:
entryName += chr(read_chr)
offset += 2
offset += 6
if entryType == 1:
packed_files_offset[0] += 20
offset = recurse(fd, prefix + entryName, offset)
elif entryType == 2:
packed_files_offset[0] += 20
fileOffset = packed_files_offset[0]
# File size.
fileSize = uint32(fd, offset) - 20
offset += 24
packed_files_offset[0] += fileSize
file_list.extend([[prefix + entryName, fileOffset, fileSize]])
return offset
bytesAre = type(b'a'[0]).__name__
packed_files_offset = [84] # Array for non-global function-modifiable variable.
folder_list = []
file_list = []
with open(LDD_ASSET_PATH, "rb") as fd:
file_format = ''.join(struct.unpack('<ssss', fd.read(4)))
if file_format != 'LIFF':
return
print file_format
recurse(fd, "", (uint32(fd, 72) + 64))
for i in file_list:
print i
# read()