Inceptum/launcher/src/main/java/io/gitlab/jfronny/inceptum/launcher/gson/RulesAdapter.java

62 lines
2.4 KiB
Java

package io.gitlab.jfronny.inceptum.launcher.gson;
import io.gitlab.jfronny.commons.OSUtils;
import io.gitlab.jfronny.gson.JsonParseException;
import io.gitlab.jfronny.gson.stream.JsonReader;
import io.gitlab.jfronny.gson.stream.JsonWriter;
import io.gitlab.jfronny.inceptum.launcher.model.mojang.Rules;
import java.io.IOException;
public class RulesAdapter {
public static void write(Rules rules, JsonWriter writer) throws IOException {
throw new UnsupportedOperationException();
}
public static Rules read(JsonReader reader) throws IOException {
boolean valid = true;
reader.beginArray();
while (reader.hasNext()) {
if (!valid) {
reader.skipValue();
continue;
}
reader.beginObject();
String actionType = null;
boolean hasFeatures = false;
String osName = null;
String osVersion = null;
while (reader.hasNext()) {
switch (reader.nextName()) {
case "action" -> actionType = reader.nextString();
case "features" -> {
reader.skipValue();
hasFeatures = true;
}
case "os" -> {
reader.beginObject();
while (reader.hasNext()) {
switch (reader.nextName()) {
case "name" -> osName = reader.nextString();
case "version" -> osVersion = reader.nextString();
default -> reader.skipValue();
}
}
reader.endObject();
}
}
}
reader.endObject();
if (actionType == null || (!actionType.equals("allow") && !actionType.equals("disallow"))) {
throw new JsonParseException("Unexpected action in argument: " + actionType);
}
if (hasFeatures) valid = false;
if (osName != null && !OSUtils.TYPE.mojName.equals(osName)) valid = false;
if (osVersion != null && !System.getProperty("os.version").matches(osVersion)) valid = false;
if (actionType.equals("disallow")) valid = !valid;
}
reader.endArray();
return new Rules(valid);
}
}