mirror of
https://github.com/Anvilcraft/modpacktools
synced 2024-09-30 04:59:05 +02:00
port util class to kotlin and improve CreateModlist.kt
This commit is contained in:
parent
2d987ee40e
commit
b064b63dee
4 changed files with 133 additions and 135 deletions
|
@ -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<Integer> 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);
|
||||
|
||||
|
|
|
@ -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<String, String> 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<String, String> 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;
|
||||
}
|
||||
}
|
|
@ -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<AddonscriptJSON.Meta> {
|
||||
println("Getting mods... this may take a while (TODO)")
|
||||
val asJson = Main.MPJH.json
|
||||
val mods = ArrayList<AddonscriptJSON.Meta>()
|
||||
|
||||
|
@ -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()}
|
||||
|
|
88
src/main/kotlin/ley/anvil/modpacktools/util/Util.kt
Normal file
88
src/main/kotlin/ley/anvil/modpacktools/util/Util.kt
Normal file
|
@ -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, String>): 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, String>): 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
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue