Schema hell

This commit is contained in:
cyberpwn 2021-08-30 08:09:48 -04:00
parent fbdb060e10
commit 801c44389e
5 changed files with 118 additions and 5 deletions

View File

@ -202,6 +202,23 @@ public class Iris extends VolmitPlugin implements Listener {
return v;
}
public static KList<Class<?>> getClasses(String s, Class<? extends Annotation> slicedClass) {
JarScanner js = new JarScanner(instance.getJarFile(), s);
KList<Class<?>> v = new KList<>();
J.attempt(js::scan);
for (Class<?> i : js.getClasses()) {
if (slicedClass == null || i.isAnnotationPresent(slicedClass)) {
try {
v.add(i);
} catch (Throwable ignored) {
}
}
}
return v;
}
public static KList<Object> initialize(String s) {
return initialize(s, null);
}

View File

@ -80,6 +80,7 @@ public class IrisData implements ExclusionStrategy, TypeAdapterFactory {
private ResourceLoader<IrisScript> scriptLoader;
private ResourceLoader<IrisCave> caveLoader;
private ResourceLoader<IrisRavine> ravineLoader;
private KMap<String, KList<String>> possibleSnippets;
private Gson gson;
private Gson snippetLoader;
private GsonBuilder builder;
@ -206,6 +207,7 @@ public class IrisData implements ExclusionStrategy, TypeAdapterFactory {
}
public synchronized void hotloaded() {
possibleSnippets = new KMap<>();
builder = new GsonBuilder()
.addDeserializationExclusionStrategy(this)
.addSerializationExclusionStrategy(this)
@ -450,4 +452,22 @@ public class IrisData implements ExclusionStrategy, TypeAdapterFactory {
}
};
}
public KList<String> getPossibleSnippets(String f) {
return possibleSnippets.computeIfAbsent(f, (k) -> {
KList<String> l = new KList<>();
File snippetFolder = new File(getDataFolder(), "snippet/" + f);
if(snippetFolder.exists() && snippetFolder.isDirectory())
{
for(File i : snippetFolder.listFiles())
{
l.add("snippet/" + f + "/" + i.getName().split("\\Q.\\E")[0]);
}
}
return l;
});
}
}

View File

@ -88,7 +88,9 @@ public class ResourceLoader<T extends IrisRegistrant> {
}
o.put("fileMatch", new JSONArray(fm.toArray()));
o.put("schema", new SchemaBuilder(objectClass, manager).compute());
o.put("url", "./.iris/schema/" + getFolderName() + "-schema.json");
File a = new File(getManager().getDataFolder(), ".iris/schema/" + getFolderName() + "-schema.json");
J.attemptAsync(() -> IO.writeAll(a, new SchemaBuilder(objectClass, manager).compute().toString(4)));
return o;
}

View File

@ -35,11 +35,13 @@ import com.volmit.iris.engine.object.IrisObject;
import com.volmit.iris.engine.object.IrisObjectPlacement;
import com.volmit.iris.engine.object.IrisRegion;
import com.volmit.iris.engine.object.IrisSpawner;
import com.volmit.iris.engine.object.annotations.Snippet;
import com.volmit.iris.engine.platform.PlatformChunkGenerator;
import com.volmit.iris.util.collection.KList;
import com.volmit.iris.util.collection.KMap;
import com.volmit.iris.util.collection.KSet;
import com.volmit.iris.util.exceptions.IrisException;
import com.volmit.iris.util.format.Form;
import com.volmit.iris.util.io.IO;
import com.volmit.iris.util.json.JSONArray;
import com.volmit.iris.util.json.JSONObject;
@ -274,6 +276,41 @@ public class IrisProject {
}
}
for(Class<?> i : Iris.getClasses("com.volmit.iris.engine.object.", Snippet.class))
{
try
{
String snipType = i.getDeclaredAnnotation(Snippet.class).value();
JSONObject o = new JSONObject();
KList<String> fm = new KList<>();
for (int g = 1; g < 8; g++) {
fm.add("/snippet/" + snipType + Form.repeat("/*", g) + ".json");
}
o.put("fileMatch", new JSONArray(fm.toArray()));
o.put("url", "./.iris/schema/snippet/" + snipType + "-schema.json");
schemas.put(o);
File a = new File(dm.getDataFolder(), ".iris/schema/snippet/" + snipType + "-schema.json");
J.attemptAsync(() -> {
try
{
IO.writeAll(a, new SchemaBuilder(i, dm).compute().toString(4));
}
catch(Throwable e)
{
e.printStackTrace();
}
});
}
catch(Throwable e)
{
e.printStackTrace();
}
}
settings.put("json.schemas", schemas);
ws.put("settings", settings);

View File

@ -120,10 +120,6 @@ public class SchemaBuilder {
JSONObject property = buildProperty(k, c);
if (property.getBoolean("!required")) {
required.put(k.getName());
}
property.remove("!required");
properties.put(k.getName(), property);
}
@ -134,6 +130,21 @@ public class SchemaBuilder {
o.put("properties", properties);
if(c.isAnnotationPresent(Snippet.class))
{
JSONObject anyOf = new JSONObject();
JSONArray arr = new JSONArray();
JSONObject str = new JSONObject();
str.put("type", "string");
arr.put(o);
arr.put(str);
anyOf.put("anyOf", arr);
anyOf.put("description", getDescription(c));
return anyOf;
}
return o;
}
@ -554,6 +565,32 @@ public class SchemaBuilder {
prop.put("type", type);
prop.put("description", d.toString("\n"));
if(k.getType().isAnnotationPresent(Snippet.class))
{
JSONObject anyOf = new JSONObject();
JSONArray arr = new JSONArray();
JSONObject str = new JSONObject();
str.put("type", "string");
String key = "enum-snippet-" + k.getType().getDeclaredAnnotation(Snippet.class).value();
str.put("$ref", "#/definitions/"+key);
if (!definitions.containsKey(key)) {
JSONObject j = new JSONObject();
JSONArray snl = new JSONArray();
data.getPossibleSnippets(k.getType().getDeclaredAnnotation(Snippet.class).value()).forEach(snl::put);
j.put("enum", snl);
definitions.put(key, j);
}
arr.put(prop);
arr.put(str);
anyOf.put("anyOf", arr);
anyOf.put("description", d.toString("\n"));
anyOf.put("!required", k.isAnnotationPresent(Required.class));
return anyOf;
}
return prop;
}