Make DFU error-tolerant to mod custom generator types (#3213)

* Make DFU error-tolerant to mod custom generator types.

* Fix license. Rename mixin.

* Fix license.

* Disable remap in Schema2832Mixin
This commit is contained in:
qouteall 2023-08-13 23:01:27 +08:00 committed by GitHub
parent 0d99377dd7
commit 8536527b69
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 250 additions and 1 deletions

View file

@ -0,0 +1,21 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.fabricmc.fabric.impl.dimension;
public interface TaggedChoiceExtension {
void fabric$setFailSoft(boolean cond);
}

View file

@ -0,0 +1,21 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.fabricmc.fabric.impl.dimension;
public interface TaggedChoiceTypeExtension {
void fabric$setFailSoft(boolean cond);
}

View file

@ -0,0 +1,56 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.fabricmc.fabric.mixin.dimension;
import java.util.Map;
import java.util.function.Supplier;
import com.mojang.datafixers.DSL;
import com.mojang.datafixers.types.Type;
import com.mojang.datafixers.types.templates.TaggedChoice;
import com.mojang.datafixers.types.templates.TypeTemplate;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
import net.minecraft.datafixer.schema.Schema2832;
import net.fabricmc.fabric.impl.dimension.TaggedChoiceExtension;
@Mixin(Schema2832.class)
public class Schema2832Mixin {
/**
* Make the DSL.taggedChoiceLazy to ignore mod custom generator types and not cause deserialization failure.
*/
@Redirect(
method = {
"method_38837", "method_38838"
},
at = @At(
value = "INVOKE",
target = "Lcom/mojang/datafixers/DSL;taggedChoiceLazy(Ljava/lang/String;Lcom/mojang/datafixers/types/Type;Ljava/util/Map;)Lcom/mojang/datafixers/types/templates/TaggedChoice;",
remap = false
)
)
private static <K> TaggedChoice<K> redirectTaggedChoiceLazy(
String name, Type<K> keyType, Map<K, Supplier<TypeTemplate>> templates
) {
TaggedChoice<K> result = DSL.taggedChoiceLazy(name, keyType, templates);
((TaggedChoiceExtension) (Object) result).fabric$setFailSoft(true);
return result;
}
}

View file

@ -0,0 +1,57 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.fabricmc.fabric.mixin.dimension;
import com.mojang.datafixers.types.Type;
import com.mojang.datafixers.types.templates.TaggedChoice;
import com.mojang.datafixers.util.Pair;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import net.fabricmc.fabric.impl.dimension.TaggedChoiceExtension;
import net.fabricmc.fabric.impl.dimension.TaggedChoiceTypeExtension;
@Mixin(value = TaggedChoice.class, remap = false)
public class TaggedChoiceMixin implements TaggedChoiceExtension {
@Unique
boolean failSoft = false;
@Override
public void fabric$setFailSoft(boolean cond) {
failSoft = cond;
}
/**
* Pass the failSoft information into TaggedChoice.TaggedChoiceType.
*/
@SuppressWarnings("rawtypes")
@Inject(
method = "lambda$apply$0", at = @At("RETURN"), remap = false
)
private void onApply(Pair key, CallbackInfoReturnable<Type> cir) {
if (failSoft) {
Type returnValue = cir.getReturnValue();
if (returnValue instanceof TaggedChoice.TaggedChoiceType<?> taggedChoiceType) {
((TaggedChoiceTypeExtension) (Object) taggedChoiceType).fabric$setFailSoft(true);
}
}
}
}

View file

@ -0,0 +1,69 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.fabricmc.fabric.mixin.dimension;
import com.mojang.datafixers.types.Type;
import com.mojang.datafixers.types.templates.TaggedChoice;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import net.fabricmc.fabric.impl.dimension.TaggedChoiceTypeExtension;
@Mixin(value = TaggedChoice.TaggedChoiceType.class, remap = false)
public class TaggedChoiceTypeMixin<K> implements TaggedChoiceTypeExtension {
@Unique
private static final Logger LOGGER = LoggerFactory.getLogger("TaggedChoiceType_DimDataFix");
@Shadow(remap = false)
@Final
protected Object2ObjectMap<K, Type<?>> types;
@Unique
private boolean failSoft;
/**
* Make the DSL.taggedChoiceLazy to ignore mod custom generator types and not cause deserialization failure.
* The Codec.PASSTHROUGH will not make Dynamic to be deserialized and serialized to Dynamic.
* This will avoid deserialization failure from DFU when upgrading level.dat that contains mod custom generator types.
*/
@Inject(
method = "getCodec", at = @At("HEAD"), cancellable = true, remap = false
)
private void onGetCodec(K k, CallbackInfoReturnable<DataResult<? extends Codec<?>>> cir) {
if (failSoft) {
if (!types.containsKey(k)) {
LOGGER.warn("Not recognizing key {}. Using pass-through codec. {}", k, this);
cir.setReturnValue(DataResult.success(Codec.PASSTHROUGH));
}
}
}
@Override
public void fabric$setFailSoft(boolean cond) {
failSoft = cond;
}
}

View file

@ -4,7 +4,10 @@
"compatibilityLevel": "JAVA_16",
"mixins": [
"EntityMixin",
"RegistryCodecsMixin"
"RegistryCodecsMixin",
"Schema2832Mixin",
"TaggedChoiceMixin",
"TaggedChoiceTypeMixin"
],
"injectors": {
"defaultRequire": 1

View file

@ -0,0 +1,22 @@
{
"$comments": [
"CompoundListCodec is sensitive to the order of elements in hash map in NbtCompound.",
"In CompoundListCodec, when one entry fails deserialization,",
"entries before it will remain but entries after it will be lost.",
"Coincidentally, fabric_dimension:void's position in hash map is after all vanilla dimensions.",
"When fabric_dimension:void fails deserialization in DFU, the vanilla dimensions will still be deserialized.",
"But a mod dimension's id could be before the vanilla dimensions in the hash map,",
"which will make it deserialize before vanilla dimensions and cause vanilla dimension lost.",
"This dimension fabric_dimension:void_earilier_in_hash_map has a different hashcode ",
"and is before minecraft:the_nether in the hash map, so it can reproduce that issue."
],
"generator": {
"type": "fabric_dimension:void",
"custom_bool": true,
"biome_source": {
"type": "minecraft:fixed",
"biome": "minecraft:plains"
}
},
"type": "fabric_dimension:void_type"
}