Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pickle
- from warnings import warn
- from path import Path
- from commands.server import ServerCommand
- from core import GAME_NAME
- from core import SOURCE_ENGINE
- from engines.server import engine_server
- from engines.server import global_vars
- from entities.entity import Entity
- from entities.factories import factory_dictionary
- from entities.helpers import index_from_edict
- from entities.helpers import remove_entity
- from filters.entities import EntityIter
- from filters.players import PlayerIter
- from players.bots import bot_manager
- from players.entity import PlayerEntity
- # Set to the path where all virtuals are held
- # Virtual files should be in <path>/<GAME_NAME>/server/ directory
- VIRTUAL_PATH = Path('E:/Virtuals')
- DUMP_BASE_PATH = Path(__file__).parent
- ENGINE_DUMP_PATH = DUMP_BASE_PATH.joinpath(SOURCE_ENGINE)
- if not ENGINE_DUMP_PATH.isdir():
- ENGINE_DUMP_PATH.mkdir()
- GAME_DUMP_FILE = ENGINE_DUMP_PATH.joinpath(GAME_NAME + '.db')
- ENGINE_DUMP_FILE = DUMP_BASE_PATH.joinpath(SOURCE_ENGINE + '.db')
- ALL_FILE = DUMP_BASE_PATH.joinpath('all.db')
- value_types = ('virtual_function', 'input', 'keyvalue', 'property')
- class NoMapLoadedError(Exception):
- """No map is currently loaded."""
- if not global_vars.map_name:
- raise NoMapLoadedError(NoMapLoadedError.__doc__)
- def get_virtuals(server_class):
- game_virtuals = VIRTUAL_PATH.joinpath(GAME_NAME, 'server')
- class_virtuals = list()
- if not game_virtuals.isdir():
- warn('No virtuals for game "{0}"'.format(GAME_NAME))
- return None
- class_file = game_virtuals.joinpath(server_class + '.txt')
- if not class_file.isfile():
- warn('No virtuals for class "{0}" in game "{1}"'.format(
- server_class, GAME_NAME))
- return None
- start = tuple('_ZN{0}{1},_ZNK{0}{1}'.format(
- len(server_class), server_class).split(','))
- with class_file.open() as open_file:
- contents = open_file.readlines()
- for line in contents:
- line = line.strip()
- if line.split()[1].startswith(start):
- class_virtuals.append(line)
- return class_virtuals
- server_classes = dict()
- for classname in sorted(factory_dictionary):
- created = False
- if classname == 'worldspawn':
- entity = Entity(0)
- elif classname == 'player':
- for entity in PlayerIter(return_types='player'):
- break
- else:
- created = True
- entity = PlayerEntity(index_from_edict(
- bot_manager.create_bot('test_bot')))
- else:
- for entity in EntityIter(classname, True, 'entity'):
- break
- else:
- try:
- entity = Entity.create(classname)
- created = True
- except (OverflowError, ValueError):
- continue
- if classname != entity.classname:
- continue
- for server_class in entity.server_classes:
- name = server_class.__name__
- if name in server_classes:
- continue
- server_classes[name] = {
- 'virtual_function': get_virtuals(name),
- 'input': list(server_class.inputs),
- 'keyvalue': list(server_class.keyvalues),
- 'property': list(server_class.properties),
- }
- if created:
- if classname == 'player':
- engine_server.server_command('kickid {0};'.format(entity.userid))
- else:
- print('\tRemoving:', classname, entity.index)
- #remove_entity(entity.index)
- print('DONE!!!')
- with GAME_DUMP_FILE.open('wb') as open_file:
- pickle.dump(server_classes, open_file)
- print('dumped')
- @ServerCommand('compare')
- def compare_databases(command):
- try:
- compare_type = command[1]
- except IndexError:
- print('No type given...')
- return
- if compare_type not in ('games', 'engines'):
- print('Given type must be "games" or "engines"')
- return
- if compare_type == 'games':
- compare_games()
- else:
- compare_engines()
- def compare_games():
- for engine_dir in DUMP_BASE_PATH.dirs():
- if engine_dir.namebase in ('__pycache__', 'results'):
- continue
- for start_file in engine_dir.files():
- if not VIRTUAL_PATH.joinpath(start_file.namebase).isdir():
- continue
- with start_file.open('rb') as open_file:
- result = pickle.load(open_file)
- break
- else:
- warn('No virtuals for engine "{0}"'.format(engine_dir.namebase))
- continue
- for file in engine_dir.files():
- if file == start_file:
- continue
- with file.open('rb') as open_file:
- current = pickle.load(open_file)
- for server_class in list(result):
- if server_class not in current:
- del result[server_class]
- else:
- for key in value_types:
- if current[server_class][key] is None:
- continue
- for value in list(result[server_class][key]):
- if value not in current[server_class][key]:
- result[server_class][key].remove(value)
- engine_file = DUMP_BASE_PATH.joinpath(engine_dir.namebase + '.db')
- with engine_file.open('wb') as open_file:
- pickle.dump(result, open_file)
- def compare_engines():
- if not ENGINE_DUMP_FILE.isfile():
- print('Engine dump file not written!!!')
- return
- with ENGINE_DUMP_FILE.open('rb') as open_file:
- result = pickle.load(open_file)
- for file in DUMP_BASE_PATH.files('*.db'):
- if file.namebase == SOURCE_ENGINE:
- continue
- with file.open('rb') as open_file:
- current = pickle.load(open_file)
- for server_class in list(result):
- if server_class not in current:
- del result[server_class]
- else:
- for key in value_types:
- if current[server_class][key] is None:
- continue
- for value in list(result[server_class][key]):
- if value not in current[server_class][key]:
- result[server_class][key].remove(value)
- with ALL_FILE.open('wb') as open_file:
- pickle.dump(result, open_file)
- @ServerCommand('dump_attributes')
- def dump_attributes(command):
- if not ALL_FILE.isfile():
- print('No engine compare file created!!!')
- return
- result_dir = DUMP_BASE_PATH.joinpath('results')
- if result_dir.isdir():
- print('Remove results directory before proceding!!')
- return
- result_dir.mkdir()
- with ALL_FILE.open('rb') as open_file:
- all_contents = pickle.load(open_file)
- for server_class in all_contents:
- file = result_dir.joinpath(server_class + '.txt')
- with file.open('w') as open_file:
- for key in value_types:
- if not all_contents[server_class][key]:
- continue
- open_file.write('{0}\n'.format(key))
- for value in sorted(all_contents[server_class][key]):
- open_file.write(' {0}\n'.format(value))
- with file.open() as open_file:
- contents = open_file.read()
- if not len(contents):
- file.remove()
- for engine in DUMP_BASE_PATH.dirs():
- if engine.namebase in ('__pycache__', 'results'):
- continue
- engine_dir = result_dir.joinpath(engine.namebase)
- engine_dir.mkdir()
- engine_file = DUMP_BASE_PATH.joinpath(engine.namebase + '.db')
- with engine_file.open('rb') as open_file:
- engine_contents = pickle.load(open_file)
- for server_class in engine_contents:
- file = engine_dir.joinpath(server_class + '.txt')
- with file.open('w') as open_file:
- for key in value_types:
- if engine_contents[server_class][key] is None:
- continue
- contents = set(engine_contents[server_class][key])
- contents.difference_update(set(all_contents.get(
- server_class, {}).get(key, list())))
- if not contents:
- continue
- open_file.write('{0}\n'.format(key))
- for value in sorted(contents):
- open_file.write(' {0}\n'.format(value))
- with file.open() as open_file:
- contents = open_file.read()
- if not len(contents):
- file.remove()
- for game in engine.files():
- game_dir = engine_dir.joinpath(game.namebase)
- game_dir.mkdir()
- game_file = DUMP_BASE_PATH.joinpath(
- engine.namebase, game.namebase + '.db')
- with game_file.open('rb') as open_file:
- game_contents = pickle.load(open_file)
- for server_class in game_contents:
- file = game_dir.joinpath(server_class + '.txt')
- with file.open('w') as open_file:
- for key in value_types:
- if game_contents[server_class][key] is None:
- continue
- contents = set(game_contents[server_class][key])
- contents.difference_update(set(engine_contents.get(
- server_class, {}).get(key, list())))
- if not contents:
- continue
- open_file.write('{0}\n'.format(key))
- for value in sorted(contents):
- open_file.write(' {0}\n'.format(value))
- with file.open() as open_file:
- contents = open_file.read()
- if not len(contents):
- file.remove()
- if not len(game_dir.files()):
- game_dir.rmdir()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement