Inceptum/src/main/java/io/gitlab/jfronny/inceptum/gson/RulesDeserializer.java

45 lines
1.7 KiB
Java
Raw Normal View History

2021-10-29 22:50:42 +02:00
package io.gitlab.jfronny.inceptum.gson;
2021-10-27 22:00:08 +02:00
import com.google.gson.*;
2021-10-29 22:50:42 +02:00
import io.gitlab.jfronny.inceptum.model.mojang.Rules;
import io.gitlab.jfronny.inceptum.util.OSCheck;
2021-10-27 22:00:08 +02:00
import java.lang.reflect.Type;
public class RulesDeserializer implements JsonDeserializer<Rules> {
@Override
public Rules deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
boolean valid = true;
for (JsonElement rule : json.getAsJsonArray()) {
JsonObject ro = rule.getAsJsonObject();
2021-10-28 20:19:09 +02:00
String actionType = ro.get("action").getAsJsonPrimitive().getAsString();
if (!actionType.equals("allow") && !actionType.equals("disallow")) {
throw new JsonParseException("Unexpected action in argument: " + actionType);
}
boolean matched = true;
2021-10-27 22:00:08 +02:00
if (ro.has("features")) { //TODO support has_custom_resolution
2021-10-28 20:19:09 +02:00
matched = false;
2021-10-27 22:00:08 +02:00
}
if (ro.has("os")) {
JsonObject osObject = ro.get("os").getAsJsonObject();
if (osObject.has("name")) {
if (!OSCheck.OS.getMojName().equals(osObject.get("name").getAsString())) {
2021-10-28 20:19:09 +02:00
matched = false;
2021-10-27 22:00:08 +02:00
}
}
if (osObject.has("version")) {
if (!System.getProperty("os.version").matches(osObject.get("version").getAsString())) {
2021-10-28 20:19:09 +02:00
matched = false;
2021-10-27 22:00:08 +02:00
}
}
}
2021-10-28 20:19:09 +02:00
if (actionType.equals("disallow")) matched = !matched;
if (!matched) {
valid = false;
break;
}
2021-10-27 22:00:08 +02:00
}
return new Rules(valid);
}
}