Update scrapper.py

Cleaned up code a bit
This commit is contained in:
Earthnuker 2017-10-08 00:04:37 +00:00
parent cef9944fc6
commit 9a30112866

View file

@ -1,65 +1,11 @@
import argparse import argparse
from collections import OrderedDict from collections import OrderedDict
import configparser
import glob import glob
import os import os
import shutil import shutil
from construct import * from construct import *
from tqdm import tqdm from tqdm import tqdm
setglobalstringencoding(None) setglobalstringencoding(None)
def find_file(name):
global scrap_dir
for folder in glob.glob(os.path.join(scrap_dir, 'extracted', '*.packed')):
for root, folders, files in os.walk(folder):
for filename in files:
path = os.path.join(root, filename)
if filename == name:
yield path
def get_config(conf_parse, section, var, default=None):
return conf_parse[section].get(var, default)
def patch_config(path, section, var, value):
config = configparser.ConfigParser()
config.read(path)
if get_config(config, section, var) == value:
return
config[section][var] = value
with open(path, 'w') as conf:
config.write(conf)
return True
def enable_debug_console_gui():
'''enable debug console (GUI)'''
for path in find_file('m3d.ini'):
print('Found', path)
return patch_config(path, 'video', 'ConsolaWnd', 'SI')
def enable_debug_console_txt():
'''enable debug console (Text Mode)'''
path = "Test"
for path in find_file('m3d.ini'):
print('Found', path)
return patch_config(path, 'video', 'ConsolaTxt', 'SI')
print(path)
patches = [
enable_debug_console_gui,
enable_debug_console_txt,
]
def yn(prompt, default='n'):
c = ['y', 'n']
default = default.lower()
assert default in c
c[c.index(default)] = c[c.index(default)].upper()
prompt += ' ({}) '.format('/'.join(c))
return (input(prompt) or default).lower()[0] == 'y'
ScrapFile = Struct( ScrapFile = Struct(
'path'/PascalString(Int32ul), 'path'/PascalString(Int32ul),
@ -76,15 +22,14 @@ DummyFile = Struct(
PackedHeader = Struct( PackedHeader = Struct(
Const(b'BFPK'), Const(b'BFPK'),
Const(b'\0\0\0\0'), Const(b'\0\0\0\0'),
'files_cnt'/Rebuild(Int32ul,len_(this.files)), 'files'/PrefixedArray(Int32ul,ScrapFile),
'files'/ScrapFile[this.files_cnt],
'offset'/Tell, 'offset'/Tell,
) )
DummyHeader = Struct( DummyHeader = Struct(
Const(b'BFPK'), Const(b'BFPK'),
Const(b'\0\0\0\0'), Const(b'\0\0\0\0'),
'files_cnt'/Rebuild(Int32ul,len_(this.files)), 'files_cnt'/Rebuild(Int32ul,len_(this.files)),
'files'/DummyFile[this.files_cnt], 'files'/PrefixedArray(Int32ul,DummyFile),
'offset'/Tell, 'offset'/Tell,
) )
parser = argparse.ArgumentParser(description='Unpack and Repack .packed files') parser = argparse.ArgumentParser(description='Unpack and Repack .packed files')
@ -92,8 +37,6 @@ parser.add_argument('-u', '--unpack', action='store_true',
help='unpack file to \'extracted\' directory') help='unpack file to \'extracted\' directory')
parser.add_argument('-r', '--repack', action='store_true', parser.add_argument('-r', '--repack', action='store_true',
help='repack file from \'extracted\' directory') help='repack file from \'extracted\' directory')
parser.add_argument('-p', '--patch', action='store_true',
help='apply a premade patch')
parser.add_argument( parser.add_argument(
'--reset', '--reset',
@ -105,6 +48,7 @@ parser.add_argument(
'scrap_dir', 'scrap_dir',
metavar='Scrapland Directory', metavar='Scrapland Directory',
type=str, type=str,
default=".",
help='Scrapland installation directory') help='Scrapland installation directory')
options = parser.parse_args() options = parser.parse_args()
scrap_dir = os.path.abspath(options.scrap_dir) scrap_dir = os.path.abspath(options.scrap_dir)
@ -128,7 +72,7 @@ if options.reset:
shutil.rmtree('extracted') shutil.rmtree('extracted')
exit('Done!') exit('Done!')
if not (options.unpack or options.repack or options.patch): if not (options.unpack or options.repack):
parser.print_help() parser.print_help()
exit() exit()
pstatus = '' pstatus = ''
@ -156,30 +100,10 @@ if options.unpack:
print('\r' + ' ' * len(pstatus) + '\r', end='', flush=True) print('\r' + ' ' * len(pstatus) + '\r', end='', flush=True)
os.chdir(scrap_dir) os.chdir(scrap_dir)
if (options.unpack and options.repack) and not options.patch: if (options.unpack and options.repack):
#input('Press enter to rebuild *.packed files from folders in \'extracted\' dir...') # noqa input('Press enter to rebuild *.packed files from folders in \'extracted\' dir...') # noqa
pass pass
if options.patch:
print()
print("Enter Nothing to continue")
for n, patch in enumerate(patches, 1):
print('{}. {}'.format(n, patch.__doc__.strip()))
while 1:
n = input('Patch to apply: ')
if not n:
break
n = int(n) - 1
if 0 <= n < len(patches):
res = patches[n]()
if res is True:
print('Applied Succesfully!')
elif res is None:
print('Already applied.')
elif res is False:
print('Error')
print()
def file_gen(files,offset=0): def file_gen(files,offset=0):
for real_path,size,path in files: for real_path,size,path in files:
file=dict( file=dict(
@ -188,6 +112,7 @@ def file_gen(files,offset=0):
size=size) size=size)
yield file yield file
offset+=file['size'] offset+=file['size']
def make_header(files,offset=0): def make_header(files,offset=0):
files_list=list(file_gen(files,offset)) files_list=list(file_gen(files,offset))
return DummyHeader.build(dict(files=files_list)) return DummyHeader.build(dict(files=files_list))