Guest User

Wander Wonder .FLD Pack v1

a guest
Apr 12th, 2015
100
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os
  2. import struct
  3. import glob
  4. specfile = 'fldfilespec.csv'    #Define constant value
  5. specpath = 'fldpathspec.csv'    #Define constant value
  6.  
  7. #Gets data
  8. def get_data(filename):
  9.     totalbytes = os.path.getsize(filename)
  10.     infile = open(filename, 'rb')
  11.     totalfiledata = infile.read(totalbytes)
  12.     infile.close()
  13.     return totalfiledata
  14.  
  15. #This is the workhorse function. Like the name says, it packs up an FLD file.
  16. def fld_pack(targetfile):
  17.     for filename, filelist in filespec: #From the filespec data, finds the file list.
  18.         if filename == targetfile:      #Breaks out of the loop when correct file is found
  19.             break
  20.     for filename, path in pathspec:     #From the pathspec data, find the path.
  21.         if filename == targetfile:      #Breaks out of the loop when correct file is found
  22.             break
  23.     with open(targetfile,'wb') as f:
  24.         f.write('FLDF0200')             #Super-special secret code
  25. #The start of the index table is computed by:
  26. #0x14 (where the path info would go) + length of the path string + 1
  27. #The 1 is added because there MUST be at least one null byte following the path string
  28. #After this value is computed, it needs to be 4-byte aligned
  29.         tablestart = 0x14 + len(path) + 1   #Find start of the index table
  30.         while tablestart % 4 != 0:          #4-byte align the index table start value
  31.             tablestart += 1
  32.         f.write(struct.pack('<I',tablestart))   #Write the index table start position because it's part of the FLD file spec
  33.         f.write(struct.pack('<I',len(filelist)))    #Write how many files are in this .fld file
  34.         f.write('\x00'*4)                       #Write four null bytes because it's part of the FLD file spec
  35.         f.write(path)                           #Write the path info
  36.         while f.tell() != tablestart:           #Pad out the path info with nulls until the start of the index table
  37.             f.write('\x00')
  38. #The position of the start of the file data is computed by:
  39. #Start of the index table + [number of files] * 0x14
  40. #This is because, in the index table, the entry for each file is 0x14 long
  41.         datapos = tablestart + len(filelist)*0x14   #Compute the position where the file data starts
  42.         for filename in filelist:               #Loop over the file list
  43.             f.write(filename + '\x00'*(12-len(filename)))   #Write the filename
  44.             f.write(struct.pack('<I',datapos))              #Write where in the file the filename goes
  45.             fsize = os.path.getsize(path + filename)        #Find how big the file is
  46.             f.write(struct.pack('<I',fsize))                #Write the file size
  47.             datapos += fsize                                #Compute position of next file
  48.         for filename in filelist:               #Loop over the file list AGAIN
  49.             f.write(get_data(path + filename))  #Write the data for each file to the end
  50.  
  51. global filespec, pathspec   #I made these globals so the workhorse function can access their values
  52. filespec = []               #Initialize variables
  53. pathspec = []
  54. with open(specfile) as f:   #Read in the SPECFILE file
  55.     filename = ''
  56.     for line in f:
  57.         line = line.translate(None,'\r\n')  #Delete line break characters at end of line
  58.         line = line.split(',')              #Make a list, splitting along the comma
  59.         if line[0] == filename:             #If the filename is the same as the previous line
  60.             filespec[-1][-1].append(line[1])    #Append the sub-file name to the file list
  61.         else:                               #If the filename is different than the previous line
  62.             filespec.append([line[0],[line[1]]])    #Append a new entry to the list
  63.             filename = line[0]                      #Update 'current' filename
  64. with open(specpath) as f:   #Read in the SPECPATH file
  65.     for line in f:
  66.         line = line.translate(None,'\r\n')  #Delete line break characters at end of line
  67.         pathspec.append(line.split(','))    #Append the filename and path to the list
  68.  
  69. for fldfile in filespec[0][1]:  #Loop over the file list for wander.fld
  70.     fld_pack(fldfile)           #Pack each sub-fld file
  71. fld_pack('wander.fld')          #Then pack wander.fld itself
RAW Paste Data