Using WorldDataAPI to keep track of pacth-level

This commit is contained in:
Frank Bauer 2021-07-20 00:23:49 +02:00
parent f595cdbbed
commit 4ad3ff8277
3 changed files with 42 additions and 34 deletions

View file

@ -5,8 +5,9 @@ import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.NbtIo;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.chunk.storage.RegionFile;
import ru.bclib.BCLib;
import ru.bclib.api.WorldDataAPI;
import ru.bclib.config.Configs;
import ru.bclib.config.SessionConfig;
import ru.bclib.util.Logger;
import java.io.DataInputStream;
@ -15,26 +16,27 @@ import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DataFixerAPI {
static final Logger LOGGER = new Logger("DataFixerAPI");
public static void fixData(SessionConfig config) {
public static void fixData(File dir) {
if (!Configs.MAIN_CONFIG.getBoolean(Configs.MAIN_PATCH_CATEGORY, "applyPatches", true)) {
LOGGER.info("World Patches are disabled");
return;
}
final File dir = config.levelFolder;
MigrationProfile data = Patch.createMigrationData(config);
MigrationProfile data = Patch.createMigrationData(WorldDataAPI.getCompoundTag(BCLib.MOD_ID, Configs.MAIN_PATCH_CATEGORY));
if (!data.hasAnyFixes()) {
LOGGER.info("Everything up to date");
return;
}
List<File> regions = getAllRegions(dir, null);
boolean[] allOk = {true};
regions.parallelStream()
.forEach((file) -> {
try {
@ -78,11 +80,23 @@ public class DataFixerAPI {
region.close();
}
catch (Exception e) {
allOk[0] = false;
e.printStackTrace();
}
});
data.markApplied();
if (allOk[0]) {
data.markApplied();
WorldDataAPI.saveFile(BCLib.MOD_ID);
}
}
static CompoundTag patchConfTag = null;
static CompoundTag getPatchData(){
if (patchConfTag==null) {
patchConfTag = WorldDataAPI.getCompoundTag(BCLib.MOD_ID, Configs.MAIN_PATCH_CATEGORY);
}
return patchConfTag;
}
private static boolean fixItemArrayWithID(ListTag items, boolean[] changed, MigrationProfile data, boolean recursive) {
@ -115,6 +129,15 @@ public class DataFixerAPI {
return list;
}
/**
* register a new Patch
*
* @param patch A #Supplier that will instantiate the new Patch Object
*/
public static void registerPatch(Supplier<Patch> patch) {
Patch.getALL().add(patch.get());
}
/**
* Get mod version from string. String should be in format: %d.%d.%d
*
@ -131,9 +154,9 @@ public class DataFixerAPI {
final Matcher matcher = Pattern.compile(semanticVersionPattern)
.matcher(version);
if (matcher.find()) {
if (matcher.groupCount() > 0) res = (Integer.parseInt(matcher.group(1)) & 0xFF) << 20;
if (matcher.groupCount() > 1) res |= (Integer.parseInt(matcher.group(2)) & 0xFF) << 12;
if (matcher.groupCount() > 2) res |= Integer.parseInt(matcher.group(3)) & 0xFFF;
if (matcher.groupCount() > 0) res = (Integer.parseInt(matcher.group(1)) & 0xFF) << 22;
if (matcher.groupCount() > 1) res |= (Integer.parseInt(matcher.group(2)) & 0xFF) << 14;
if (matcher.groupCount() > 2) res |= Integer.parseInt(matcher.group(3)) & 0x3FFF;
}
return res;
@ -150,9 +173,9 @@ public class DataFixerAPI {
* @return {@link String} mod version.
*/
public static String getModVersion(int version) {
int a = (version >> 20) & 0xFF;
int b = (version >> 12) & 0xFF;
int c = version & 0xFFF;
int a = (version >> 22) & 0xFF;
int b = (version >> 14) & 0xFF;
int c = version & 0x3FFF;
return String.format(Locale.ROOT, "%d.%d.%d", a, b, c);
}

View file

@ -2,8 +2,6 @@ package ru.bclib.api.datafixer;
import net.minecraft.nbt.CompoundTag;
import org.jetbrains.annotations.NotNull;
import ru.bclib.config.Configs;
import ru.bclib.config.PathConfig;
import java.util.Collections;
import java.util.HashMap;
@ -14,9 +12,9 @@ import java.util.stream.Collectors;
class MigrationProfile {
final Set<String> mods;
final Map<String, String> idReplacements;
private final PathConfig config;
private final CompoundTag config;
MigrationProfile(PathConfig config) {
MigrationProfile(CompoundTag config) {
this.config = config;
this.mods = Collections.unmodifiableSet(Patch.getALL()
@ -46,14 +44,13 @@ class MigrationProfile {
final public void markApplied() {
for (String modID : mods) {
DataFixerAPI.LOGGER.info("Updating Patch-Level for '{}' from {} to {}", modID, currentPatchLevel(modID), Patch.maxPatchLevel(modID));
config.setString(Configs.MAIN_PATCH_CATEGORY, modID, Patch.maxPatchVersion(modID));
config.putString(modID, Patch.maxPatchVersion(modID));
}
config.saveChanges();
}
public String currentPatchVersion(@NotNull String modID) {
return config.getString(Configs.MAIN_PATCH_CATEGORY, modID, "0.0.0");
if (!config.contains(modID)) return "0.0.0";
return config.getString(modID);
}
public int currentPatchLevel(@NotNull String modID) {

View file

@ -1,13 +1,12 @@
package ru.bclib.api.datafixer;
import net.minecraft.nbt.CompoundTag;
import org.jetbrains.annotations.NotNull;
import ru.bclib.config.PathConfig;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
public abstract class Patch {
private static List<Patch> ALL = new ArrayList<>(10);
@ -24,8 +23,6 @@ public abstract class Patch {
/**
* The Mod-ID that registered this Patch
*
* @return The ID
*/
@NotNull
@ -35,15 +32,6 @@ public abstract class Patch {
return ALL;
}
/**
* register a new Patch
*
* @param patch A #Supplier that will instantiate the new Patch Object
*/
public static void registerPatch(Supplier<Patch> patch) {
ALL.add(patch.get());
}
/**
* Returns the highest Patch-Version that is available for the given mod. If no patches were
* registerd for the mod, this will return 0.0.0
@ -137,10 +125,10 @@ public abstract class Patch {
* <p>
* A {@link #Patch} with a given {@link #level} is only included if the patch-level of the
* world is less
*
* @param config The current patch-level configuration
* @return a new {@link MigrationProfile} Object.
*/
static MigrationProfile createMigrationData(PathConfig config) {
static MigrationProfile createMigrationData(CompoundTag config) {
return new MigrationProfile(config);
}