Skip to content

Commit

Permalink
updated to 3.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Prunoideae committed Sep 3, 2022
1 parent 8d8b907 commit 320d2a7
Show file tree
Hide file tree
Showing 28 changed files with 903 additions and 20 deletions.
11 changes: 5 additions & 6 deletions common/src/main/java/com/probejs/ProbeCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
import com.probejs.document.parser.processor.DocumentProviderHandler;
import com.probejs.formatter.ClassResolver;
import com.probejs.formatter.NameResolver;
import com.probejs.info.ClassInfo;
import com.probejs.info.MethodInfo;
import com.probejs.jdoc.document.DocumentClass;
import dev.latvian.mods.kubejs.KubeJSPaths;
import dev.latvian.mods.kubejs.item.ingredient.IngredientJS;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.TextComponent;
Expand Down Expand Up @@ -105,12 +108,8 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
.then(Commands.literal("test")
.requires(source -> false)
.executes(context -> {
Arrays.stream(ServerLevel.class.getMethods())
.map(method -> new MethodInfo(method, ServerLevel.class))
.forEach(method -> {
ProbeJS.LOGGER.info(method.getName());
ProbeJS.LOGGER.info(method.isDefaultMethod());
});
DocumentClass document = DocumentClass.fromJava(ClassInfo.getOrCache(IngredientJS.class));
ProbeJS.LOGGER.info(document.serialize().toString());
return Command.SINGLE_SUCCESS;
}))
);
Expand Down
2 changes: 2 additions & 0 deletions common/src/main/java/com/probejs/ProbeJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.probejs.jdoc.Serde;
import dev.architectury.event.events.common.CommandRegistrationEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -17,5 +18,6 @@ public class ProbeJS {

public static void init() {
CommandRegistrationEvent.EVENT.register((dispatcher, selection) -> ProbeCommands.register(dispatcher));
Serde.init();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class DummyBindingEvent extends BindingsEvent {
private final HashMap<String, BaseFunction> functionDump = new HashMap<>();

public DummyBindingEvent(ScriptManager manager) {
super(manager, null, null);
super(manager, null);
}

@Override
Expand Down
5 changes: 1 addition & 4 deletions common/src/main/java/com/probejs/formatter/SpecialTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ public String format(ITypeInfo typeInfo) {
if (resolvedReturn instanceof TypeInfoVariable) {
resolvedReturn = variableMap.getOrDefault(resolvedReturn.getTypeName(), new TypeInfoClass(Object.class));
}
//Use special to support most of the special wrappers in the callback.
//Only problem is that it might be problematic when actually applying the methods...
//But for most of the time, FunctionalInterface is meant to be passed into Java.
return "((%s) => %s)".formatted(String.join(", ", formattedParam), new FormatterType(resolvedReturn, true).format(0, 0));
return "((%s) => %s)".formatted(String.join(", ", formattedParam), new FormatterType(resolvedReturn, false).format(0, 0));
}
}

Expand Down
10 changes: 9 additions & 1 deletion common/src/main/java/com/probejs/info/ClassInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private ClassInfo(Class<?> clazz) {
fldInfo = Arrays.stream(clazzRaw.getFields())
.filter(f -> f.getDeclaringClass() == clazz)
.map(FieldInfo::new)
.filter(f -> ClassResolver.acceptField(f.getName()))
.filter(f -> ClassResolver.acceptField(f.getName()) || ProbeConfig.INSTANCE.allowObfuscated)
.filter(f -> !f.shouldHide())
.filter(f -> !f.isTransient())
.collect(Collectors.toList());
Expand Down Expand Up @@ -127,4 +127,12 @@ public String getName() {
return name;
}

public ITypeInfo getSuperClassType() {
return InfoTypeResolver.resolveType(clazzRaw.getGenericSuperclass());
}

public List<ITypeInfo> getInterfaceTypes() {
return Arrays.stream(clazzRaw.getGenericInterfaces()).map(InfoTypeResolver::resolveType).collect(Collectors.toList());
}

}
4 changes: 2 additions & 2 deletions common/src/main/java/com/probejs/info/MethodInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class MethodInfo {
private ITypeInfo returnType;
private List<ParamInfo> params;
private List<ITypeInfo> typeVariables;
public static final Remapper RUNTIME = RemappingHelper.createModRemapper();
public static final Remapper RUNTIME = RemappingHelper.getMinecraftRemapper();

private static String getRemappedOrDefault(Method method, Class<?> from) {
String s = method.getName();
Expand All @@ -40,7 +40,7 @@ private static String getRemappedOrDefault(Method method, Class<?> from) {
}
from = from.getSuperclass();
}
return s;
return s.isEmpty() ? method.getName() : s;
}

public MethodInfo(Method method, Class<?> from) {
Expand Down
7 changes: 7 additions & 0 deletions common/src/main/java/com/probejs/jdoc/IDocumentProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.probejs.jdoc;

import com.probejs.jdoc.document.AbstractDocument;

public interface IDocumentProvider<T extends AbstractDocument> {
T genDoc();
}
6 changes: 6 additions & 0 deletions common/src/main/java/com/probejs/jdoc/IPropertyProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.probejs.jdoc;

import com.probejs.jdoc.property.AbstractProperty;

public interface IPropertyProvider<T extends AbstractProperty> {
}
9 changes: 9 additions & 0 deletions common/src/main/java/com/probejs/jdoc/ISerde.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.probejs.jdoc;

import com.google.gson.JsonObject;

public interface ISerde {
JsonObject serialize();

void deserialize(JsonObject object);
}
4 changes: 4 additions & 0 deletions common/src/main/java/com/probejs/jdoc/Manager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.probejs.jdoc;

public class Manager {
}
115 changes: 115 additions & 0 deletions common/src/main/java/com/probejs/jdoc/Serde.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package com.probejs.jdoc;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.probejs.info.ClassInfo;
import com.probejs.info.type.*;
import com.probejs.jdoc.document.AbstractDocument;
import com.probejs.jdoc.document.DocumentClass;
import com.probejs.jdoc.document.DocumentField;
import com.probejs.jdoc.document.DocumentMethod;
import com.probejs.jdoc.property.*;

import java.lang.reflect.*;
import java.util.Collection;
import java.util.function.Supplier;

public class Serde {
public static void init() {
//Types
AbstractProperty.PROPERTY_TYPE_REGISTRY.put(PropertyType.Array.class, "type:array");
AbstractProperty.PROPERTY_TYPE_REGISTRY.put(PropertyType.Clazz.class, "type:class");
AbstractProperty.PROPERTY_TYPE_REGISTRY.put(PropertyType.Parameterized.class, "type:parameterized");
AbstractProperty.PROPERTY_TYPE_REGISTRY.put(PropertyType.Variable.class, "type:variable");
AbstractProperty.PROPERTY_TYPE_REGISTRY.put(PropertyType.Primitive.class, "type:primitive");
AbstractProperty.PROPERTY_TYPE_REGISTRY.put(PropertyType.Intersection.class, "type:intersection");
AbstractProperty.PROPERTY_TYPE_REGISTRY.put(PropertyType.Union.class, "type:union");

//Properties
AbstractProperty.PROPERTY_TYPE_REGISTRY.put(PropertyComment.class, "property:comment");
AbstractProperty.PROPERTY_TYPE_REGISTRY.put(PropertyHide.class, "property:hide");
AbstractProperty.PROPERTY_TYPE_REGISTRY.put(PropertyMod.class, "property:mod");
AbstractProperty.PROPERTY_TYPE_REGISTRY.put(PropertyModify.class, "property:modify");
AbstractProperty.PROPERTY_TYPE_REGISTRY.put(PropertyRename.class, "property:rename");
AbstractProperty.PROPERTY_TYPE_REGISTRY.put(PropertyReturns.class, "property:returns");
AbstractProperty.PROPERTY_TYPE_REGISTRY.put(PropertyParam.class, "property:param");

//Documents
AbstractDocument.DOCUMENT_TYPE_REGISTRY.put(DocumentClass.class, "document:class");
AbstractDocument.DOCUMENT_TYPE_REGISTRY.put(DocumentMethod.class, "document:method");
AbstractDocument.DOCUMENT_TYPE_REGISTRY.put(DocumentField.class, "document:field");

}

public static AbstractDocument deserializeDocument(JsonObject obj) {
String type = obj.get("type").getAsString();
try {
AbstractDocument doc = AbstractDocument.DOCUMENT_TYPE_REGISTRY.inverse().get(type).getDeclaredConstructor().newInstance();
doc.deserialize(obj);
return doc;
} catch (InstantiationException | IllegalAccessException | InvocationTargetException |
NoSuchMethodException e) {
e.printStackTrace();
}
return null;
}

public static AbstractProperty deserializeProperty(JsonObject obj) {
String type = obj.get("type").getAsString();
try {
AbstractProperty property = AbstractProperty.PROPERTY_TYPE_REGISTRY.inverse().get(type).getDeclaredConstructor().newInstance();
property.deserialize(obj);
return property;
} catch (InstantiationException | IllegalAccessException | InvocationTargetException |
NoSuchMethodException e) {
e.printStackTrace();
}
return null;
}

private static PropertyType constructType(Supplier<PropertyType> builder, ITypeInfo type) {
PropertyType property = builder.get();
property.deserializeFromType(type);
return property;
}

public static PropertyType deserializeFromJavaType(ITypeInfo type) {
if (type instanceof TypeInfoClass) {
return constructType(PropertyType.Clazz::new, type);
}
if (type instanceof TypeInfoArray) {
return constructType(PropertyType.Array::new, type);
}
if (type instanceof TypeInfoVariable) {
return constructType(PropertyType.Variable::new, type);
}
if (type instanceof TypeInfoParameterized) {
return constructType(PropertyType.Parameterized::new, type);
}
if (type instanceof TypeInfoWildcard) {
return constructType(PropertyType.Clazz::new, new TypeInfoClass(Object.class));
}
return null;
}

public static JsonArray serializeCollection(Iterable<? extends ISerde> serdes) {
JsonArray result = new JsonArray();
serdes.forEach(serde -> result.add(serde.serialize()));
return result;
}

@SuppressWarnings("unchecked")
public static <T extends AbstractDocument> void deserializeDocuments(Collection<T> serdes, JsonElement jsonArray) {
for (JsonElement element : jsonArray.getAsJsonArray()) {
serdes.add((T) deserializeDocument(element.getAsJsonObject()));
}
}

@SuppressWarnings("unchecked")
public static <T extends AbstractProperty> void deserializeProperties(Collection<T> serdes, JsonElement jsonArray) {
for (JsonElement element : jsonArray.getAsJsonArray()) {
serdes.add((T) deserializeProperty(element.getAsJsonObject()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.probejs.jdoc.document;

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.probejs.jdoc.ISerde;
import com.probejs.jdoc.Serde;
import com.probejs.jdoc.property.AbstractProperty;
import com.probejs.jdoc.property.PropertyHide;
import com.probejs.jdoc.property.PropertyMod;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
* Represents a document.
* <p>
* You must ensure that the parameter-less constructor <b>does not</b> depend
* on any other stateful objects. As this document might be constructed at any time.
*/
public abstract class AbstractDocument<T extends AbstractDocument<T>> implements ISerde {
public static final BiMap<Class<? extends AbstractDocument<?>>, String> DOCUMENT_TYPE_REGISTRY = HashBiMap.create();

protected List<AbstractProperty> properties = new ArrayList<>();

public abstract T merge(T other);

public abstract T copy();

public JsonObject serialize() {
JsonObject obj = new JsonObject();
obj.addProperty("type", DOCUMENT_TYPE_REGISTRY.get(this.getClass()));
JsonArray jsonProperties = new JsonArray();
for (AbstractProperty property : properties) {
jsonProperties.add(property.serialize());
}
obj.add("properties", jsonProperties);
return obj;
}

public void deserialize(JsonObject object) {
JsonArray propertiesJson = object.get("properties").getAsJsonArray();
for (JsonElement element : propertiesJson) {
AbstractProperty property = Serde.deserializeProperty(element.getAsJsonObject());
this.properties.add(property);
}
}

public Optional<AbstractProperty> findProperty(Class<? extends AbstractProperty> property) {
return this.properties.stream().filter(p -> p.getClass() == property).findFirst();
}

public boolean hasProperty(Class<? extends AbstractProperty> property) {
return findProperty(property).isPresent();
}

public List<AbstractProperty> findProperties(Predicate<AbstractProperty> predicate) {
return this.properties.stream().filter(predicate).collect(Collectors.toList());
}

@SuppressWarnings("unchecked")
public <T extends AbstractProperty> List<T> findPropertiesOf(Class<T> property) {
return this.properties.stream().filter(prop -> property.isAssignableFrom(prop.getClass())).map(prop -> (T) prop).collect(Collectors.toList());
}

public boolean allModsLoaded() {
return findPropertiesOf(PropertyMod.class).stream().allMatch(PropertyMod::isModLoaded);
}

public boolean isHidden() {
return hasProperty(PropertyHide.class);
}
}
Loading

0 comments on commit 320d2a7

Please sign in to comment.