Skip to content

Commit

Permalink
Initial work on system to include packs from resourcepacks
Browse files Browse the repository at this point in the history
  • Loading branch information
lukebemish committed Dec 4, 2023
1 parent 3d51393 commit f4542ab
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import com.google.common.base.Suppliers;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import com.mojang.serialization.Codec;
import com.mojang.serialization.JsonOps;
Expand All @@ -22,16 +23,18 @@
import java.util.Locale;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.zip.ZipFile;

public record Config(HashMap<String, ExtractionState> extract) {
public record Config(HashMap<String, ExtractionState> extract, HashMap<String, Boolean> fromResourcePacks) {
public static final Codec<Config> CODEC = RecordCodecBuilder.create(i -> i.group(
Codec.unboundedMap(Codec.STRING, StringRepresentable.fromEnum(ExtractionState::values)).xmap(HashMap::new, Function.identity()).fieldOf("extract").forGetter(Config::extract)
Codec.unboundedMap(Codec.STRING, StringRepresentable.fromEnum(ExtractionState::values)).xmap(HashMap::new, Function.identity()).fieldOf("extract").forGetter(Config::extract),
Codec.unboundedMap(Codec.STRING, Codec.BOOL).fieldOf("from_resource_packs").xmap(HashMap::new, Function.identity()).forGetter(Config::fromResourcePacks)
).apply(i, Config::new));

public static final Supplier<Config> INSTANCE = Suppliers.memoize(Config::readFromConfig);

private static Config getDefault() {
return new Config(new HashMap<>());
return new Config(new HashMap<>(), new HashMap<>());
}

private static Config readFromConfig() {
Expand Down Expand Up @@ -64,7 +67,32 @@ private static Config readFromConfig() {
DefaultResources.META_FILE_PATH, modId, e);
}
});
config = new Config(map);

var resourcePacks = new HashMap<String, Boolean>();
var originalResourcePacks = config.fromResourcePacks();
Path resourcePacksPath = Services.PLATFORM.getResourcePackDir();
try (var paths = Files.list(resourcePacksPath)) {
paths.forEach(path -> {
String fileName = path.getFileName().toString();
boolean detect;
if (fileName.endsWith(".zip") || fileName.endsWith(".jar")) {
detect = checkZipForMeta(path);
} else if (Files.isDirectory(path)) {
detect = checkPathForMeta(path);
} else {
return;
}
if (detect) {
resourcePacks.put(fileName, originalResourcePacks.getOrDefault(fileName, true));
}
});
} catch (IOException e) {
if (Files.exists(resourcePacksPath)) {
DefaultResources.LOGGER.warn("Could not read resource packs from {}!", resourcePacksPath, e);
}
}

config = new Config(map, resourcePacks);

try {
writeConfig(configPath, config);
Expand All @@ -75,6 +103,36 @@ private static Config readFromConfig() {
return config;
}

private static boolean checkZipForMeta(Path path) {
try (var zipFile = new ZipFile(path.toFile());
var reader = zipFile.getInputStream(zipFile.getEntry("pack.mcmeta"))) {
if (reader != null) {
JsonObject object = DefaultResources.GSON.fromJson(new InputStreamReader(reader), JsonObject.class);
JsonElement meta = object.get(DefaultResources.MOD_ID);
var result = DefaultResourcesMetadataSection.CODEC.parse(JsonOps.INSTANCE, meta);
return result.result().isPresent() && result.result().get().detect();
}
} catch (Exception e) {
DefaultResources.LOGGER.error("Could not read {}, which looks like a zip file, for resource pack detection; ignoring.", path.getFileName(), e);
}
return false;
}

private static boolean checkPathForMeta(Path path) {
var metaFile = path.resolve("pack.mcmeta");
if (Files.exists(metaFile)) {
try (var reader = Files.newBufferedReader(metaFile)) {
JsonObject object = DefaultResources.GSON.fromJson(reader, JsonObject.class);
JsonElement meta = object.get(DefaultResources.MOD_ID);
var result = DefaultResourcesMetadataSection.CODEC.parse(JsonOps.INSTANCE, meta);
return result.result().isPresent() && result.result().get().detect();
} catch (Exception e) {
DefaultResources.LOGGER.error("Could not read {} for resource pack detection; ignoring.", path.getFileName(), e);
}
}
return false;
}

private static void writeConfig(Path path, Config config) throws IOException {
if (!Files.exists(path.getParent()))
Files.createDirectories(path.getParent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ public static ResourceProvider assembleResourceProvider() {
} catch (IOException ignored) {
}
providers.addAll(Services.PLATFORM.getJarProviders());
Config.INSTANCE.get().fromResourcePacks().forEach((name, enabled) -> {
if (enabled) {
Path path = Services.PLATFORM.getResourcePackDir().resolve(name);
if (Files.isDirectory(path)) {
providers.add(new PathResourceProvider(path));
} else if (Files.isRegularFile(path)) {
providers.add(new ZipResourceProvider(path));
}
}
});
return new GroupedResourceProvider(providers);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dev.lukebemish.defaultresources.impl;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;

public record DefaultResourcesMetadataSection(boolean detect) {
public static final Codec<DefaultResourcesMetadataSection> CODEC = RecordCodecBuilder.create(i -> i.group(
Codec.BOOL.optionalFieldOf("detect", false).forGetter(DefaultResourcesMetadataSection::detect)
).apply(i, DefaultResourcesMetadataSection::new));
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public interface IPlatform {
void extractResources();

Collection<ResourceProvider> getJarProviders();
Path getResourcePackDir();

Path getConfigDir();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public Collection<ResourceProvider> getJarProviders() {
return providers;
}

@Override
public Path getResourcePackDir() {
return FMLPaths.GAMEDIR.get().resolve("resourcepacks");
}

@Override
public Path getConfigDir() {
return FMLPaths.CONFIGDIR.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public Collection<ResourceProvider> getJarProviders() {
return providers;
}

@Override
public Path getResourcePackDir() {
return QuiltLoader.getGameDir().resolve("resourcepacks");
}

@Override
public Path getConfigDir() {
return QuiltLoader.getConfigDir();
Expand Down
2 changes: 1 addition & 1 deletion version.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#Fri Jun 09 23:49:45 UTC 2023
version=2.0.0
version=2.1.0

0 comments on commit f4542ab

Please sign in to comment.