Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
fix: no longer hard crash on failed auto update
Browse files Browse the repository at this point in the history
  • Loading branch information
Commander07 committed Aug 16, 2023
1 parent ae81c93 commit b39c631
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 76 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ loaderVersion = 0.14.19
fabricVersion = 0.78.0+1.19.4
loomVersion = 1.1-SNAPSHOT
# Mod Properties
modVersion = 0.2.12-1.19.4
modVersion = 0.2.13-1.19.4
mavenGroup = io.github.nbcss
archivesBaseName = wynnlib
# Kotlin
Expand Down
77 changes: 2 additions & 75 deletions src/main/kotlin/io/github/nbcss/wynnlib/WynnLibEntry.kt
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
package io.github.nbcss.wynnlib

import com.google.gson.Gson
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import io.github.nbcss.wynnlib.abilities.IconTexture
import io.github.nbcss.wynnlib.data.Identification
import io.github.nbcss.wynnlib.data.MajorId
import io.github.nbcss.wynnlib.data.PowderSpecial
import io.github.nbcss.wynnlib.events.ClientTickEvent
import io.github.nbcss.wynnlib.events.EventRegistry
import io.github.nbcss.wynnlib.events.RenderWorldEvent
import io.github.nbcss.wynnlib.function.AutoUpdater
import io.github.nbcss.wynnlib.items.identity.ConfigurableItem
import io.github.nbcss.wynnlib.registry.*
import io.github.nbcss.wynnlib.timer.status.StatusType
import io.github.nbcss.wynnlib.utils.FileUtils
import io.github.nbcss.wynnlib.utils.Scheduler
import net.fabricmc.api.ModInitializer
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
import kotlin.system.exitProcess


@Suppress("UNUSED")
Expand All @@ -31,72 +23,7 @@ object WynnLibEntry: ModInitializer {

override fun onInitialize() {
// Auto update
val gson = Gson()
val httpClient = HttpClient.newBuilder().build()
val wynntils = gson.fromJson(httpClient.send(HttpRequest.newBuilder().uri(URI.create("https://raw.githubusercontent.com/Wynntils/WynntilsWebsite-API/master/urls.json")).build(), HttpResponse.BodyHandlers.ofString()).body(), JsonArray::class.java)
var ingEndpoint: JsonObject? = null
var gearEndpoint: JsonObject? = null
for (endpoint in wynntils.asJsonArray.asList().drop(1)) {
if (endpoint.asJsonObject["id"].asString == "dataStaticIngredients") {
ingEndpoint = endpoint.asJsonObject
} else if (endpoint.asJsonObject["id"].asString == "dataStaticGear") {
gearEndpoint = endpoint.asJsonObject
}
}
if (ingEndpoint == null) {
error("ing_endpoint is null")
} else if (gearEndpoint == null) {
error("gear_endpoint is null")
}
// Gear
val gear_current = FileUtils.readFile("config/WynnLib/Equipments.json")
if ((gear_current == null) || (gearEndpoint["md5"] != gear_current["md5"])) {
val itemsJson = gson.fromJson(httpClient.send(HttpRequest.newBuilder().uri(URI.create(gearEndpoint["url"].asString)).build(), HttpResponse.BodyHandlers.ofString()).body(), JsonObject::class.java)
itemsJson.addProperty("version", "0.2.10")
itemsJson.add("data", itemsJson["items"])
itemsJson.remove("items")
itemsJson.addProperty("md5", gearEndpoint["md5"].asString)

// Yes this is terribly inefficient but it will only be ran on install or api update which means it will run at most once a month
// It will also increase file size but i doubt that will be an issue + i dont want to add checks for all fields to avoid a crash with missing keys
val equDefFields = gson.fromJson(FileUtils.getResource("assets/wynnlib/data/EquipmentDefaultFields.json")?.reader(), JsonObject::class.java)
for (i in itemsJson["data"].asJsonArray.asList().indices) {
val equ = itemsJson["data"].asJsonArray[i].asJsonObject
for (key in equDefFields[equ["category"].asString].asJsonObject.keySet()) {
if (!equ.has(key)) {
equ.add(key, equDefFields[equ["category"].asString].asJsonObject[key])
}
}
}

FileUtils.writeFile("config/WynnLib/Equipments.json", itemsJson)
// FileUtils.writeFile(FileUtils.getResourceURL("assets/wynnlib/data/Equipments.json").toString().replace("file:/", ""), itemsJson)
}
// Ings
val ings_current = FileUtils.readFile("config/WynnLib/Ingredients.json")
if ((ings_current == null) || (ingEndpoint["md5"] != ings_current["md5"])) {
val ingsJson = gson.fromJson(httpClient.send(HttpRequest.newBuilder().uri(URI.create(ingEndpoint["url"].asString)).build(), HttpResponse.BodyHandlers.ofString()).body(), JsonObject::class.java)
ingsJson.addProperty("version", "0.2.10")
ingsJson.add("data", ingsJson["ingredients"])
ingsJson.remove("ingredients")
ingsJson.addProperty("md5", ingEndpoint["md5"].asString)

// Yes this is terribly inefficient but it will only be ran on install or api update which means it will run at most once a month
// It will also increase file size but i doubt that will be an issue + i dont want to add checks for all fields to avoid a crash with missing keys
val ingDefFields = gson.fromJson(FileUtils.getResource("assets/wynnlib/data/IngredientDefaultFields.json")?.reader(), JsonObject::class.java)
for (i in ingsJson["data"].asJsonArray.asList().indices) {
val ing = ingsJson["data"].asJsonArray[i].asJsonObject
for (key in ingDefFields.keySet()) {
if (!ing.has(key)) {
ing.add(key, ingDefFields[key])
}
}
}

FileUtils.writeFile("config/WynnLib/Ingredients.json", ingsJson)
// FileUtils.writeFile(FileUtils.getResourceURL("assets/wynnlib/data/Ingredients.json").toString().replace("file:/", ""), ingsJson)
}

AutoUpdater.update()
//Reload Settings & auto saving
Settings.reload()
ClientTickEvents.END_CLIENT_TICK.register(ClientTickEvents.EndTick {
Expand Down
122 changes: 122 additions & 0 deletions src/main/kotlin/io/github/nbcss/wynnlib/function/AutoUpdater.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package io.github.nbcss.wynnlib.function

import com.google.gson.Gson
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import io.github.nbcss.wynnlib.utils.FileUtils
import java.io.IOException
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse

object AutoUpdater {
private val endpoints = "https://raw.githubusercontent.com/Wynntils/WynntilsWebsite-API/master/urls.json"
private val gson = Gson()
private val httpClient = HttpClient.newHttpClient()

fun update(): Boolean {
try {
val wynntils = gson.fromJson(
httpClient.send(
HttpRequest.newBuilder()
.uri(URI.create(endpoints))
.build(), HttpResponse.BodyHandlers.ofString()
).body(), JsonArray::class.java
)
var ingEndpoint: JsonObject? = null
var gearEndpoint: JsonObject? = null
for (endpoint in wynntils.asJsonArray.asList().drop(1)) {
if (endpoint.asJsonObject["id"].asString == "dataStaticIngredients") {
ingEndpoint = endpoint.asJsonObject
} else if (endpoint.asJsonObject["id"].asString == "dataStaticGear") {
gearEndpoint = endpoint.asJsonObject
}
}
if (ingEndpoint == null) {
error("ingEndpoint is null")
} else if (gearEndpoint == null) {
error("gearEndpoint is null")
}
// Gear
val gear_current = FileUtils.readFile("config/WynnLib/Equipments.json")
if ((gear_current == null) || (gearEndpoint["md5"] != gear_current["md5"])) {
val itemsJson = gson.fromJson(
httpClient.send(
HttpRequest.newBuilder().uri(URI.create(gearEndpoint["url"].asString)).build(),
HttpResponse.BodyHandlers.ofString()
).body(), JsonObject::class.java
)
itemsJson.addProperty("version", "0.2.10")
itemsJson.add("data", itemsJson["items"])
itemsJson.remove("items")
itemsJson.addProperty("md5", gearEndpoint["md5"].asString)

// Yes this is terribly inefficient but it will only be ran on install or api update which means it will run at most once a month
// It will also increase file size but i doubt that will be an issue + i dont want to add checks for all fields to avoid a crash with missing keys
val equDefFields = gson.fromJson(
FileUtils.getResource("assets/wynnlib/data/EquipmentDefaultFields.json")?.reader(),
JsonObject::class.java
)
for (i in itemsJson["data"].asJsonArray.asList().indices) {
val equ = itemsJson["data"].asJsonArray[i].asJsonObject
for (key in equDefFields[equ["category"].asString].asJsonObject.keySet()) {
if (!equ.has(key)) {
equ.add(key, equDefFields[equ["category"].asString].asJsonObject[key])
}
}
}

FileUtils.writeFile("config/WynnLib/Equipments.json", itemsJson)
}
// Ings
val ings_current = FileUtils.readFile("config/WynnLib/Ingredients.json")
if ((ings_current == null) || (ingEndpoint["md5"] != ings_current["md5"])) {
val ingsJson = gson.fromJson(
httpClient.send(
HttpRequest.newBuilder().uri(URI.create(ingEndpoint["url"].asString)).build(),
HttpResponse.BodyHandlers.ofString()
).body(), JsonObject::class.java
)
ingsJson.addProperty("version", "0.2.10")
ingsJson.add("data", ingsJson["ingredients"])
ingsJson.remove("ingredients")
ingsJson.addProperty("md5", ingEndpoint["md5"].asString)

// Yes this is terribly inefficient but it will only be ran on install or api update which means it will run at most once a month
// It will also increase file size but i doubt that will be an issue + i dont want to add checks for all fields to avoid a crash with missing keys
val ingDefFields = gson.fromJson(
FileUtils.getResource("assets/wynnlib/data/IngredientDefaultFields.json")?.reader(),
JsonObject::class.java
)
for (i in ingsJson["data"].asJsonArray.asList().indices) {
val ing = ingsJson["data"].asJsonArray[i].asJsonObject
for (key in ingDefFields.keySet()) {
if (!ing.has(key)) {
ing.add(key, ingDefFields[key])
}
}
}

FileUtils.writeFile("config/WynnLib/Ingredients.json", ingsJson)
}
} catch (e: IOException) {
// Recover
val gear = FileUtils.readFile("config/WynnLib/Equipments.json")
val ings = FileUtils.readFile("config/WynnLib/Ingredients.json")
val fallback_data = gson.fromJson("{\"version\": \"null\",\"data\": [],\"md5\": \"\"}", JsonObject::class.java)

if (gear == null) {
FileUtils.writeFile("config/WynnLib/Equipments.json", fallback_data)
}
if (ings == null) {
FileUtils.writeFile("config/WynnLib/Ingredients.json", fallback_data)
}

// Log
println("WynnLib failed to auto update: $e")
return false
}
return true
}
}

0 comments on commit b39c631

Please sign in to comment.