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

45 lines
1.7 KiB
Java

package io.gitlab.jfronny.inceptum.gson;
import com.google.gson.*;
import io.gitlab.jfronny.inceptum.model.mojang.Rules;
import io.gitlab.jfronny.inceptum.util.Utils;
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();
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;
if (ro.has("features")) { //TODO support has_custom_resolution
matched = false;
}
if (ro.has("os")) {
JsonObject osObject = ro.get("os").getAsJsonObject();
if (osObject.has("name")) {
if (!Utils.OS.equals(osObject.get("name").getAsString())) {
matched = false;
}
}
if (osObject.has("version")) {
if (!System.getProperty("os.version").matches(osObject.get("version").getAsString())) {
matched = false;
}
}
}
if (actionType.equals("disallow")) matched = !matched;
if (!matched) {
valid = false;
break;
}
}
return new Rules(valid);
}
}