Skip to content

Commit

Permalink
Data versioning, default extraction, and removing marker files
Browse files Browse the repository at this point in the history
Marker files have silently been removed - this should not affect anything in a breaking fashion.
  • Loading branch information
lukebemish committed Feb 1, 2024
1 parent bfd594f commit 633332e
Show file tree
Hide file tree
Showing 10 changed files with 258 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (C) 2024 Luke Bemish, and contributors
* SPDX-License-Identifier: LGPL-3.0-or-later
*/

package dev.lukebemish.defaultresources.api;

import dev.lukebemish.defaultresources.impl.DefaultResources;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;

/**
* Allows mods to provide custom logic for when the extracted default resources are outdated, but cannot be updated due
* to having been changed on disk.
*/
public interface OutdatedResourcesListener {

/**
* Called when the listener is fired.
* @param oldDataVersion the data version of the outdated resources on disk, or {@code null} if none is present
* @param newDataVersion the data version of the new resources the mod provides, or {@code null} if none is present
*/
void resourcesOutdated(@Nullable String oldDataVersion, @Nullable String newDataVersion);

/**
* Registers a listener to be notified when the extracted default resources are outdated, but cannot be updated due
* to having been changed on disk.
* @param modId the mod ID to listen for
* @param listener the listener to call if the resources cannot be updated
*/
@SuppressWarnings("unused")
static void register(String modId, OutdatedResourcesListener listener) {
DefaultResources.delegate(() -> {
Optional<String> oldVersion = DefaultResources.OUTDATED_TARGETS.get(modId);
Optional<String> newVersion = DefaultResources.MOD_TARGETS.get(modId);
if (oldVersion != null && newVersion != null) {
listener.resourcesOutdated(oldVersion.orElse(null), newVersion.orElse(null));
}
}, () -> DefaultResources.addListener(modId, listener));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@
import dev.lukebemish.defaultresources.impl.DefaultResources;
import dev.lukebemish.defaultresources.impl.Services;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.PackResources;
import net.minecraft.server.packs.PackType;
import net.minecraft.server.packs.resources.IoSupplier;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Stream;

Expand All @@ -25,9 +22,10 @@
*/
public interface ResourceProvider {
/**
* This should be run before your config file is written too, to ensure that DefaultResources takes it into account
* when figuring out whether to extract default resources.
* This is no longer needed; simply call {@link ResourceProvider#instance()} when you need resources. DefaultResources
* no longer uses config files as markers.
*/
@Deprecated
static void forceInitialization() {
ResourceProvider.instance();
}
Expand All @@ -41,8 +39,8 @@ static void forceInitialization() {
static ResourceProvider instance() {
if (DefaultResources.RESOURCE_PROVIDER == null) {
Services.PLATFORM.extractResources();
DefaultResources.cleanupExtraction();
DefaultResources.RESOURCE_PROVIDER = DefaultResources.assembleResourceProvider();
DefaultResources.cleanupExtraction();
}
return DefaultResources.RESOURCE_PROVIDER;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private static Config readFromConfig() {
ModMetaFile metaFile = ModMetaFile.CODEC.parse(JsonOps.INSTANCE, object).getOrThrow(false, e -> {
});
if (!map.containsKey(modId)) {
map.put(modId, metaFile.extractsByDefault() ? ExtractionState.EXTRACT : ExtractionState.UNEXTRACTED);
map.put(modId, metaFile.extract() ? ExtractionState.EXTRACT : ExtractionState.UNEXTRACTED);
}
} catch (IOException | RuntimeException e) {
DefaultResources.LOGGER.warn("We thought there was a readable {} for mod {}, but we got an error when reading it!",
Expand Down Expand Up @@ -162,17 +162,10 @@ public void save() {
}

enum ExtractionState implements StringRepresentable {
UNEXTRACTED(false, false),
EXTRACT(true, true),
EXTRACTED(true, false);

public final boolean extractIfMissing;
public final boolean extractRegardless;

ExtractionState(boolean extractIfMissing, boolean extractRegardless) {
this.extractIfMissing = extractIfMissing;
this.extractRegardless = extractRegardless;
}
UNEXTRACTED,
EXTRACT,
EXTRACTED,
OUTDATED;

@Override
public @NotNull String getSerializedName() {
Expand Down
Loading

0 comments on commit 633332e

Please sign in to comment.