Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import sys
- import ctypes as ct
- import fnmatch
- import argparse
- import shutil
- excludeList=list()
- excludeListWildcards=False
- hllib=ct.cdll.LoadLibrary(".\\HLLib")
- hllib.hlPackageGetRoot.restype=ct.c_void_p
- hllib.hlFolderGetItemByPath.argtypes=[ct.c_void_p,ct.c_char_p,ct.c_int]
- hllib.hlFolderGetItemByPath.restype=ct.c_void_p
- hllib.hlFolderFindFirst.argtypes=[ct.c_void_p,ct.c_char_p,ct.c_int]
- hllib.hlFolderFindFirst.restype=ct.c_void_p
- hllib.hlFolderFindNext.argtypes=[ct.c_void_p,ct.c_void_p,ct.c_char_p,ct.c_int]
- hllib.hlFolderFindNext.restype=ct.c_void_p
- hllib.hlFolderGetItemByPath.argtypes=[ct.c_void_p,ct.c_char_p,ct.c_int]
- hllib.hlFolderGetItemByPath.restype=ct.c_void_p
- hllib.hlItemExtract.argtypes=[ct.c_void_p,ct.c_char_p]
- hllib.hlItemExtract.restype=ct.c_int
- hllib.hlItemGetPath.argtypes=[ct.c_void_p,ct.c_char_p,ct.c_int]
- #hllib.hlItemGetPath.restype=ct.c_void
- hllib.hlPackageGetItemAttribute.argtypes=[ct.c_void_p,ct.c_int,ct.c_void_p]
- hllib.hlPackageGetItemAttribute.restype=ct.c_int
- class Atrribute(ct.Structure):
- _fields_=[("eAttributeType",ct.c_int),
- ("lpName",ct.c_char*252),
- ("bValue",ct.c_int)]
- def extractFile(rootDir,path,outPath):
- item=hllib.hlFolderGetItemByPath(rootDir,path.encode(),0x01)
- if not item:
- return False
- itemPath=bytes(260)
- hllib.hlItemGetPath(item,itemPath,len(itemPath))
- fname=itemPath[5:itemPath.index(b"\0")].decode() # Skip "root/" and null terminator.
- destPath=os.path.join(outPath,fname)
- folderPath=os.path.dirname(destPath)
- os.makedirs(folderPath,exist_ok=True)
- hllib.hlItemExtract(item,folderPath.encode())
- return True
- def loadExcludeList(rootDir,excludeLst,outPath):
- global excludeList
- global excludeListWildcards
- lstName=r"reslists\%s\exclude.lst" % excludeLst
- if not extractFile(rootDir,lstName,outPath):
- return False
- f=open(os.path.join(outPath,lstName),"r")
- excludeList=f.read().splitlines()
- f.close()
- shutil.rmtree(os.path.join(outPath,"reslists"))
- for entry in excludeList:
- if "*" in entry or "?" in entry:
- excludeListWildcards=True
- break
- return True
- def extractGCF(inGCF,outPath,extractAll,hldsPath):
- if not os.path.isfile(inGCF):
- print("No such file %s" % inGCF)
- return False
- print(os.path.basename(inGCF)+"\n"+"-"*72)
- hllib.hlInitialize()
- packageType=hllib.hlGetPackageTypeFromName(inGCF.encode())
- pkg=ct.c_int()
- ret=hllib.hlCreatePackage(packageType,ct.pointer(pkg))
- hllib.hlBindPackage(pkg)
- mode=0x01|0x08|0x10
- ret=hllib.hlPackageOpenFile(inGCF.encode(),mode)
- rootDir=ct.c_void_p(hllib.hlPackageGetRoot())
- if hldsPath:
- if not loadExcludeList(rootDir,hldsPath,outPath):
- print("exclude.lst not found")
- return False
- # Find and extract files with Copy Locally attribute.
- searchString="*"
- item=hllib.hlFolderFindFirst(rootDir,searchString.encode(),0x01)
- while item:
- itemPath=bytes(260)
- hllib.hlItemGetPath(item,itemPath,len(itemPath))
- fname=itemPath[5:itemPath.index(b"\0")].decode() # Skip "root/" and null terminator.
- destPath=os.path.join(outPath,fname)
- shouldExtract=True
- if hldsPath:
- for entry in excludeList:
- if not entry:
- continue
- if fname.startswith(entry) or (excludeListWildcards and fnmatch.fnmatch(fname,entry)):
- shouldExtract=False
- break
- elif not extractAll:
- attrib1=Atrribute()
- hllib.hlPackageGetItemAttribute(item,1,ct.pointer(attrib1)) # Copy Locally
- attrib2=Atrribute()
- hllib.hlPackageGetItemAttribute(item,2,ct.pointer(attrib2)) # Overwrite Local Copy
- attrib3=Atrribute()
- hllib.hlPackageGetItemAttribute(item,3,ct.pointer(attrib3)) # Backup Local Copy
- shouldExtract=(attrib1.bValue and
- ((attrib2.bValue and not attrib3.bValue) or not os.path.isfile(destPath)))
- if shouldExtract:
- print(fname)
- folderPath=os.path.dirname(destPath)
- os.makedirs(folderPath,exist_ok=True)
- hllib.hlItemExtract(item,folderPath.encode())
- item=hllib.hlFolderFindNext(rootDir,item,searchString.encode(),0x01)
- print("")
- hllib.hlPackageClose()
- hllib.hlDeletePackage(pkg)
- hllib.hlShutdown()
- return True
- if __name__ == "__main__":
- parser=argparse.ArgumentParser()
- parser.add_argument("inGCF",type=str)
- parser.add_argument("outPath",type=str)
- parser.add_argument("--all",action='store_true',default=False)
- parser.add_argument("--hlds",type=str,default=None)
- args=parser.parse_args()
- extractGCF(args.inGCF,args.outPath,args.all,args.hlds)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement