update mobile timestamp check

This commit is contained in:
Anthony Calosa
2024-10-28 12:07:22 +08:00
parent e97b77cc8f
commit b807c604e3
2 changed files with 34 additions and 43 deletions

View File

@@ -3,14 +3,12 @@ package forge.assets;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.TimeZone;
import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.files.FileHandle;
import forge.gui.GuiBase; import forge.gui.GuiBase;
import forge.util.TextUtil; import forge.util.BuildInfo;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
@@ -30,7 +28,6 @@ public class AssetsDownloader {
private final static ImmutableList<String> downloadIgnoreExit = ImmutableList.of("Download", "Ignore", "Exit"); private final static ImmutableList<String> downloadIgnoreExit = ImmutableList.of("Download", "Ignore", "Exit");
private final static ImmutableList<String> downloadExit = ImmutableList.of("Download", "Exit"); private final static ImmutableList<String> downloadExit = ImmutableList.of("Download", "Exit");
//if not sharing desktop assets, check whether assets are up to date
public static void checkForUpdates(boolean exited, Runnable runnable) { public static void checkForUpdates(boolean exited, Runnable runnable) {
if (exited) if (exited)
return; return;
@@ -52,7 +49,11 @@ public class AssetsDownloader {
final String versionText = isSnapshots ? snapsURL + "version.txt" : releaseURL + "version.txt"; final String versionText = isSnapshots ? snapsURL + "version.txt" : releaseURL + "version.txt";
FileHandle assetsDir = Gdx.files.absolute(ForgeConstants.ASSETS_DIR); FileHandle assetsDir = Gdx.files.absolute(ForgeConstants.ASSETS_DIR);
FileHandle resDir = Gdx.files.absolute(ForgeConstants.RES_DIR); FileHandle resDir = Gdx.files.absolute(ForgeConstants.RES_DIR);
FileHandle buildTxtFileHandle = Gdx.files.classpath("build.txt");
final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
boolean verifyUpdatable = false;
boolean mandatory = false; boolean mandatory = false;
Date snapsTimestamp = null, buildTimeStamp;
String message; String message;
boolean connectedToInternet = Forge.getDeviceAdapter().isConnectedToInternet(); boolean connectedToInternet = Forge.getDeviceAdapter().isConnectedToInternet();
@@ -74,31 +75,30 @@ public class AssetsDownloader {
String snapsBZ2URL = "https://downloads.cardforge.org/dailysnapshots/"; String snapsBZ2URL = "https://downloads.cardforge.org/dailysnapshots/";
installerURL = isSnapshots ? snapsBZ2URL : releaseBZ2URL; installerURL = isSnapshots ? snapsBZ2URL : releaseBZ2URL;
} }
//TODO build version String snapsBuildDate = "", buildDate = "";
/*String buildver = ""; if (isSnapshots) {
SimpleDateFormat DateFor = TextUtil.getSimpleDate(); URL url = new URL(snapsURL + "build.txt");
Calendar calendar = Calendar.getInstance(); snapsTimestamp = format.parse(FileUtil.readFileToString(url));
Date buildDateOriginal = null; snapsBuildDate = snapsTimestamp.toString();
try { if (!GuiBase.isAndroid()) {
FileHandle build = Gdx.files.classpath("build.txt"); buildDate = BuildInfo.getTimestamp().toString();
if (build.exists()) { verifyUpdatable = BuildInfo.verifyTimestamp(snapsTimestamp);
SimpleDateFormat original = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); } else {
original.setTimeZone(TimeZone.getTimeZone("UTC")); if (buildTxtFileHandle.exists()) {
Date buildDate = original.parse(build.readString()); buildTimeStamp = format.parse(buildTxtFileHandle.readString());
buildDateOriginal = original.parse(build.readString()); buildDate = buildTimeStamp.toString();
calendar.setTime(buildDate); verifyUpdatable = snapsTimestamp.after(buildTimeStamp);
DateFor.setTimeZone(TimeZone.getDefault()); }
buildver = "\nForge Build: " + DateFor.format(calendar.getTime());
} }
} catch (Exception e) { } else {
e.printStackTrace(); verifyUpdatable = !StringUtils.isEmpty(version) && !versionString.equals(version);
}*/ }
if (!StringUtils.isEmpty(version) && !versionString.equals(version)) { if (verifyUpdatable) {
Forge.getSplashScreen().prepareForDialogs(); Forge.getSplashScreen().prepareForDialogs();
message = "A new version of Forge is available (" + version + ").\n" + message = "A new version of Forge is available." + version + "\n" + snapsBuildDate + "\n" +
"You are currently on an older version (" + versionString + ").\n\n" + "You are currently on an older version." + versionString + "\n" + buildDate + "\n" +
"Would you like to update to the new version now?"; "Would you like to update to the new version now?";
if (!Forge.getDeviceAdapter().isConnectedToWifi()) { if (!Forge.getDeviceAdapter().isConnectedToWifi()) {
message += " If so, you may want to connect to wifi first. The download is around " + (GuiBase.isAndroid() ? apkSize : packageSize) + "."; message += " If so, you may want to connect to wifi first. The download is around " + (GuiBase.isAndroid() ? apkSize : packageSize) + ".";
@@ -183,30 +183,21 @@ public class AssetsDownloader {
return; //if version matches what had been previously saved and FSkin isn't requesting assets download, no need to download assets return; //if version matches what had been previously saved and FSkin isn't requesting assets download, no need to download assets
} }
FileHandle f = Gdx.files.classpath("build.txt"); FileHandle resBuildDate = resDir.child("build.txt");
FileHandle t = resDir.child("build.txt"); if (buildTxtFileHandle.exists() && resBuildDate.exists()) {
if (f.exists() && t.exists()) { String buildString = buildTxtFileHandle.readString();
String buildString = f.readString(); String target = resBuildDate.readString();
String target = t.readString();
try { try {
Date buildDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(buildString); Date buildDate = format.parse(buildString);
Date targetDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(target); Date targetDate = format.parse(target);
// if res folder has same build date then continue loading assets // if res folder has same build date then continue loading assets
if (buildDate.equals(targetDate) && versionString.equals(FileUtil.readFileToString(versionFile.file()))) { if (buildDate.equals(targetDate) && versionString.equals(FileUtil.readFileToString(versionFile.file()))) {
run(runnable); run(runnable);
return; return;
} }
mandatory = true; mandatory = true;
//format to local date build += "Installed resources date: " + target + "\n\n";
SimpleDateFormat original = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); log = Forge.getDeviceAdapter().getLatestChanges(GITHUB_COMMITS_URL_ATOM, buildDate, snapsTimestamp);
original.setTimeZone(TimeZone.getTimeZone("UTC"));
targetDate = original.parse(target);
Calendar calendar = Calendar.getInstance();
calendar.setTime(targetDate);
SimpleDateFormat simpleDate = TextUtil.getSimpleDate();
simpleDate.setTimeZone(TimeZone.getDefault());
build += "Installed resources date: " + simpleDate.format(calendar.getTime()) + "\n\n";
log = Forge.getDeviceAdapter().getLatestChanges(GITHUB_COMMITS_URL_ATOM, null, null);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@@ -155,7 +155,7 @@ public class AutoUpdater {
} }
} }
catch (Exception e) { catch (Exception e) {
e.printStackTrace(); SOptionPane.showOptionDialog(e.getMessage(), localizer.getMessage("lblError"), null, ImmutableList.of("Ok"));
return false; return false;
} }
// If version doesn't match, it's assummably newer. // If version doesn't match, it's assummably newer.