diff --git a/src/main/java/ley/anvil/modpacktools/commandhelper/ModInfo.java b/src/main/java/ley/anvil/modpacktools/commandhelper/ModInfo.java index 8335387..8ca8cd4 100644 --- a/src/main/java/ley/anvil/modpacktools/commandhelper/ModInfo.java +++ b/src/main/java/ley/anvil/modpacktools/commandhelper/ModInfo.java @@ -6,7 +6,7 @@ import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gson.annotations.SerializedName; import ley.anvil.modpacktools.Main; -import ley.anvil.modpacktools.util.Util; +import ley.anvil.modpacktools.util.UtilKt; import java.io.File; import java.io.IOException; @@ -38,20 +38,20 @@ public class ModInfo { try { System.out.println("Getting Info From Curse API"); File manifestFile = new File(Main.CONFIG.JAR_LOCATION, Main.CONFIG.CONFIG - .getPath(String.class, - "Locations", - "manifestFile" - )); + .getPath(String.class, + "Locations", + "manifestFile" + )); //Read manifest - JsonObject manifest = Util.readJsonFile(manifestFile); + JsonObject manifest = UtilKt.readAsJson(manifestFile); JsonArray files = manifest.getAsJsonArray("files"); ArrayList fileIds = new ArrayList<>(); files.forEach(file -> fileIds.add(((JsonObject)file).get("projectID").getAsInt())); - String responseStr = Util.httpPostString(new URL("https://addons-ecs.forgesvc.net/api/v2/addon"), - fileIds.toString(), - "application/json; charset=utf-8", - Collections.singletonMap("Accept", "application/json") + String responseStr = UtilKt.httpPostStr(new URL("https://addons-ecs.forgesvc.net/api/v2/addon"), + fileIds.toString(), + "application/json; charset=utf-8", + Collections.singletonMap("Accept", "application/json") ); JsonArray response = (JsonArray)JsonParser.parseString(responseStr); diff --git a/src/main/java/ley/anvil/modpacktools/util/Util.java b/src/main/java/ley/anvil/modpacktools/util/Util.java deleted file mode 100644 index ee45a5f..0000000 --- a/src/main/java/ley/anvil/modpacktools/util/Util.java +++ /dev/null @@ -1,104 +0,0 @@ -package ley.anvil.modpacktools.util; - -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import okhttp3.MediaType; -import okhttp3.Request; -import okhttp3.RequestBody; -import okhttp3.Response; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.URL; -import java.util.Map; - -import static ley.anvil.modpacktools.Main.HTTP_CLIENT; - -public class Util { - /** - * Reads a Json File - * - * @param file the file to read - * @return the file content as JsonObject - */ - public static JsonObject readJsonFile(File file) { - try { - BufferedReader br = new BufferedReader(new FileReader(file)); - String inputLine; - StringBuffer sb = new StringBuffer(); - while((inputLine = br.readLine()) != null) { - sb.append(inputLine); - } - br.close(); - return (JsonObject)JsonParser.parseString(sb.toString()); - }catch(IOException e) { - e.printStackTrace(); - } - return null; - } - - /** - * sends a http post request - * - * @param url the url to send the request to - * @param contentType what content type should be used. Example: {@code MediaType.parse("application/json; utf-8")} - * @param payload the payload to send - * @param additionalHeaders additional headers that should be added to the request - * @return the response as string - */ - public static String httpPostString(URL url, String payload, MediaType contentType, Map additionalHeaders) throws IOException { - Request.Builder builder = new Request.Builder().url(url) - .post(RequestBody.create(payload, contentType)); - - additionalHeaders.forEach(builder::addHeader); - - Response resp = HTTP_CLIENT.newCall(builder.build()).execute(); - String rString = resp.body().string(); - resp.close(); - return rString; - } - - /** - * sends a http post request - * - * @param url the url to send the request to - * @param contentType what content type should be used. Example: {@code "application/json; utf-8"} - * @param payload the payload to send - * @param additionalHeaders additional headers that should be added to the request - * @return the response as string - */ - public static String httpPostString(URL url, String payload, String contentType, Map additionalHeaders) throws IOException { - return httpPostString( - url, - payload, - MediaType.get(contentType), - additionalHeaders - ); - } - - /** - * Sanitizes a URL to be valid by encoding illegal chars like spaces - * - * @param url the URL to sanitize - * @return the sanitized URL - */ - public static URL sanitizeURL(URL url) { - try { - URI uri = new URI(url.getProtocol(), - url.getUserInfo(), - url.getHost(), - url.getPort(), - url.getPath(), - url.getQuery(), - url.getRef()); - return uri.toURL(); - }catch(MalformedURLException | URISyntaxException ignored) { - } - return null; - } -} diff --git a/src/main/kotlin/ley/anvil/modpacktools/commands/CreateModlist.kt b/src/main/kotlin/ley/anvil/modpacktools/commands/CreateModlist.kt index e131ee2..50b048e 100644 --- a/src/main/kotlin/ley/anvil/modpacktools/commands/CreateModlist.kt +++ b/src/main/kotlin/ley/anvil/modpacktools/commands/CreateModlist.kt @@ -54,28 +54,40 @@ class CreateModlist : ICommand { private fun doHtml(outFile: File): CommandReturn { println("Making HTML file $outFile") val writer = FileWriter(outFile) - val html = body( - table( - tr( - td(b("Name")), - td(b("Contributors")) - ), - each(getMods()) { - tr(s(it.name), - a(it.name) - .withHref(it.website) - //Open in new tab - .withRel("noopener noreferrer") - .withTarget("_blank"), - ul( - each(it.contributors) {contr -> - li(contr.name) - } + val html = html( + head( + style( + ".img {width:100px;}" + ) + ), + body( + table( + tr( + td(), + td(b("Name")), + td(b("Contributors")) + ), + each(getMods()) { + tr( + td(a( + img().withSrc(it.icon) + .withClass("img") + ).withHref(it.website) + ), + td(a(it.name) + .withHref(it.website) + //Open in new tab + .withRel("noopener noreferrer") + .withTarget("_blank")), + td(ul( + each(it.contributors) {contr -> + li(contr.name) + } + )) ) - ) - } - ) - ).render() + } + ) + )).render() writer.write(html) writer.close() @@ -83,6 +95,7 @@ class CreateModlist : ICommand { } private fun getMods(): List { + println("Getting mods... this may take a while (TODO)") val asJson = Main.MPJH.json val mods = ArrayList() @@ -90,6 +103,7 @@ class CreateModlist : ICommand { for(rel in asJson.defaultVersion.getRelations("client", false, null)) { val meta = rel.getMeta(asJson.indexes) + println("got info for file ${meta.name}") if(meta.name != null) mods.add(meta) else println("meta name == null") } return mods.sortedBy {m -> m.name.toLowerCase()} diff --git a/src/main/kotlin/ley/anvil/modpacktools/util/Util.kt b/src/main/kotlin/ley/anvil/modpacktools/util/Util.kt new file mode 100644 index 0000000..3b6f62c --- /dev/null +++ b/src/main/kotlin/ley/anvil/modpacktools/util/Util.kt @@ -0,0 +1,88 @@ +package ley.anvil.modpacktools.util + +import com.google.gson.JsonObject +import com.google.gson.JsonParser +import ley.anvil.modpacktools.Main.HTTP_CLIENT +import okhttp3.MediaType +import okhttp3.MediaType.Companion.toMediaType +import okhttp3.Request +import okhttp3.RequestBody.Companion.toRequestBody +import java.io.File +import java.io.FileReader +import java.io.IOException +import java.net.URI +import java.net.URL + +/** + * Reads a Json File + * + * @receiver the file to read from + * @return the file content as JsonObject + */ +fun File.readAsJson(): JsonObject { + val reader = FileReader(this) + val out = JsonParser.parseReader(reader) as JsonObject + reader.close() + return out +} + +/** + * sends a http post request + * + * @receiver the url to send the request to + * @param contentType what content type should be used. Example: {@code MediaType.parse("application/json; utf-8")} + * @param payload the payload to send + * @param additionalHeaders additional headers that should be added to the request + * @return the response as string + */ +@Throws(IOException::class) +fun URL.httpPostStr(payload: String, contentType: MediaType? = null, additionalHeaders: Map): String? { + val builder = Request.Builder() + .url(this) + .post(payload.toRequestBody(contentType)) + + additionalHeaders.forEach {builder.addHeader(it.key, it.value)} + val resp = HTTP_CLIENT.newCall(builder.build()).execute() + val ret = resp.body?.string() + resp.close() + return ret +} + +/** + * sends a http post request + * + * @receiver the url to send the request to + * @param contentType what content type should be used. Example: `"application/json; utf-8"` + * @param payload the payload to send + * @param additionalHeaders additional headers that should be added to the request + * @return the response as string + */ +@Throws(IOException::class) +fun URL.httpPostStr(payload: String, contentType: String, additionalHeaders: Map): String? { + return this.httpPostStr( + payload, + contentType.toMediaType(), + additionalHeaders + ) +} + +/** + * Sanitizes a URL to be valid by encoding illegal chars like spaces + * + * @receiver the URL to sanitize + * @return the sanitized URL + */ + +fun URL.sanitize(): URL? { + return try { + URI(this.protocol, + this.userInfo, + this.host, + this.port, + this.path, + this.query, + this.ref).toURL() + } catch(e: Exception) { + null + } +} \ No newline at end of file