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.structure.Structure;
import com.dfsek.terra.api.util.StringUtil;
import net.querz.nbt.io.NBTDeserializer;
import net.querz.nbt.tag.ByteArrayTag;
import net.querz.nbt.tag.CompoundTag;
@ -42,7 +44,7 @@ public class SpongeSchematicAddon extends TerraAddon {
CheckedRegistry<Structure> structureRegistry = event.getPack().getOrCreateRegistry(Structure.class);
event.getPack().getLoader().open("", ".schem").thenEntries(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));
}
}).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.structure.LootTable;
import com.dfsek.terra.api.structure.Structure;
import com.dfsek.terra.api.util.StringUtil;
@Addon("structure-terrascript-loader")
@ -38,7 +39,9 @@ public class TerraScriptAddon extends TerraAddon {
event.getPack().getLoader().open("", ".tesf").thenEntries(entries -> {
for(Map.Entry<String, InputStream> entry : entries) {
try {
StructureScript structureScript = new StructureScript(entry.getValue(), platform, structureRegistry, lootRegistry,
String id = StringUtil.fileName(entry.getKey());
StructureScript structureScript = new StructureScript(entry.getValue(), id, platform, structureRegistry,
lootRegistry,
event.getPack().getRegistryFactory().create());
structureRegistry.register(structureScript.getID(), structureScript);
} catch(ParseException e) {

View File

@ -55,8 +55,6 @@ public class Parser {
private final Map<String, FunctionBuilder<? extends Function<?>>> functions = new HashMap<>();
private final List<String> ignoredFunctions = new ArrayList<>();
private String id;
public Parser(String data) {
this.data = data;
}
@ -79,17 +77,7 @@ public class Parser {
* @throws ParseException If parsing fails.
*/
public Block parse() {
Tokenizer tokens = new Tokenizer(data);
// 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);
return parseBlock(new Tokenizer(data), new HashMap<>(), false);
}
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());
}
public String getID() {
return id;
}
private List<Returnable<?>> getArgs(Tokenizer tokens, Map<String, Returnable.ReturnType> variableMap) {
List<Returnable<?>> args = new ArrayList<>();

View File

@ -52,9 +52,8 @@ public class StructureScript implements Structure {
private final String id;
private final Cache<Vector3, StructureBuffer> cache;
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) {
Parser parser;
try {
@ -62,6 +61,7 @@ public class StructureScript implements Structure {
} catch(IOException e) {
throw new RuntimeException(e);
}
this.id = id;
functionRegistry.forEach(parser::registerFunction); // Register registry functions.
@ -92,7 +92,7 @@ public class StructureScript implements Structure {
.registerFunction("rotationDegrees", new ZeroArgFunctionBuilder<>(arguments -> arguments.getRotation().getDegrees(),
Returnable.ReturnType.NUMBER))
.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("pow", new BinaryNumberFunctionBuilder(
(number, number2) -> FastMath.pow(number.doubleValue(), number2.doubleValue())))
@ -117,8 +117,6 @@ public class StructureScript implements Structure {
}
block = parser.parse();
this.id = parser.getID();
tempID = id;
this.platform = platform;
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,
/**
* ID declaration
*/
ID,
/**
* For loop initializer token
*/

View File

@ -190,9 +190,6 @@ public class Tokenizer {
if(tokenString.equals("fail"))
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()));
}

View File

@ -1,5 +1,3 @@
id "testScript";
bool thing1 = 2 > (2+2) || false;
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;
}
}
}