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