diff options
author | SHsuperCM <shsupercm@gmail.com> | 2022-02-12 18:26:07 +0200 |
---|---|---|
committer | SHsuperCM <shsupercm@gmail.com> | 2022-02-12 18:26:07 +0200 |
commit | f681f4f8811881b6a7b349f3b215c74a27a7ed4d (patch) | |
tree | ec68d33a0f2e63b9a3bb0f16a4d3534393f730f7 /src | |
parent | 4d0abf10e4c8e66f06b61b52aa3dafef0d535e79 (diff) | |
download | CITResewn-f681f4f8811881b6a7b349f3b215c74a27a7ed4d.tar.gz CITResewn-f681f4f8811881b6a7b349f3b215c74a27a7ed4d.tar.bz2 CITResewn-f681f4f8811881b6a7b349f3b215c74a27a7ed4d.zip |
Implemented CIT parsing/loading
Diffstat (limited to 'src')
7 files changed, 92 insertions, 53 deletions
diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITTypeContainer.java b/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITTypeContainer.java index d691ab2..e207d00 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITTypeContainer.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/api/CITTypeContainer.java @@ -3,7 +3,7 @@ package shcm.shsupercm.fabric.citresewn.api; import shcm.shsupercm.fabric.citresewn.pack.cit.CIT; import shcm.shsupercm.fabric.citresewn.pack.cit.CITType; -import java.util.Collection; +import java.util.List; import java.util.function.Supplier; public abstract class CITTypeContainer<T extends CITType> implements CITDisposable { @@ -18,5 +18,5 @@ public abstract class CITTypeContainer<T extends CITType> implements CITDisposab this.id = id; } - public abstract void load(Collection<CIT> parsedCITs); + public abstract void load(List<CIT> parsedCITs); } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/builtin/WeightCondition.java b/src/main/java/shcm/shsupercm/fabric/citresewn/builtin/WeightCondition.java index 9882d7b..f258df4 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/builtin/WeightCondition.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/builtin/WeightCondition.java @@ -3,7 +3,6 @@ package shcm.shsupercm.fabric.citresewn.builtin; import io.shcm.shsupercm.fabric.fletchingtable.api.Entrypoint; import shcm.shsupercm.fabric.citresewn.api.CITConditionContainer; import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; -import shcm.shsupercm.fabric.citresewn.pack.GlobalProperties; import shcm.shsupercm.fabric.citresewn.pack.cit.CITCondition; import shcm.shsupercm.fabric.citresewn.pack.cit.CITContext; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; @@ -16,7 +15,7 @@ public class WeightCondition extends CITCondition { public int weight = 0; @Override - public void load(PropertyValue value, PropertyGroup properties, GlobalProperties globalProperties) throws CITParsingException { + public void load(PropertyValue value, PropertyGroup properties) throws CITParsingException { this.weight = parseInteger(value, properties); } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java index 4945120..c82b505 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/PackParser.java @@ -2,16 +2,25 @@ package shcm.shsupercm.fabric.citresewn.pack; import net.fabricmc.fabric.impl.resource.loader.GroupResourcePack; import net.fabricmc.loader.api.FabricLoader; -import net.minecraft.resource.ResourceManager; -import net.minecraft.resource.ResourceNotFoundException; -import net.minecraft.resource.ResourcePack; -import net.minecraft.resource.ResourceType; +import net.minecraft.resource.*; import net.minecraft.util.Identifier; import shcm.shsupercm.fabric.citresewn.CITResewn; +import shcm.shsupercm.fabric.citresewn.builtin.WeightCondition; +import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; import shcm.shsupercm.fabric.citresewn.mixin.GroupResourcePackAccessor; +import shcm.shsupercm.fabric.citresewn.pack.cit.CIT; +import shcm.shsupercm.fabric.citresewn.pack.cit.CITCondition; +import shcm.shsupercm.fabric.citresewn.pack.cit.CITRegistry; +import shcm.shsupercm.fabric.citresewn.pack.cit.CITType; +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 java.io.IOException; +import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.Set; import java.util.function.Consumer; import java.util.function.Function; @@ -47,4 +56,58 @@ public class PackParser { } }); } + + public static List<CIT> loadCITs(ResourceManager resourceManager) { + List<CIT> cits = new ArrayList<>(); + + for (String root : ROOTS) + for (Identifier identifier : resourceManager.findResources(root + "/cit", s -> s.endsWith(".properties"))) { + String packName = null; + try (Resource resource = resourceManager.getResource(identifier)) { + cits.add(parseCIT(PropertyGroup.tryParseGroup(packName = resource.getResourcePackName(), identifier, resource.getInputStream()))); + } catch (Exception e) { + CITResewn.logErrorLoading("Errored while loading cit: " + identifier + (packName == null ? "" : " from " + packName)); + e.printStackTrace(); + } + } + + return cits; + } + + + public static CIT parseCIT(PropertyGroup properties) throws CITParsingException { + CITType citType = CITRegistry.parseType(properties); + + ArrayList<CITCondition> conditions = new ArrayList<>(); + + for (Map.Entry<PropertyKey, Set<PropertyValue>> entry : properties.properties.entrySet()) { + if (entry.getKey().path().equals("type") && entry.getKey().namespace().equals("citresewn")) + continue; + + for (PropertyValue value : entry.getValue()) + conditions.add(CITRegistry.parseCondition(entry.getKey(), value, properties)); + } + + for (CITCondition condition : new ArrayList<>(conditions)) + for (Class<? extends CITCondition> siblingConditionType : condition.siblingConditions()) + conditions.replaceAll( + siblingCondition -> siblingConditionType == siblingCondition.getClass() ? + condition.modifySibling(siblingConditionType, siblingCondition) : + siblingCondition); + + WeightCondition weight = new WeightCondition(); + + conditions.removeIf(condition -> { + if (condition instanceof WeightCondition weightCondition) { + weight.weight = weightCondition.weight; + return true; + } + + return condition == null; + }); + + citType.load(conditions, properties); + + return new CIT(properties.identifier, properties.packName, citType, conditions.toArray(new CITCondition[0]), weight.weight); + } } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/ActiveCITs.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/ActiveCITs.java index 62f011e..afc228c 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/ActiveCITs.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/ActiveCITs.java @@ -7,6 +7,8 @@ import shcm.shsupercm.fabric.citresewn.api.CITDisposable; import shcm.shsupercm.fabric.citresewn.pack.GlobalProperties; import shcm.shsupercm.fabric.citresewn.pack.PackParser; +import java.util.*; + public class ActiveCITs implements CITDisposable { private ActiveCITs() {} private static ActiveCITs active = null; @@ -20,6 +22,8 @@ public class ActiveCITs implements CITDisposable { private ActiveCITs() {} public final GlobalProperties globalProperties = new GlobalProperties(); + public final Map<Class<? extends CITType>, List<CIT>> cits = new IdentityHashMap<>(); + public static ActiveCITs load(ResourceManager resourceManager, Profiler profiler) { profiler.push("citresewn:disposing"); if (active != null) { @@ -32,6 +36,15 @@ public class ActiveCITs implements CITDisposable { private ActiveCITs() {} profiler.swap("citresewn:load_global_properties"); PackParser.loadGlobalProperties(resourceManager, active.globalProperties); active.globalProperties.callHandlers(); + + profiler.swap("citresewn:load_cits"); + for (CIT cit : PackParser.loadCITs(resourceManager)) + active.cits.computeIfAbsent(cit.type.getClass(), type -> new ArrayList<>()).add(cit); + for (Map.Entry<Class<? extends CITType>, List<CIT>> entry : active.cits.entrySet()) { + entry.getValue().sort(Comparator.<CIT>comparingInt(cit -> cit.weight).reversed().thenComparing(cit -> cit.propertiesIdentifier.toString())); + CITRegistry.load(entry.getKey(), entry.getValue()); + } + profiler.pop(); return ActiveCITs.active = active; diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITCondition.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITCondition.java index 2de5be1..b5aa315 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITCondition.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITCondition.java @@ -1,7 +1,6 @@ package shcm.shsupercm.fabric.citresewn.pack.cit; import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; -import shcm.shsupercm.fabric.citresewn.pack.GlobalProperties; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; @@ -9,11 +8,7 @@ import java.util.Collections; import java.util.Set; public abstract class CITCondition { - public abstract void load(PropertyValue value, PropertyGroup properties, GlobalProperties globalProperties) throws CITParsingException; - - public Set<Class<? extends CITType>> acceptedTypes() { - return null; - } + public abstract void load(PropertyValue value, PropertyGroup properties) throws CITParsingException; public Set<Class<? extends CITCondition>> siblingConditions() { return Collections.emptySet(); diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITRegistry.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITRegistry.java index fe0bcc7..e24e598 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITRegistry.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITRegistry.java @@ -4,10 +4,8 @@ import net.fabricmc.loader.api.FabricLoader; import net.minecraft.util.Identifier; import shcm.shsupercm.fabric.citresewn.api.CITConditionContainer; import shcm.shsupercm.fabric.citresewn.api.CITTypeContainer; -import shcm.shsupercm.fabric.citresewn.builtin.WeightCondition; import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; import shcm.shsupercm.fabric.citresewn.ex.UnknownCITTypeException; -import shcm.shsupercm.fabric.citresewn.pack.GlobalProperties; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyKey; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyValue; @@ -42,43 +40,15 @@ public class CITRegistry { } } - public static CIT load(PropertyGroup properties, GlobalProperties globalProperties) throws CITParsingException { - CITType citType = parseType(properties); - - ArrayList<CITCondition> conditions = new ArrayList<>(); - - for (Map.Entry<PropertyKey, Set<PropertyValue>> entry : properties.properties.entrySet()) { - if (entry.getKey().path().equals("type") && entry.getKey().namespace().equals("citresewn")) - continue; - - for (PropertyValue value : entry.getValue()) - conditions.add(parseCondition(entry.getKey(), value, properties, globalProperties)); - } - - for (CITCondition condition : new ArrayList<>(conditions)) - for (Class<? extends CITCondition> siblingConditionType : condition.siblingConditions()) - conditions.replaceAll( - siblingCondition -> siblingConditionType == siblingCondition.getClass() ? - condition.modifySibling(siblingConditionType, siblingCondition) : - siblingCondition); - - WeightCondition weight = new WeightCondition(); - - conditions.removeIf(condition -> { - if (condition instanceof WeightCondition weightCondition) { - weight.weight = weightCondition.weight; - return true; + public static void load(Class<? extends CITType> type, List<CIT> cits) { + for (CITTypeContainer<? extends CITType> typeContainer : TYPES.values()) + if (typeContainer.type == type) { + typeContainer.load(cits); + break; } - - return condition == null; - }); - - citType.load(conditions, properties, globalProperties); - - return new CIT(properties.identifier, properties.packName, citType, conditions.toArray(new CITCondition[0]), weight.weight); } - public static CITCondition parseCondition(PropertyKey key, PropertyValue value, PropertyGroup properties, GlobalProperties globalProperties) throws CITParsingException { + public static CITCondition parseCondition(PropertyKey key, PropertyValue value, PropertyGroup properties) throws CITParsingException { CITConditionContainer<? extends CITCondition> conditionContainer = CONDITIONS.get(key); if (conditionContainer == null) { logWarnLoading("Skipping condition: " + CITParsingException.descriptionOf("Unknown condition type", properties, value.position())); @@ -86,7 +56,7 @@ public class CITRegistry { } CITCondition condition = conditionContainer.createCondition.get(); - condition.load(value, properties, globalProperties); + condition.load(value, properties); return condition; } diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITType.java b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITType.java index bbfa0e9..4504c01 100644 --- a/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITType.java +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/pack/cit/CITType.java @@ -1,11 +1,10 @@ package shcm.shsupercm.fabric.citresewn.pack.cit; import shcm.shsupercm.fabric.citresewn.ex.CITParsingException; -import shcm.shsupercm.fabric.citresewn.pack.GlobalProperties; import shcm.shsupercm.fabric.citresewn.pack.format.PropertyGroup; import java.util.List; public abstract class CITType { - public abstract void load(List<? extends CITCondition> conditions, PropertyGroup properties, GlobalProperties globalProperties) throws CITParsingException; + public abstract void load(List<? extends CITCondition> conditions, PropertyGroup properties) throws CITParsingException; } |