aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/shcm/shsupercm/fabric/citresewn/pack/CITParser.java
blob: 03ea512afc6bbe0af1934001f7748a1ebe1e2d85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package shcm.shsupercm.fabric.citresewn.pack;

import net.fabricmc.fabric.impl.resource.loader.GroupResourcePack;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.resource.ResourcePack;
import net.minecraft.resource.ResourceType;
import net.minecraft.util.Identifier;
import org.apache.commons.lang3.StringUtils;
import shcm.shsupercm.fabric.citresewn.CITResewn;
import shcm.shsupercm.fabric.citresewn.ex.CITParseException;
import shcm.shsupercm.fabric.citresewn.mixin.core.GroupResourcePackAccessor;
import shcm.shsupercm.fabric.citresewn.pack.cits.*;

import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Parses cits from resourcepacks
 */
public final class CITParser { private CITParser() {}
    /**
     * CIT type registry.
     */
    public static final Map<String, CITConstructor> REGISTRY = new HashMap<>();
    static {
        REGISTRY.put("item", CITItem::new);
        REGISTRY.put("armor", CITArmor::new);
        REGISTRY.put("elytra", CITElytra::new);
        REGISTRY.put("enchantment", CITEnchantment::new);
    }

    /**
     * Parses cit entries from an ordered collection of resourcepacks.
     * @param packs packs to parse
     * @return a collection of parsed CITs
     */
    public static Collection<CIT> parseCITs(Collection<ResourcePack> packs) {
        return packs.stream()
                .map(CITParser::parse)
                .flatMap(Collection::stream)
                .flatMap(pack -> pack.cits.stream())
                .collect(Collectors.toList());
    }

    /**
     * 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 (FabricLoader.getInstance().isModLoaded("fabric-resource-loader-v0")) {
            Collection<CITPack> group = parseFabricGroup(resourcePack);
            if (group != null)
                return group;
        }

        final CITPack citPack = new CITPack(resourcePack);

        Collection<Identifier> packProperties = new ArrayList<>();
        for (String namespace : resourcePack.getNamespaces(ResourceType.CLIENT_RESOURCES)) {
            packProperties.addAll(resourcePack.findResources(ResourceType.CLIENT_RESOURCES, namespace, "citresewn/cit", Integer.MAX_VALUE - 53, s -> s.endsWith(".properties")));
            if (resourcePack.contains(ResourceType.CLIENT_RESOURCES, new Identifier(namespace, "citresewn/cit.properties")))
                packProperties.add(new Identifier(namespace, "citresewn/cit.properties"));

            packProperties.addAll(resourcePack.findResources(ResourceType.CLIENT_RESOURCES, namespace, "optifine/cit", Integer.MAX_VALUE - 53, s -> s.endsWith(".properties")));
            if (resourcePack.contains(ResourceType.CLIENT_RESOURCES, new Identifier(namespace, "optifine/cit.properties")))
                packProperties.add(new Identifier(namespace, "optifine/cit.properties"));

            packProperties.addAll(resourcePack.findResources(ResourceType.CLIENT_RESOURCES, namespace, "mcpatcher/cit", Integer.MAX_VALUE - 53, s -> s.endsWith(".properties")));
            if (resourcePack.contains(ResourceType.CLIENT_RESOURCES, new Identifier(namespace, "mcpatcher/cit.properties")))
                packProperties.add(new Identifier(namespace, "mcpatcher/cit.properties"));
        }

        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(is);

                        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);
        }
    }

    public static Collection<CITPack> parseFabricGroup(ResourcePack resourcePack) {
        if (!(resourcePack instanceof GroupResourcePack))
            return null;

        return ((GroupResourcePackAccessor) resourcePack).getPacks().stream()
                        .map(CITParser::parse)
                        .flatMap(Collection::stream)
                        .collect(Collectors.toList());
    }

    public interface CITConstructor {
        CIT cit(CITPack pack, Identifier identifier, Properties properties) throws CITParseException;
    }
}