-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvtfswapper.py
110 lines (71 loc) · 2.69 KB
/
vtfswapper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""
vtfswapper.py
By DKY
Version 0.0.0
This program randomly swaps the names of every *.vtf file in a Source Engine
mod's \materials folder, resulting in completely FUBAR'd textures.
Why you would actually want to do this, I have no idea.
"""
import os
import sys
from itertools import izip
from random import SystemRandom
import vdfutils
_r = SystemRandom()
_REMAP_FILE_NAME = 'vtfRemap.txt'
def find_vtfs(path):
""" Takes a directory and returns all the *.vtf files in that directory
and its sub-directories.
"""
vtfs = []
for item in os.listdir(path):
itemPath = os.path.join(path, item)
if os.path.isdir(itemPath):
vtfs += find_vtfs(itemPath)
elif item.endswith('.vtf'):
vtfs.append(itemPath)
return vtfs
def rename_vtfs(remapDict):
""" Renames every *.vtf file in the given remap dictionary to its
corresponding shuffled name.
"""
def underscored(path):
''' Returns the name of the file at the given path, except with an
underscore prepended to the filename. The returned value is also a
full path to that file.
'''
fileName = os.path.basename(path)
fileDir = os.path.dirname(path)
return os.path.join(fileDir, '_{}'.format(fileName))
# Prepend all of the existing filenames with underscores, to prevent
# renaming conflicts.
for vtfPath in remapDict:
os.rename(vtfPath, underscored(vtfPath))
print "\t... Pass 1 complete."
# Rename all the files to their corresponding new names
for vtfPath, newName in remapDict.iteritems():
os.rename(underscored(vtfPath), newName)
print "\t... Pass 2 complete."
def main():
print "Finding VTF files...",
here = os.getcwd()
materialsDir = os.path.join(here, 'materials')
vtfs = find_vtfs(materialsDir)
print "Found {} files.".format(len(vtfs))
print "Shuffling names..."
shuffledNames = vtfs[:]
_r.shuffle(shuffledNames)
print "Building name remap dictionary..."
# Dictionary that maps names to shuffled names
remapDict = dict(izip(vtfs, shuffledNames))
print "Renaming VTF files..."
rename_vtfs(remapDict)
print "Saving name remap dictionary for future restoration..."
remapFilePath = os.path.join(here, _REMAP_FILE_NAME)
with open(remapFilePath, 'w') as f:
f.write(vdfutils.format_vdf({'remap' : remapDict}))
print "Done! Enjoy your FUBAR'd textures!"
raw_input("Press [ENTER] to continue...")
return 0
if __name__ == '__main__':
sys.exit(main())