aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/fr/umlv/java/wallj/viewer/Main.java
blob: d133f7874d6e4d0c60af2bbcdf49ef0941b91cc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package fr.umlv.java.wallj.viewer;

import fr.umlv.java.wallj.board.Board;
import fr.umlv.java.wallj.board.BoardParser;
import fr.umlv.java.wallj.board.BoardValidator;
import fr.umlv.zen5.Application;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.*;
import java.util.*;
import java.util.List;

public class Main {

  private static final String DEFAULT_MAP_NAME = "/maps/level0.txt";

  private static FileSystem fileSystemForContext(URI uri) throws URISyntaxException, IOException {
    boolean isInJar = Objects.equals(Main.class.getProtectionDomain().getCodeSource().getLocation().getProtocol(), "jar");
    if (isInJar) {//JAR from command line handling
      Map<String, String> env = new HashMap<>();
      env.put("create", "true");
      return FileSystems.newFileSystem(uri, env);
    }
    return FileSystems.getDefault();//IDE Handling
  }

  private static List<Path> getResourcePaths() throws URISyntaxException, IOException {
    final URI uri = Main.class.getResource(DEFAULT_MAP_NAME).toURI();
    LinkedList<Path> paths = new LinkedList<>();
    String strFolder = System.getProperty("levels");
    if (strFolder != null) {
      Path pathFolder = Paths.get(strFolder);
      Files.newDirectoryStream(pathFolder).forEach(paths::add);
    } else {
      FileSystem fileSystem = fileSystemForContext(uri);
      paths.add(Paths.get(uri));
    }
    return paths;
  }

  //TODO Split Parse and validation + add useless return to satisfy this crazy compiler 
  private static Board validateBoardFromPath(Path path) {
    try {
      BoardValidator boardValidator = new BoardValidator(BoardParser.parse(path));
      return boardValidator.validate(BoardValidator.Constraint::hasActualReachableBlocks, "Some supposed reachable blocks are not reachable.")
       .validate(BoardValidator.Constraint::hasMandatoryBlocks, "Some mandatory blocks are missing.")
       .validate(BoardValidator.Constraint::isBounded, "The board is not correctly bounded.")
       .validate(BoardValidator.Constraint::isHollow, "The board must have a unique and simple interior.")
       .get();
    } catch (IOException e) {
      System.err.println(e.getMessage());
      System.exit(2);
    } catch (BoardValidator.ValidationException e) {
      System.err.println(path.toString() + ':');
      for (Throwable throwable : e.getSuppressed()) {
        System.err.println(throwable.getMessage());
      }
      System.exit(3);
    }
  }

  private static List<Board> validateBoardsFromPaths(List<Path> boardPaths) {
    List<Board> boards = new LinkedList<>();
    for (Path path : boardPaths) {
      boards.add(validateBoardFromPath(path));
    }
    return boards;
  }

  public static void main(String[] args) {
    try {
      Viewer viewer = new Viewer(validateBoardsFromPaths(Main.getResourcePaths()));
      Application.run(Color.WHITE, viewer::eventLoop);
    } catch (URISyntaxException e) {
      System.err.println(e.getMessage());
      System.exit(1);
    } catch (IOException e) {
      System.err.println(e.getMessage());
      System.exit(2);
    }
  }
}