remove TerraScript ID keyword in favor of file name

This commit is contained in:
dfsek 2021-10-17 11:16:24 -07:00
parent ab100c85a1
commit 18bc083431
8 changed files with 39 additions and 45 deletions

View File

@ -12,6 +12,8 @@ import com.dfsek.terra.api.inject.annotations.Inject;
import com.dfsek.terra.api.registry.CheckedRegistry; import com.dfsek.terra.api.registry.CheckedRegistry;
import com.dfsek.terra.api.structure.Structure; import com.dfsek.terra.api.structure.Structure;
import com.dfsek.terra.api.util.StringUtil;
import net.querz.nbt.io.NBTDeserializer; import net.querz.nbt.io.NBTDeserializer;
import net.querz.nbt.tag.ByteArrayTag; import net.querz.nbt.tag.ByteArrayTag;
import net.querz.nbt.tag.CompoundTag; import net.querz.nbt.tag.CompoundTag;
@ -42,7 +44,7 @@ public class SpongeSchematicAddon extends TerraAddon {
CheckedRegistry<Structure> structureRegistry = event.getPack().getOrCreateRegistry(Structure.class); CheckedRegistry<Structure> structureRegistry = event.getPack().getOrCreateRegistry(Structure.class);
event.getPack().getLoader().open("", ".schem").thenEntries(entries -> { event.getPack().getLoader().open("", ".schem").thenEntries(entries -> {
for(Map.Entry<String, InputStream> entry : entries) { for(Map.Entry<String, InputStream> entry : entries) {
String id = entry.getKey().substring(0, entry.getKey().length() - ".schem".length()); String id = StringUtil.fileName(entry.getKey());
structureRegistry.register(id, convert(entry.getValue(), id)); structureRegistry.register(id, convert(entry.getValue(), id));
} }
}).close(); }).close();

View File

@ -18,6 +18,7 @@ import com.dfsek.terra.api.inject.annotations.Inject;
import com.dfsek.terra.api.registry.CheckedRegistry; import com.dfsek.terra.api.registry.CheckedRegistry;
import com.dfsek.terra.api.structure.LootTable; import com.dfsek.terra.api.structure.LootTable;
import com.dfsek.terra.api.structure.Structure; import com.dfsek.terra.api.structure.Structure;
import com.dfsek.terra.api.util.StringUtil;
@Addon("structure-terrascript-loader") @Addon("structure-terrascript-loader")
@ -33,20 +34,22 @@ public class TerraScriptAddon extends TerraAddon {
.getHandler(FunctionalEventHandler.class) .getHandler(FunctionalEventHandler.class)
.register(this, ConfigPackPreLoadEvent.class) .register(this, ConfigPackPreLoadEvent.class)
.then(event -> { .then(event -> {
CheckedRegistry<Structure> structureRegistry = event.getPack().getOrCreateRegistry(Structure.class); CheckedRegistry<Structure> structureRegistry = event.getPack().getOrCreateRegistry(Structure.class);
CheckedRegistry<LootTable> lootRegistry = event.getPack().getOrCreateRegistry(LootTable.class); CheckedRegistry<LootTable> lootRegistry = event.getPack().getOrCreateRegistry(LootTable.class);
event.getPack().getLoader().open("", ".tesf").thenEntries(entries -> { event.getPack().getLoader().open("", ".tesf").thenEntries(entries -> {
for(Map.Entry<String, InputStream> entry : entries) { for(Map.Entry<String, InputStream> entry : entries) {
try { try {
StructureScript structureScript = new StructureScript(entry.getValue(), platform, structureRegistry, lootRegistry, String id = StringUtil.fileName(entry.getKey());
event.getPack().getRegistryFactory().create()); StructureScript structureScript = new StructureScript(entry.getValue(), id, platform, structureRegistry,
structureRegistry.register(structureScript.getID(), structureScript); lootRegistry,
} catch(ParseException e) { event.getPack().getRegistryFactory().create());
throw new LoadException("Failed to load script: ", e); structureRegistry.register(structureScript.getID(), structureScript);
} catch(ParseException e) {
throw new LoadException("Failed to load script: ", e);
}
} }
} }).close();
}).close(); })
})
.failThrough(); .failThrough();
} }
} }

View File

@ -55,8 +55,6 @@ public class Parser {
private final Map<String, FunctionBuilder<? extends Function<?>>> functions = new HashMap<>(); private final Map<String, FunctionBuilder<? extends Function<?>>> functions = new HashMap<>();
private final List<String> ignoredFunctions = new ArrayList<>(); private final List<String> ignoredFunctions = new ArrayList<>();
private String id;
public Parser(String data) { public Parser(String data) {
this.data = data; this.data = data;
} }
@ -79,17 +77,7 @@ public class Parser {
* @throws ParseException If parsing fails. * @throws ParseException If parsing fails.
*/ */
public Block parse() { public Block parse() {
Tokenizer tokens = new Tokenizer(data); return parseBlock(new Tokenizer(data), new HashMap<>(), false);
// Parse ID
ParserUtil.checkType(tokens.consume(), Token.Type.ID); // First token must be ID
Token idToken = tokens.get();
ParserUtil.checkType(tokens.consume(), Token.Type.STRING); // Second token must be string literal containing ID
ParserUtil.checkType(tokens.consume(), Token.Type.STATEMENT_END);
this.id = idToken.getContent();
return parseBlock(tokens, new HashMap<>(), false);
} }
private Keyword<?> parseLoopLike(Tokenizer tokens, Map<String, Returnable.ReturnType> variableMap, boolean loop) throws ParseException { private Keyword<?> parseLoopLike(Tokenizer tokens, Map<String, Returnable.ReturnType> variableMap, boolean loop) throws ParseException {
@ -429,10 +417,6 @@ public class Parser {
throw new UnsupportedOperationException("Unsupported function: " + identifier.getContent()); throw new UnsupportedOperationException("Unsupported function: " + identifier.getContent());
} }
public String getID() {
return id;
}
private List<Returnable<?>> getArgs(Tokenizer tokens, Map<String, Returnable.ReturnType> variableMap) { private List<Returnable<?>> getArgs(Tokenizer tokens, Map<String, Returnable.ReturnType> variableMap) {
List<Returnable<?>> args = new ArrayList<>(); List<Returnable<?>> args = new ArrayList<>();

View File

@ -52,9 +52,8 @@ public class StructureScript implements Structure {
private final String id; private final String id;
private final Cache<Vector3, StructureBuffer> cache; private final Cache<Vector3, StructureBuffer> cache;
private final Platform platform; private final Platform platform;
private String tempID;
public StructureScript(InputStream inputStream, Platform platform, Registry<Structure> registry, Registry<LootTable> lootRegistry, public StructureScript(InputStream inputStream, String id, Platform platform, Registry<Structure> registry, Registry<LootTable> lootRegistry,
Registry<FunctionBuilder<?>> functionRegistry) { Registry<FunctionBuilder<?>> functionRegistry) {
Parser parser; Parser parser;
try { try {
@ -62,6 +61,7 @@ public class StructureScript implements Structure {
} catch(IOException e) { } catch(IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
this.id = id;
functionRegistry.forEach(parser::registerFunction); // Register registry functions. functionRegistry.forEach(parser::registerFunction); // Register registry functions.
@ -92,7 +92,7 @@ public class StructureScript implements Structure {
.registerFunction("rotationDegrees", new ZeroArgFunctionBuilder<>(arguments -> arguments.getRotation().getDegrees(), .registerFunction("rotationDegrees", new ZeroArgFunctionBuilder<>(arguments -> arguments.getRotation().getDegrees(),
Returnable.ReturnType.NUMBER)) Returnable.ReturnType.NUMBER))
.registerFunction("print", .registerFunction("print",
new UnaryStringFunctionBuilder(string -> platform.getDebugLogger().info("[" + tempID + "] " + string))) new UnaryStringFunctionBuilder(string -> platform.getDebugLogger().info("[" + id + "] " + string)))
.registerFunction("abs", new UnaryNumberFunctionBuilder(number -> FastMath.abs(number.doubleValue()))) .registerFunction("abs", new UnaryNumberFunctionBuilder(number -> FastMath.abs(number.doubleValue())))
.registerFunction("pow", new BinaryNumberFunctionBuilder( .registerFunction("pow", new BinaryNumberFunctionBuilder(
(number, number2) -> FastMath.pow(number.doubleValue(), number2.doubleValue()))) (number, number2) -> FastMath.pow(number.doubleValue(), number2.doubleValue())))
@ -117,8 +117,6 @@ public class StructureScript implements Structure {
} }
block = parser.parse(); block = parser.parse();
this.id = parser.getID();
tempID = id;
this.platform = platform; this.platform = platform;
this.cache = CacheBuilder.newBuilder().maximumSize(platform.getTerraConfig().getStructureCache()).build(); this.cache = CacheBuilder.newBuilder().maximumSize(platform.getTerraConfig().getStructureCache()).build();
} }

View File

@ -218,10 +218,6 @@ public class Token {
* Fail statement. Like return keyword, but specifies that generation has failed. * Fail statement. Like return keyword, but specifies that generation has failed.
*/ */
FAIL, FAIL,
/**
* ID declaration
*/
ID,
/** /**
* For loop initializer token * For loop initializer token
*/ */

View File

@ -190,9 +190,6 @@ public class Tokenizer {
if(tokenString.equals("fail")) if(tokenString.equals("fail"))
return new Token(tokenString, Token.Type.FAIL, new Position(reader.getLine(), reader.getIndex())); return new Token(tokenString, Token.Type.FAIL, new Position(reader.getLine(), reader.getIndex()));
if(tokenString.equals("id"))
return new Token(tokenString, Token.Type.ID, new Position(reader.getLine(), reader.getIndex()));
return new Token(tokenString, Token.Type.IDENTIFIER, new Position(reader.getLine(), reader.getIndex())); return new Token(tokenString, Token.Type.IDENTIFIER, new Position(reader.getLine(), reader.getIndex()));
} }

View File

@ -1,5 +1,3 @@
id "testScript";
bool thing1 = 2 > (2+2) || false; bool thing1 = 2 > (2+2) || false;
if(2 > 2 || 3 + 4 <= 2 && 4 + 5 > 2 / 3) { if(2 > 2 || 3 + 4 <= 2 && 4 + 5 > 2 / 3) {

View File

@ -0,0 +1,16 @@
package com.dfsek.terra.api.util;
import java.io.File;
public class StringUtil {
public static String fileName(String path) {
if(path.contains(File.separator)) {
return path.substring(path.lastIndexOf(File.separatorChar) + 1, path.lastIndexOf('.'));
} else if(path.contains(".")) {
return path.substring(0, path.lastIndexOf('.'));
} else {
return path;
}
}
}