A small Python tool for map packing

Started by wjssz
17/02/2014 10:28
Page 1 of 1
wjssz17/02/2014 10:28(edited 17/02/2014 10:50)

This tool checks the structure of map directories. If it gives many messages, then we should check if some maps were extracted to wrong directory.
If you are making a map pack, you may do this check before final packing.
Hope this tool could help you.


#-------------------------------------------------------------------------------
# Name: check_dirs.py
# Purpose: check the map directories of cs1.6
# Usage: modify in this file, and put this file into
# parent folder of . Run this, it will dump the
# questionable locations to analysis_msg.txt
# Created: 17/02/2014
#
# For Python 3.x
#-------------------------------------------------------------------------------


#coding=utf-8
import os, time, builtins

#! modify this before run
mydir = 'cstrike_downloads'

# structure table
dicts = {
'maps' : {'.bsp', '.res', '.pts'},
'gfx' : {'.bmp', '.tga'},
'sprites' : {'.spr'},
'models' : {'.mdl'},
'overviews' : {'.bmp', '.tga'},
'sound' : {'.wav'},
'*root*' : {'.wad'}
}

# give warining if these filetypes appear in other directories
some_types = {'.bsp', '.res', '.pts', '.spr', '.mdl', '.wad'}

all_types = set()
msg_count = 0
f = open('analysis_msg.txt','w')


def init():
for item in dicts:
all_types.update(dicts[item])


def output(path, name, sub_dir):
global msg_count, dicts, some_types, all_types
msg_count += 1

asize = os.path.getsize(path)
atime = time.gmtime(os.path.getmtime(path))

print('msg:', msg_count, file = f)
print(path, file = f)
print('{0:>12}'.format(format(asize,',')), 'bytes ', \
time.strftime('%Y-%m-%d %H:%M:%S', atime), file = f)
if sub_dir != '':
print(dicts[sub_dir], file = f)
print(file = f)


def traverse(root_dir, sub_dir):
for item_name in os.listdir(root_dir):
path = os.path.join(root_dir, item_name)

if os.path.isdir(path):
if sub_dir == '*root*':
traverse(path, item_name)
elif sub_dir == 'maps':
output(path, item_name, '')
else:
traverse(path, sub_dir)

else:
atype = os.path.splitext(item_name)[1]
if sub_dir in dicts:
if atype not in dicts[sub_dir] and atype in all_types:
output(path, item_name, sub_dir)
else:
if atype in some_types:
output(path, item_name, '')


def main():

print('==begin==')
init()

try:
traverse(mydir, '*root*')
except IOError:
print(' exception:', IOError)
f.close()
return

f.close()
print(msg_count, 'messages outputed')
print('==end==')


if __name__ == '__main__':
main()

PsYxOpAt mapper17/02/2014 15:23

Better use Python 2.7. Python 3+ is not that stable yet imo.

There is a problem with write permissions on the file. I added gfx/env path, .wav format in some_types variable and better output messages. I will see if I can extend this code these days.

#------------------------------------------------------------------------------- 
# Name: check_dirs.py
# Purpose: check the map directories of cs1.6
# Usage: modify in this file, and put this file into
# parent folder of . Run this, it will dump the
# questionable locations to analysis_msg.txt
# Created: 17/02/2014
#
# For Python 2.7
#-------------------------------------------------------------------------------


#coding=utf-8
import os, time

#! modify this before run
mydir = 'path\to\cstrike'

# structure table
dicts = {
'maps' : {'.bsp', '.res', '.pts'},
'gfx' : {'.bmp', '.tga'},
'gfx/env' : {'.bmp', '.tga'},
'sprites' : {'.spr'},
'models' : {'.mdl'},
'overviews' : {'.bmp', '.tga'},
'sound' : {'.wav'},
'*root*' : {'.wad'}
}

# give warining if these filetypes appear in other directories
some_types = {'.bsp', '.res', '.pts', '.spr', '.mdl', '.wad', '.wav'}

all_types = set()
msg_count = 0
f = open('analysis_msg.txt','w')


def init():
for item in dicts:
all_types.update(dicts[item])


def output(path, name, sub_dir):

global msg_count, dicts, some_types, all_types
msg_count += 1

asize = os.path.getsize(path)
atime = time.gmtime(os.path.getmtime(path))

print('\r')
print('Message:', msg_count)
print('File path: ' + path)
print('File size: ' + format(format(asize)) + ' bytes')
print('File created on: ' + time.strftime('%Y-%m-%d %H:%M:%S', atime))
if sub_dir != '':
print(dicts[sub_dir])
#print(f)


def traverse(root_dir, sub_dir):
for item_name in os.listdir(root_dir):
path = os.path.join(root_dir, item_name)

if os.path.isdir(path):
if sub_dir == '*root*':
traverse(path, item_name)
elif sub_dir == 'maps':
output(path, item_name, '')
else:
traverse(path, sub_dir)

else:
atype = os.path.splitext(item_name)[1]
if sub_dir in dicts:
if atype not in dicts[sub_dir] and atype in all_types:
output(path, item_name, sub_dir)
else:
if atype in some_types:
output(path, item_name, '')


def main():

print('==begin==')
init()

try:
traverse(mydir, '*root*')
except IOError:
print(' exception:', IOError)
f.close()
return

f.close()
print(msg_count, 'messages outputed')
print('==end==')


if __name__ == '__main__':
main()

Keo mapper17/02/2014 20:17

and where can we find such a wizard tool?

dEMolite mapper17/02/2014 23:52

you must use python which can execute scripts out of a cmd.

wjssz18/02/2014 02:39(edited 18/02/2014 02:56)

Stanimir Dimitrov
This is my first Python program, glad to see your improvement this poor tool.
:)


'gfx/env' : {'.bmp', '.tga'},

This will not work, because of the below part only recognize the first sub-directory

if os.path.isdir(path):
if sub_dir == '*root*':
traverse(path, item_name)
elif sub_dir == 'maps':
output(path, item_name, '')
else:
traverse(path, sub_dir)

wjssz18/02/2014 02:59

Another tool for generating map list


#-------------------------------------------------------------------------------
# Name: map_list.py
# Purpose: list the current maps of cs1.6
# Usage: modify in this file, and put this file into
# parent folder of . Run this, it will dump the
# list to map_list.txt
# Created: 17/02/2014
#
# For Python 3.x
#-------------------------------------------------------------------------------


#coding=utf-8
import os, time

# modify this before run
mydir = 'cstrike_downloads'

def main():
f = open('map_list.txt','w')
count = 0

for item_name in os.listdir(mydir+'\maps'):
path = os.path.join(mydir+'\maps', item_name)
if not os.path.isdir(path):
atype = os.path.splitext(item_name)[1]
if atype == '.bsp':
count += 1
asize = os.path.getsize(path)
atime = time.gmtime(os.path.getmtime(path))
print(item_name, ' [', count, ']\n', \
'{0:>12}'.format(format(asize,',')), 'bytes ', \
time.strftime('%Y-%m-%d %H:%M:%S', atime), \
'\n', file = f)
f.close()
print(count, 'maps found')

if __name__ == '__main__':
main()

PsYxOpAt mapper18/02/2014 06:35

@Keo, I will create an exe file these days.

Nokkan18/02/2014 12:20

Send me the correct code and I'll make an executable file

PsYxOpAt mapper18/02/2014 14:37

It's not finished yet. I will extend this code these days, probably at the weekend.

wjssz20/02/2014 05:13(edited 20/02/2014 05:14)

I add some output messages, hope it will help you understand what happened.
Feel free to modify it, you'd better post the final version to a new thread, this thread has many semi-finished codes.


#-------------------------------------------------------------------------------
# Name: check_dirs.py
# Purpose: check the map directories of cs1.6
# Usage: modify in this file, and put this file into
# parent folder of . Run this, it will dump the
# questionable locations to analysis_msg.txt
# Created: 17/02/2014
#
# For Python 3.x
#-------------------------------------------------------------------------------


#coding=utf-8
import os, time, builtins

#! modify this before run
mydir = 'cstrike_downloads'

# structure table
dicts = {
'maps' : {'.bsp', '.res', '.pts'},
'gfx' : {'.bmp', '.tga'},
'sprites' : {'.spr'},
'models' : {'.mdl'},
'overviews' : {'.bmp', '.tga'},
'sound' : {'.wav'},
'*root*' : {'.wad'}
}

# give warining if these filetypes appear in other directories
some_types = {'.bsp', '.res', '.pts', '.spr', '.mdl', '.wad', '.wav'}

all_types = set()
msg_count = 0
f = 0

def init():
for item in dicts:
all_types.update(dicts[item])


def output(path, sub_dir):
global msg_count, f

asize = os.path.getsize(path)
atime = time.gmtime(os.path.getmtime(path))
msg_count += 1

s = 'msg: ' + str(msg_count) + '\n'
s += path + '\n'
if sub_dir == '*maps*':
s += 'maps folder should not has sub-folder'
else:
s += '{0:>12}'.format(format(asize,',')) + ' bytes ' + \
time.strftime('%Y-%m-%d %H:%M:%S', atime)
s += '\n'

if sub_dir != '' and sub_dir in dicts:
s += 'this folder should be these types: ' + str(dicts[sub_dir])
else:
ts = os.path.splitext(path)[1]
s += ts + ' file in other directory'

s += '\n\n'

f.write(s);


def traverse(root_dir, sub_dir):
for item_name in os.listdir(root_dir):
path = os.path.join(root_dir, item_name)

if os.path.isdir(path):
if sub_dir == '*root*':
traverse(path, item_name)
elif sub_dir == 'maps':
output(path, '*maps*')
traverse(path, sub_dir)
else:
traverse(path, sub_dir)

else:
atype = os.path.splitext(item_name)[1]
if sub_dir in dicts:
if atype not in dicts[sub_dir] and atype in all_types:
output(path, sub_dir)
else:
if atype in some_types:
output(path, '')


def main():
global msg_count, f

print('==begin==')
init()

with open(mydir + '_anlys_msg.txt','w') as f:
traverse(mydir, '*root*')

print(msg_count, 'messages outputed')
print('==end==')


if __name__ == '__main__':
main()

wjssz10/03/2014 08:46

I modified them, they are much cleaner and readability than before.

check structure of map directories:


#-------------------------------------------------------------------------------
# Name: check_dirs.py
# Purpose: check the map directories of cs1.6
# Usage: modify in this file, and put this file into
# parent folder of . Run this, it will dump the
# questionable locations to analysis_msg.txt
# Created: 2014-03-10
#-------------------------------------------------------------------------------

#coding=utf-8
import os, time

# ! modify this before run
mydir = 'cstrike_downloads'

# structure table
dicts = {
'maps' : {'.bsp', '.res', '.pts'},
'gfx' : {'.bmp', '.tga'},
'sprites' : {'.spr'},
'models' : {'.mdl'},
'overviews' : {'.bmp', '.tga'},
'sound' : {'.wav'},
'*root*' : {'.wad'}
}

# give warining if these filetypes appear in other directories
some_types = {'.bsp', '.res', '.pts', '.spr', '.mdl', '.wad', '.wav'}
all_types = set()

def file_info(path):
asize = os.path.getsize(path)
atime = time.gmtime(os.path.getmtime(path))

ret = '{0:>12}'.format(format(asize,',')) + \
' bytes ' + \
time.strftime('%Y-%m-%d %H:%M:%S', atime)

return ret


def traverse(root_dir, sub_dir):
for item_name in os.listdir(root_dir):
path = os.path.join(root_dir, item_name)

if os.path.isdir(path): # is folder

if sub_dir == 'maps': # a subforder in maps forder
yield path + '\nmaps folder should not has sub-folder\n'

t_sub_dir = item_name if sub_dir == '*root*' else sub_dir
for r in traverse(path, t_sub_dir):
yield r

else: # is file
atype = os.path.splitext(item_name)[1].lower()

if sub_dir in dicts:
if atype not in dicts[sub_dir] and atype in all_types:
yield path + '\n' + \
file_info(path) + '\n' + \
'this folder should be these types: ' + \
str(dicts[sub_dir]).upper() + '\n'

else:
if atype in some_types:
yield path + '\n' + \
file_info(path) + '\n' + \
atype.upper() + ' file in other directory\n'


def main():
print('==begin==')

# init
msg_count = 0
for item in dicts:
all_types.update(dicts[item])

with open(mydir + '_anlys_msg.txt','w') as f:
for r in traverse(mydir, '*root*'):
f.write(r + '\n')
msg_count += 1

print(msg_count, 'messages outputed')
print('==end==')


if __name__ == '__main__':
main()


generate map list:

#-------------------------------------------------------------------------------
# Name: map_list.py
# Purpose: list the current maps of cs1.6
# Usage: modify in this file, and put this file into
# parent folder of . Run this, it will dump the
# list to map_list.txt
# Created: 2014-03-10
#-------------------------------------------------------------------------------


#coding=utf-8
import os, time

# modify this before run
mydir = 'cstrike_downloads'

count = 0

def get_list():
global count
dir = mydir+'\maps'

for item_name in os.listdir(dir):
path = os.path.join(dir, item_name)

if not os.path.isdir(path):
atype = os.path.splitext(item_name)[1].lower()
if atype == '.bsp':
count += 1
asize = os.path.getsize(path)
atime = time.gmtime(os.path.getmtime(path))
s = '{0:<30}'.format(item_name) + ' [' + str(count) + ']\n' + \
'{0:>12}'.format(format(asize,',')) + ' bytes '+ \
time.strftime('%Y-%m-%d %H:%M:%S', atime)+ \
'\n\n'
yield s


def main():
global count

with open(mydir+'_maplist.txt','w') as f:
for msg in get_list():
f.write(msg)

print(count, 'maps found')

if __name__ == '__main__':
main()

dEMolite mapper10/03/2014 15:28

pts can be deleted since none else then the mapper needs it.

Page 1 of 1

Please log in to post replies.