示例#1
0
 def try_parse(self, text):
     try:
         self.cmds_run.append(text)
         if len(self.cmds_run) > 10:
             self.cmds_run.pop(0)
         return Command.run_commands([Command(text)])
     except ParsingException as e:
         AutoFix.get().error(str(e))
         return False
示例#2
0
 def _end_loading(self):
     mdl0 = self.mdl0
     import_path_map = self.__normalize_image_path_map(
         self.import_textures_map)
     self._import_images(import_path_map)
     mdl0.rebuild_header()
     self.brres.add_mdl0(mdl0)
     if self.is_map:
         mdl0.add_map_bones()
     os.chdir(self.cwd)
     if self.ENCODE_PRESET:
         if not self.ENCODE_PRESET_ON_NEW or (
                 self.replacement_model is None
                 and self.json_polygon_encoding is None):
             Command('preset ' + self.ENCODE_PRESET + ' for * in ' +
                     self.brres.name + ' model ' +
                     self.mdl0.name).run_cmd()
     AutoFix.info('\t... finished in {} secs'.format(
         round(time.time() - self.start, 2)))
     if self.encoder:
         self.encoder.after_encode(mdl0)
     return mdl0
示例#3
0
 def test_load(self):
     self.assertTrue(Command('load {}'.format(self._get_test_fname('presets.txt'))).run_cmd())
     self.assertTrue(Command.PRESETS.get('load_preset'))
示例#4
0
def load_config(app_dir=None, loudness=None, autofix_level=None):
    if app_dir is None:
        app_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)),
                               'etc', 'abmatt')
    conf = Config.get_instance(os.path.join(app_dir, 'config.conf'))
    tmp_dir = os.path.join(app_dir, 'temp_files')
    converter = ImgConverter(tmp_dir)
    Tex0.converter = converter
    if not loudness:
        loudness = conf['loudness']
    if loudness:
        try:
            AutoFix.set_loudness(loudness)
        except ValueError:
            AutoFix.warn('Invalid loudness level {}'.format(loudness))
    AutoFix.set_fix_level(autofix_level, turn_off_fixes)
    if not len(conf):
        AutoFix.warn('No configuration detected (etc/abmatt/config.conf).')
        return
    Command.set_max_brres_files(conf)
    # Matching stuff
    MATCHING.set_case_sensitive(conf['case_sensitive'])
    MATCHING.set_partial_matching(conf['partial_matching'])
    MATCHING.set_regex_enable(conf['regex_matching'])
    # Autofixes
    try:
        SubFile.FORCE_VERSION = validBool(conf['force_version'])
    except ValueError:
        pass
    try:
        Brres.REMOVE_UNUSED_TEXTURES = validBool(
            conf['remove_unused_textures'])
    except ValueError:
        pass
    try:
        Layer.MINFILTER_AUTO = validBool(conf['minfilter_auto'])
    except ValueError:
        pass
    set_rename_unknown(conf['rename_unknown_refs'])
    set_remove_unknown(conf['remove_unknown_refs'])
    set_remove_unused(conf['remove_unused_refs'])
    try:
        Mdl0.DETECT_MODEL_NAME = validBool(conf['detect_model_name'])
    except ValueError:
        pass
    try:
        Shader.MAP_ID_AUTO = validBool(conf['map_id_auto'])
    except ValueError:
        pass
    try:
        Material.DEFAULT_COLOR = parse_color(conf['default_material_color'])
    except ValueError:
        pass
    try:
        Tex0.RESIZE_TO_POW_TWO = validBool(conf['resize_pow_two'])
    except ValueError:
        pass
    try:
        Tex0.set_max_image_size(validInt(conf['max_image_size'], 0, 10000))
    except (TypeError, ValueError):
        pass
    try:
        Geometry.ENABLE_VERTEX_COLORS = validBool(conf['enable_vertex_colors'])
    except ValueError:
        pass
    Converter.ENCODE_PRESET = conf['encode_preset']
    resample = conf['img_resample']
    if resample is not None:
        ImgConverterI.set_resample(resample)
    if conf['material_library']:
        MaterialLibrary.LIBRARY_PATH = conf.config.get('material_library')
    else:
        MaterialLibrary.LIBRARY_PATH = os.path.join(app_dir, 'mat_lib.brres')
    return conf
示例#5
0
def parse_args(argv, app_dir):
    interactive = overwrite = debug = False
    type = ""
    cmd_args = None
    command = destination = brres_file = command_file = model = value = key = ""
    autofix = loudness = None
    name = None
    no_normals = no_colors = single_bone = no_uvs = moonview = patch = False
    do_help = False
    for i in range(len(argv)):
        if argv[i][0] == '-':
            if i != 0:
                cmd_args = argv[:i]
                argv = argv[i:]
            break

    try:
        opts, args = getopt.gnu_getopt(argv, "ahd:oc:t:k:v:n:b:m:f:iul:g", [
            "auto-fix", "help", "destination=", "overwrite", "command=",
            "type=", "key=", "value=", "name=", "brres=", "model=", "file=",
            "interactive", "loudness=", "debug", "single-bone", "no-colors",
            "no-normals", "no-uvs", "moonview", "patch"
        ])
    except getopt.GetoptError as e:
        print(e)
        print(USAGE)
        sys.exit(2)

    for opt, arg in opts:
        if opt in ("-h", "--help"):
            do_help = True
        elif opt in ('-b', '--brres'):
            brres_file = arg
        elif opt in ("-f", "--file"):
            command_file = arg
        elif opt in ("-d", "--destination"):
            destination = arg
        elif opt in ("-o", "--overwrite"):
            overwrite = True
        elif opt in ("-k", "--key"):
            key = arg
        elif opt in ("-v", "--value"):
            value = arg
        elif opt in ("-t", "--type"):
            type = arg
        elif opt in ("-n", "--name"):
            name = arg
        elif opt in ("-m", "--model"):
            model = arg
        elif opt in ("-c", "--command"):
            command = arg
        elif opt in ("-i", "--interactive"):
            interactive = True
        elif opt in ("-a", "--auto-fix"):
            autofix = arg
        elif opt in ("-l", "--loudness"):
            loudness = arg
        elif opt in ("-g", "--debug"):
            debug = True
        elif opt == '--single-bone':
            single_bone = True
        elif opt == '--no-normals':
            no_normals = True
        elif opt == '--no-colors':
            no_colors = True
        elif opt == '--no-uvs':
            no_uvs = True
        elif opt == '--patch':
            patch = True
        elif opt == '--moonview':
            moonview = True
        else:
            print("Unknown option '{}'".format(opt))
            print(USAGE)
            sys.exit(2)
    if args:
        if cmd_args:
            cmd_args.extend(args)
        else:
            cmd_args = args
    if do_help:
        if not command and cmd_args:
            command = cmd_args[0]
        hlp(command)
        sys.exit()

    app_dir = os.path.dirname(os.path.dirname(app_dir))
    while not os.path.exists(os.path.join(app_dir, 'etc')):
        app_dir = os.path.dirname(app_dir)
        if not app_dir:
            AutoFix.error('Failed to find folder "etc"')
            break
    if debug and loudness is None:
        loudness = 5
    if app_dir:
        app_dir = os.path.join(app_dir, 'etc', 'abmatt')
        config = load_config(app_dir, loudness, autofix)
    Command.APP_DIR = app_dir
    Command.DEBUG = debug
    Brres.MOONVIEW = moonview
    cmds = []
    if cmd_args:
        if cmd_args[0] == 'convert':
            if single_bone:
                cmd_args.append('--single-bone')
            if no_colors:
                cmd_args.append('--no-colors')
            if no_normals:
                cmd_args.append('--no-normals')
            if no_uvs:
                cmd_args.append('--no-uvs')
            if patch:
                cmd_args.append('--patch')
        cmds.append(Command(arg_list=cmd_args))
    if command:
        args = [command, type]
        if key:
            if value:
                args.append(key + ':' + value)
            else:
                args.append(key)
        if not name and key != 'keys':
            name = '*'
        if name or model:
            args.extend(['for', name])
            if model:
                args.extend(['in', 'model', model])
        if command == 'convert':
            if single_bone:
                args.append('--single-bone')
            if no_colors:
                args.append('--no-colors')
            if no_normals:
                args.append('--no-normals')
            if no_uvs:
                args.append('--no-uvs')
            if patch:
                args.append('--patch')
        cmds.append(Command(arg_list=args))
    if destination:
        Command.DESTINATION = destination
        Brres.DESTINATION = destination
    if overwrite:
        Command.OVERWRITE = overwrite
        Brres.OVERWRITE = overwrite
    if brres_file:
        try:
            Command.updateSelection(brres_file)
        except NoSuchFile as e:
            AutoFix.exception(e, True)

    if command_file:
        try:
            filecmds = Command.load_commandfile(command_file)
            if filecmds:
                cmds = cmds + filecmds
        except NoSuchFile as err:
            AutoFix.error(err)

    # Run Commands
    if cmds:
        if not Command.run_commands(cmds):
            sys.exit(1)
    if interactive:
        Shell().cmdloop('Interactive shell started...')
    return Brres.OPEN_FILES
示例#6
0
def parse_args(argv, app_dir):
    interactive = overwrite = debug = False
    cmd_string = type = ""
    command = destination = brres_file = command_file = model = value = key = ""
    autofix = loudness = None
    name = None
    do_help = False
    for i in range(len(argv)):
        if argv[i][0] == '-':
            if i != 0:
                cmd_string = ' '.join(argv[:i])
                argv = argv[i:]
            break

    try:
        opts, args = getopt.getopt(argv, "hd:oc:t:k:v:n:b:m:f:iul:g", [
            "help", "destination=", "overwrite", "command=", "type=", "key=",
            "value=", "name=", "brres=", "model=", "file=", "interactive",
            "loudness=", "debug"
        ])
    except getopt.GetoptError as e:
        print(e)
        print(USAGE)
        sys.exit(2)

    for opt, arg in opts:
        if opt in ("-h", "--help"):
            do_help = True
        elif opt in ('-b', '--brres'):
            brres_file = arg
        elif opt in ("-f", "--file"):
            command_file = arg
        elif opt in ("-d", "--destination"):
            destination = arg
        elif opt in ("-o", "--overwrite"):
            overwrite = True
        elif opt in ("-k", "--key"):
            key = arg
        elif opt in ("-v", "--value"):
            value = arg
        elif opt in ("-t", "--type"):
            type = arg
        elif opt in ("-n", "--name"):
            name = arg
        elif opt in ("-m", "--model"):
            model = arg
        elif opt in ("-c", "--command"):
            command = arg
        elif opt in ("-i", "--interactive"):
            interactive = True
        elif opt in ("-a", "--auto-fix"):
            autofix = arg
        elif opt in ("-l", "--loudness"):
            loudness = arg
        elif opt in ("-g", "--debug"):
            debug = True
        else:
            print("Unknown option '{}'".format(opt))
            print(USAGE)
            sys.exit(2)
    if args:
        if cmd_string:
            cmd_string += ' ' + ' '.join(args)
        else:
            cmd_string = ' '.join(args)
    if do_help:
        if not command and cmd_string:
            command = cmd_string.split()[0]
        hlp(command)
        sys.exit()

    app_dir = os.path.join(
        os.path.join(os.path.dirname(os.path.dirname(app_dir)), 'etc'),
        'abmatt')
    if debug and loudness is None:
        loudness = 5
    config = load_config(app_dir, loudness, autofix)
    Command.APP_DIR = app_dir
    Command.DEBUG = debug
    cmds = []
    if cmd_string:
        cmds.append(Command(cmd_string))
    if command:
        cmd = command + ' ' + type
        if key:
            cmd += ' ' + key
            if value:
                cmd += ':' + value
        if not name and key != 'keys':
            name = '*'
        if name or model:
            cmd += ' for ' + name
            if model:
                cmd += ' in model ' + model
        cmds.append(Command(cmd))
    if destination:
        Command.DESTINATION = destination
        Brres.DESTINATION = destination
    if overwrite:
        Command.OVERWRITE = overwrite
        Brres.OVERWRITE = overwrite
    if brres_file:
        try:
            Command.updateSelection(brres_file)
        except NoSuchFile as e:
            AutoFix.get().exception(e, True)

    if command_file:
        try:
            filecmds = Command.load_commandfile(command_file)
            if filecmds:
                cmds = cmds + filecmds
        except NoSuchFile as err:
            AutoFix.get().error(err)

    # Run Commands
    if cmds:
        if not Command.run_commands(cmds):
            sys.exit(1)
    if interactive:
        Shell().cmdloop('Interactive shell started...')
    return Command.OPEN_FILES