Skip to content

Commit

Permalink
Fix update system
Browse files Browse the repository at this point in the history
  • Loading branch information
TFSMads committed Feb 27, 2024
1 parent 6d6d6ea commit b7ca7c5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,34 +40,24 @@ private JsonObject getJsonFromUrl(String stringURL) {
}
reader.close();

// Parse JSON response
JsonElement jsonElement = JsonParser.parseString(response.toString());
return jsonElement.getAsJsonObject();
//JsonElement jsonElement = JsonParser.parseString(response.toString());
return new JsonParser().parse(response.toString()).getAsJsonObject();
} catch (Exception e) {
UnikAPI.LOGGER.printStackTrace(Logger.LOG_LEVEL.FINER, e);
UnikAPI.LOGGER.debug("Failed to get JSON from URL: " + stringURL, Logger.DEBUG_LEVEL.MEDIUM);
}
return null;
}

private String getUrl() {
if(url == null) {
JsonObject jsonObject = getJsonFromUrl("https://raw.githubusercontent.com/TransporterAddon/TransporterPriceAPI/main/info.json");
return "https://transporter-price-api-479a42778d13.herokuapp.com";

String url = (jsonObject != null && jsonObject.get("url") != null) ? jsonObject.get("url").getAsString() : null;

if(url != null) {
this.url = url;
} else {
this.url = "https://transporter-price-api-479a42778d13.herokuapp.com";
}

UnikAPI.LOGGER.debug("Transporter Price API URL: " + url, Logger.DEBUG_LEVEL.MEDIUM);
}
return url;
}

public int getSellValueFromPriceServer(String itemName) {
UnikAPI.LOGGER.debug("Hent item pris: " + itemName, Logger.DEBUG_LEVEL.LOW);
JsonObject jsonObject = getJsonFromUrl(getUrl() + "/items/" + itemName);
UnikAPI.LOGGER.debug("Hent item pris (response): " + jsonObject, Logger.DEBUG_LEVEL.TESTING);
if(jsonObject == null) {
return 0;
}
Expand Down
34 changes: 32 additions & 2 deletions src/main/java/ml/volder/transporter/updater/UpdateManager.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package ml.volder.transporter.updater;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import ml.volder.transporter.TransporterAddon;
import ml.volder.transporter.gui.UpdateFailedScreen;
import ml.volder.unikapi.UnikAPI;
Expand Down Expand Up @@ -33,8 +36,35 @@ private static class UpdateInfoJson {
}

public static boolean isUpToDate() {
//Updated through FlintUpdater.
return true;
InputStream transporterInputStream = UpdateManager.class.getClassLoader().getResourceAsStream("transporter.json");
if(transporterInputStream != null){
Reader r = null;
try {
r = new InputStreamReader(transporterInputStream, "UTF-8");
} catch (UnsupportedEncodingException e) {
return true;
}
JsonObject transporterJson = new JsonParser().parse(r).getAsJsonObject();
if(transporterJson.has("autoUpdate") && !transporterJson.get("autoUpdate").getAsBoolean())
return true;
}
Gson gson = new Gson();
UpdateInfoJson localUpdateInfo;
UpdateInfoJson remoteUpdateInfo;
try {
InputStream updateInfoSteam = UpdateManager.class.getClassLoader().getResourceAsStream("transporter/updateInfo.json");
Reader reader = new InputStreamReader(updateInfoSteam, "UTF-8");
localUpdateInfo = gson.fromJson(reader, UpdateInfoJson.class);

InputStream remoteInputStream = new URL("https://github.com/TFSMads/transporter/releases/latest/download/updateInfo.json").openStream();
reader = new InputStreamReader(remoteInputStream, "UTF-8");
remoteUpdateInfo = gson.fromJson(reader, UpdateInfoJson.class);
} catch (IOException ignored) {
return true;
}

currentVersion = localUpdateInfo.version;
return localUpdateInfo.version.equals(remoteUpdateInfo.version);
}


Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/transporter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"autoUpdate": true
}

0 comments on commit b7ca7c5

Please sign in to comment.