Advertisement
Nicknine

GCF Game Preparator

Jan 25th, 2022 (edited)
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.88 KB | None | 0 0
  1. import os
  2. import sys
  3. import ctypes as ct
  4. import fnmatch
  5. import argparse
  6. import shutil
  7.  
  8. excludeList=list()
  9. excludeListWildcards=False
  10.  
  11. hllib=ct.cdll.LoadLibrary(".\\HLLib")
  12.  
  13. hllib.hlPackageGetRoot.restype=ct.c_void_p
  14. hllib.hlFolderGetItemByPath.argtypes=[ct.c_void_p,ct.c_char_p,ct.c_int]
  15. hllib.hlFolderGetItemByPath.restype=ct.c_void_p
  16. hllib.hlFolderFindFirst.argtypes=[ct.c_void_p,ct.c_char_p,ct.c_int]
  17. hllib.hlFolderFindFirst.restype=ct.c_void_p
  18. hllib.hlFolderFindNext.argtypes=[ct.c_void_p,ct.c_void_p,ct.c_char_p,ct.c_int]
  19. hllib.hlFolderFindNext.restype=ct.c_void_p
  20. hllib.hlFolderGetItemByPath.argtypes=[ct.c_void_p,ct.c_char_p,ct.c_int]
  21. hllib.hlFolderGetItemByPath.restype=ct.c_void_p
  22. hllib.hlItemExtract.argtypes=[ct.c_void_p,ct.c_char_p]
  23. hllib.hlItemExtract.restype=ct.c_int
  24. hllib.hlItemGetPath.argtypes=[ct.c_void_p,ct.c_char_p,ct.c_int]
  25. #hllib.hlItemGetPath.restype=ct.c_void
  26. hllib.hlPackageGetItemAttribute.argtypes=[ct.c_void_p,ct.c_int,ct.c_void_p]
  27. hllib.hlPackageGetItemAttribute.restype=ct.c_int
  28.  
  29. class Atrribute(ct.Structure):
  30.     _fields_=[("eAttributeType",ct.c_int),
  31.               ("lpName",ct.c_char*252),
  32.               ("bValue",ct.c_int)]
  33.  
  34. def extractFile(rootDir,path,outPath):
  35.     item=hllib.hlFolderGetItemByPath(rootDir,path.encode(),0x01)
  36.     if not item:
  37.         return False
  38.  
  39.     itemPath=bytes(260)
  40.     hllib.hlItemGetPath(item,itemPath,len(itemPath))
  41.     fname=itemPath[5:itemPath.index(b"\0")].decode() # Skip "root/" and null terminator.
  42.     destPath=os.path.join(outPath,fname)
  43.  
  44.     folderPath=os.path.dirname(destPath)
  45.     os.makedirs(folderPath,exist_ok=True)
  46.     hllib.hlItemExtract(item,folderPath.encode())
  47.     return True
  48.  
  49. def loadExcludeList(rootDir,excludeLst,outPath):
  50.     global excludeList
  51.     global excludeListWildcards
  52.     lstName=r"reslists\%s\exclude.lst" % excludeLst
  53.     if not extractFile(rootDir,lstName,outPath):
  54.         return False
  55.  
  56.     f=open(os.path.join(outPath,lstName),"r")
  57.     excludeList=f.read().splitlines()
  58.     f.close()
  59.     shutil.rmtree(os.path.join(outPath,"reslists"))
  60.  
  61.     for entry in excludeList:
  62.         if "*" in entry or "?" in entry:
  63.             excludeListWildcards=True
  64.             break
  65.  
  66.     return True
  67.  
  68. def extractGCF(inGCF,outPath,extractAll,hldsPath):
  69.     if not os.path.isfile(inGCF):
  70.         print("No such file %s" % inGCF)
  71.         return False
  72.  
  73.     print(os.path.basename(inGCF)+"\n"+"-"*72)
  74.  
  75.     hllib.hlInitialize()
  76.     packageType=hllib.hlGetPackageTypeFromName(inGCF.encode())
  77.     pkg=ct.c_int()
  78.     ret=hllib.hlCreatePackage(packageType,ct.pointer(pkg))
  79.     hllib.hlBindPackage(pkg)
  80.     mode=0x01|0x08|0x10
  81.     ret=hllib.hlPackageOpenFile(inGCF.encode(),mode)
  82.     rootDir=ct.c_void_p(hllib.hlPackageGetRoot())
  83.  
  84.     if hldsPath:
  85.         if not loadExcludeList(rootDir,hldsPath,outPath):
  86.             print("exclude.lst not found")
  87.             return False
  88.  
  89.     # Find and extract files with Copy Locally attribute.
  90.     searchString="*"
  91.     item=hllib.hlFolderFindFirst(rootDir,searchString.encode(),0x01)
  92.     while item:
  93.         itemPath=bytes(260)
  94.         hllib.hlItemGetPath(item,itemPath,len(itemPath))
  95.         fname=itemPath[5:itemPath.index(b"\0")].decode() # Skip "root/" and null terminator.
  96.         destPath=os.path.join(outPath,fname)
  97.  
  98.         shouldExtract=True
  99.         if hldsPath:
  100.             for entry in excludeList:
  101.                 if not entry:
  102.                     continue
  103.  
  104.                 if fname.startswith(entry) or (excludeListWildcards and fnmatch.fnmatch(fname,entry)):
  105.                     shouldExtract=False
  106.                     break
  107.         elif not extractAll:
  108.             attrib1=Atrribute()
  109.             hllib.hlPackageGetItemAttribute(item,1,ct.pointer(attrib1)) # Copy Locally
  110.             attrib2=Atrribute()
  111.             hllib.hlPackageGetItemAttribute(item,2,ct.pointer(attrib2)) # Overwrite Local Copy
  112.             attrib3=Atrribute()
  113.             hllib.hlPackageGetItemAttribute(item,3,ct.pointer(attrib3)) # Backup Local Copy
  114.             shouldExtract=(attrib1.bValue and
  115.                            ((attrib2.bValue and not attrib3.bValue) or not os.path.isfile(destPath)))
  116.  
  117.         if shouldExtract:
  118.             print(fname)
  119.             folderPath=os.path.dirname(destPath)
  120.             os.makedirs(folderPath,exist_ok=True)
  121.             hllib.hlItemExtract(item,folderPath.encode())
  122.  
  123.         item=hllib.hlFolderFindNext(rootDir,item,searchString.encode(),0x01)
  124.  
  125.     print("")
  126.     hllib.hlPackageClose()
  127.     hllib.hlDeletePackage(pkg)
  128.     hllib.hlShutdown()
  129.     return True
  130.  
  131. if __name__ == "__main__":
  132.     parser=argparse.ArgumentParser()
  133.     parser.add_argument("inGCF",type=str)
  134.     parser.add_argument("outPath",type=str)
  135.     parser.add_argument("--all",action='store_true',default=False)
  136.     parser.add_argument("--hlds",type=str,default=None)
  137.     args=parser.parse_args()
  138.  
  139.     extractGCF(args.inGCF,args.outPath,args.all,args.hlds)
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement