Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import struct
- import glob
- specfile = 'fldfilespec.csv' #Define constant value
- specpath = 'fldpathspec.csv' #Define constant value
- #Gets data
- def get_data(filename):
- totalbytes = os.path.getsize(filename)
- infile = open(filename, 'rb')
- totalfiledata = infile.read(totalbytes)
- infile.close()
- return totalfiledata
- #This is the workhorse function. Like the name says, it packs up an FLD file.
- def fld_pack(targetfile):
- for filename, filelist in filespec: #From the filespec data, finds the file list.
- if filename == targetfile: #Breaks out of the loop when correct file is found
- break
- for filename, path in pathspec: #From the pathspec data, find the path.
- if filename == targetfile: #Breaks out of the loop when correct file is found
- break
- with open(targetfile,'wb') as f:
- f.write('FLDF0200') #Super-special secret code
- #The start of the index table is computed by:
- #0x14 (where the path info would go) + length of the path string + 1
- #The 1 is added because there MUST be at least one null byte following the path string
- #After this value is computed, it needs to be 4-byte aligned
- tablestart = 0x14 + len(path) + 1 #Find start of the index table
- while tablestart % 4 != 0: #4-byte align the index table start value
- tablestart += 1
- f.write(struct.pack('<I',tablestart)) #Write the index table start position because it's part of the FLD file spec
- f.write(struct.pack('<I',len(filelist))) #Write how many files are in this .fld file
- f.write('\x00'*4) #Write four null bytes because it's part of the FLD file spec
- f.write(path) #Write the path info
- while f.tell() != tablestart: #Pad out the path info with nulls until the start of the index table
- f.write('\x00')
- #The position of the start of the file data is computed by:
- #Start of the index table + [number of files] * 0x14
- #This is because, in the index table, the entry for each file is 0x14 long
- datapos = tablestart + len(filelist)*0x14 #Compute the position where the file data starts
- for filename in filelist: #Loop over the file list
- f.write(filename + '\x00'*(12-len(filename))) #Write the filename
- f.write(struct.pack('<I',datapos)) #Write where in the file the filename goes
- fsize = os.path.getsize(path + filename) #Find how big the file is
- f.write(struct.pack('<I',fsize)) #Write the file size
- datapos += fsize #Compute position of next file
- for filename in filelist: #Loop over the file list AGAIN
- f.write(get_data(path + filename)) #Write the data for each file to the end
- global filespec, pathspec #I made these globals so the workhorse function can access their values
- filespec = [] #Initialize variables
- pathspec = []
- with open(specfile) as f: #Read in the SPECFILE file
- filename = ''
- for line in f:
- line = line.translate(None,'\r\n') #Delete line break characters at end of line
- line = line.split(',') #Make a list, splitting along the comma
- if line[0] == filename: #If the filename is the same as the previous line
- filespec[-1][-1].append(line[1]) #Append the sub-file name to the file list
- else: #If the filename is different than the previous line
- filespec.append([line[0],[line[1]]]) #Append a new entry to the list
- filename = line[0] #Update 'current' filename
- with open(specpath) as f: #Read in the SPECPATH file
- for line in f:
- line = line.translate(None,'\r\n') #Delete line break characters at end of line
- pathspec.append(line.split(',')) #Append the filename and path to the list
- for fldfile in filespec[0][1]: #Loop over the file list for wander.fld
- fld_pack(fldfile) #Pack each sub-fld file
- fld_pack('wander.fld') #Then pack wander.fld itself
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement