Advertisement
rPoXoTauJIo

kits_script.py

Jan 23rd, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.24 KB | None | 0 0
  1. import os
  2. import sys
  3. import zipfile
  4. import time
  5.  
  6. path_kits_zipname = 'objects_common_server.zip'
  7. path_mapserver_zipname = 'server.zip'
  8.  
  9. path_content = 'content'
  10. path_levels = 'levels'
  11.  
  12. path_kits = 'kits'
  13. path_gamemodes = 'gamemodes'
  14.  
  15. gamemodes = [
  16.     'gpm_cq',
  17.     'gpm_skirmish',
  18.     'gpm_insurgency',
  19.     'gpm_coop',
  20.     'gpm_cnc',
  21.     'gpm_vehicles',
  22.     'gpm_objective',
  23.     'sp1',
  24.     'sp2',
  25.     'sp3',
  26.     ]
  27. layers = [
  28.     '16',
  29.     '32',
  30.     '64',
  31.     '128',
  32.     ]
  33.  
  34. kits_list = []
  35. #maps_kits_details = {} # 'dirname' : {'gamemode':{'layer':{'templates: {'cpname_*: object'}, 'spawners' : {'cpname_*: object'}}}}
  36.  
  37. def debug_data(maps_kits_details):
  38.  
  39.     fo = open('kits_topas.txt', 'w')
  40.    
  41.     for mapname in maps_kits_details:
  42.         #print('checking map {} from {}'.format(mapname, maps_kits_details.keys()))
  43.         for gamemode in maps_kits_details[mapname]:
  44.             #print('checking gamemode {}'.format(gamemode))
  45.             for layer in maps_kits_details[mapname][gamemode]:
  46.                 #print('checking layer {}'.format(layer))
  47.                 #print('--{}/{}/{}'.format(mapname, gamemode, layer))
  48.                 #print('maps_kits_details[{}][{}][{}] = \n{}\n\n'.format(mapname, gamemode, layer, maps_kits_details[mapname][gamemode][layer]))
  49.                 #print('maps_kits_details[mapname][gamemode][0] = \n{}'.format(maps_kits_details[mapname][gamemode][0]))
  50.                 if len(maps_kits_details[mapname][gamemode][layer].spawners) != 0:
  51.                     fo.write('--------------------------------------------------------------------------------\n')
  52.                     fo.write('{}[{}][{}]:\n\n'.format(mapname, gamemode, layer))
  53.                 for spawner in maps_kits_details[mapname][gamemode][layer].spawners:
  54.                     spawner_template_name = maps_kits_details[mapname][gamemode][layer].spawners[spawner].spawner_name
  55.                     spawner_position = maps_kits_details[mapname][gamemode][layer].spawners[spawner].position
  56.                     spawner_template = maps_kits_details[mapname][gamemode][layer].templates[spawner_template_name]#.spawner_object_template
  57.                     string_create = 'Object.create {}\nObject.absolutePosition {}/{}/{}'.format(spawner_template_name,*spawner_position)
  58.                     string_template = 'ObjectTemplate.setObjectTemplate {} {}'.format(spawner_template.spawner_team, spawner_template.spawner_object_template)
  59.                     print(string_create)
  60.                     print(string_template + '\n')
  61.                     fo.write(string_create + '\n')
  62.                     fo.write(string_template + '\n\n')
  63.                    
  64.     fo.close()
  65.  
  66.  
  67. def set_maps_kits_details():
  68.     maps_kits_details = {}
  69.     for dirname, dirnames, filenames in os.walk(path_levels):
  70.         if path_mapserver_zipname in filenames:
  71.             map_details = get_maps_kits_data(os.path.join(dirname, path_mapserver_zipname))
  72.             #print('checking {}'.format(os.path.join(dirname, path_mapserver_zipname)))
  73.             levelname = dirname.split('\\')[-1]
  74.             maps_kits_details[levelname] = {}
  75.             maps_kits_details[levelname] = map_details
  76.             print('parsed {}, len = ({})'.format(levelname, len(maps_kits_details)))
  77.             #break
  78.     return maps_kits_details
  79.  
  80. def get_maps_kits_data(maparchive):
  81.  
  82.     class MapDetails:
  83.         def __init__(self, templates, spawners):
  84.             self.templates = templates
  85.             self.spawners = spawners
  86.  
  87.     map_details = {}
  88.     with zipfile.ZipFile(maparchive) as mapzip:
  89.         filelist = mapzip.namelist()
  90.         for filename in filelist:
  91.             #print(os.path.join(mapzip, filename))
  92.             if filename.split('/')[0].lower() == path_gamemodes and filename.split('.')[-1] == 'con' and filename.split('.')[0].split('/')[-1].lower() == 'gameplayobjects':
  93.                 with mapzip.open(filename, 'r') as gpofile:
  94.                     newlines = []
  95.                     for line in gpofile:
  96.                         newlines.append(line.decode("utf-8").strip())
  97.                     gpofile.close()
  98.                 #print(os.path.join(maparchive, filename))
  99.                 templates, spawners = get_map_kits_data(newlines)
  100.                 gamemode = filename.split('.')[0].split('/')[-3].lower()
  101.                 layer = filename.split('.')[0].split('/')[-2].lower()
  102.                 if gamemode in gamemodes and layer in layers:
  103.                     map_details[gamemode] = {}
  104.                     map_details[gamemode][layer] = MapDetails(templates, spawners)
  105.                 else:
  106.                     print('[{}][{}]not in shit!!!'.format(gamemode, layer))
  107.                     print('gamemodes = [')
  108.                     for g in gamemodes:
  109.                         print('    {}'.format(g))
  110.                     print('    ]')
  111.                     time.sleep(1)
  112.                     print(os.path.join(maparchive, filename))
  113.                     time.sleep(2)
  114.                     #mapzip.close()
  115.                     #sys.exit()
  116.         mapzip.close()
  117.     return map_details
  118.  
  119. def get_map_kits_data(lines):
  120.    
  121.     spawner_templates = {}
  122.     spawner_objects = {}
  123.     active_object = [None]
  124.    
  125.     class ObjectSpawnerTemplate:
  126.         def __init__(self, spawner_name, start):
  127.             self.start = start
  128.             self.spawner_name = spawner_name.replace('\n', '').replace('\r', '')
  129.             self.spawner_object_template = None
  130.             self.spawner_team = None
  131.             self.active = False
  132.  
  133.         def set_data(self):
  134.             if active_object[0] != None:
  135.                 active_object[0].active = False
  136.             active_object[0] = self
  137.             active_object[0].active = True
  138.  
  139.             line_id = self.start
  140.             while self.active == True and len(lines) > line_id:
  141.                 line = lines[line_id]
  142.                 if line.lower().startswith('objecttemplate.setobjecttemplate'):
  143.                     spawner_template_parts = line.split(' ')
  144.                     self.spawner_object_template = spawner_template_parts[-1].replace('\n', '').replace('\r', '')
  145.                     self.spawner_team = spawner_template_parts[-2]
  146.                     self.active = False
  147.                 line_id += 1
  148.  
  149.     class ObjectTemplate:
  150.         def __init__(self, spawner_name, start):
  151.             self.start = start
  152.             self.spawner_name = spawner_name.replace('\n', '').replace('\r', '')
  153.             self.position = (None, None, None)
  154.             self.active = False
  155.  
  156.         def set_data(self):
  157.             if active_object[0] != None:
  158.                 active_object[0].active = False
  159.             active_object[0] = self
  160.             active_object[0].active = True
  161.  
  162.             line_id = self.start
  163.             while self.active == True and len(lines) > line_id:
  164.                 line = lines[line_id]
  165.                 if line.lower().startswith('object.absoluteposition'):
  166.                     object_spawner_parts = line.replace('\n', '').replace('\r', '').split(' ')
  167.                     self.position = object_spawner_parts[-1].split('/')
  168.                     self.active = False
  169.                 line_id += 1
  170.                
  171.     counter = 0
  172.     for line in lines:
  173.         counter += 1
  174.         #print(line)
  175.         #time.sleep(0.5)
  176.         line_parts = line.split(' ')
  177.         if len(line_parts) > 2:
  178.             if line_parts[0].lower() == 'objecttemplate.create':
  179.                 if line_parts[1].lower() == 'objectspawner':
  180.                     spawner_template = ObjectSpawnerTemplate(line_parts[2], counter)
  181.                     spawner_template.set_data()
  182.                     #print('[{}]{}'.format(counter, line))
  183.                     #print('[{}]ObjectTemplate.setObjectTemplate {} {}'.format(counter, spawner_template.spawner_team, spawner_template.spawner_object_template))
  184.                     #time.sleep(1)
  185.                     if spawner_template.spawner_object_template in kits_list:
  186.                         spawner_templates[spawner_template.spawner_name] = spawner_template
  187.                     #print('class template = "{}"'.format(spawner_template.spawner_object_template))
  188.     counter = 0
  189.     for line in lines:
  190.         counter += 1
  191.         line_parts = line.strip().split(' ')
  192.         if len(line_parts) >= 2:
  193.             #print(line_parts)
  194.             #time.sleep(0.02)
  195.             if line_parts[0].lower() == 'object.create':
  196.                 #print('spawning1 {}'.format(line_parts[1]))
  197.                 #print('len of templates = [{}]'.format(len(spawner_templates)))
  198.                 if line_parts[1] in spawner_templates.keys():
  199.                     #print('spawning2 {}'.format(line_parts[1]))
  200.                     #time.sleep(1)
  201.                     spawner = ObjectTemplate(line_parts[1], counter)
  202.                     spawner.set_data()
  203.                     spawner_objects[spawner.spawner_name] = spawner
  204.                     #print('[{}]{}'.format(counter, line))
  205.                     #print('[{}]Object.absolutePosition {}/{}/{}'.format(counter, *spawner.position))
  206.     return (spawner_templates, spawner_objects)
  207.  
  208.    
  209.    
  210. def set_kits_templates():
  211.     with zipfile.ZipFile(os.path.join(path_content, path_kits_zipname)) as contentzip:
  212.         filelist = contentzip.namelist()
  213.         for filename in filelist:
  214.             if filename.split('/')[0] == path_kits and filename.split('.')[-1] == 'con':
  215.                 with contentzip.open(filename, 'r') as kitfile:
  216.                     lines = kitfile.read().splitlines()
  217.                     kitfile.close()
  218.                 for line in lines:
  219.                     line = line.decode("utf-8")
  220.                     new_line = ' '.join(line.split(' ')[0:2])
  221.                     #print(new_line)
  222.                     if new_line == 'ObjectTemplate.create Kit':
  223.                         add_kit(line)
  224.                     #time.sleep(1)
  225.         contentzip.close()
  226.     print("Added {} kits to checklist".format(len(kits_list)))
  227.  
  228. def add_kit(string):
  229.     global kits_list
  230.    
  231.     kit_parts = string.split(' ')
  232.     kit = kit_parts[-1]
  233.     kits_list.append(kit)
  234.     #if kit.startswith('meinsurgent'):
  235.     #    print("Added kit ['{}'] to list".format(kit))
  236.  
  237.  
  238. def main():
  239.     set_kits_templates()
  240.     dataset = set_maps_kits_details()
  241.     debug_data(dataset)
  242.  
  243. if __name__ == '__main__':
  244.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement