mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-30 01:34:04 +00:00
Host support for built-in classes Iris's classes hold (studio schema, snippets, bounded palette cache); tests for the placement settings
This commit is contained in:
1 parent
49b82f7c16
commit
7da0ccde83
17 files changed
+579
-100
No files matched your search
@@ -734,11 +734,11 @@ public class IrisData implements ExclusionStrategy, TypeAdapterFactory {
|
||||
|
||||
@Override
|
||||
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
|
||||
if (!typeToken.getRawType().isAnnotationPresent(Snippet.class)) {
|
||||
String snippetType = snippetFolder(typeToken.getRawType());
|
||||
if (snippetType == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String snippetType = typeToken.getRawType().getDeclaredAnnotation(Snippet.class).value();
|
||||
String snippedBase = "snippet/" + snippetType + "/";
|
||||
|
||||
return new TypeAdapter<>() {
|
||||
@@ -788,6 +788,17 @@ public class IrisData implements ExclusionStrategy, TypeAdapterFactory {
|
||||
};
|
||||
}
|
||||
|
||||
/** The snippet folder of a class Iris's or a built-in module's {@code @Snippet} names, or null. */
|
||||
private static String snippetFolder(Class<?> type) {
|
||||
if (type.isAnnotationPresent(Snippet.class)) {
|
||||
return type.getDeclaredAnnotation(Snippet.class).value();
|
||||
}
|
||||
if (type.isAnnotationPresent(com.volmit.config.Snippet.class)) {
|
||||
return type.getDeclaredAnnotation(com.volmit.config.Snippet.class).value();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public KList<String> getPossibleSnippets(String f) {
|
||||
return possibleSnippets.computeIfAbsent(f, (k) -> {
|
||||
KList<String> l = new KList<>();
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
package art.arcane.iris.core.modules;
|
||||
|
||||
import art.arcane.iris.core.loader.IrisData;
|
||||
import art.arcane.iris.engine.data.cache.LazyBoundedCache;
|
||||
import art.arcane.iris.engine.object.material.IrisBlockData;
|
||||
import art.arcane.iris.engine.object.material.IrisMaterialPalette;
|
||||
import art.arcane.iris.engine.object.prefab.TileData;
|
||||
@@ -51,9 +52,12 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
* the seed the palette's old {@code RNG} had.
|
||||
*/
|
||||
final class IrisBlockRegistry implements BlockRegistry {
|
||||
/** Enough for the palettes of a pack with their world seeds; seeds that differ per call (a placement's) wear out the oldest. */
|
||||
private static final int PALETTE_CACHE_SIZE = 256;
|
||||
|
||||
private final IrisData data;
|
||||
private final Map<BlockSpec, IrisBlockData> specs = new ConcurrentHashMap<>();
|
||||
private final Map<SeededPalette, PaletteSampler> palettes = new ConcurrentHashMap<>();
|
||||
private final LazyBoundedCache<SeededPalette, PaletteSampler> palettes = new LazyBoundedCache<>(PALETTE_CACHE_SIZE);
|
||||
|
||||
/** @param data the pack whose {@code blocks} folder custom blocks come from */
|
||||
IrisBlockRegistry(IrisData data) {
|
||||
|
||||
@@ -115,8 +115,13 @@ final class ModuleSchemaRenderer {
|
||||
return property;
|
||||
}
|
||||
|
||||
/** Whether the pack's modules describe the class {@code typeName}. */
|
||||
boolean describes(String typeName) {
|
||||
return schema.types().stream().anyMatch(type -> type.name().equals(typeName));
|
||||
}
|
||||
|
||||
/** The definition key of a settings class, built the first time a property names it. */
|
||||
private String definition(String typeName) {
|
||||
String definition(String typeName) {
|
||||
String key = DEFINITION_PREFIX + typeName.replace('.', '-').replace('$', '-').toLowerCase(Locale.ROOT);
|
||||
if (definitions.containsKey(key)) {
|
||||
return key;
|
||||
|
||||
@@ -82,6 +82,7 @@ import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
@@ -403,13 +404,7 @@ public class SchemaBuilder {
|
||||
|
||||
/** Iris's own definition of one of its classes that a built-in module's settings hold, as Iris's fields describe it. */
|
||||
private String hostDefinition(String typeName) {
|
||||
Class<?> type = hostType(typeName);
|
||||
String key = objectDefinitionKey(type);
|
||||
if (!definitions.containsKey(key)) {
|
||||
definitions.put(key, new JSONObject());
|
||||
definitions.put(key, buildProperties(type));
|
||||
}
|
||||
return key;
|
||||
return objectDefinition(hostType(typeName));
|
||||
}
|
||||
|
||||
private static Class<?> hostType(String typeName) {
|
||||
@@ -420,6 +415,24 @@ public class SchemaBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
/** The definition key of a class an Iris field holds: the module's own description of a held type, else Iris's. */
|
||||
private String objectDefinition(Class<?> type) {
|
||||
if (isHeldType(type)) {
|
||||
return moduleSchemas().definition(type.getName());
|
||||
}
|
||||
String key = objectDefinitionKey(type);
|
||||
if (!definitions.containsKey(key)) {
|
||||
definitions.put(key, new JSONObject());
|
||||
definitions.put(key, buildProperties(type));
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
||||
/** Whether a built-in module registered {@code type} because Iris's classes hold it, so its fields are the module's. */
|
||||
private boolean isHeldType(Class<?> type) {
|
||||
return type.isAnnotationPresent(com.volmit.config.Desc.class) && moduleSchemas().describes(type.getName());
|
||||
}
|
||||
|
||||
private static String objectDefinitionKey(Class<?> type) {
|
||||
return "obj-" + type.getCanonicalName().replaceAll("\\Q.\\E", "-").toLowerCase();
|
||||
}
|
||||
@@ -738,12 +751,7 @@ public class SchemaBuilder {
|
||||
}
|
||||
} else {
|
||||
fancyType = k.getType().getSimpleName().replaceAll("\\QIris\\E", "") + " (Object)";
|
||||
String key = objectDefinitionKey(k.getType());
|
||||
if (!definitions.containsKey(key)) {
|
||||
definitions.put(key, new JSONObject());
|
||||
definitions.put(key, buildProperties(k.getType()));
|
||||
}
|
||||
prop.put("$ref", "#/definitions/" + key);
|
||||
prop.put("$ref", "#/definitions/" + objectDefinition(k.getType()));
|
||||
}
|
||||
}
|
||||
case "array" -> {
|
||||
@@ -766,13 +774,8 @@ public class SchemaBuilder {
|
||||
case "number" -> fancyType = "List of Numbers";
|
||||
case "object" -> {
|
||||
fancyType = "List of " + t.type().getSimpleName().replaceAll("\\QIris\\E", "") + "s (Objects)";
|
||||
String key = objectDefinitionKey(t.type());
|
||||
if (!definitions.containsKey(key)) {
|
||||
definitions.put(key, new JSONObject());
|
||||
definitions.put(key, buildProperties(t.type()));
|
||||
}
|
||||
JSONObject items = new JSONObject();
|
||||
items.put("$ref", "#/definitions/" + key);
|
||||
items.put("$ref", "#/definitions/" + objectDefinition(t.type()));
|
||||
prop.put("items", items);
|
||||
}
|
||||
case "string" -> {
|
||||
@@ -895,16 +898,16 @@ public class SchemaBuilder {
|
||||
boolean present = !typeDesc.isBlank();
|
||||
if (present) d.add(typeDesc);
|
||||
|
||||
Snippet snippet = k.getType().getDeclaredAnnotation(Snippet.class);
|
||||
if (snippet == null) {
|
||||
Optional<String> snippetFolder = snippetFolder(k.getType());
|
||||
if (snippetFolder.isEmpty()) {
|
||||
ArrayType array = k.getType().getDeclaredAnnotation(ArrayType.class);
|
||||
if (array != null) {
|
||||
snippet = array.type().getDeclaredAnnotation(Snippet.class);
|
||||
snippetFolder = snippetFolder(array.type());
|
||||
}
|
||||
}
|
||||
|
||||
if (snippet != null) {
|
||||
String sm = snippet.value();
|
||||
if (snippetFolder.isPresent()) {
|
||||
String sm = snippetFolder.get();
|
||||
if (present) d.add(" ");
|
||||
d.add("You can instead specify \"snippet/" + sm + "/some-name.json\" to use a snippet file instead of specifying it here.");
|
||||
present = false;
|
||||
@@ -940,6 +943,16 @@ public class SchemaBuilder {
|
||||
return buildSnippet(prop, k.getType());
|
||||
}
|
||||
|
||||
/** The snippet folder of a class, as Iris's or the module's {@code @Snippet} names it. */
|
||||
private static Optional<String> snippetFolder(Class<?> type) {
|
||||
Snippet snippet = type.getDeclaredAnnotation(Snippet.class);
|
||||
if (snippet != null) {
|
||||
return Optional.of(snippet.value());
|
||||
}
|
||||
com.volmit.config.Snippet moduleSnippet = type.getDeclaredAnnotation(com.volmit.config.Snippet.class);
|
||||
return moduleSnippet == null ? Optional.empty() : Optional.of(moduleSnippet.value());
|
||||
}
|
||||
|
||||
private JSONObject buildSnippet(JSONObject prop, Class<?> type) {
|
||||
Snippet snippet = type.getDeclaredAnnotation(Snippet.class);
|
||||
if (snippet == null) return prop;
|
||||
@@ -1007,15 +1020,14 @@ public class SchemaBuilder {
|
||||
@NotNull
|
||||
private <T> String addEnum(Class<?> type, JSONObject prop, KList<String> description, T[] values, Function<T, String> function) {
|
||||
JSONArray a = new JSONArray();
|
||||
boolean advanced = type.isAnnotationPresent(Desc.class);
|
||||
boolean advanced = type.isAnnotationPresent(Desc.class) || type.isAnnotationPresent(com.volmit.config.Desc.class);
|
||||
for (T gg : values) {
|
||||
if (advanced) {
|
||||
try {
|
||||
JSONObject j = new JSONObject();
|
||||
String name = function.apply(gg);
|
||||
j.put("const", name);
|
||||
Desc dd = type.getField(name).getAnnotation(Desc.class);
|
||||
String desc = dd == null ? ("No Description for " + name) : dd.value();
|
||||
String desc = constantDescription(type, name);
|
||||
j.put("description", desc);
|
||||
j.put("x-intellij-html-description", desc.replace("\n", "<br>"));
|
||||
a.put(j);
|
||||
@@ -1040,6 +1052,16 @@ public class SchemaBuilder {
|
||||
return type.getSimpleName().replaceAll("\\QIris\\E", "");
|
||||
}
|
||||
|
||||
private static String constantDescription(Class<?> enumType, String name) throws NoSuchFieldException {
|
||||
Field constant = enumType.getField(name);
|
||||
Desc described = constant.getAnnotation(Desc.class);
|
||||
if (described != null) {
|
||||
return described.value();
|
||||
}
|
||||
com.volmit.config.Desc moduleDescribed = constant.getAnnotation(com.volmit.config.Desc.class);
|
||||
return moduleDescribed == null ? "No Description for " + name : moduleDescribed.value();
|
||||
}
|
||||
|
||||
private JSONObject buildBlockPropertyJson(PlatformBlockProperty property) {
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("type", property.jsonType());
|
||||
@@ -1114,7 +1136,8 @@ public class SchemaBuilder {
|
||||
return "object";
|
||||
}
|
||||
|
||||
if (!c.isAnnotationPresent(Desc.class) && c.getCanonicalName().startsWith("art.arcane.iris.")) {
|
||||
if (!c.isAnnotationPresent(Desc.class) && !c.isAnnotationPresent(com.volmit.config.Desc.class)
|
||||
&& c.getCanonicalName().startsWith("art.arcane.iris.")) {
|
||||
warnings.addIfMissing("Unsupported Type: " + c.getCanonicalName() + " Did you forget @Desc?");
|
||||
}
|
||||
|
||||
@@ -1136,6 +1159,10 @@ public class SchemaBuilder {
|
||||
return r.getDeclaredAnnotation(Desc.class).value();
|
||||
}
|
||||
|
||||
if (r.isAnnotationPresent(com.volmit.config.Desc.class)) {
|
||||
return r.getDeclaredAnnotation(com.volmit.config.Desc.class).value();
|
||||
}
|
||||
|
||||
if (!r.isPrimitive() && !r.equals(KList.class) && !r.equals(KMap.class) && r.getCanonicalName().startsWith("art.arcane.")) {
|
||||
warnings.addIfMissing("Missing @Desc on " + r.getSimpleName() + " in " + (r.getDeclaringClass() != null ? r.getDeclaringClass().getCanonicalName() : " NOSRC"));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user