Skip to content

Commit

Permalink
Move helper functions to BracketHelper
Browse files Browse the repository at this point in the history
Signed-off-by: TheSilkMiner <thesilkminer@outlook.com>
  • Loading branch information
TheSilkMiner committed May 3, 2022
1 parent 2ab08c3 commit 23ad6de
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package com.blamejared.contenttweaker.core;

import com.blamejared.contenttweaker.core.api.ContentTweakerConstants;
import com.blamejared.contenttweaker.core.plugin.PluginManager;
import com.blamejared.contenttweaker.core.service.ServiceManager;
import com.blamejared.contenttweaker.core.zen.ContentTweakerZenConstants;
import com.blamejared.contenttweaker.core.zen.bracket.ContentTweakerBrackets;
import com.blamejared.crafttweaker.api.CraftTweakerConstants;
import com.blamejared.crafttweaker.api.plugin.CraftTweakerPlugin;
import com.blamejared.crafttweaker.api.plugin.IBracketParserRegistrationHandler;
import com.blamejared.crafttweaker.api.plugin.ICraftTweakerPlugin;
import com.blamejared.crafttweaker.api.plugin.IJavaNativeIntegrationRegistrationHandler;
import com.blamejared.crafttweaker.api.plugin.IListenerRegistrationHandler;
import com.blamejared.crafttweaker.api.plugin.ILoaderRegistrationHandler;
import com.blamejared.crafttweaker.api.plugin.IScriptLoadSourceRegistrationHandler;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.blamejared.contenttweaker.core.api.zen.bracket;

import com.blamejared.contenttweaker.core.zen.rt.ResourceLocationNative;
import com.blamejared.crafttweaker.api.util.ParseUtil;
import net.minecraft.ResourceLocationException;
import net.minecraft.resources.ResourceLocation;
import org.openzen.zencode.shared.CodePosition;
import org.openzen.zencode.shared.CompileException;
import org.openzen.zencode.shared.CompileExceptionCode;
import org.openzen.zenscript.lexer.ParseException;
import org.openzen.zenscript.parser.expression.ParsedCallArguments;
import org.openzen.zenscript.parser.expression.ParsedExpression;
import org.openzen.zenscript.parser.expression.ParsedExpressionCall;
import org.openzen.zenscript.parser.expression.ParsedExpressionMember;
import org.openzen.zenscript.parser.expression.ParsedExpressionString;

import java.util.List;
import java.util.Objects;
import java.util.function.Supplier;

public final class BracketHelper {
@FunctionalInterface
public interface ParseOperation<T> {
T execute() throws ParseException;
}

private BracketHelper() {}

public static ResourceLocation locationOrThrow(final CodePosition position, final String in) throws ParseException {
return locationOrThrow(position, in, () -> "Unable to convert given string \"" + in + "\" to resource location");
}

public static ResourceLocation locationOrThrow(final CodePosition position, final String in, final Supplier<String> message) throws ParseException {
Objects.requireNonNull(position);
Objects.requireNonNull(message);
try {
return new ResourceLocation(Objects.requireNonNull(in, "null"));
} catch (final NullPointerException | ResourceLocationException e) {
throw new ParseException(position, message.get(), e);
}
}

public static ParsedExpression locationArgument(final CodePosition position, final ResourceLocation location) {
Objects.requireNonNull(position);
Objects.requireNonNull(location);
final ParsedExpression receiver = ParseUtil.staticMemberExpression(position, ResourceLocationNative.CLASS_NAME);
final ParsedExpression of = new ParsedExpressionMember(position, receiver, "of", null);
final ParsedExpressionString namespace = new ParsedExpressionString(position, location.getNamespace(), false);
final ParsedExpressionString path = new ParsedExpressionString(position, location.getPath(), false);
final ParsedCallArguments arguments = new ParsedCallArguments(null, List.of(namespace, path));
return new ParsedExpressionCall(position, of, arguments);
}

public static <F> F parseToCompile(final CodePosition position, final ParseOperation<F> operation) throws CompileException {
try {
return operation.execute();
} catch (final ParseException e) {
final CompileException exception = new CompileException(position, CompileExceptionCode.PARSE_ERROR, e.message);
exception.initCause(e);
throw exception;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.blamejared.contenttweaker.core.api.object.ObjectFactory;
import com.blamejared.contenttweaker.core.api.object.ObjectFactoryMapping;
import com.blamejared.contenttweaker.core.api.object.ObjectType;
import com.blamejared.contenttweaker.core.api.zen.bracket.BracketHelper;
import com.blamejared.contenttweaker.core.registry.MetaRegistry;
import com.blamejared.contenttweaker.core.zen.ContentTweakerZenConstants;
import com.blamejared.contenttweaker.core.zen.rt.Unknown;
Expand All @@ -21,7 +22,6 @@
import org.openzen.zencode.java.ZenCodeType;
import org.openzen.zencode.shared.CodePosition;
import org.openzen.zencode.shared.CompileException;
import org.openzen.zencode.shared.CompileExceptionCode;
import org.openzen.zenscript.codemodel.partial.IPartialExpression;
import org.openzen.zenscript.codemodel.scope.ExpressionScope;
import org.openzen.zenscript.lexer.ParseException;
Expand Down Expand Up @@ -60,11 +60,6 @@ public static ObjectFactory<Unknown> factoryFor() {
}

private static final class BracketMetaFactoryExpression<T> extends ParsedExpression {
@FunctionalInterface
private interface ParseOperation<T> {
T run() throws ParseException;
}

private final ObjectType<T> type;

public BracketMetaFactoryExpression(final CodePosition position, final ObjectType<T> type) {
Expand Down Expand Up @@ -103,28 +98,18 @@ private IParsedType findFactoryGeneric() throws CompileException {
}

private ParsedExpression findArgument() throws CompileException {
return this.rethrowing(() -> ParseUtil.createResourceLocationArgument(this.position, this.type.id().location()));
return BracketHelper.parseToCompile(this.position, () -> BracketHelper.locationArgument(this.position, this.type.id().location()));
}

private <L> IParsedType readParsedType(final Class<L> type) throws CompileException {
return this.rethrowing(() -> ParseUtil.readParsedType(this.findClassNameFor(type), this.position));
return BracketHelper.parseToCompile(this.position, () -> ParseUtil.readParsedType(this.findClassNameFor(type), this.position));
}

private <L> String findClassNameFor(final Class<L> type) {
final IScriptLoader loader = CraftTweakerAPI.getScriptRunManager().currentRunInfo().loader();
final IZenClassRegistry classes = CraftTweakerAPI.getRegistry().getZenClassRegistry();
return classes.getNameFor(loader, type).orElseThrow();
}

private <F> F rethrowing(final ParseOperation<F> operation) throws CompileException {
try {
return operation.run();
} catch (final ParseException e) {
final CompileException exception = new CompileException(this.position, CompileExceptionCode.PARSE_ERROR, e.message);
exception.initCause(e);
throw exception;
}
}
}

private static final String META_FACTORY_CLASS = ContentTweakerZenConstants.RT_PACKAGE + ".FactoryBracketMetaFactory";
Expand All @@ -133,6 +118,7 @@ public static Stream<String> dump() {
return Registry.REGISTRY.stream()
.map(Registry::key)
.map(ResourceKey::location)
.sorted()
.map(ResourceLocation::toString)
.map("<factory:%s>"::formatted);
}
Expand All @@ -146,7 +132,7 @@ public ParsedExpression parse(final CodePosition position, final ZSTokenParser t

private ResourceLocation findRegistryId(final CodePosition position, final ZSTokenParser tokens) throws ParseException {
final String bracketContents = ParseUtil.readBracketContent(position, tokens);
return ContentTweakerBrackets.locationOrThrow(
return BracketHelper.locationOrThrow(
position,
bracketContents,
() -> "Expected a registry identifier in the form <factory:modid:name>, but instead found " + bracketContents
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.blamejared.contenttweaker.core.zen.rt;

import com.blamejared.contenttweaker.core.api.ContentTweakerConstants;
import com.blamejared.contenttweaker.core.zen.ContentTweakerZenConstants;
import com.blamejared.crafttweaker.api.annotation.ZenRegister;
import com.blamejared.crafttweaker_annotations.annotations.NativeTypeRegistration;
import net.minecraft.resources.ResourceLocation;
import org.openzen.zencode.java.ZenCodeType;

import java.util.Objects;

@NativeTypeRegistration(value = ResourceLocation.class, zenCodeName = ResourceLocationNative.CLASS_NAME)
@SuppressWarnings("unused")
@ZenRegister(loaders = ContentTweakerConstants.CONTENT_LOADER_ID)
public final class ResourceLocationNative {
public static final String CLASS_NAME = ContentTweakerZenConstants.MAIN_PACKAGE + ".resource.ResourceLocation";

private ResourceLocationNative() {}

@ZenCodeType.StaticExpansionMethod("of")
public static ResourceLocation of(final String namespace, final String path) {
return new ResourceLocation(namespace, path); // TODO("Panic on ResLocException")
}

@ZenCodeType.Getter("namespace")
public static String namespace(final ResourceLocation $this) {
return $this.getNamespace();
}

@ZenCodeType.Getter("path")
public static String path(final ResourceLocation $this) {
return $this.getPath();
}

@ZenCodeType.Operator(ZenCodeType.OperatorType.EQUALS)
public static boolean is(final ResourceLocation $this, final ResourceLocation other) {
return Objects.equals($this, other);
}

@ZenCodeType.Caster(implicit = true)
public static String asString(final ResourceLocation $this) {
return $this.toString();
}
}

0 comments on commit 23ad6de

Please sign in to comment.