Skip to content

Commit

Permalink
Allow Manually Reapplying Data Fixers
Browse files Browse the repository at this point in the history
  • Loading branch information
IntegerLimit committed Jul 19, 2024
1 parent 3933f19 commit d1bafec
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 17 deletions.
11 changes: 11 additions & 0 deletions src/main/java/com/nomiceu/nomilabs/config/LabsVersionConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ public class LabsVersionConfig {
@Config.RequiresMcRestart
public static String formattedVersion = "v1.0.0";

@Config.Comment({
"Manual Fix Version. Increment this to have Data Fixers be POTENTIALLY applied.",
"Note that if a chunk or inventory is loaded on the same fix version multiple times, it will only be fixed the first time.",
"Note that no warning will be displayed before game load.",
"The data fix version itself will be this config + the Labs internal fix version.",
"[default: 0]" })
@Config.LangKey("config.nomilabs.version.manual-fix-version")
@Config.RangeInt(min = 0)
@Config.RequiresMcRestart
public static int manualFixVersion = 0;

static {
ConfigAnytime.register(LabsVersionConfig.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public enum MessageType {
CONFIRM
}

// Don't forget to update this in en_us.lang as well!
public static class Components {

public static String[] getIntro() {
Expand Down Expand Up @@ -63,5 +62,12 @@ public static String[] getDoNotExit() {
translate("nomilabs.fixer.do_not_revert.2")
};
}

public static String[] getInvalidManualVersion() {
return new String[] {
translate("nomilabs.fixer.invalid_manual_version.1"),
translate("nomilabs.fixer.invalid_manual_version.2")
};
}
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/nomiceu/nomilabs/remap/LabsRemapHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import net.minecraftforge.fml.common.ZipperUtil;

import com.nomiceu.nomilabs.NomiLabs;
import com.nomiceu.nomilabs.config.LabsVersionConfig;
import com.nomiceu.nomilabs.remap.datafixer.DataFixerHandler;
import com.nomiceu.nomilabs.remap.datafixer.LabsFixes;
import com.nomiceu.nomilabs.remap.datafixer.storage.BlockRewriter;
import com.nomiceu.nomilabs.remap.datafixer.storage.BlockStateLike;
import com.nomiceu.nomilabs.remap.datafixer.storage.CompoundRewriter;
Expand Down Expand Up @@ -160,6 +162,15 @@ public static Map<BlockPos, NBTTagCompound> getPosToTileEntityMap(NBTTagCompound
return posToTileEntityCache;
}

/**
* This method gets the version which should be reported by all Labs Data Fixers.
* <p>
* This combines the internal version with the config version, allowing for forced re-application.
*/
public static int getReportedVersion() {
return LabsFixes.CURRENT + LabsVersionConfig.manualFixVersion;
}

public static void abort() {
DataFixerHandler.close();
StartupQuery.abort();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import com.nomiceu.nomilabs.LabsValues;
import com.nomiceu.nomilabs.NomiLabs;
import com.nomiceu.nomilabs.config.LabsVersionConfig;
import com.nomiceu.nomilabs.mixinhelper.RemappableForgeRegistry;
import com.nomiceu.nomilabs.remap.LabsRemapHelper;
import com.nomiceu.nomilabs.remap.datafixer.fixes.BlockFixer;
Expand Down Expand Up @@ -88,7 +89,7 @@ public static void preInit() {
fmlFixer.registerVanillaWalker(FixTypes.CHUNK, new ChunkWalker());

// Fixers
ModFixs fixs = fmlFixer.init(LabsValues.LABS_MODID, LabsFixes.CURRENT);
ModFixs fixs = fmlFixer.init(LabsValues.LABS_MODID, LabsRemapHelper.getReportedVersion());
fixs.registerFix(LabsFixTypes.FixerTypes.ITEM, new ItemFixer());
fixs.registerFix(LabsFixTypes.FixerTypes.CHUNK, new BlockFixer());
fixs.registerFix(LabsFixTypes.FixerTypes.TILE_ENTITY, new TileEntityFixer());
Expand All @@ -114,16 +115,39 @@ public static void onWorldLoad(SaveHandler save) {
if (mapFile.exists()) {
DataFixerHandler.worldSavedData = LabsWorldFixData.load(mapFile);

// Shortcut: If saved version == Current Version, Exit
// Check if manual fix version has been decremented
// This could break things, so throw an exception
if (LabsVersionConfig.manualFixVersion < DataFixerHandler.worldSavedData.savedManualFixVersion) {
NomiLabs.LOGGER.fatal("Manual Data Fix Version has been decremented. This is not supported!");
sendMessage(MessageType.NOTIFY, Components.getInvalidManualVersion());
LabsRemapHelper.abort();
}

// No need to increment version, the fix version, not the stored version, is saved
// Still need to call as otherwise it isn't actually changed
LabsWorldFixData.save(mapFile, DataFixerHandler.worldSavedData);

// Shortcut: If saved version == Current Version, Exit (No NEW Fixes)
if (DataFixerHandler.worldSavedData.savedFixVersion == LabsFixes.CURRENT) {
NomiLabs.LOGGER.info("This world's internal data version is up to date.");

// Check only relevant if internal fix version hasn't changed
// Else, manual version increment doesn't matter
if (LabsVersionConfig.manualFixVersion != DataFixerHandler.worldSavedData.savedManualFixVersion) {
NomiLabs.LOGGER.info(
"The Manual Data Fix Version has been increased. New Version: {}. Enabling Data Fixers...",
LabsVersionConfig.manualFixVersion);
}

DataFixerHandler.worldSavedData = null;
NomiLabs.LOGGER.info("This world's data version is up to date.");
return;
}
NomiLabs.LOGGER.info("This world's data version needs updating. New Version: {}.", LabsFixes.CURRENT);
NomiLabs.LOGGER.info("This world's internal data version needs updating. New Version: {}.",
LabsFixes.CURRENT);
} else {
DataFixerHandler.worldSavedData = new LabsWorldFixData();
NomiLabs.LOGGER.info("This world was saved without a data version. New Version: {}.", LabsFixes.CURRENT);
NomiLabs.LOGGER.info("This world was saved without a data version info. New Version: {}.",
LabsFixes.CURRENT);
}

determineNeededFixesAndLog();
Expand Down Expand Up @@ -152,10 +176,6 @@ public static void onWorldLoad(SaveHandler save) {
checked = true;

LabsRemapHelper.createWorldBackup();

// No need to increment version, the fix version, not the stored version, is saved
// Still need to call as otherwise it isn't actually changed
LabsWorldFixData.save(mapFile, DataFixerHandler.worldSavedData);
}

private static void getInfoFromSave(SaveHandler save) {
Expand Down Expand Up @@ -199,6 +219,7 @@ private static void determineNeededFixesAndLog() {
// This normally means it is a new world.
// Sometimes the level.dat file is created first, but usually this runs after it is created.
// If the level.dat file is created first, its mod list is equal to the current one.
// Regardless, NEW fixes are not required.
if (LabsValues.LABS_VERSION.equals(savedLabsVersion))
return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ public class LabsFixes {
public static final int NEW = Integer.MAX_VALUE;

/**
* The current data format version. Increment this when breaking changes are made and the
* data mixer must be applied. If this is not incremented, nothing will be applied.
* The current data format version. Increment this when breaking changes are made and
* data fixers should be applied. If this is not incremented, nothing will be applied to loaded worlds and
* inventories.
*/
public static final int CURRENT = 4;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,30 @@

import com.nomiceu.nomilabs.NomiLabs;
import com.nomiceu.nomilabs.config.LabsConfig;
import com.nomiceu.nomilabs.config.LabsVersionConfig;

public class LabsWorldFixData extends WorldSavedData {

// Fix Version stored in this world.
// Fix Versions stored in this world.
public int savedFixVersion;
public int savedManualFixVersion;

// Base Data Key
public static final String BASE_DATA_KEY = "data";

// Key used to store fix version
public static final String VERSION_KEY = "Version";

// Key used to store manual fix version. Used for log status messages.
public static final String MANUAL_VERSION_KEY = "ManualVersion";

public LabsWorldFixData() {
super(LabsFixes.DATA_NAME);

if (LabsConfig.advanced.enableNomiCEuDataFixes) savedFixVersion = LabsFixes.DEFAULT_NOMI_CEU;
else savedFixVersion = LabsFixes.DEFAULT;

savedManualFixVersion = 0;
}

public void processSavedLabsVersion(String savedLabsVersion) {
Expand All @@ -43,14 +51,23 @@ public void readFromNBT(NBTTagCompound nbt) {
savedFixVersion = nbt.getInteger(VERSION_KEY);
NomiLabs.LOGGER.info("This world was previously loaded with Fix Version {}.", savedFixVersion);
} else
NomiLabs.LOGGER.info(
NomiLabs.LOGGER.warn(
"This world was previously loaded without a saved Fix Version, possibly due to corruption, defaulting to {}.",
savedFixVersion);

if (nbt.hasKey(MANUAL_VERSION_KEY, Constants.NBT.TAG_ANY_NUMERIC)) {
savedManualFixVersion = nbt.getInteger(MANUAL_VERSION_KEY);
NomiLabs.LOGGER.info("This world was previously loaded with Manual Fix Version {}.", savedManualFixVersion);
} else
NomiLabs.LOGGER.info("This world was saved without a Manual Fix Version. Defaulting to {}.",
savedManualFixVersion);
}

@Override
public @NotNull NBTTagCompound writeToNBT(NBTTagCompound compound) {
compound.setInteger(VERSION_KEY, LabsFixes.CURRENT); // Save the current fix version, not the saved version
compound.setInteger(MANUAL_VERSION_KEY, LabsVersionConfig.manualFixVersion); // Save current manual fix version,
// not the saved manual version
return compound;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class BlockFixer implements IFixableData {

@Override
public int getFixVersion() {
return LabsFixes.CURRENT;
return LabsRemapHelper.getReportedVersion();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.jetbrains.annotations.NotNull;

import com.nomiceu.nomilabs.NomiLabs;
import com.nomiceu.nomilabs.remap.LabsRemapHelper;
import com.nomiceu.nomilabs.remap.datafixer.DataFix;
import com.nomiceu.nomilabs.remap.datafixer.LabsFixes;
import com.nomiceu.nomilabs.remap.datafixer.storage.ItemStackLike;
Expand All @@ -15,7 +16,7 @@ public class ItemFixer implements IFixableData {

@Override
public int getFixVersion() {
return LabsFixes.CURRENT;
return LabsRemapHelper.getReportedVersion();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.jetbrains.annotations.NotNull;

import com.nomiceu.nomilabs.NomiLabs;
import com.nomiceu.nomilabs.remap.LabsRemapHelper;
import com.nomiceu.nomilabs.remap.datafixer.DataFix;
import com.nomiceu.nomilabs.remap.datafixer.LabsFixes;
import com.nomiceu.nomilabs.remap.datafixer.types.LabsFixTypes;
Expand All @@ -14,7 +15,7 @@ public class TileEntityFixer implements IFixableData {

@Override
public int getFixVersion() {
return LabsFixes.CURRENT;
return LabsRemapHelper.getReportedVersion();
}

@Override
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/assets/nomilabs/lang/en_us.lang
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ nomilabs.tooltip=Normal Nomi Labs Configs
nomilabs-version.tooltip=Nomi Labs Modpack Version Config

config.nomilabs.version.formatted-version=Formatted Modpack Version
config.nomilabs.version.manual-fix-version=[ADVANCED] Manual Fix Version

config.nomilabs.content=Content Settings
config.nomilabs.content.tooltip=Content Settings
Expand Down Expand Up @@ -163,6 +164,9 @@ nomilabs.fixer.mode_check.4=Press 'No' if you are not sure! (It will cancel worl
nomilabs.fixer.do_not_exit.1=§cDo not interrupt the loading process!§r
nomilabs.fixer.do_not_exit.2=If interrupted, load again from the backup, as your world may be corrupted!

nomilabs.fixer.invalid_manual_version.1=§cManual Fix Version has been Decremented!§r
nomilabs.fixer.invalid_manual_version.2=§cReport this to the Modpack Author!§r

# Commands
nomilabs.command.gs_hand.material=Material ID:
nomilabs.command.gs_hand.material_stack=Material Stack:
Expand Down

0 comments on commit d1bafec

Please sign in to comment.