Untitled

mail@pastecode.io avatar
unknown
java
a year ago
3.2 kB
5
Indexable
Never
  /**
   * Checks if the current version of the launcher is outdated.
   *
   * <p>It checks the current version of the launcher against the latest release on GitHub. The
   * version itself uses a custom format, which is as follows:
   *
   * <p>{@code 1.mm.ddyy} where {@code 1} is the major version that increments every new year,
   * {@code mm} is the month without leading zeros, {@code dd} is the day with leading zeros, and
   * {@code yy} is the year with leading zeros.
   *
   * <p>If we're working with a development build, the version will append {@code _##} at the end,
   * which is a value with a range of {@code 01-99} that increments every time a new pre-release
   * build is published on GitHub. and will reset whenever the month increments.
   *
   * @return {@code true} if the current version of the launcher is outdated, {@code false}
   * @see <a href="https://github.com/Kawaxte/TwentyTenLauncher/releases/latest">Latest Release</a>
   */
  public static boolean isOutdated() {
    if (Objects.isNull(outdated)) {
      SwingWorker<Boolean, Void> worker =
          new SwingWorker<Boolean, Void>() {
            @Override
            protected Boolean doInBackground() {
              HttpTransport transport = new NetHttpTransport();

              HttpRequestFactory factory = transport.createRequestFactory();
              try {
                HttpRequest request =
                    factory.buildGetRequest(
                        getGenericUrls()[1]
                            .set("accept", "application/vnd.github+json")
                            .set("X-GitHub-Api-Version", "2022-11-28"));
                HttpResponse response = request.execute();

                String body = response.parseAsString();
                JSONArray array = new JSONArray(body);
                String tagName = array.getJSONObject(0).getString("tag_name");
                String implVersion = this.getClass().getPackage().getImplementationVersion();
                if (Objects.isNull(implVersion)) {
                  implVersion = "1.99.9999_99"; // This can be used for testing purposes
                }
                return Objects.compare(implVersion, tagName, String::compareTo) < 0;
              } catch (UnknownHostException uhe) {
                LauncherUtils.swapContainers(
                    LauncherPanel.getInstance(),
                    new LauncherNoNetworkPanel(
                        LauncherLanguageUtils.getLNPPKeys()[1], uhe.getMessage()));
              } catch (IOException ioe) {
                LOGGER.error("Cannot check for updates", ioe);
              }
              return false;
            }
          };
      worker.execute();

      try {
        outdated = worker.get();
      } catch (InterruptedException ie) {
        Thread.currentThread().interrupt();

        LOGGER.error("Interrupted while checking for updates", ie);
      } catch (ExecutionException ee) {
        Throwable cause = ee.getCause();

        LOGGER.error("Error while checking for updates", cause);
      } finally {
        worker.cancel(true);
      }
    }
    return outdated;
  }