diff options
| author | SHsuperCM <shsupercm@gmail.com> | 2022-02-05 08:32:41 +0200 |
|---|---|---|
| committer | SHsuperCM <shsupercm@gmail.com> | 2022-02-05 09:03:44 +0200 |
| commit | fcd73195b483075c962986994f32dc53dd2f91cd (patch) | |
| tree | 60dcdb4fae0314fb4a9fbfb7dece0edbd3eeae5b /src/main/java/shcm/shsupercm/fabric/citresewn/pack | |
| parent | 1a9e72178881ca41321c730eb69ce28f0b6ad171 (diff) | |
| download | CITResewn-fcd73195b483075c962986994f32dc53dd2f91cd.tar.gz CITResewn-fcd73195b483075c962986994f32dc53dd2f91cd.tar.bz2 CITResewn-fcd73195b483075c962986994f32dc53dd2f91cd.zip | |
Moved some stuff around
Diffstat (limited to 'src/main/java/shcm/shsupercm/fabric/citresewn/pack')
6 files changed, 180 insertions, 9 deletions
diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/ActiveCITs.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/ActiveCITs.java index 5523c84..642b950 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/ActiveCITs.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/ActiveCITs.java @@ -3,7 +3,7 @@ package shcm.shsupercm.fabric.citresewn.pack; import net.fabricmc.loader.api.FabricLoader; import net.minecraft.resource.ResourceManager; import net.minecraft.util.profiler.Profiler; -import shcm.shsupercm.fabric.citresewn.util.Disposable; +import shcm.shsupercm.fabric.citresewn.api.Disposable; public class ActiveCITs implements Disposable { private ActiveCITs() {} private static ActiveCITs active = null; diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/GlobalProperties.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/GlobalProperties.java index 76f5d26..39d551a 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/GlobalProperties.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/GlobalProperties.java @@ -4,10 +4,10 @@ import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.loader.api.entrypoint.EntrypointContainer; import net.minecraft.util.Identifier; import net.minecraft.util.InvalidIdentifierException; -import shcm.shsupercm.fabric.citresewn.format.PropertyGroup; -import shcm.shsupercm.fabric.citresewn.format.PropertyKey; -import shcm.shsupercm.fabric.citresewn.format.PropertyValue; -import shcm.shsupercm.fabric.citresewn.registry.api.GlobalPropertiesHandler; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyKey; +import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; +import shcm.shsupercm.fabric.citresewn.api.GlobalPropertiesHandler; import java.io.IOException; import java.io.InputStream; @@ -43,11 +43,11 @@ public class GlobalProperties extends PropertyGroup { for (Map.Entry<PropertyKey, Set<PropertyValue>> entry : properties.entrySet()) if (entry.getKey().namespace().equals(containerNamespace)) { - PropertyValue value = null; - for (PropertyValue v : entry.getValue()) - value = v; + PropertyValue lastValue = null; + for (PropertyValue value : entry.getValue()) + lastValue = value; - container.getEntrypoint().globalProperty(entry.getKey().path(), value); + container.getEntrypoint().globalProperty(entry.getKey().path(), lastValue); } } } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertiesGroupAdapter.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertiesGroupAdapter.java new file mode 100644 index 0000000..896e81e --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertiesGroupAdapter.java @@ -0,0 +1,103 @@ +package shcm.shsupercm.fabric.citresewn.pack.format; + +import net.minecraft.util.Identifier; +import net.minecraft.util.InvalidIdentifierException; + +import java.io.*; +import java.nio.charset.StandardCharsets; +import java.util.Properties; + +public class PropertiesGroupAdapter extends PropertyGroup { + public static final String EXTENSION = ".properties"; + + protected PropertiesGroupAdapter(String packName, Identifier identifier) { + super(packName, identifier); + } + + @Override + public String getExtension() { + return EXTENSION; + } + + @Override + public PropertyGroup load(String packName, Identifier identifier, InputStream is) throws IOException, InvalidIdentifierException { + try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) { + String line; + int linePos = 0, multilineSkip = 0; + while ((line = reader.readLine()) != null) { + linePos++; + line = line.stripLeading(); + if (line.isEmpty() || line.startsWith("#") || line.startsWith("!")) + continue; + + while (line.endsWith("\\")) { + String nextLine = reader.readLine(); + linePos++; + multilineSkip++; + if (nextLine == null) + nextLine = ""; + nextLine = nextLine.stripLeading(); + + if (nextLine.startsWith("#") || nextLine.startsWith("!")) + continue; + + line = line.substring(0, line.length() - 1) + "\\n" + nextLine; + } + + StringBuilder builder = new StringBuilder(); + + String key = null, keyMetadata = null; + + for (int i = 0; i < line.length(); i++) { + char c = line.charAt(i); + + if (c == '\\') { // escape + c = switch (c = line.charAt(++i)) { + case 'n' -> '\n'; + case 'r' -> '\r'; + case 'f' -> '\f'; + case 't' -> '\t'; + case 'u' -> { + if (i + 4 >= line.length()) + yield c; + + //todo implement manually + java.util.Properties properties = new Properties(); + properties.load(new StringReader("k=\\u" + line.charAt(i + 1) + line.charAt(i + 2) + line.charAt(i + 3) + line.charAt(i + 4))); + String k = properties.getProperty("k"); + if (k.length() == 1) { + i += 4; + yield k.charAt(0); + } + yield c; + } + + default -> c; + }; + + } else if (key == null && c == '=') { + key = builder.toString().stripTrailing(); + int metadataIndex = key.indexOf('.'); + if (metadataIndex >= 0) { + keyMetadata = key.substring(metadataIndex + 1); + key = key.substring(0, metadataIndex); + } + + builder = new StringBuilder(); + for (i++; i < line.length() && Character.isWhitespace(line.charAt(i)); i++); + i--; + continue; + } + + + builder.append(c); + } + + int pos = linePos - multilineSkip; + multilineSkip = 0; + this.put(pos, key, keyMetadata, "=", builder.toString()); + } + } + return this; + } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyGroup.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyGroup.java new file mode 100644 index 0000000..8fcb135 --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyGroup.java @@ -0,0 +1,50 @@ +package shcm.shsupercm.fabric.citresewn.pack.format; + +import net.minecraft.util.Identifier; +import net.minecraft.util.InvalidIdentifierException; + +import java.io.IOException; +import java.io.InputStream; +import java.util.*; + +public abstract class PropertyGroup { + public final Map<PropertyKey, Set<PropertyValue>> properties = new LinkedHashMap<>(); + public final Identifier identifier; + public final String packName; + + protected PropertyGroup(String packName, Identifier identifier) { + this.packName = packName; + this.identifier = identifier; + } + + public abstract String getExtension(); + + public abstract PropertyGroup load(String packName, Identifier identifier, InputStream is) throws IOException, InvalidIdentifierException; + + protected void put(int position, String key, String keyMetadata, String delimiter, String value) throws InvalidIdentifierException { + Objects.requireNonNull(key); + Objects.requireNonNull(value); + + this.properties.computeIfAbsent(PropertyKey.of(key), id -> new LinkedHashSet<>()).add(new PropertyValue(keyMetadata, value, delimiter, position)); + } + + public Set<PropertyValue> get(String namespace, String... pathAliases) { + Set<PropertyValue> values = new LinkedHashSet<>(); + + for (String path : pathAliases) { + Set<PropertyValue> possibleValues = this.properties.get(new PropertyKey(namespace, path)); + if (possibleValues != null) + values.addAll(possibleValues); + } + + return values; + } + + public static PropertyGroup tryParseGroup(String packName, Identifier identifier, InputStream is) throws IOException { + PropertyGroup group = null; + if (identifier.getPath().endsWith(PropertiesGroupAdapter.EXTENSION)) + group = new PropertiesGroupAdapter(packName, identifier); + + return group == null ? null : group.load(packName, identifier, is); + } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyKey.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyKey.java new file mode 100644 index 0000000..b861a63 --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyKey.java @@ -0,0 +1,14 @@ +package shcm.shsupercm.fabric.citresewn.pack.format; + +public record PropertyKey(String namespace, String path) { + public static PropertyKey of(String key) { + String[] split = new String[] {"citresewn", key}; + int i = key.indexOf(':'); + if (i >= 0) { + split[1] = key.substring(i + 1); + if (i >= 1) + split[0] = key.substring(0, i); + } + return new PropertyKey(split[0], split[1]); + } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyValue.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyValue.java new file mode 100644 index 0000000..9a0c4e3 --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/format/PropertyValue.java @@ -0,0 +1,4 @@ +package shcm.shsupercm.fabric.citresewn.pack.format; + +public record PropertyValue(String keyMetadata, String value, String delimiter, int position) { +} |
