Search in sources :

Example 1 with CommandLineWrapper

use of entry.CommandLineWrapper in project solution-finder by knewjade.

the class PathSettingParser method parse.

public Optional<PathSettings> parse() throws FinderParseException {
    Options options = createOptions();
    CommandLineParser parser = new DefaultParser();
    CommandLine commandLine = parseToCommandLine(options, parser, commands);
    CommandLineWrapper wrapper = new NormalCommandLineWrapper(commandLine);
    PathSettings settings = new PathSettings();
    // help
    if (wrapper.hasOption("help")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("path [options]", options);
        return Optional.empty();
    }
    // フィールド・最大削除ラインの設定
    if (wrapper.hasOption("tetfu")) {
        // テト譜から
        Optional<String> tetfuData = wrapper.getStringOption("tetfu");
        if (!tetfuData.isPresent())
            throw new FinderParseException("Should specify option value: --tetfu");
        String encoded = Tetfu.removeDomainData(tetfuData.get());
        wrapper = loadTetfu(encoded, parser, options, wrapper, settings);
    } else {
        // フィールドファイルから
        Optional<String> fieldPathOption = wrapper.getStringOption("field-path");
        String fieldPath = fieldPathOption.orElse(DEFAULT_FIELD_TXT);
        Path path = Paths.get(fieldPath);
        Charset charset = Charset.forName(CHARSET_NAME);
        try {
            LinkedList<String> fieldLines = Files.lines(path, charset).map(str -> {
                if (str.contains("#"))
                    return str.substring(0, str.indexOf('#'));
                return str;
            }).map(String::trim).filter(s -> !s.isEmpty()).collect(Collectors.toCollection(LinkedList::new));
            if (fieldLines.isEmpty())
                throw new FinderParseException("Should specify clear-line & field-definition in field file");
            String removeDomainData = Tetfu.removeDomainData(fieldLines.get(0));
            if (Tetfu.isDataLater115(removeDomainData)) {
                // テト譜から
                wrapper = loadTetfu(removeDomainData, parser, options, wrapper, settings);
            } else {
                // 固定ピースの指定があるか
                Optional<Boolean> reservedOption = wrapper.getBoolOption("reserved");
                reservedOption.ifPresent(settings::setReserved);
                // 最大削除ラインの設定
                int maxClearLine = Integer.valueOf(fieldLines.pollFirst());
                settings.setMaxClearLine(maxClearLine);
                // フィールドの設定
                String fieldMarks = String.join("", fieldLines);
                ColoredField coloredField = ColoredFieldFactory.createColoredField(fieldMarks);
                if (settings.isReserved()) {
                    settings.setFieldWithReserved(coloredField, maxClearLine);
                } else {
                    settings.setField(coloredField, maxClearLine);
                }
            }
        } catch (NumberFormatException e) {
            throw new FinderParseException("Cannot read clear-line from " + fieldPath);
        } catch (IOException e) {
            throw new FinderParseException("Cannot open field file", e);
        }
    }
    // ドロップの設定
    Optional<String> dropType = wrapper.getStringOption("drop");
    try {
        dropType.ifPresent(type -> {
            String key = dropType.orElse("softdrop");
            try {
                settings.setDropType(key);
            } catch (FinderParseException e) {
                throw new RuntimeException(e);
            }
        });
    } catch (Exception e) {
        throw new FinderParseException("Unsupported format: format=" + dropType.orElse("<empty>"));
    }
    // ホールドの設定
    Optional<Boolean> isUsingHold = wrapper.getBoolOption("hold");
    isUsingHold.ifPresent(settings::setUsingHold);
    // キャッシュ
    Optional<Integer> cachedMinBit = wrapper.getIntegerOption("cached-bit");
    cachedMinBit.ifPresent(settings::setCachedMinBit);
    // ログファイルの設定
    Optional<String> logFilePath = wrapper.getStringOption("log-path");
    logFilePath.ifPresent(settings::setLogFilePath);
    // アウトプットファイルの設定
    Optional<String> outputBaseFilePath = wrapper.getStringOption("output-base");
    outputBaseFilePath.ifPresent(settings::setOutputBaseFilePath);
    // 最大レイヤーの設定
    Optional<Integer> maxLayerNumber = wrapper.getIntegerOption("max-layer");
    Optional<PathLayer> pathLayer = maxLayerNumber.map(this::getPathLayer);
    pathLayer.ifPresent(settings::setPathLayer);
    // 出力タイプの設定
    Optional<String> outputType = wrapper.getStringOption("format");
    Optional<String> keyType = wrapper.getStringOption("key");
    try {
        outputType.ifPresent(type -> {
            String key = keyType.orElse("none");
            try {
                settings.setOutputType(type, key);
            } catch (FinderParseException e) {
                throw new RuntimeException(e);
            }
        });
    } catch (Exception e) {
        throw new FinderParseException("Unsupported format: format=" + outputType.orElse("<empty>"));
    }
    // 出力分割の設定
    Optional<Boolean> isSplit = wrapper.getBoolOption("split");
    isSplit.ifPresent(settings::setTetfuSplit);
    // スレッド数の設定
    Optional<Integer> threadCount = wrapper.getIntegerOption("threads");
    threadCount.ifPresent(settings::setThreadCount);
    // 探索パターンの設定
    if (wrapper.hasOption("patterns")) {
        // パターン定義から
        Optional<String> patternOption = wrapper.getStringOption("patterns");
        assert patternOption.isPresent();
        String patternValue = patternOption.get();
        List<String> patterns = Arrays.stream(patternValue.split(PATTERN_DELIMITER)).collect(Collectors.toList());
        settings.setPatterns(patterns);
    } else {
        // パターンファイルから
        Optional<String> patternPathOption = wrapper.getStringOption("patterns-path");
        String patternPath = patternPathOption.orElse(DEFAULT_PATTERNS_TXT);
        Path path = Paths.get(patternPath);
        Charset charset = Charset.forName(CHARSET_NAME);
        try {
            List<String> patterns = Files.lines(path, charset).collect(Collectors.toList());
            settings.setPatterns(patterns);
        } catch (IOException e) {
            throw new FinderParseException("Cannot open patterns file", e);
        }
    }
    return Optional.of(settings);
}
Also used : TetfuPage(common.tetfu.TetfuPage) Arrays(java.util.Arrays) ColorType(common.tetfu.common.ColorType) NormalCommandLineWrapper(entry.NormalCommandLineWrapper) Files(java.nio.file.Files) org.apache.commons.cli(org.apache.commons.cli) IOException(java.io.IOException) Tetfu(common.tetfu.Tetfu) FinderParseException(exceptions.FinderParseException) ColorConverter(common.tetfu.common.ColorConverter) Rotate(core.srs.Rotate) Collectors(java.util.stream.Collectors) List(java.util.List) ColoredFieldFactory(common.tetfu.field.ColoredFieldFactory) MinoFactory(core.mino.MinoFactory) Charset(java.nio.charset.Charset) Paths(java.nio.file.Paths) PriorityCommandLineWrapper(entry.PriorityCommandLineWrapper) Optional(java.util.Optional) CommandLineWrapper(entry.CommandLineWrapper) LinkedList(java.util.LinkedList) Path(java.nio.file.Path) ColoredField(common.tetfu.field.ColoredField) Mino(core.mino.Mino) NormalCommandLineWrapper(entry.NormalCommandLineWrapper) Path(java.nio.file.Path) ColoredField(common.tetfu.field.ColoredField) Charset(java.nio.charset.Charset) IOException(java.io.IOException) IOException(java.io.IOException) FinderParseException(exceptions.FinderParseException) FinderParseException(exceptions.FinderParseException) NormalCommandLineWrapper(entry.NormalCommandLineWrapper) PriorityCommandLineWrapper(entry.PriorityCommandLineWrapper) CommandLineWrapper(entry.CommandLineWrapper)

Example 2 with CommandLineWrapper

use of entry.CommandLineWrapper in project solution-finder by knewjade.

the class MoveSettingParser method loadTetfu.

private CommandLineWrapper loadTetfu(String data, CommandLineParser parser, Options options, CommandLineWrapper wrapper, MoveSettings settings) throws FinderParseException {
    // テト譜面のエンコード
    List<TetfuPage> decoded = encodeTetfu(data);
    // 指定されたページを抽出
    int page = wrapper.getIntegerOption("page").orElse(1);
    TetfuPage tetfuPage = extractTetfuPage(decoded, page);
    // コメントの抽出
    // 先頭が数字ではない(--clear-line -p *p7のようになる)場合でも、parserはエラーにならない
    // データ取得時にOptional.emptyがかえるだけ
    String comment = "--clear-line " + tetfuPage.getComment();
    List<String> splitComment = Arrays.stream(comment.split(" ")).map(String::trim).filter(s -> !s.isEmpty()).collect(Collectors.toList());
    // コマンド引数を配列に変換
    String[] commentArgs = new String[splitComment.size()];
    splitComment.toArray(commentArgs);
    // オプションとして読み込む
    try {
        CommandLine commandLineTetfu = parseToCommandLine(options, parser, commentArgs);
        CommandLineWrapper newWrapper = new NormalCommandLineWrapper(commandLineTetfu);
        // 削除ラインが読み取れればOK
        newWrapper.getIntegerOption("clear-line");
        wrapper = new PriorityCommandLineWrapper(Arrays.asList(wrapper, newWrapper));
    } catch (FinderParseException ignore) {
    }
    // 最大削除ラインの設定
    Optional<Integer> maxClearLineOption = wrapper.getIntegerOption("clear-line");
    maxClearLineOption.ifPresent(settings::setMaxClearLine);
    // フィールドを設定
    ColoredField coloredField = tetfuPage.getField();
    if (tetfuPage.isPutMino()) {
        ColorType colorType = tetfuPage.getColorType();
        Rotate rotate = tetfuPage.getRotate();
        int x = tetfuPage.getX();
        int y = tetfuPage.getY();
        ColorConverter colorConverter = new ColorConverter();
        Mino mino = new Mino(colorConverter.parseToBlock(colorType), rotate);
        coloredField.putMino(mino, x, y);
    }
    settings.setField(coloredField);
    return wrapper;
}
Also used : TetfuPage(common.tetfu.TetfuPage) Arrays(java.util.Arrays) ColorType(common.tetfu.common.ColorType) NormalCommandLineWrapper(entry.NormalCommandLineWrapper) Files(java.nio.file.Files) org.apache.commons.cli(org.apache.commons.cli) IOException(java.io.IOException) Tetfu(common.tetfu.Tetfu) FinderParseException(exceptions.FinderParseException) ColorConverter(common.tetfu.common.ColorConverter) Rotate(core.srs.Rotate) Collectors(java.util.stream.Collectors) List(java.util.List) ColoredFieldFactory(common.tetfu.field.ColoredFieldFactory) MinoFactory(core.mino.MinoFactory) Charset(java.nio.charset.Charset) Paths(java.nio.file.Paths) PriorityCommandLineWrapper(entry.PriorityCommandLineWrapper) Optional(java.util.Optional) CommandLineWrapper(entry.CommandLineWrapper) LinkedList(java.util.LinkedList) Path(java.nio.file.Path) ColoredField(common.tetfu.field.ColoredField) Mino(core.mino.Mino) TetfuPage(common.tetfu.TetfuPage) ColoredField(common.tetfu.field.ColoredField) Rotate(core.srs.Rotate) NormalCommandLineWrapper(entry.NormalCommandLineWrapper) FinderParseException(exceptions.FinderParseException) NormalCommandLineWrapper(entry.NormalCommandLineWrapper) PriorityCommandLineWrapper(entry.PriorityCommandLineWrapper) CommandLineWrapper(entry.CommandLineWrapper) ColorType(common.tetfu.common.ColorType) ColorConverter(common.tetfu.common.ColorConverter) Mino(core.mino.Mino) PriorityCommandLineWrapper(entry.PriorityCommandLineWrapper)

Example 3 with CommandLineWrapper

use of entry.CommandLineWrapper in project solution-finder by knewjade.

the class SetupSettingParser method loadTetfu.

private CommandLineWrapper loadTetfu(String data, CommandLineParser parser, Options options, CommandLineWrapper wrapper, SetupSettings settings) throws FinderParseException {
    // テト譜面のエンコード
    List<TetfuPage> decoded = encodeTetfu(data);
    // 指定されたページを抽出
    int page = wrapper.getIntegerOption("page").orElse(1);
    TetfuPage tetfuPage = extractTetfuPage(decoded, page);
    // コメントの抽出
    String comment = tetfuPage.getComment();
    List<String> splitComment = Arrays.stream(comment.split(" ")).map(String::trim).filter(s -> !s.isEmpty()).collect(Collectors.toList());
    // コマンド引数を配列に変換
    String[] commentArgs = new String[splitComment.size()];
    splitComment.toArray(commentArgs);
    // オプションとして読み込む
    try {
        CommandLine commandLineTetfu = parseToCommandLine(options, parser, commentArgs);
        CommandLineWrapper newWrapper = new NormalCommandLineWrapper(commandLineTetfu);
        wrapper = new PriorityCommandLineWrapper(Arrays.asList(wrapper, newWrapper));
    } catch (FinderParseException ignore) {
    }
    // 固定ピースの指定があるか
    // Optional<Boolean> reservedOption = wrapper.getBoolOption("reserved");
    // reservedOption.ifPresent(settings::setReserved);
    // マージン色の指定があるか
    Optional<String> fillColorOption = wrapper.getStringOption("fill");
    if (fillColorOption.isPresent()) {
        settings.setFillColorType(fillColorOption.get());
    }
    // マージン色の指定があるか
    Optional<String> marginColorOption = wrapper.getStringOption("margin");
    if (marginColorOption.isPresent()) {
        settings.setMarginColorType(marginColorOption.get());
    }
    // フィールドを設定
    ColoredField coloredField = tetfuPage.getField();
    if (tetfuPage.isPutMino()) {
        ColorType colorType = tetfuPage.getColorType();
        Rotate rotate = tetfuPage.getRotate();
        int x = tetfuPage.getX();
        int y = tetfuPage.getY();
        ColorConverter colorConverter = new ColorConverter();
        Mino mino = new Mino(colorConverter.parseToBlock(colorType), rotate);
        coloredField.putMino(mino, x, y);
    }
    // 最大削除ラインの設定
    Optional<Integer> maxHeightOption = wrapper.getIntegerOption("line");
    int maxHeight = maxHeightOption.orElse(coloredField.getUsingHeight());
    if (settings.isReserved()) {
        Field initField = FieldFactory.createField(maxHeight);
        Field needFilledField = FieldFactory.createField(maxHeight);
        Field notFilledField = FieldFactory.createField(maxHeight);
        ColorType marginColorType = settings.getMarginColorType();
        ColorType fillColorType = settings.getFillColorType();
        for (int y = 0; y < maxHeight; y++) {
            for (int x = 0; x < 10; x++) {
                ColorType colorType = coloredField.getColorType(x, y);
                if (colorType.equals(marginColorType)) {
                    coloredField.setColorType(ColorType.Empty, x, y);
                } else if (colorType.equals(fillColorType)) {
                    coloredField.setColorType(ColorType.Empty, x, y);
                    needFilledField.setBlock(x, y);
                } else {
                    switch(colorType) {
                        case Gray:
                            initField.setBlock(x, y);
                            notFilledField.setBlock(x, y);
                            coloredField.setColorType(ColorType.Empty, x, y);
                            break;
                        case Empty:
                            notFilledField.setBlock(x, y);
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        settings.setFieldWithReserved(initField, needFilledField, notFilledField, coloredField, maxHeight);
    } else {
        Field initField = FieldFactory.createField(maxHeight);
        Field needFilledField = FieldFactory.createField(maxHeight);
        Field notFilledField = FieldFactory.createField(maxHeight);
        ColorType marginColorType = settings.getMarginColorType();
        ColorType fillColorType = settings.getFillColorType();
        for (int y = 0; y < maxHeight; y++) {
            for (int x = 0; x < 10; x++) {
                ColorType colorType = coloredField.getColorType(x, y);
                if (colorType.equals(marginColorType)) {
                // skip
                } else if (colorType.equals(fillColorType)) {
                    needFilledField.setBlock(x, y);
                } else {
                    switch(colorType) {
                        case Empty:
                            notFilledField.setBlock(x, y);
                            break;
                        case Gray:
                        default:
                            initField.setBlock(x, y);
                            notFilledField.setBlock(x, y);
                            break;
                    }
                }
            }
        }
        settings.setField(initField, needFilledField, notFilledField, maxHeight);
    }
    return wrapper;
}
Also used : TetfuPage(common.tetfu.TetfuPage) Arrays(java.util.Arrays) ColorType(common.tetfu.common.ColorType) org.apache.commons.cli(org.apache.commons.cli) Tetfu(common.tetfu.Tetfu) FinderParseException(exceptions.FinderParseException) ColoredFieldFactory(common.tetfu.field.ColoredFieldFactory) MinoFactory(core.mino.MinoFactory) Charset(java.nio.charset.Charset) FieldFactory(core.field.FieldFactory) LinkedList(java.util.LinkedList) Path(java.nio.file.Path) NormalCommandLineWrapper(entry.NormalCommandLineWrapper) Files(java.nio.file.Files) IOException(java.io.IOException) ColorConverter(common.tetfu.common.ColorConverter) Rotate(core.srs.Rotate) Collectors(java.util.stream.Collectors) List(java.util.List) Field(core.field.Field) Paths(java.nio.file.Paths) PriorityCommandLineWrapper(entry.PriorityCommandLineWrapper) Optional(java.util.Optional) CommandLineWrapper(entry.CommandLineWrapper) ColoredField(common.tetfu.field.ColoredField) Mino(core.mino.Mino) TetfuPage(common.tetfu.TetfuPage) ColoredField(common.tetfu.field.ColoredField) Rotate(core.srs.Rotate) Field(core.field.Field) ColoredField(common.tetfu.field.ColoredField) NormalCommandLineWrapper(entry.NormalCommandLineWrapper) FinderParseException(exceptions.FinderParseException) NormalCommandLineWrapper(entry.NormalCommandLineWrapper) PriorityCommandLineWrapper(entry.PriorityCommandLineWrapper) CommandLineWrapper(entry.CommandLineWrapper) ColorType(common.tetfu.common.ColorType) ColorConverter(common.tetfu.common.ColorConverter) Mino(core.mino.Mino) PriorityCommandLineWrapper(entry.PriorityCommandLineWrapper)

Example 4 with CommandLineWrapper

use of entry.CommandLineWrapper in project solution-finder by knewjade.

the class SetupSettingParser method parse.

public Optional<SetupSettings> parse() throws FinderParseException {
    Options options = createOptions();
    CommandLineParser parser = new DefaultParser();
    CommandLine commandLine = parseToCommandLine(options, parser, commands);
    CommandLineWrapper wrapper = new NormalCommandLineWrapper(commandLine);
    SetupSettings settings = new SetupSettings();
    // help
    if (wrapper.hasOption("help")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("percent [options]", options);
        return Optional.empty();
    }
    // フィールド・最大削除ラインの設定
    if (wrapper.hasOption("tetfu")) {
        // テト譜から
        Optional<String> tetfuData = wrapper.getStringOption("tetfu");
        if (!tetfuData.isPresent())
            throw new FinderParseException("Should specify option value: --tetfu");
        String encoded = Tetfu.removeDomainData(tetfuData.get());
        wrapper = loadTetfu(encoded, parser, options, wrapper, settings);
    } else {
        // フィールドファイルから
        Optional<String> fieldPathOption = wrapper.getStringOption("field-path");
        String fieldPath = fieldPathOption.orElse(DEFAULT_FIELD_TXT);
        Path path = Paths.get(fieldPath);
        Charset charset = Charset.forName(CHARSET_NAME);
        try {
            LinkedList<String> fieldLines = Files.lines(path, charset).map(str -> {
                if (str.contains("#"))
                    return str.substring(0, str.indexOf('#'));
                return str;
            }).map(String::trim).filter(s -> !s.isEmpty()).collect(Collectors.toCollection(LinkedList::new));
            if (fieldLines.isEmpty())
                throw new FinderParseException("Should specify field-definition in field file");
            String removeDomainData = Tetfu.removeDomainData(fieldLines.get(0));
            if (Tetfu.isDataLater115(removeDomainData)) {
                // テト譜から
                wrapper = loadTetfu(removeDomainData, parser, options, wrapper, settings);
            } else {
                // 固定ピースの指定があるか
                Optional<Boolean> reservedOption = wrapper.getBoolOption("reserved");
                reservedOption.ifPresent(settings::setReserved);
                // 最大削除ラインの設定
                int maxHeightForce = -1;
                try {
                    maxHeightForce = Integer.valueOf(fieldLines.peekFirst());
                    // 読み込みに成功したときだけ進める
                    fieldLines.pollFirst();
                } catch (Exception ignore) {
                }
                // フィールドの設定
                String fieldMarks = String.join("", fieldLines);
                parseField(fieldMarks, settings, maxHeightForce);
            }
        } catch (IOException e) {
            throw new FinderParseException("Cannot open field file", e);
        }
    }
    // ログファイルの設定
    Optional<String> logFilePath = wrapper.getStringOption("log-path");
    logFilePath.ifPresent(settings::setLogFilePath);
    // アウトプットファイルの設定
    Optional<String> outputBaseFilePath = wrapper.getStringOption("output-base");
    outputBaseFilePath.ifPresent(settings::setOutputBaseFilePath);
    // ドロップの設定
    Optional<String> dropType = wrapper.getStringOption("drop");
    try {
        dropType.ifPresent(type -> {
            String key = dropType.orElse("softdrop");
            try {
                settings.setDropType(key);
            } catch (FinderParseException e) {
                throw new RuntimeException(e);
            }
        });
    } catch (Exception e) {
        throw new FinderParseException("Unsupported format: format=" + dropType.orElse("<empty>"));
    }
    // 探索パターンの設定
    if (wrapper.hasOption("patterns")) {
        // パターン定義から
        Optional<String> patternOption = wrapper.getStringOption("patterns");
        assert patternOption.isPresent();
        String patternValue = patternOption.get();
        List<String> patterns = Arrays.stream(patternValue.split(PATTERN_DELIMITER)).collect(Collectors.toList());
        settings.setPatterns(patterns);
    } else {
        // パターンファイルから
        Optional<String> patternPathOption = wrapper.getStringOption("patterns-path");
        String patternPath = patternPathOption.orElse(DEFAULT_PATTERNS_TXT);
        Path path = Paths.get(patternPath);
        Charset charset = Charset.forName(CHARSET_NAME);
        try {
            List<String> patterns = Files.lines(path, charset).collect(Collectors.toList());
            settings.setPatterns(patterns);
        } catch (IOException e) {
            throw new FinderParseException("Cannot open patterns file", e);
        }
    }
    return Optional.of(settings);
}
Also used : Path(java.nio.file.Path) TetfuPage(common.tetfu.TetfuPage) Arrays(java.util.Arrays) ColorType(common.tetfu.common.ColorType) org.apache.commons.cli(org.apache.commons.cli) Tetfu(common.tetfu.Tetfu) FinderParseException(exceptions.FinderParseException) ColoredFieldFactory(common.tetfu.field.ColoredFieldFactory) MinoFactory(core.mino.MinoFactory) Charset(java.nio.charset.Charset) FieldFactory(core.field.FieldFactory) LinkedList(java.util.LinkedList) Path(java.nio.file.Path) NormalCommandLineWrapper(entry.NormalCommandLineWrapper) Files(java.nio.file.Files) IOException(java.io.IOException) ColorConverter(common.tetfu.common.ColorConverter) Rotate(core.srs.Rotate) Collectors(java.util.stream.Collectors) List(java.util.List) Field(core.field.Field) Paths(java.nio.file.Paths) PriorityCommandLineWrapper(entry.PriorityCommandLineWrapper) Optional(java.util.Optional) CommandLineWrapper(entry.CommandLineWrapper) ColoredField(common.tetfu.field.ColoredField) Mino(core.mino.Mino) Charset(java.nio.charset.Charset) IOException(java.io.IOException) FinderParseException(exceptions.FinderParseException) IOException(java.io.IOException) NormalCommandLineWrapper(entry.NormalCommandLineWrapper) FinderParseException(exceptions.FinderParseException) NormalCommandLineWrapper(entry.NormalCommandLineWrapper) PriorityCommandLineWrapper(entry.PriorityCommandLineWrapper) CommandLineWrapper(entry.CommandLineWrapper)

Example 5 with CommandLineWrapper

use of entry.CommandLineWrapper in project solution-finder by knewjade.

the class FigUtilSettingParser method parse.

public Optional<FigUtilSettings> parse() throws FinderParseException {
    Options options = createOptions();
    CommandLineParser parser = new DefaultParser();
    CommandLine commandLine = parseToCommandLine(options, parser);
    CommandLineWrapper wrapper = new NormalCommandLineWrapper(commandLine);
    FigUtilSettings settings = new FigUtilSettings();
    // help
    if (wrapper.hasOption("help")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("util fig [options]", options);
        return Optional.empty();
    }
    // ホールドの設定
    Optional<Boolean> isUsingHold = wrapper.getBoolOption("hold");
    isUsingHold.ifPresent(settings::setUsingHold);
    // ループの設定
    Optional<Boolean> isInfiniteLoop = wrapper.getBoolOption("loop");
    isInfiniteLoop.ifPresent(settings::setInfiniteLoop);
    // アウトプットファイルの設定
    Optional<String> outputPath = wrapper.getStringOption("output");
    outputPath.ifPresent(settings::setOutputFilePath);
    // ディレイタイムの設定
    Optional<Integer> delay = wrapper.getIntegerOption("delay");
    try {
        delay.ifPresent(value -> {
            if (value <= 0)
                throw new RuntimeException("Delay should be positive: delay=" + value);
            settings.setDelay(value);
        });
    } catch (RuntimeException e) {
        throw new FinderParseException(e.getMessage());
    }
    // フレームの設定
    Optional<String> frameTypeName = wrapper.getStringOption("frame");
    try {
        Optional<FrameType> frameType = frameTypeName.map(frame -> {
            try {
                return parseFrameType(frame);
            } catch (UnsupportedDataTypeException e) {
                throw new RuntimeException(e);
            }
        });
        frameType.ifPresent(settings::setFrameType);
    } catch (RuntimeException e) {
        throw new FinderParseException("Unsupported frame: frame=" + frameTypeName.orElse("<empty>"));
    }
    // フォーマットの設定
    Optional<String> formatName = wrapper.getStringOption("format");
    try {
        Optional<FigFormat> figFormat = formatName.map(format -> {
            try {
                return parseFigFormat(format);
            } catch (UnsupportedDataTypeException e) {
                throw new RuntimeException(e);
            }
        });
        figFormat.ifPresent(settings::setFigFormat);
    } catch (RuntimeException e) {
        throw new FinderParseException("Unsupported format: format=" + formatName.orElse("<empty>"));
    }
    // 高さの設定
    Optional<Integer> height = wrapper.getIntegerOption("line");
    try {
        height.ifPresent(value -> {
            if (value == 0)
                throw new RuntimeException("Line should be positive or -1: line=" + value);
            settings.setHeight(value);
        });
    } catch (RuntimeException e) {
        throw new FinderParseException(e.getMessage());
    }
    // テト譜の設定
    if (wrapper.hasOption("tetfu")) {
        // パラメータから
        Optional<String> tetfuData = wrapper.getStringOption("tetfu");
        if (!tetfuData.isPresent())
            throw new FinderParseException("Should specify option value: --tetfu");
        String encoded = Tetfu.removeDomainData(tetfuData.get());
        wrapper = loadTetfu(encoded, wrapper, settings);
    } else {
        // フィールドファイルから
        Optional<String> fieldPathOption = wrapper.getStringOption("field-path");
        String fieldPath = fieldPathOption.orElse(DEFAULT_FIELD_TXT);
        Path path = Paths.get(fieldPath);
        Charset charset = Charset.forName(CHARSET_NAME);
        try {
            LinkedList<String> fieldLines = Files.lines(path, charset).map(str -> {
                if (str.contains("#"))
                    return str.substring(0, str.indexOf('#'));
                return str;
            }).map(String::trim).filter(s -> !s.isEmpty()).collect(Collectors.toCollection(LinkedList::new));
            if (fieldLines.isEmpty())
                throw new FinderParseException("Should specify field-definition in field file");
            String encoded = fieldLines.get(0);
            String removeDomainData = Tetfu.removeDomainData(encoded);
            wrapper = loadTetfu(removeDomainData, wrapper, settings);
        } catch (IOException e) {
            throw new FinderParseException("Cannot open field file", e);
        }
    }
    // ネクストの設定
    Optional<Integer> next = wrapper.getIntegerOption("next");
    Optional<Integer> positiveNext = next.map(integer -> 0 < integer ? integer : 0);
    positiveNext.ifPresent(settings::setNextBoxCount);
    return Optional.of(settings);
}
Also used : TetfuPage(common.tetfu.TetfuPage) NormalCommandLineWrapper(entry.NormalCommandLineWrapper) Files(java.nio.file.Files) org.apache.commons.cli(org.apache.commons.cli) FrameType(util.fig.FrameType) IOException(java.io.IOException) Tetfu(common.tetfu.Tetfu) FinderParseException(exceptions.FinderParseException) ColorConverter(common.tetfu.common.ColorConverter) Collectors(java.util.stream.Collectors) List(java.util.List) MinoFactory(core.mino.MinoFactory) Charset(java.nio.charset.Charset) Paths(java.nio.file.Paths) Optional(java.util.Optional) UnsupportedDataTypeException(javax.activation.UnsupportedDataTypeException) CommandLineWrapper(entry.CommandLineWrapper) LinkedList(java.util.LinkedList) Path(java.nio.file.Path) NormalCommandLineWrapper(entry.NormalCommandLineWrapper) FrameType(util.fig.FrameType) UnsupportedDataTypeException(javax.activation.UnsupportedDataTypeException) Path(java.nio.file.Path) Charset(java.nio.charset.Charset) IOException(java.io.IOException) FinderParseException(exceptions.FinderParseException) NormalCommandLineWrapper(entry.NormalCommandLineWrapper) CommandLineWrapper(entry.CommandLineWrapper)

Aggregations

CommandLineWrapper (entry.CommandLineWrapper)9 NormalCommandLineWrapper (entry.NormalCommandLineWrapper)9 FinderParseException (exceptions.FinderParseException)9 IOException (java.io.IOException)9 Charset (java.nio.charset.Charset)9 Path (java.nio.file.Path)9 LinkedList (java.util.LinkedList)9 Optional (java.util.Optional)9 Tetfu (common.tetfu.Tetfu)8 TetfuPage (common.tetfu.TetfuPage)8 ColorConverter (common.tetfu.common.ColorConverter)8 ColoredField (common.tetfu.field.ColoredField)8 MinoFactory (core.mino.MinoFactory)8 PriorityCommandLineWrapper (entry.PriorityCommandLineWrapper)8 Files (java.nio.file.Files)8 Paths (java.nio.file.Paths)8 List (java.util.List)8 Collectors (java.util.stream.Collectors)8 org.apache.commons.cli (org.apache.commons.cli)8 ColorType (common.tetfu.common.ColorType)7