mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-12-02 02:44:33 +00:00
perf: replace biome tag checks with bitset columns
This commit is contained in:
1 parent
6ddf039e49
commit
8a940f7e1f
45 files changed
+938
-314
No files matched your search
+23
-1
@@ -11,6 +11,7 @@ import com.dfsek.tectonic.api.config.template.ValidatedConfigTemplate;
|
||||
import com.dfsek.tectonic.api.config.template.annotations.Default;
|
||||
import com.dfsek.tectonic.api.config.template.annotations.Final;
|
||||
import com.dfsek.tectonic.api.config.template.annotations.Value;
|
||||
import com.dfsek.tectonic.api.exception.ValidationException;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
@@ -21,6 +22,7 @@ import com.dfsek.terra.api.Platform;
|
||||
import com.dfsek.terra.api.config.AbstractableTemplate;
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.config.meta.Meta;
|
||||
import com.dfsek.terra.api.registry.key.RegistryKey;
|
||||
import com.dfsek.terra.api.world.biome.PlatformBiome;
|
||||
|
||||
|
||||
@@ -48,13 +50,29 @@ public class BiomeTemplate implements AbstractableTemplate, ValidatedConfigTempl
|
||||
@Default
|
||||
private @Meta Set<@Meta String> tags = new HashSet<>();
|
||||
|
||||
private Set<RegistryKey> platformTags = Set.of();
|
||||
|
||||
public BiomeTemplate(ConfigPack pack, Platform platform) {
|
||||
this.pack = pack;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean validate() {
|
||||
public boolean validate() throws ValidationException {
|
||||
color |= 0xff000000; // Alpha adjustment
|
||||
Set<RegistryKey> parsed = new HashSet<>();
|
||||
for(String tag : tags) {
|
||||
int colon = tag.indexOf(':');
|
||||
if(colon < 0) continue;
|
||||
// RegistryKey.parse accepts an empty namespace or ID.
|
||||
if(colon == 0) throw new ValidationException("Invalid namespaced biome tag \"" + tag + "\": the namespace is empty");
|
||||
if(colon == tag.length() - 1) throw new ValidationException("Invalid namespaced biome tag \"" + tag + "\": the ID is empty");
|
||||
try {
|
||||
parsed.add(RegistryKey.parse(tag));
|
||||
} catch(IllegalArgumentException e) {
|
||||
throw new ValidationException("Invalid namespaced biome tag \"" + tag + "\": " + e.getMessage());
|
||||
}
|
||||
}
|
||||
platformTags = Set.copyOf(parsed);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -66,6 +84,10 @@ public class BiomeTemplate implements AbstractableTemplate, ValidatedConfigTempl
|
||||
return tags;
|
||||
}
|
||||
|
||||
public Set<RegistryKey> getPlatformTags() {
|
||||
return platformTags;
|
||||
}
|
||||
|
||||
public int getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
+14
-5
@@ -7,10 +7,11 @@
|
||||
|
||||
package com.dfsek.terra.addons.biome;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import com.dfsek.terra.api.properties.Context;
|
||||
import com.dfsek.terra.api.registry.key.RegistryKey;
|
||||
import com.dfsek.terra.api.world.biome.Biome;
|
||||
import com.dfsek.terra.api.world.biome.PlatformBiome;
|
||||
|
||||
@@ -24,6 +25,7 @@ public class UserDefinedBiome implements Biome {
|
||||
private final BiomeTemplate config;
|
||||
private final int color;
|
||||
private final Set<String> tags;
|
||||
private final Set<RegistryKey> platformTags;
|
||||
|
||||
private final Context context = new Context();
|
||||
private final int intID;
|
||||
@@ -33,10 +35,12 @@ public class UserDefinedBiome implements Biome {
|
||||
this.id = config.getID();
|
||||
this.config = config;
|
||||
this.color = config.getColor();
|
||||
this.tags = config.getTags();
|
||||
this.intID = INT_ID_COUNTER.getAndIncrement();
|
||||
tags.add("BIOME:" + id);
|
||||
tags.add("ALL");
|
||||
Set<String> allTags = new HashSet<>(config.getTags());
|
||||
allTags.add("BIOME:" + id);
|
||||
allTags.add("ALL");
|
||||
this.tags = Set.copyOf(allTags);
|
||||
this.platformTags = Set.copyOf(config.getPlatformTags());
|
||||
this.intID = config.getPack().getBiomeTagIndex().register(tags);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -64,6 +68,11 @@ public class UserDefinedBiome implements Biome {
|
||||
return tags;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<RegistryKey> getPlatformTags() {
|
||||
return platformTags;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getIntID() {
|
||||
return intID;
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
package biome;
|
||||
|
||||
import com.dfsek.tectonic.api.exception.ValidationException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import com.dfsek.terra.addons.biome.BiomeTemplate;
|
||||
import com.dfsek.terra.addons.biome.UserDefinedBiome;
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.registry.key.RegistryKey;
|
||||
import com.dfsek.terra.api.world.biome.tag.BiomeTagIndex;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
|
||||
class PlatformTagsTest {
|
||||
private static BiomeTemplate template(String... tags) throws ReflectiveOperationException {
|
||||
BiomeTagIndex index = new BiomeTagIndex();
|
||||
ConfigPack pack = (ConfigPack) Proxy.newProxyInstance(ConfigPack.class.getClassLoader(), new Class<?>[]{ ConfigPack.class },
|
||||
(proxy, method, args) -> {
|
||||
if(method.getName().equals("getBiomeTagIndex")) return index;
|
||||
throw new UnsupportedOperationException(method.getName());
|
||||
});
|
||||
BiomeTemplate template = new BiomeTemplate(pack, null);
|
||||
set(template, "id", "TEST");
|
||||
set(template, "tags", new HashSet<>(Set.of(tags)));
|
||||
return template;
|
||||
}
|
||||
|
||||
private static void set(BiomeTemplate template, String field, Object value) throws ReflectiveOperationException {
|
||||
Field f = BiomeTemplate.class.getDeclaredField(field);
|
||||
f.setAccessible(true);
|
||||
f.set(template, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
void platformTags() throws Exception {
|
||||
BiomeTemplate template = template("USE_RIVER", "minecraft:is_forest", "c:hot");
|
||||
template.validate();
|
||||
UserDefinedBiome biome = new UserDefinedBiome(null, template);
|
||||
|
||||
assertEquals(Set.of(RegistryKey.parse("minecraft:is_forest"), RegistryKey.parse("c:hot")), biome.getPlatformTags());
|
||||
assertEquals(Set.of("USE_RIVER", "minecraft:is_forest", "c:hot", "BIOME:TEST", "ALL"), biome.getTags());
|
||||
assertThrows(UnsupportedOperationException.class, () -> biome.getTags().add("X"));
|
||||
assertFalse(template.getTags().contains("BIOME:TEST"));
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "{0}")
|
||||
@CsvSource(delimiter = '|', value = {
|
||||
"minecraft:Bad Tag | minecraft:Bad Tag",
|
||||
":is_forest | empty",
|
||||
"minecraft: | empty",
|
||||
})
|
||||
void invalidNamespacedTag(String tag, String reasonPart) throws Exception {
|
||||
ValidationException e = assertThrows(ValidationException.class, template(tag)::validate);
|
||||
assertTrue(e.getMessage().contains("\"" + tag + "\""), e.getMessage());
|
||||
assertTrue(e.getMessage().contains(reasonPart), e.getMessage());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user