From 5f7fe3ba5d607de0ca0589f37db0574837858dd5 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Sun, 13 Oct 2024 02:11:57 +0500 Subject: upd: game updater now checks from assets url --- android/AndroidManifest.xml | 2 + assets/i18n/en_us.json | 5 + assets/i18n/ru_ru.json | 5 + build.gradle | 1 + core/src/kz/ilotterytea/maxon/MaxonConstants.java | 10 +- core/src/kz/ilotterytea/maxon/MaxonGame.java | 13 ++ .../kz/ilotterytea/maxon/screens/MenuScreen.java | 24 ++++ .../kz/ilotterytea/maxon/utils/GameUpdater.java | 156 +++++++-------------- 8 files changed, 111 insertions(+), 105 deletions(-) diff --git a/android/AndroidManifest.xml b/android/AndroidManifest.xml index 352f4c1..389cc31 100644 --- a/android/AndroidManifest.xml +++ b/android/AndroidManifest.xml @@ -4,6 +4,8 @@ + + tasks = new ArrayList<>(); private Sound clickSound; + private static boolean suggestedUpdate = false; + public MenuScreen() { this.game = MaxonGame.getInstance(); + // Suggest an update + if (!GameUpdater.CLIENT_IS_ON_LATEST_VERSION && !suggestedUpdate) { + GDXButtonDialog bDialog = game.getDialogWindows().newDialog(GDXButtonDialog.class); + + bDialog.setTitle(game.locale.TranslatableText("updater.title")); + bDialog.setMessage(game.locale.TranslatableText("updater.message")); + + bDialog.setClickListener(button -> { + if (button == 1) { + Gdx.net.openURI(MaxonConstants.GAME_APP_URL); + } + }); + + bDialog.addButton(game.locale.TranslatableText("updater.no")); + bDialog.addButton(game.locale.TranslatableText("updater.yes")); + + bDialog.build().show(); + suggestedUpdate = true; + } + // Stage and skin: this.stage = new Stage(new ScreenViewport()); this.stage.addAction(Actions.sequence(Actions.alpha(0.0f), Actions.alpha(1.0f, 1f))); diff --git a/core/src/kz/ilotterytea/maxon/utils/GameUpdater.java b/core/src/kz/ilotterytea/maxon/utils/GameUpdater.java index 0133d21..1c4ef94 100644 --- a/core/src/kz/ilotterytea/maxon/utils/GameUpdater.java +++ b/core/src/kz/ilotterytea/maxon/utils/GameUpdater.java @@ -1,127 +1,75 @@ package kz.ilotterytea.maxon.utils; +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.Net; +import com.badlogic.gdx.net.HttpRequestBuilder; import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; +import kz.ilotterytea.maxon.MaxonConstants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; -import java.io.BufferedReader; -import java.io.InputStreamReader; -import java.net.URL; -import java.util.Objects; +import java.util.ArrayList; +public class GameUpdater implements Net.HttpResponseListener { + private final Logger logger = LoggerFactory.getLogger(GameUpdater.class.getName()); + public static boolean CLIENT_IS_ON_LATEST_VERSION; -public class GameUpdater { - static class Release { - String url; - String html_url; - String assets_url; - String upload_url; - String tarball_url; - String zipball_url; - String discussion_url; - int id; - String node_id; - String tag_name; - String target_commitish; - String name; - String body; - boolean draft; - boolean prerelease; - String created_at; - String published_at; - Author author; - Assets[] assets; - } + public void checkLatestUpdate() { + Net.HttpRequest request = + new HttpRequestBuilder() + .newRequest() + .method(Net.HttpMethods.GET) + .url(MaxonConstants.GAME_VERSIONS_FILE_URL) + .timeout(5000) + .build(); - static class Author { - String login; - int id; - String node_id; - String avatar_url; - String gravatar_id; - String url; - String html_url; - String followers_url; - String following_url; - String gists_url; - String starred_url; - String subscriptions_url; - String organizations_url; - String repos_url; - String events_url; - String received_events_url; - String type; - boolean site_admin; + Gdx.net.sendHttpRequest(request, this); } - static class Assets { - String url; - String browser_download_url; - int id; - String node_id; - String name; - String label; - String state; - String content_type; - int size; - int download_count; - String created_at; - String updated_at; - Uploader uploader; - } + @Override + public void handleHttpResponse(Net.HttpResponse httpResponse) { + String response = httpResponse.getResultAsString(); - static class Uploader { - String login; - int id; - String node_id; - String avatar_url; - String gravatar_id; - String url; - String html_url; - String followers_url; - String following_url; - String gists_url; - String starred_url; - String subscriptions_url; - String organizations_url; - String repos_url; - String events_url; - String received_events_url; - String type; - boolean site_admin; - } + if (response == null) { + logger.error("Got null in response"); + CLIENT_IS_ON_LATEST_VERSION = true; + return; + } - private static String readUrl(String urlString) throws Exception { - BufferedReader reader = null; - try { - URL url = new URL(urlString); - reader = new BufferedReader(new InputStreamReader(url.openStream())); - StringBuffer buffer = new StringBuffer(); - int read; - char[] chars = new char[1024]; - while ((read = reader.read(chars)) != -1) - buffer.append(chars, 0, read); + Gson gson = new Gson(); + ArrayList versions = gson.fromJson(response, new TypeToken>(){}.getType()); - return buffer.toString(); - } finally { - if (reader != null) - reader.close(); + try { + GameVersion latestVersion = versions.get(0); + CLIENT_IS_ON_LATEST_VERSION = latestVersion.getVersion().equals(MaxonConstants.GAME_VERSION); + } catch (Exception e) { + logger.error("Failed to find the latest version"); + CLIENT_IS_ON_LATEST_VERSION = true; } } - public static boolean isLatestRelease(String githubTag) throws Exception { - return Objects.equals(githubTag, getLatestRelease().tag_name); + @Override + public void failed(Throwable t) { + logger.error(t.getMessage()); + CLIENT_IS_ON_LATEST_VERSION = true; } - public static String getLatestVersion() throws Exception { - return getLatestRelease().tag_name; + @Override + public void cancelled() { + logger.info("Cancelled"); + CLIENT_IS_ON_LATEST_VERSION = true; } - public static Release getLatestRelease() throws Exception { - String url_link = "https://api.github.com/repos/NotDankEnough/MaxonPettingSim/releases/latest"; - - String json = readUrl(url_link); + private static class GameVersion { + private String version; - Gson gson = new Gson(); + public String getVersion() { + return version; + } - return gson.fromJson(json, Release.class); + public void setVersion(String version) { + this.version = version; + } } } -- cgit v1.2.3