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
1 changed files with 138 additions and 213 deletions

View File

@ -1,65 +1,11 @@
import argparse
from collections import OrderedDict
import configparser
import glob
import os
import shutil
from construct import *
from tqdm import tqdm
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(
'path'/PascalString(Int32ul),
@ -76,15 +22,14 @@ DummyFile = Struct(
PackedHeader = Struct(
Const(b'BFPK'),
Const(b'\0\0\0\0'),
'files_cnt'/Rebuild(Int32ul,len_(this.files)),
'files'/ScrapFile[this.files_cnt],
'files'/PrefixedArray(Int32ul,ScrapFile),
'offset'/Tell,
)
DummyHeader = Struct(
Const(b'BFPK'),
Const(b'\0\0\0\0'),
'files_cnt'/Rebuild(Int32ul,len_(this.files)),
'files'/DummyFile[this.files_cnt],
'files'/PrefixedArray(Int32ul,DummyFile),
'offset'/Tell,
)
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')
parser.add_argument('-r', '--repack', action='store_true',
help='repack file from \'extracted\' directory')
parser.add_argument('-p', '--patch', action='store_true',
help='apply a premade patch')
parser.add_argument(
'--reset',
@ -105,6 +48,7 @@ parser.add_argument(
'scrap_dir',
metavar='Scrapland Directory',
type=str,
default=".",
help='Scrapland installation directory')
options = parser.parse_args()
scrap_dir = os.path.abspath(options.scrap_dir)
@ -128,7 +72,7 @@ if options.reset:
shutil.rmtree('extracted')
exit('Done!')
if not (options.unpack or options.repack or options.patch):
if not (options.unpack or options.repack):
parser.print_help()
exit()
pstatus = ''
@ -156,30 +100,10 @@ if options.unpack:
print('\r' + ' ' * len(pstatus) + '\r', end='', flush=True)
os.chdir(scrap_dir)
if (options.unpack and options.repack) and not options.patch:
#input('Press enter to rebuild *.packed files from folders in \'extracted\' dir...') # noqa
if (options.unpack and options.repack):
input('Press enter to rebuild *.packed files from folders in \'extracted\' dir...') # noqa
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):
for real_path,size,path in files:
file=dict(
@ -188,6 +112,7 @@ def file_gen(files,offset=0):
size=size)
yield file
offset+=file['size']
def make_header(files,offset=0):
files_list=list(file_gen(files,offset))
return DummyHeader.build(dict(files=files_list))