Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master-1.20-lts' into master-1.20
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Jun 21, 2024
2 parents bae1e64 + 746a381 commit f5d9194
Show file tree
Hide file tree
Showing 15 changed files with 147 additions and 18 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod_version=1.0.1
minecraft_version=1.20.4
neoforge_version=20.4.160-beta
cyclopscore_version=1.19.0-423
integrateddynamics_version=1.21.2-741
integrateddynamics_version=1.21.2-744
release_type=release
fingerprint=bd0353b3e8a2810d60dd584e256e364bc3bedd44

Expand Down
11 changes: 11 additions & 0 deletions resources/changelog/1.19.2-1.0.2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
As always, don't forget to backup your world before updating!
Requires CyclopsCore version 1.19.0 or higher.

Changes:
* Add script context to errors during translation

Fixes:
* Automatically invalidate network errors on script changes, Closes #5
* Don't hard-crash on unsupported conversions to NBT, Closes #8
* Fix missing type checking for script variables in aspects, Closes #9

11 changes: 11 additions & 0 deletions resources/changelog/1.20.1-1.0.2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
As always, don't forget to backup your world before updating!
Requires CyclopsCore version 1.19.0 or higher.

Changes:
* Add script context to errors during translation

Fixes:
* Automatically invalidate network errors on script changes, Closes #5
* Don't hard-crash on unsupported conversions to NBT, Closes #8
* Fix missing type checking for script variables in aspects, Closes #9

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.cyclops.integratedscripting.api.evaluate.translation;

import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import org.cyclops.integrateddynamics.api.evaluate.EvaluationException;

/**
Expand All @@ -8,6 +10,10 @@
*/
public interface IEvaluationExceptionFactory {

public EvaluationException createError(String message);
public default EvaluationException createError(String message) {
return this.createError(Component.literal(message));
}

public EvaluationException createError(MutableComponent message);

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.cyclops.integrateddynamics.api.evaluate.variable.IVariable;
import org.cyclops.integrateddynamics.api.network.INetwork;
import org.cyclops.integrateddynamics.api.network.IPartNetwork;
import org.cyclops.integrateddynamics.core.evaluate.variable.ValueHelpers;
import org.cyclops.integrateddynamics.core.evaluate.variable.ValueTypes;
import org.cyclops.integrateddynamics.core.helper.L10NValues;
import org.cyclops.integrateddynamics.core.item.VariableFacadeBase;
Expand Down Expand Up @@ -81,7 +82,16 @@ public void validate(INetwork network, IPartNetwork partNetwork, IValidator vali
} else if (scriptingNetwork.getScript(this.disk, this.path) == null) {
validator.addError(Component.translatable("script.integratedscripting.error.path_not_in_network", this.disk, this.path.toString()));
} else if (scriptingNetwork.getScript(this.disk, this.path).getMember(this.member) == null) {
validator.addError(Component.translatable("script.integratedscripting.error.member_not_in_network", this.disk, this.path.toString(), this.member));
validator.addError(Component.translatable("script.integratedscripting.error.member_not_in_network", this.disk, this.member, this.path.toString()));
}

// Check if the expected type corresponds to the actual value's type produced by this script.
IValue value = scriptingNetwork.getScript(this.disk, this.path).getMember(this.member).getValue();
if (!ValueHelpers.correspondsTo(containingValueType, value.getType())) {
validator.addError(Component.translatable("script.integratedscripting.error.invalid_type",
this.member, this.path.toString(), this.disk,
Component.translatable(containingValueType.getTranslationKey()),
Component.translatable(value.getType().getTranslationKey())));
}
} catch (EvaluationException e) {
validator.addError(e.getErrorMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.cyclops.integratedscripting.api.network.IScript;
import org.cyclops.integratedscripting.api.network.IScriptFactory;
import org.cyclops.integratedscripting.api.network.IScriptingData;
import org.cyclops.integratedscripting.evaluate.EvaluationExceptionResolutionHelpers;
import org.cyclops.integratedscripting.evaluate.ScriptHelpers;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.PolyglotException;
Expand Down Expand Up @@ -48,7 +49,9 @@ public IScript getScript(int disk, Path path) throws EvaluationException {
try {
graalContext.eval(source);
} catch (PolyglotException e) {
throw new EvaluationException(Component.translatable("script.integratedscripting.error.script_read", path.toString(), disk, e.getMessage()));
throw EvaluationExceptionResolutionHelpers.resolveOnScriptChange(
new EvaluationException(Component.translatable("script.integratedscripting.error.script_read", path.toString(), disk, e.getMessage())),
disk, path);
}
Wrapper<IScriptingData.IDiskScriptsChangeListener> diskListener = new Wrapper<>();
return new GraalScript(graalContext, languageBinding, scriptInvalidateListener -> {
Expand All @@ -68,7 +71,9 @@ public IScript getScript(int disk, Path path) throws EvaluationException {
}
}, disk, path, null);
} catch (IOException e) {
throw new EvaluationException(Component.translatable("script.integratedscripting.error.script_read", path.toString(), disk, e.getMessage()));
throw EvaluationExceptionResolutionHelpers.resolveOnScriptChange(
new EvaluationException(Component.translatable("script.integratedscripting.error.script_read", path.toString(), disk, e.getMessage())),
disk, path);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.cyclops.integratedscripting.evaluate;

import org.cyclops.cyclopscore.datastructure.Wrapper;
import org.cyclops.integrateddynamics.api.evaluate.EvaluationException;
import org.cyclops.integratedscripting.api.network.IScriptingData;
import org.cyclops.integratedscripting.core.network.ScriptingNetworkHelpers;

import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.nio.file.Path;

/**
* @author rubensworks
*/
public class EvaluationExceptionResolutionHelpers {

// Holds weak references of created EvaluationExceptions
private static final ReferenceQueue<? super EvaluationException> EVALUATION_EXCEPTION_REFERENCE_QUEUE = new ReferenceQueue<>();

/**
* Indicate that the given EvaluationException must be resolved when the given script is changed.
* @param evaluationException An evaluation exception.
* @param disk A script disk.
* @param path A script path.
* @return The given exception.
*/
public static EvaluationException resolveOnScriptChange(EvaluationException evaluationException, int disk, Path path) {
Wrapper<IScriptingData.IDiskScriptsChangeListener> listener = new Wrapper<>();
listener.set(createListener(new EvaluationExceptionReference(evaluationException, EVALUATION_EXCEPTION_REFERENCE_QUEUE, disk, listener), disk, path));
ScriptingNetworkHelpers.getScriptingData().addListener(disk, listener.get());
return evaluationException;
}

/**
* Call this periodically to flush stale entries in
* {@link EvaluationExceptionResolutionHelpers#EVALUATION_EXCEPTION_REFERENCE_QUEUE}.
*/
public static void expungeStaleEvaluationExceptions() {
for (Object x; (x = EVALUATION_EXCEPTION_REFERENCE_QUEUE.poll()) != null; ) {
((EvaluationExceptionReference) x).removeListener();
}
}

protected static IScriptingData.IDiskScriptsChangeListener createListener(EvaluationExceptionReference evaluationExceptionReference, int disk, Path path) {
return scriptPathRelative -> {
if (scriptPathRelative.equals(path)) {
EvaluationException exception = evaluationExceptionReference.get();
if (exception != null) {
exception.resolve();
}
ScriptingNetworkHelpers.getScriptingData().removeListener(disk, evaluationExceptionReference.listener.get());
}
};
}

public static class EvaluationExceptionReference extends WeakReference<EvaluationException> {

private final int disk;
private final Wrapper<IScriptingData.IDiskScriptsChangeListener> listener;

public EvaluationExceptionReference(
EvaluationException referent,
ReferenceQueue<? super EvaluationException> queue,
int disk,
Wrapper<IScriptingData.IDiskScriptsChangeListener> listener
) {
super(referent, queue);
this.disk = disk;
this.listener = listener;
}

public void removeListener() {
ScriptingNetworkHelpers.getScriptingData().removeListener(disk, listener.get());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,15 @@ public static String getPathExtension(Path path) {
}

public static IEvaluationExceptionFactory getDummyEvaluationExceptionFactory() {
return message -> new EvaluationException(Component.literal(message));
return EvaluationException::new;
}

public static IEvaluationExceptionFactory getEvaluationExceptionFactory(int disk, Path path,String member) {
return message -> new EvaluationException(Component.translatable("script.integratedscripting.error.script_exec", member, path.toString(), disk, message));
public static IEvaluationExceptionFactory getEvaluationExceptionFactory(int disk, Path path, String member) {
EvaluationExceptionResolutionHelpers.expungeStaleEvaluationExceptions();

return message -> EvaluationExceptionResolutionHelpers.resolveOnScriptChange(
new EvaluationException(Component.translatable("script.integratedscripting.error.script_exec", member, path.toString(), disk, message)),
disk, path);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public <V extends IValue> IValueTranslator getValueTypeTranslator(IValueType<V>
public <V extends IValue> Value translateToGraal(Context context, V value, IEvaluationExceptionFactory exceptionFactory) throws EvaluationException {
IValueTranslator translator = getValueTypeTranslator(value.getType());
if (translator == null) {
throw new EvaluationException(Component.translatable("valuetype.integratedscripting.error.translation.unknown_to_graal", value.getType()));
throw exceptionFactory.createError(Component.translatable("valuetype.integratedscripting.error.translation.unknown_to_graal", value.getType()));
}
return translator.translateToGraal(context, value, exceptionFactory);
}
Expand All @@ -70,7 +70,7 @@ public IValueTranslator getScriptValueTranslator(Value scriptValue) {
public <V extends IValue> V translateFromGraal(Context context, Value value, IEvaluationExceptionFactory exceptionFactory) throws EvaluationException {
IValueTranslator translator = getScriptValueTranslator(value);
if (translator == null) {
throw new EvaluationException(Component.translatable("valuetype.integratedscripting.error.translation.unknown_from_graal", value));
throw exceptionFactory.createError(Component.translatable("valuetype.integratedscripting.error.translation.unknown_from_graal", value));
}
return (V) translator.translateFromGraal(context, value, exceptionFactory);
}
Expand All @@ -79,11 +79,11 @@ public <V extends IValue> V translateFromGraal(Context context, Value value, IEv
public <V extends IValue> Tag translateToNbt(Context context, V value, IEvaluationExceptionFactory exceptionFactory) throws EvaluationException {
IValueTranslator translator = getValueTypeTranslator(value.getType());
if (translator == null) {
throw new EvaluationException(Component.translatable("valuetype.integratedscripting.error.translation.unknown_to_graal_nbt", value.getType()));
throw exceptionFactory.createError(Component.translatable("valuetype.integratedscripting.error.translation.unknown_to_graal_nbt", value.getType()));
}
if (translator.canTranslateNbt()) {
return translator.translateToNbt(context, value, exceptionFactory);
}
throw new EvaluationException(Component.translatable("valuetype.integratedscripting.error.translation.nbt_unmatched", value.getType()));
throw exceptionFactory.createError(Component.translatable("valuetype.integratedscripting.error.translation.nbt_unmatched", value.getType()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.SneakyThrows;
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.Component;
import org.cyclops.integrateddynamics.api.evaluate.operator.IOperator;
import org.cyclops.integrateddynamics.api.evaluate.variable.IValue;
import org.cyclops.integrateddynamics.core.evaluate.operator.CurriedOperator;
Expand Down Expand Up @@ -84,6 +85,6 @@ public boolean hasMember(String key) {
@SneakyThrows
@Override
public void putMember(String key, Value value) {
throw new UnsupportedOperationException("Can not put members in static values of " + valueType.getTypeName());
throw exceptionFactory.createError(Component.translatable("valuetype.integratedscripting.error.translation.proxyobject_putMember", key, valueType.getTypeName()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public boolean canTranslateNbt() {
@Override
public Value translateToGraal(Context context, ValueTypeList.ValueList value, IEvaluationExceptionFactory exceptionFactory) throws EvaluationException {
if (value.getRawValue().isInfinite()) {
throw new EvaluationException(Component.translatable("valuetype.integratedscripting.error.translation.list_infinite"));
throw exceptionFactory.createError(Component.translatable("valuetype.integratedscripting.error.translation.list_infinite"));
}

// The following should work according to the docs, but doesn't. So we fallback to a sub-optimal approach.
Expand All @@ -67,7 +67,7 @@ public ValueTypeList.ValueList translateFromGraal(Context context, Value value,
@Override
public Tag translateToNbt(Context context, ValueTypeList.ValueList value, IEvaluationExceptionFactory exceptionFactory) throws EvaluationException {
if (value.getRawValue().isInfinite()) {
throw new EvaluationException(Component.translatable("valuetype.integratedscripting.error.translation.list_infinite"));
throw exceptionFactory.createError(Component.translatable("valuetype.integratedscripting.error.translation.list_infinite"));
}

ListTag tag = new ListTag();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public Value translateTag(Context context, Tag tag, IEvaluationExceptionFactory
case Tag.TAG_LONG_ARRAY -> {
return context.asValue(((LongArrayTag) tag).getAsLongArray());
}
default -> throw new EvaluationException(Component.translatable("valuetype.integratedscripting.error.translation.nbt_unknown", tag.getType().getPrettyName()));
default -> throw exceptionFactory.createError(Component.translatable("valuetype.integratedscripting.error.translation.nbt_unknown", tag.getType().getPrettyName()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.Component;
import org.cyclops.integrateddynamics.api.evaluate.EvaluationException;
import org.cyclops.integrateddynamics.api.evaluate.operator.IOperator;
import org.cyclops.integrateddynamics.api.evaluate.variable.IValue;
Expand Down Expand Up @@ -118,6 +119,6 @@ public V translateFromGraal(Context context, Value value, IEvaluationExceptionFa

@Override
public Tag translateToNbt(Context context, V value, IEvaluationExceptionFactory exceptionFactory) throws EvaluationException {
throw new UnsupportedOperationException("translateToNbt is not supported");
throw exceptionFactory.createError(Component.translatable("valuetype.integratedscripting.error.translation.unsupported_translateToNbt", Component.translatable(value.getType().getTranslationKey()), value.getType().toCompactString(value)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public ValueTypeOperator.ValueOperator translateFromGraal(Context context, Value

@Override
public Tag translateToNbt(Context context, ValueTypeOperator.ValueOperator value, IEvaluationExceptionFactory exceptionFactory) throws EvaluationException {
throw new UnsupportedOperationException("translateToNbt is not supported");
throw exceptionFactory.createError(Component.translatable("valuetype.integratedscripting.error.translation.unsupported_translateToNbt", Component.translatable(value.getType().getTranslationKey()), value.getType().toCompactString(value)));
}

public static class GraalOperator extends OperatorBase {
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/assets/integratedscripting/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
"valuetype.integratedscripting.error.translation.list_infinite": "Can not translate an infinite list into a scripting language.",
"valuetype.integratedscripting.error.translation.nbt_unknown": "Can not translate NBT members of type %s.",
"valuetype.integratedscripting.error.translation.nbt_unmatched": "Could not interpret the field %s as NBT.",
"valuetype.integratedscripting.error.translation.unsupported_translateToNbt": "Illegal conversion of %s \"%s\" to NBT.",
"valuetype.integratedscripting.error.translation.proxyobject_putMember": "Illegal adding of key \"%s\" to read-only %s",

"_comment": "Operators",
"operator.integratedscripting.integratedscript.basename": "Integrated Script %s",
Expand All @@ -57,6 +59,7 @@
"script.integratedscripting.error.unsupported_language": "The language of script \"%s\" is not supported.",
"script.integratedscripting.error.script_read": "An error occurred when reading the script \"%s\" at disk %s: %s",
"script.integratedscripting.error.script_exec": "An error occurred when executing the member \"%s\" in script \"%s\" at disk %s: %s",
"script.integratedscripting.error.invalid_type": "Script with member \"%s\" in script \"%s\" at disk %s is expected to expose a variable with type %s while %s is found.",

"_comment": "Items",
"item.integratedscripting.scripting_disk": "Scripting Disk",
Expand Down

0 comments on commit f5d9194

Please sign in to comment.