Once I get that upgrade to 36-hour days, I will tackle that. – Mychaeel

Legacy:Utft

From Unreal Wiki, The Unreal Engine Documentation Site
Jump to: navigation, search

A cache cleaner by sweavo.

Code[edit]

Copy and paste this into your text editor, save as utft.


#!/usr/bin/python -tt
#
# $Id: utft 91 2007-11-15 17:39:50Z sweavo $
#
# By Sweavo November 2007
#
# Unreal Tournament File Tool for UT2004
#
# A cache-cleaner written in python for cygwin
#
# usage:
#
#   utft {command} {pattern}
# 
#   {command}:
#     ls      lists all files in the cache that match {pattern}
#     get     moves all files from the cache that match {pattern} to a directory 
#             named after the current time.
#     check   check whether everything in the Cache.ini is present in the cache
#             and report those missing
#     cleanup fix Cache.ini by removing entries for files that are missing.
#
#   {pattern}:
#     matches the start of the filename that would be created (e.g. 
#     VCTF-WS-StupdDeathRocket.ut2as rather than 20e9c02c809e098209580.uxx)
#     It is a python regexp rather than a nice shell glob.  And if you are typing
#     it in a cygwin shell, you'll need to add extra backslashes and stuff.
#
# Examples:
#
#   utft ls ONS-        lists all ONS maps
#   utft get .\*Stupid  gets all files with stupid in the name
#   utft get utx$       gets all .utx textures ($ matches end of name)
#
# Notes:
#
#   In order to avoid messing up your installation directories, the get command 
#   does not put the files into the right directories, but into a directory 
#   called Cleaned-YYYYMMDD-HHMMSS where YYYYMMDD-HHMMSS is the date and time
#   you ran the tool.  Under that directory, correct directories are used, so
#   you can simply drag-n-drop from there into the real dirs if you are 
#   convinced those files are the ones you really want.
#
# Example:
#
#   Before:
#     UT2004
#       +-Cache
#       |   +- 2309820348230948203.uxx   
#       |   +- 2340982034982438953.uxx   
#       +-Maps
#       +-Textures
#
#   After:
#     UT2004
#       +-Cache
#       +-Cleaned-20071114-233006
#       |   +-Maps
#       |   |   +-VCTF-TheCapOnTheMap.ut2
#       |   +-Textures
#       |       +-TexTuren.utx
#       +-Maps
#       +-Textures
#
#   Then, drag them:
#     UT2004
#       +-Cache
#       +-Cleaned-20071114-233006
#       +-Maps
#       |   +-VCTF-TheCapOnTheMap.ut2
#       +-Textures
#           +-TexTuren.utx

import os,re,shutil,sys,time

# Where your UT is installed
UTDIR="/cygdrive/c/ut2004"

# If you get told about an unknown filetype, add it here.
EXTDEST={
    'ka':   'KarmaData',
    'ogg':  'Music',
    'uax':  'Sounds',
    'u':    'System',
    'ukx':  'Animations',
    'usx':  'StaticMeshes',
    'ut2':  'Maps',
    'utx':  'Textures',
    'xml':  'Speech',
    }

# "unique" directory name for the got files.
DESTDIR=time.strftime('Cleaned-%Y%m%d-%H%M%S')
## Try this next line if you want to go 'instantly live' with the extracted files
#DESTDIR='.'

# Friendly mkdir by Trent Mick
# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/82465
def _mkdir(newdir):
    """works the way a good mkdir should :)
        - already exists, silently complete
        - regular file in the way, raise an exception
        - parent directory(ies) does not exist, make them as well
    """
    if os.path.isdir(newdir):
        pass
    elif os.path.isfile(newdir):
        raise OSError("a file with the same name as the desired " \
                      "dir, '%s', already exists." % newdir)
    else:
        head, tail = os.path.split(newdir)
        if head and not os.path.isdir(head):
            _mkdir(head)
        if tail:
            os.mkdir(newdir)
# end of Trent Mick's code

# Grab the command and pattern from the commandline
if len(sys.argv)>1:
    command=sys.argv[1]
    if len(sys.argv)>2:
        pattern=sys.argv[2]
    else:
        pattern='.*'

# outfile is the new cache.ini, should we choose to write it.
outfile=[]

inCache=False
for l in open('%s/Cache/cache.ini'%UTDIR): # Go through every line in Cache.ini
    if inCache:
        if '=' in l:  # it's one of our lines
            (name,new)=l.strip().split('=')
            if re.match(pattern,new): 
                # pattern matches, so let's see what we're supposed to do with the file
                if command=='ls':
                    print new

                elif command=='get':
                    (base,ext)=new.split('.')
                    if not ext in EXTDEST.keys():
                        print "%s: no destination known for extension %s. Skipped."%(new,ext)
                    elif not os.path.isfile('%s/Cache/%s.uxx'%(UTDIR,name)):
                        print "%s: source file does not exist any more. Skipped."%(new)
                    else:
                        print "Cache/%s.uxx -> %s/%s/%s"%(name,DESTDIR,EXTDEST[ext],new)
                        _mkdir('%s/%s/%s'%(UTDIR,DESTDIR,EXTDEST[ext]))
                        shutil.move('%s/Cache/%s.uxx'%(UTDIR,name),'%s/%s/%s/%s'%(UTDIR,DESTDIR,EXTDEST[ext],new))

                elif command=='check':
                    (base,ext)=new.split('.')
                    if not ext in EXTDEST.keys():
                        print "%s: no destination known for extension %s."%(new,ext)
                    elif not os.path.isfile('%s/Cache/%s.uxx'%(UTDIR,name)):
                        print "%s: source file does not exist any more."%(new)

                elif command=='cleanup':
                    if os.path.isfile('%s/Cache/%s.uxx'%(UTDIR,name)):
                        outfile=outfile+[l]
                        
    else: # We have'nt seen the "[cache]" tag yet, or we've received lines that didn't have blah=blah in them.
        outfile=outfile+[l]
        if '[cache]' in l.lower():
            inCache=True

# This could probably be added for "get" command as well, save the user having to always do "cleanup".
if command=='cleanup':
    f=open('%s/Cache/cache.ini'%UTDIR,'w')
    f.writelines(outfile)
    f.close()