Skip to content

Commit 521b822

Browse files
committed
a REALLY big fix for deps
1 parent b8fd837 commit 521b822

4 files changed

Lines changed: 163 additions & 16 deletions

File tree

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@
4949
<artifactId>picocli</artifactId>
5050
<version>4.7.7</version>
5151
</dependency>
52+
<dependency>
53+
<groupId>org.semver4j</groupId>
54+
<artifactId>semver4j</artifactId>
55+
<version>6.0.0</version>
56+
</dependency>
5257
<dependency>
5358
<groupId>org.eclipse.jgit</groupId>
5459
<artifactId>org.eclipse.jgit</artifactId>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.benjamin538.index;
2+
3+
// net
4+
import java.net.URI;
5+
import java.net.http.HttpRequest;
6+
import java.net.http.HttpResponse;
7+
import java.net.http.HttpClient;
8+
9+
// config
10+
import com.benjamin538.config.ConfigGet;
11+
12+
// json
13+
import org.json.JSONObject;
14+
import org.json.JSONArray;
15+
16+
public class GetVersions {
17+
public static JSONArray getVersions(HttpClient client, String modid) {
18+
try {
19+
String url = ConfigGet.getIndexUrl() + "/v1/mods/" + modid + "/versions";
20+
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(url)).header("User-Agent", "GeodeCLI").GET().build();
21+
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
22+
if (response.statusCode() != 200) {
23+
return null;
24+
}
25+
return new JSONObject(response.body()).getJSONObject("payload").getJSONArray("data");
26+
} catch (Exception ex) {
27+
return null;
28+
}
29+
}
30+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.benjamin538.index;
2+
3+
// logging
4+
// import com.benjamin538.util.Logging;
5+
6+
// picocli
7+
import picocli.CommandLine.Command;
8+
import picocli.CommandLine.Option;
9+
import picocli.CommandLine.Parameters;
10+
11+
@Command(
12+
name = "install",
13+
description = "Install a mod from the index to the current profile"
14+
)
15+
public class IndexInstall implements Runnable {
16+
// private Logging logger = new Logging();
17+
@Option(names = {"-h", "--help"}, description = "Print help")
18+
boolean help;
19+
@Parameters(description = "Mod ID to install")
20+
String id;
21+
@Parameters(description = "Mod version to install, defaults to latest", defaultValue = "latest")
22+
String version;
23+
@Override
24+
public void run() {
25+
// TODO: ts
26+
}
27+
}

src/main/java/com/benjamin538/project/CheckDeps.java

Lines changed: 101 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package com.benjamin538.project;
2+
23
// net stuff
34
import java.net.http.HttpClient;
45
import java.net.http.HttpRequest;
@@ -18,8 +19,8 @@
1819
import org.json.JSONArray;
1920
import org.json.JSONException;
2021

21-
// getting url
22-
import com.benjamin538.config.ConfigGet;
22+
// getting versions
23+
import com.benjamin538.index.GetVersions;
2324

2425
// logging
2526
import com.benjamin538.util.Logging;
@@ -33,16 +34,22 @@
3334
import java.util.Set;
3435
import java.util.HashSet;
3536
import java.util.Enumeration;
37+
import java.util.HashMap;
3638
import java.util.Iterator;
39+
import java.util.Map;
3740
import java.util.zip.ZipEntry;
3841
import java.util.zip.ZipFile;
3942

43+
// semver
44+
import org.semver4j.Semver;
45+
4046
@Command(
4147
name = "check",
4248
description = "Check & install the dependencies for this project"
4349
)
4450
public class CheckDeps implements Runnable {
4551
private Logging logger = new Logging();
52+
private boolean errors = false;
4653
@Option(names = {"-h", "--help"}, description = "Print help", usageHelp = true)
4754
boolean help;
4855
@Option(names = {"-p", "--platform"}, description = "The platform checked used for platform-specific dependencies. If not specified, uses current host platform if possible")
@@ -72,34 +79,106 @@ public void run() {
7279
} catch(JSONException ex) {
7380
logger.fatal("Dependencies not found");
7481
}
75-
if(dependencies.isEmpty()) {
82+
if (modJSON.has("gd")) {
83+
if (modJSON.get("gd") instanceof JSONObject) {
84+
JSONObject gdVer = modJSON.getJSONObject("gd");
85+
switch (platform) {
86+
case OS.win:
87+
case OS.windows:
88+
case OS.linux:
89+
if (!(gdVer.get("win") instanceof String)) {
90+
logger.fatal("Geometry Dash version not specified for Windows, please specify one in mod.json");
91+
}
92+
break;
93+
case OS.mac_os:
94+
case OS.mac_intel:
95+
case OS.mac_arm:
96+
if (!(gdVer.get("mac") instanceof String)) {
97+
logger.fatal("Geometry Dash version not specified for macOS, please specify one in mod.json");
98+
}
99+
break;
100+
case OS.android:
101+
case OS.android64:
102+
case OS.android32:
103+
if (!(gdVer.get("android") instanceof String)) {
104+
logger.fatal("Geometry Dash version not specified for Android, please specify one in mod.json");
105+
}
106+
break;
107+
case OS.ios:
108+
if (!(gdVer.get("ios") instanceof String)) {
109+
logger.fatal("Geometry Dash version not specified for iOS, please specify one in mod.json");
110+
}
111+
break;
112+
}
113+
}
114+
}
115+
if (dependencies.isEmpty()) {
76116
return;
77117
}
118+
Path buildFolder = Paths.get(folder, "geode-deps");
119+
Map<String, String> externalMap = new HashMap<>();
120+
for(String ext : externals) {
121+
if(ext.contains(":")) {
122+
String[] parts = ext.split(":");
123+
externalMap.put(parts[0], parts[1]);
124+
} else {
125+
externalMap.put(ext, null);
126+
}
127+
}
78128
try {
129+
if (!Files.exists(buildFolder)) {
130+
Files.createDirectories(buildFolder);
131+
}
79132
Iterator<String> keys = dependencies.keys();
80133
HttpClient client = HttpClient.newBuilder().followRedirects(HttpClient.Redirect.NORMAL).build();
81134
while(keys.hasNext()) {
82135
String mod = keys.next();
83136
String modVersion = dependencies.getString(mod);
84-
if(externals.contains(mod + ":" + modVersion)) {
85-
continue;
137+
if(externalMap.containsKey(mod)) {
138+
String extVersion = externalMap.get(mod);
139+
if (extVersion != null) {
140+
try {
141+
boolean matches = new Semver(extVersion.replace("v", "")).satisfies(modVersion.replace("v", ""));
142+
if(!matches) {
143+
logger.fail("External dependency '" + mod + "' version '" + extVersion + "' does not match required '" + modVersion + "'");
144+
errors = true;
145+
} else {
146+
logger.info("Dependency '" + mod + "' found as external");
147+
}
148+
} catch(Exception ex) {
149+
logger.fatal("Unable to get version: " + ex.getMessage());
150+
if(!extVersion.equals(modVersion)) {
151+
logger.fail("External dependency '" + mod + "' version '" + extVersion + "' does not match required '" + modVersion + "' (note: optionality is ignored when verifying external dependencies)");
152+
errors = true;
153+
} else {
154+
logger.info("Dependency '" + mod + "' found as external");
155+
}
156+
}
157+
continue;
158+
} else {
159+
logger.info("Dependency '" + mod + "' marked as external");
160+
continue;
161+
}
86162
}
87163
Path tempPath = Paths.get(System.getProperty("java.io.tmpdir"), mod + ".geode");
88164
Path buildPath = Paths.get(folder, "geode-deps", mod);
89-
// netttttt
90-
String url = ConfigGet.getIndexUrl() + "/v1/mods/" + mod + "/versions";
91165
if(!Files.exists(tempPath)) {
92-
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(url)).header("User-Agent", "GeodeCLI").GET().build();
93-
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
94-
if (response.statusCode() != 200) {
95-
logger.fatal("Bad status code on " + url + " : " + response.statusCode());
166+
JSONArray versions = GetVersions.getVersions(client, mod);
167+
JSONObject selectedVersion = null;
168+
for(int i = 0; i < versions.length(); i++) {
169+
JSONObject ver = versions.getJSONObject(i);
170+
if(new Semver(ver.getString("version")).satisfies(modVersion)) {
171+
selectedVersion = ver;
172+
break;
173+
}
96174
}
97-
JSONArray respJSON = new JSONObject(response.body()).getJSONObject("payload").getJSONArray("data");
98-
if(respJSON.isEmpty()) {
99-
logger.fatal("Dependency not found");
175+
if(selectedVersion == null) {
176+
logger.fail("Dependency '" + mod + "' version '" + modVersion + "' not found");
177+
errors = true;
178+
continue;
100179
}
180+
String downloadLink = selectedVersion.getString("download_link");
101181
logger.info("Downloading " + mod);
102-
String downloadLink = respJSON.getJSONObject(0).getString("download_link");
103182
HttpRequest downloadReq = HttpRequest.newBuilder().uri(URI.create(downloadLink)).header("User-Agent", "GeodeCLI").GET().build();
104183
Files.createDirectories(buildPath);
105184
client.send(downloadReq, HttpResponse.BodyHandlers.ofFile(tempPath));
@@ -133,13 +212,19 @@ public void run() {
133212
Files.write(depOptionsPath, depJSON.toString().getBytes());
134213
}
135214
client.close();
136-
logger.done("All dependencies resolved");
215+
if (!errors) {
216+
logger.done("All dependencies resolved");
217+
} else {
218+
logger.fatal("Some dependencies were not resolved");
219+
}
137220
} catch(JSONException ex) {
138221
logger.fatal("JSON failed: " + ex.getMessage());
139222
} catch(InterruptedException ex) {
140223
logger.fail("Interrupted");
141224
} catch(IOException ex) {
142225
logger.fatal("Client error: " + ex.getMessage());
226+
} catch(Exception ex) {
227+
logger.fatal("Unknown error: " + ex.getMessage());
143228
}
144229
// i cant believe that it IS working ❤️‍🩹
145230
}

0 commit comments

Comments
 (0)