11package com .redlimerl .mcsr .helper ;
22
33import com .google .gson .JsonArray ;
4- import com .google .gson .JsonElement ;
54import com .google .gson .JsonObject ;
65import com .redlimerl .mcsr .MCSRModLoader ;
76import com .redlimerl .mcsr .mod .FabricLoader ;
87import com .redlimerl .mcsr .mod .ModInfo ;
98
10- import java .io .ByteArrayInputStream ;
119import java .io .FileOutputStream ;
1210import java .io .IOException ;
1311import java .nio .charset .StandardCharsets ;
14- import java .nio .file .Path ;
12+ import java .nio .file .* ;
1513import java .text .SimpleDateFormat ;
1614import java .util .*;
17- import java .util .stream .Collectors ;
1815import java .util .zip .ZipEntry ;
1916import java .util .zip .ZipOutputStream ;
2017
@@ -36,12 +33,7 @@ public static JsonObject convertPack(String name, String gameVersion, FabricLoad
3633 for (ModInfo .ModVersion version : mod .versions ()) {
3734 for (String s : version .target_version ()) {
3835 if (s .equals ("1.16.1" )) {
39- if (version .url ().startsWith ("https://github.com" )) {
40- jsonArray .add (getGithubMeta (version .url ()));
41- }
42- if (version .url ().startsWith ("https://cdn.modrinth.com" )) {
43- jsonArray .add (getModrinthMeta (version .hash ()));
44- }
36+ jsonArray .add (getFileObject (version .url ().substring (version .url ().lastIndexOf ("/" ) + 1 ), version .sha1 (), version .sha512 (), version .url (), version .size ()));
4537 continue mod ;
4638 }
4739 }
@@ -76,49 +68,11 @@ public static JsonObject getFileObject(String fileName, String sha1, String sha5
7668 }
7769
7870 public static void writeZipFile (Path path , String packData ) throws IOException {
71+ Files .createDirectories (path .getParent ());
7972 ZipOutputStream out = new ZipOutputStream (new FileOutputStream (path .toFile ()));
8073 out .putNextEntry (new ZipEntry ("modrinth.index.json" ));
8174 out .write (packData .getBytes (StandardCharsets .UTF_8 ));
8275 out .closeEntry ();
8376 out .close ();
8477 }
85-
86- private static final Map <String , JsonElement > cachedApi = new HashMap <>();
87- private static final Map <String , String > cachedSha1 = new HashMap <>();
88- private static final Map <String , String > cachedSha512 = new HashMap <>();
89- public static JsonObject getGithubMeta (String url ) throws IOException {
90- Map .Entry <String , String > apiVersion = Map .entry ("X-GitHub-Api-Version" , "2022-11-28" );
91- Map .Entry <String , String > githubToken = Map .entry ("Authorization" , "token " + MCSRModLoader .GITHUB_TOKEN );
92-
93- url = url .replaceAll ("https://github.com/Minecraft-Java-Edition-Speedrunning/legal-mods/raw/(\\ w*)/" , "https://api.github.com/repos/Minecraft-Java-Edition-Speedrunning/legal-mods/contents/" );
94- url = Arrays .stream (url .split ("/" )).filter (path -> !path .endsWith (".jar" )).collect (Collectors .joining ("/" )) + "?ref=main" ;
95- JsonArray jsonArray = cachedApi .containsKey (url ) ? cachedApi .get (url ).getAsJsonArray () : HttpRequestHelper .getJsonFromUrl (url , apiVersion , githubToken ).getAsJsonArray ();
96- cachedApi .putIfAbsent (url , jsonArray );
97-
98- for (JsonElement jsonElement : jsonArray ) {
99- JsonObject data = jsonElement .getAsJsonObject ();
100- String download = data .get ("download_url" ).getAsString ();
101- ByteArrayInputStream fileInputStream = HttpRequestHelper .getInputStreamFromUrl (download );
102- String sha1 = cachedSha1 .containsKey (download ) ? cachedSha1 .get (download ) : ShaHelper .getSha1FromInputStream (fileInputStream );
103- fileInputStream .reset ();
104- cachedSha1 .putIfAbsent (download , sha1 );
105- String sha512 = cachedSha512 .containsKey (download ) ? cachedSha512 .get (download ) : ShaHelper .getSha512FromInputStream (fileInputStream );
106- cachedSha512 .putIfAbsent (download , sha512 );
107- return getFileObject (data .get ("name" ).getAsString (), sha1 , sha512 , data .get ("download_url" ).getAsString (), data .get ("size" ).getAsInt ());
108- }
109-
110- return null ;
111- }
112- public static JsonObject getModrinthMeta (String sha512 ) throws IOException {
113- String url = "https://api.modrinth.com/v2/version_file/" + sha512 + "?algorithm=sha512" ;
114- JsonObject jsonObject = cachedApi .containsKey (url ) ? cachedApi .get (url ).getAsJsonObject () : HttpRequestHelper .getJsonFromUrl (url ).getAsJsonObject ();
115- cachedApi .putIfAbsent (url , jsonObject );
116-
117- for (JsonElement jsonElement : jsonObject .getAsJsonArray ("files" )) {
118- JsonObject data = jsonElement .getAsJsonObject ();
119- return getFileObject (data .get ("filename" ).getAsString (), data .getAsJsonObject ("hashes" ).get ("sha1" ).getAsString (), sha512 , data .get ("url" ).getAsString (), data .get ("size" ).getAsInt ());
120- }
121-
122- return null ;
123- }
12478}
0 commit comments