예제 #1
0
def main():
    parser = argparse.ArgumentParser(description='Automate configuration')
    parser.add_argument('-c', dest='clean', action='store_true', help='clean generated')
    parser.add_argument('-d', dest='deploy', required=True,
                        help='deploy directory containing the original dayz server files')
    parser.add_argument('-a', dest='auth', required=True, help='auth config file')
    parser.add_argument('-o', dest='output', help='output destination directory', default=outdir.get())
    parser.add_argument('-v', dest='verbosity', help='verbosity of the output', action='count', default=0)
    parser.add_argument('-r', dest='resources', help='resources directory to use', default=resourcesdir.get())
    parser.add_argument('-m', dest='mission', help='mission name', default=mission.get())
    args = parser.parse_args()

    log_level = logging.INFO
    if args.verbosity > 0:
        log_level = logging.DEBUG
    logging.basicConfig(level=log_level, format='%(asctime)s %(levelname)s %(name)s - %(message)s')
    log = logging.getLogger(__name__)

    if args.clean:
        clean()

    # Initialize all the global resources
    outdir.set(args.output)
    deploydir.set(args.deploy)
    resourcesdir.set(args.resources)
    mission.set(args.mission)
    types.set(ElementTree.parse(pathlib.Path(deploydir.get(), 'mpmissions', mission.get(), 'db/types.xml')))
    auth.set(json.load(open(args.auth)))
    locations.initialize()
    with open(pathlib.Path(resourcesdir.get(), 'modifications/omega/mods.json')) as f:
        mods_config = json.load(f)
    for mod in mods_config:
        mods.add(mod.get('directory'))

    # Scan the configurations and apply them
    configuration.scan()
    for c in configs.get():
        c()
    # Unregistered configurations that have a special order requirement
    from carim.configuration.mods.trader import trader
    from carim.configuration.server import missions
    from carim.configuration.server import types as modify_types
    modify_types.modify_types()
    missions.sort_and_write_types_config()
    trader.trader_items()

    log.info('errors {}'.format(len(errors.get())))
    for e in errors.get():
        log.error(e)
    log.info('processed {} registered configurations'.format(len(configs.get())))
    log.info('skipped {} configurations'.format(configs.get_skipped()))
    log.debug('order: {}'.format(json.dumps([f.__name__ for f in configs.get()], indent=2)))
    log.info('complete')
예제 #2
0
def random_presets_config(directory):
    p = pathlib.Path(deploydir.get(), 'mpmissions', mission.get(),
                     'cfgrandompresets.xml')
    with open(p) as f:
        # The parser in ElementTree was having trouble with the comments in the xml file
        # So, we're removing them manually beforehand
        raw = f.read()
        raw = re.sub(r'<!--.*-->', '', raw)
        presets_xml = ElementTree.fromstring(raw)
    with open(
            pathlib.Path(resourcesdir.get(),
                         'modifications/server/random_presets.json')) as f:
        presets_modifications = json.load(f)
    for entry in presets_modifications:
        if 'cargo' in entry:
            cargo = entry.get('cargo')
            if 'chance' in entry:
                for preset in presets_xml.findall(
                        f'.//cargo[@name="{cargo}"]'):
                    preset.set('chance', entry.get('chance'))
            for item in entry.get('items'):
                for preset in presets_xml.findall(
                        './/cargo[@name="{}"]//item[@name="{}"]'.format(
                            cargo, item.get('name'))):
                    preset.set('chance', item.get('chance'))
        if 'attachments' in entry:
            attachments = entry.get('attachments')
            for item in entry.get('items'):
                for preset in presets_xml.findall(
                        './/attachments[@name="{}"]//item[@name="{}"]'.format(
                            attachments, item.get('name'))):
                    preset.set('chance', item.get('chance'))
    with file_writing.f_open(pathlib.Path(directory, 'cfgrandompresets.xml'),
                             mode='w') as f:
        f.write(file_writing.convert_to_string(presets_xml))
예제 #3
0
def remove_spawns_outside_radius(directory):
    with open(
            pathlib.Path(resourcesdir.get(),
                         'modifications/server/types_universal.json')) as f:
        type_universal_config = json.load(f)
    radius = type_universal_config.get('limit_spawn_locations_radius', 0)
    if radius <= 0:
        return
    p = pathlib.Path(deploydir.get(), 'mpmissions', mission.get(),
                     'mapgrouppos.xml')
    count = 0
    areas = list(mark[1] for mark in locations.marks[:4])
    mapgroups = ElementTree.parse(p).getroot()
    for group in mapgroups.findall('.//group'):
        raw = group.get('pos')
        # log.info('{} {}'.format(group.get('name'), raw))
        x, y, z = (float(i) for i in raw.split(' '))
        is_good = False
        for position in areas:
            if locations.overlaps(position, radius, x, z, 1):
                is_good = True
        if not is_good:
            mapgroups.remove(group)
            count += 1
            log.debug('removed group {}, {}, {}'.format(
                group.get('name'), x, z))

    if count > 0:
        log.info('removed {} groups from {}'.format(count, p.name))
    with file_writing.f_open(pathlib.Path(directory, p.name), mode='w') as f:
        f.write(file_writing.convert_to_string(mapgroups))
예제 #4
0
def spawnable_types_config(directory):
    """
    This configuration operates in an overriding fashion. That is, if a preset or list of items is specified
    for a type in the config, any existing entries for that type with the same tag and attribute will be erased.
    """
    p = pathlib.Path(deploydir.get(), 'mpmissions', mission.get(), 'cfgspawnabletypes.xml')
    with open(p) as f:
        # The parser in ElementTree was having trouble with the comments in the xml file
        # So, we're removing them manually beforehand
        raw = f.read()
        raw = re.sub(r'<!--.*-->', '', raw)
        spawnable_xml = ElementTree.fromstring(raw)
    with open(pathlib.Path(resourcesdir.get(), 'modifications/server/spawnable_types.json')) as f:
        spawnable_modifications = json.load(f)
    for type_config in spawnable_modifications:
        if len(db_types.get().getroot().findall('.//type[@name="{}"]'.format(type_config.get('type')))) == 0:
            continue
        types = spawnable_xml.findall('.//type[@name="{}"]'.format(type_config.get('type')))
        if len(types) == 0:
            new_type = ElementTree.SubElement(spawnable_xml, 'type', dict(name=type_config.get('type')))
            types = [new_type]
        for t in types:
            if 'cargo_presets' in type_config:
                handle_presets(t, 'cargo', type_config.get('cargo_presets'))
            if 'attachments_presets' in type_config:
                handle_presets(t, 'attachments', type_config.get('attachments_presets'))
            if 'cargo_items' in type_config:
                handle_items(t, 'cargo', type_config.get('cargo_items'))
            if 'attachments' in type_config:
                handle_items(t, 'attachments', type_config.get('attachments'))
    with file_writing.f_open(pathlib.Path(directory, 'cfgspawnabletypes.xml'), mode='w') as f:
        f.write(file_writing.convert_to_string(spawnable_xml))
예제 #5
0
def globals_config(directory):
    globals_xml = ElementTree.parse(
        pathlib.Path(deploydir.get(), 'mpmissions', mission.get(), 'db/globals.xml'))
    with open(pathlib.Path(resourcesdir.get(), 'modifications/server/globals.json')) as f:
        globals_modifications = json.load(f)
    for k, v in globals_modifications.items():
        item = globals_xml.getroot().find('.//var[@name="{}"]'.format(k))
        item.set('value', str(v))
    with file_writing.f_open(pathlib.Path(directory, 'globals.xml'), mode='w') as f:
        f.write(file_writing.convert_to_string(globals_xml.getroot()))
예제 #6
0
def territory_config(directory):
    with open(pathlib.Path(resourcesdir.get(), 'modifications/server/territories.json')) as f:
        territories_modifications = json.load(f)
    for p in pathlib.Path(deploydir.get(), 'mpmissions', mission.get(), 'env').glob('*.xml'):
        filename = p.name
        territory = ElementTree.parse(p).getroot()

        if filename in territories_modifications.get('ratio', dict()):
            ratio = territories_modifications.get('ratio').get(filename)
            log.info('applying ratio of {} to {}'.format(ratio, filename))
            for zone in territory.findall('.//zone'):
                dmin = zone.get('dmin')
                dmax = zone.get('dmax')
                zone.set('dmin', str(math.floor(int(dmin) * ratio)))
                zone.set('dmax', str(math.floor(int(dmax) * ratio)))

        if filename in territories_modifications.get('radius_ratio', dict()):
            ratio = territories_modifications.get('radius_ratio').get(filename)
            log.info('applying radius ratio of {} to {}'.format(ratio, filename))
            for zone in territory.findall('.//zone'):
                r = zone.get('r')
                zone.set('r', str(math.floor(float(r) * ratio)))

        if filename in territories_modifications.get('blanket', dict()):
            params = territories_modifications.get('blanket').get(filename)
            new_territory = ElementTree.Element('territory', attrib=dict(color='1910952871'))
            count = 0
            for x in range(500, 12600, 1000):
                for z in range(3750, 14850, 1000):
                    count += 1
                    ElementTree.SubElement(new_territory, 'zone', attrib={
                        'name': params.get('name'),
                        'smin': '0',
                        'smax': '0',
                        'dmin': params.get('dmin'),
                        'dmax': params.get('dmax'),
                        'x': str(x),
                        'z': str(z),
                        'r': '500'
                    })
            territory.append(new_territory)
            log.info('added {} zones in a blanket to {}'.format(count, filename))

        if '@Trader' in mods.get():
            remove_zones_if_near_traders(territory, filename)

        with file_writing.f_open(pathlib.Path(directory, filename), mode='w') as f:
            f.write(file_writing.convert_to_string(territory))
예제 #7
0
def events_config(directory):
    events_xml = ElementTree.parse(
        pathlib.Path(deploydir.get(), 'mpmissions', mission.get(), 'db/events.xml'))
    with open(pathlib.Path(resourcesdir.get(), 'modifications/server/events.json')) as f:
        events_modifications = json.load(f)
    for mod in events_modifications:
        name_re = re.compile(mod.get('name'))
        for event in events_xml.getroot():
            if name_re.match(event.get('name')):
                ratio = mod.get('ratio')
                if ratio > 0:
                    event.find('active').text = '1'
                    for item in ('nominal',):
                        i = event.find(item)
                        i.text = str(math.floor(max(1, int(i.text)) * ratio))
                else:
                    event.find('active').text = '0'
    with file_writing.f_open(pathlib.Path(directory, 'events.xml'), mode='w') as f:
        f.write(file_writing.convert_to_string(events_xml.getroot()))
예제 #8
0
def remove_building_spawns_near_traders(directory):
    p = pathlib.Path(deploydir.get(), 'mpmissions', mission.get(), 'mapgrouppos.xml')
    count = 0
    mapgroups = ElementTree.parse(p).getroot()
    for group in mapgroups.findall('.//group'):
        raw = group.get('pos')
        # log.info('{} {}'.format(group.get('name'), raw))
        x, y, z = (float(i) for i in raw.split(' '))
        clean_traders = (mark[1] for mark in locations.marks[:4])
        for position in clean_traders:
            if locations.overlaps(position, 200, x, z, 1):
                find_string = './/group[@pos="{}"]...'.format(raw)
                parents = mapgroups.findall(find_string)
                for parent in parents:
                    if group in parent:
                        count += 1
                        parent.remove(group)
                        log.debug('removed group {}, {}, {}'.format(group.get('name'), x, z))
                break
    if count > 0:
        log.info('removed {} groups from {}'.format(count, p.name))
    with file_writing.f_open(pathlib.Path(directory, p.name), mode='w') as f:
        f.write(file_writing.convert_to_string(mapgroups))
예제 #9
0
        "value": True
    }, {
        "name": "count_in_hoarder",
        "value": True
    }, {
        "name": "count_in_map",
        "value": True
    }, {
        "name": "count_in_player",
        "value": True
    }],
    "value": []
}


@decorators.server(directory='mpmissions/' + mission.get() + '/db')
def modify_types(directory):
    with open(
            pathlib.Path(resourcesdir.get(),
                         'modifications/server/types_config.json')) as f:
        type_config = json.load(f)
    for action in type_config:
        log.info(action.get('description'))
        matching = action.get('matching')
        if action['action'] == 'remove':
            process_type = remove
        elif action['action'] == 'ratio':
            process_type = ratio
        else:
            process_type = modify
        for m in matching:
예제 #10
0
def mission(_func=None, *, directory='.'):
    return located_config(_func,
                          directory=directory,
                          dir_prefix='servers/0/mpmissions/' +
                          mission_name.get())