Search in sources :

Example 1 with CITResewn

use of shcm.shsupercm.fabric.citresewn.CITResewn in project CITResewn by SHsuperCM.

the class CITParser method parse.

/**
 * Parses a resourcepack into a possible collection of citpacks that are contained within.
 * @param resourcePack pack to parse
 * @return a collection of CITPacks or an empty collection if resourcepack contains none
 */
public static Collection<CITPack> parse(ResourcePack resourcePack) {
    if (resourcePack instanceof GroupResourcePack)
        return ((GroupResourcePackAccessor) resourcePack).getPacks().stream().map(CITParser::parse).flatMap(Collection::stream).collect(Collectors.toList());
    final CITPack citPack = new CITPack(resourcePack);
    Collection<Identifier> packProperties = new ArrayList<>();
    for (String namespace : resourcePack.getNamespaces(ResourceType.CLIENT_RESOURCES)) if (Identifier.isValid(namespace))
        for (String citRoot : new String[] { "citresewn", "optifine", "mcpatcher" }) {
            packProperties.addAll(resourcePack.findResources(ResourceType.CLIENT_RESOURCES, namespace, citRoot + "/cit", Integer.MAX_VALUE - 53, s -> s.endsWith(".properties")));
            Identifier global = new Identifier(namespace, citRoot + "/cit.properties");
            if (resourcePack.contains(ResourceType.CLIENT_RESOURCES, global))
                packProperties.add(global);
        }
    boolean readGlobalProperties = false;
    for (Iterator<Identifier> iterator = packProperties.iterator(); iterator.hasNext(); ) {
        Identifier propertiesIdentifier = iterator.next();
        try {
            if (StringUtils.countMatches(propertiesIdentifier.getPath(), '/') <= 2 && propertiesIdentifier.getPath().endsWith("cit.properties")) {
                iterator.remove();
                if (!readGlobalProperties)
                    try (InputStream is = resourcePack.open(ResourceType.CLIENT_RESOURCES, propertiesIdentifier)) {
                        Properties citProperties = new Properties();
                        citProperties.load(is);
                        citPack.loadGlobalProperties(citProperties);
                        readGlobalProperties = true;
                    }
            }
        } catch (Exception e) {
            CITResewn.logErrorLoading("Skipped global properties: " + e.getMessage() + " in " + resourcePack.getName() + " -> " + propertiesIdentifier);
        }
    }
    packProperties.stream().flatMap(citIdentifier -> {
        try (InputStream is = resourcePack.open(ResourceType.CLIENT_RESOURCES, citIdentifier)) {
            Properties citProperties = new Properties();
            citProperties.load(new InputStreamReader(is, StandardCharsets.UTF_8));
            CITConstructor type = REGISTRY.get(citProperties.getProperty("type", "item"));
            if (type == null)
                throw new CITParseException(citPack.resourcePack, citIdentifier, "Unknown cit type \"" + citProperties.getProperty("type") + "\"");
            return Stream.of(type.cit(citPack, citIdentifier, citProperties));
        } catch (Exception e) {
            CITResewn.logErrorLoading(e.getMessage());
            return Stream.empty();
        }
    }).collect(Collectors.toCollection(() -> citPack.cits));
    if (citPack.cits.isEmpty())
        return Collections.emptySet();
    else {
        CITResewn.info("Found " + citPack.cits.size() + " CIT" + (citPack.cits.size() == 1 ? "" : "s") + " in " + resourcePack.getName());
        return Collections.singleton(citPack);
    }
}
Also used : CITParseException(shcm.shsupercm.fabric.citresewn.ex.CITParseException) java.util(java.util) CITResewn(shcm.shsupercm.fabric.citresewn.CITResewn) ResourcePack(net.minecraft.resource.ResourcePack) StringUtils(org.apache.commons.lang3.StringUtils) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) shcm.shsupercm.fabric.citresewn.pack.cits(shcm.shsupercm.fabric.citresewn.pack.cits) Stream(java.util.stream.Stream) ResourceType(net.minecraft.resource.ResourceType) GroupResourcePack(net.fabricmc.fabric.impl.resource.loader.GroupResourcePack) Identifier(net.minecraft.util.Identifier) GroupResourcePackAccessor(shcm.shsupercm.fabric.citresewn.mixin.core.GroupResourcePackAccessor) InputStream(java.io.InputStream) GroupResourcePack(net.fabricmc.fabric.impl.resource.loader.GroupResourcePack) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) CITParseException(shcm.shsupercm.fabric.citresewn.ex.CITParseException) Identifier(net.minecraft.util.Identifier) CITParseException(shcm.shsupercm.fabric.citresewn.ex.CITParseException)

Aggregations

InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 StandardCharsets (java.nio.charset.StandardCharsets)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 GroupResourcePack (net.fabricmc.fabric.impl.resource.loader.GroupResourcePack)1 ResourcePack (net.minecraft.resource.ResourcePack)1 ResourceType (net.minecraft.resource.ResourceType)1 Identifier (net.minecraft.util.Identifier)1 StringUtils (org.apache.commons.lang3.StringUtils)1 CITResewn (shcm.shsupercm.fabric.citresewn.CITResewn)1 CITParseException (shcm.shsupercm.fabric.citresewn.ex.CITParseException)1 GroupResourcePackAccessor (shcm.shsupercm.fabric.citresewn.mixin.core.GroupResourcePackAccessor)1 shcm.shsupercm.fabric.citresewn.pack.cits (shcm.shsupercm.fabric.citresewn.pack.cits)1