Skip to content

Instantly share code, notes, and snippets.

@deluxghost
Last active March 8, 2021 00:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deluxghost/98e50d01e3c1da649b6d2b83aea842d4 to your computer and use it in GitHub Desktop.
Save deluxghost/98e50d01e3c1da649b6d2b83aea842d4 to your computer and use it in GitHub Desktop.
TF2
Param([string]$BzipDir = '.')
Get-ChildItem -Path $BzipDir -File -Recurse | ForEach-Object ($_) {
$File = $_.FullName
& '.\bin\7z.exe' u -tbzip2 "$File.bz2" "$File"
}
import os
from datetime import datetime
FIRST_TIME = False
MAP_DIR = './data/maps'
POP_DIR = './data/scripts/population'
CACHE_FILE = 'manager.cache'
DIFF_FILE = 'diff.txt'
MAPLIST_FILE = 'index.html'
MAPCYCLE_FILE = 'mapcycle.txt'
MISSIONCYCLE_FILE = 'mvm_missioncycle.res'
CACHE = {'maps': {}, 'pops': {}}
DATA = {'maps': {}, 'pops': {}}
DATA_ADD = []
DATA_SUB = []
DATA_MOD = []
MAP_COUNT = 0
NAV_COUNT = 0
PVP_COUNT = 0
MVM_COUNT = 0
MISSION_COUNT = 0
def sizeof(num, suffix='B'):
for unit in ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']:
if abs(num) < 1024.0:
return '%3.1f%s%s' % (num, unit, suffix)
num /= 1024.0
return '%.1f%s%s' % (num, 'Y', suffix)
def load_cache():
global FIRST_TIME, CACHE
if not os.path.isfile(CACHE_FILE):
FIRST_TIME = True
return
with open(CACHE_FILE, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
filename, timestamp = line.split('\t')
dt = datetime.fromtimestamp(int(timestamp))
_, ext = os.path.splitext(filename)
if ext in ['.bsp', '.nav']:
CACHE['maps'][filename] = dt
elif ext in ['.pop']:
CACHE['pops'][filename] = dt
def save_cache():
lines = []
for entry in DATA:
for f in DATA[entry]:
timestamp = int(datetime.timestamp(DATA[entry][f]))
lines.append(f'{f}\t{timestamp}\n')
with open(CACHE_FILE, 'w', encoding='utf-8') as f:
f.writelines(lines)
def read_data(entry, entry_dir):
global DATA, DATA_ADD, DATA_SUB, DATA_MOD
cache_copy = CACHE[entry].copy()
files = [f for f in os.listdir(entry_dir) if os.path.isfile(os.path.join(entry_dir, f))]
files.sort()
for f in files:
DATA[entry][f] = datetime.fromtimestamp(int(os.path.getmtime(os.path.join(entry_dir, f))))
for f in DATA[entry]:
cache_line = cache_copy.pop(f, None)
if cache_line is None:
DATA_ADD.append(f)
elif DATA[entry][f] != cache_line:
DATA_MOD.append(f)
for c in cache_copy:
DATA_SUB.append(c)
def gen_maplist():
global MAP_COUNT, NAV_COUNT
print(f'Generating {MAPLIST_FILE}...')
html = [
'<html><head><title>Maps</title></head><body>\n',
'<style>a:link {\n',
' text-decoration: none;\n',
'}\n',
'a:visited {\n',
' text-decoration: none;\n',
'}\n',
'a:hover {\n',
' text-decoration: underline;\n',
'}\n',
'a:active {\n',
' text-decoration: underline;\n',
'}</style>\n',
'<h1>Maps</h1><hr><table><thead><tr><th>Name</th><th>Size</th></tr></thead><tbody>\n'
]
for f in DATA['maps']:
base, ext = os.path.splitext(f)
if ext == '.bsp':
MAP_COUNT += 1
elif ext == '.nav':
NAV_COUNT += 1
size = sizeof(os.path.getsize(os.path.join(MAP_DIR, f)))
print(f'{f}\t{size}')
html.append(f'<tr><td><a href="http://dx.reallct.com/maps/{f}.bz2">{f}.bz2</a></td><td>{size}</td></tr>\n')
html.append('</tbody></table></body></html>\n')
print(f'{MAP_COUNT} maps, {NAV_COUNT} nav files.')
print('Writing...')
with open(MAPLIST_FILE, 'w', encoding='utf-8') as f:
f.writelines(html)
print(f'{MAPLIST_FILE} generated.')
def gen_mapcycle():
global PVP_COUNT
print(f'Generating {MAPCYCLE_FILE}...')
maps = []
for f in DATA['maps']:
base, ext = os.path.splitext(f)
if ext == '.bsp' and not base.startswith('mvm_'):
print(f'{base}')
maps.append(f'{base}\n')
maps.sort()
PVP_COUNT = len(maps)
maps.insert(0, '\n')
print(f'{PVP_COUNT} maps.')
print('Writing...')
with open(MAPCYCLE_FILE, 'w', encoding='utf-8') as f:
f.writelines(maps)
print(f'{MAPCYCLE_FILE} generated.')
def gen_missioncycle():
global MVM_COUNT, MISSION_COUNT
print(f'Generating {MISSIONCYCLE_FILE}...')
lines = []
maps = [
'mvm_bigrock',
'mvm_coaltown',
'mvm_decoy',
'mvm_ghost_town',
'mvm_mannhattan',
'mvm_mannworks',
'mvm_rottenburg'
]
for f in DATA['maps']:
mapname, ext = os.path.splitext(f)
if ext == '.bsp' and mapname.startswith('mvm_'):
maps.append(mapname)
for mapname in maps:
print(f'{mapname}')
MVM_COUNT += 1
missions = [m for m in DATA['pops'] if m.startswith(mapname)]
missions.sort()
for m in missions:
MISSION_COUNT += 1
m, _ = os.path.splitext(m)
print(f'\t{m}')
lines.extend([
f'\t"{MISSION_COUNT}"\n',
'\t{\n',
'\t\t"count" "1"\n',
'\t\t"1"\n',
'\t\t{\n',
f'\t\t\t"map" "{mapname}"\n',
f'\t\t\t"popfile" "{m}"\n',
'\t\t}\n',
'\t}\n'
])
lines.insert(0, f'\t"categories" "{MISSION_COUNT}"\n')
lines.insert(0, '{\n')
lines.insert(0, '"mvm_missioncycle.res"\n')
lines.append('}\n')
print(f'{MVM_COUNT} maps, {MISSION_COUNT} missions.')
print('Writing...')
with open(MISSIONCYCLE_FILE, 'w', encoding='utf-8') as f:
f.writelines(lines)
print(f'{MISSIONCYCLE_FILE} generated.')
def gen_diff():
print(f'Generating {DIFF_FILE}...')
lines = []
fileset = list(set(DATA_ADD + DATA_SUB + DATA_MOD))
fileset.sort()
for f in fileset:
if f in DATA_SUB:
lines.append(f'[-] {f}\n')
if f in DATA_ADD:
lines.append(f'[+] {f}\n')
if f in DATA_MOD:
lines.append(f'[*] {f}\n')
if not lines:
print('No file diff, skipped.')
return
print('Writing...')
with open(DIFF_FILE, 'w', encoding='utf-8') as f:
f.writelines(lines)
print(f'{DIFF_FILE} generated.')
if __name__ == '__main__':
load_cache()
read_data('maps', MAP_DIR)
read_data('pops', POP_DIR)
save_cache()
gen_maplist()
gen_mapcycle()
gen_missioncycle()
gen_diff()
print('All tasks finished.')
print(f'Total {MAP_COUNT} maps, {PVP_COUNT} pvp/coop, {MVM_COUNT-7}(+7) mvm.')
print(f'Total {MISSION_COUNT} mvm missions.')
print(f'Diff: {len(DATA_ADD)} files[+], {len(DATA_SUB)} files[-], {len(DATA_MOD)} files[*].')
input()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment