diff options
author | SHsuperCM <shsupercm@gmail.com> | 2022-01-22 10:30:00 +0200 |
---|---|---|
committer | SHsuperCM <shsupercm@gmail.com> | 2022-01-22 10:30:00 +0200 |
commit | ffd25dce4e101c478e13867ebb5995f07cd68039 (patch) | |
tree | 5906dc6e06e33323e08f239908a782571f78c152 /src/main/java/shcm/shsupercm/fabric | |
parent | 818f912fc038b84a3dd855892d619af3325bc163 (diff) | |
download | CITResewn-ffd25dce4e101c478e13867ebb5995f07cd68039.tar.gz CITResewn-ffd25dce4e101c478e13867ebb5995f07cd68039.tar.bz2 CITResewn-ffd25dce4e101c478e13867ebb5995f07cd68039.zip |
Implemented custom properties parser
Diffstat (limited to 'src/main/java/shcm/shsupercm/fabric')
5 files changed, 186 insertions, 3 deletions
diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/CITResewn.java b/src/main/java/shcm/shsupercm/fabric/citresewn/CITResewn.java index 4ebfff8..0a0051f 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/CITResewn.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/CITResewn.java @@ -1,13 +1,10 @@ package shcm.shsupercm.fabric.citresewn; import net.fabricmc.api.ClientModInitializer; -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import shcm.shsupercm.fabric.citresewn.config.CITResewnConfig; -@Environment(EnvType.CLIENT) public class CITResewn implements ClientModInitializer { public static final Logger LOG = LogManager.getLogger("CITResewn"); public static CITResewn INSTANCE; diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/format/PropertiesGroupAdapter.java b/src/main/java/shcm/shsupercm/fabric/citresewn/format/PropertiesGroupAdapter.java new file mode 100644 index 0000000..c3d91ec --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/format/PropertiesGroupAdapter.java @@ -0,0 +1,103 @@ +package shcm.shsupercm.fabric.citresewn.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(Identifier identifier) { + super(identifier); + } + + @Override + public String getExtension() { + return EXTENSION; + } + + @Override + public PropertyGroup load(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/format/PropertyGroup.java b/src/main/java/shcm/shsupercm/fabric/citresewn/format/PropertyGroup.java new file mode 100644 index 0000000..d1894c7 --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/format/PropertyGroup.java @@ -0,0 +1,52 @@ +package shcm.shsupercm.fabric.citresewn.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; + + protected PropertyGroup(Identifier identifier) { + this.identifier = identifier; + } + + public abstract String getExtension(); + + public abstract PropertyGroup load(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(Identifier identifier, InputStream is) throws IOException { + PropertyGroup group; + if (identifier.getPath().endsWith(PropertiesGroupAdapter.EXTENSION)) + group = new PropertiesGroupAdapter(identifier); + else + return null; + + group.load(is); + + return group; + } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/format/PropertyKey.java b/src/main/java/shcm/shsupercm/fabric/citresewn/format/PropertyKey.java new file mode 100644 index 0000000..9adcca2 --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/format/PropertyKey.java @@ -0,0 +1,14 @@ +package shcm.shsupercm.fabric.citresewn.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/format/PropertyValue.java b/src/main/java/shcm/shsupercm/fabric/citresewn/format/PropertyValue.java new file mode 100644 index 0000000..ee33cc1 --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/format/PropertyValue.java @@ -0,0 +1,17 @@ +package shcm.shsupercm.fabric.citresewn.format; + +public class PropertyValue { + public final String keyMetadata; + public final String stringValue; + public final String delimiter; + public final int position; + + public Object value = null; + + public PropertyValue(String keyMetadata, String stringValue, String delimiter, int position) { + this.keyMetadata = keyMetadata; + this.stringValue = stringValue; + this.delimiter = delimiter; + this.position = position; + } +} |