Skip to content

Commit

Permalink
Make data generation .cache reproducible (#4259)
Browse files Browse the repository at this point in the history
* Make data generation .cache file reproducible

* Make the cache even more reproducable.

Thanks https://github.com/Gaming32/bingo/blob/b8a38e10efee9b830650f1cf69259214a9911e4d/fabric/src/main/java/io/github/gaming32/bingo/mixin/fabric/MixinHashCache_ProviderCache.java

* Use ModifyExpressionValue
  • Loading branch information
modmuss50 authored Dec 5, 2024
1 parent 7b6b225 commit a22746d
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.datagen;

import java.nio.file.Path;
import java.util.Comparator;
import java.util.Map;

import com.google.common.collect.ImmutableSet;
import com.google.common.hash.HashCode;
import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;

@Mixin(targets = "net.minecraft.data.DataCache$CachedData")
public abstract class DataCacheCachedDataMixin {
@ModifyExpressionValue(method = "write", at = @At(value = "INVOKE", target = "Lcom/google/common/collect/ImmutableMap;entrySet()Lcom/google/common/collect/ImmutableSet;"))
private ImmutableSet<Map.Entry<Path, HashCode>> sortPaths(ImmutableSet<Map.Entry<Path, HashCode>> original) {
return original.stream()
.sorted(Map.Entry.comparingByKey(Comparator.comparing(k -> normalizePath(k.toString()))))
.collect(ImmutableSet.toImmutableSet());
}

@ModifyExpressionValue(method = "write", at = @At(value = "INVOKE", target = "Ljava/nio/file/Path;toString()Ljava/lang/String;"))
private String pathToString(String original) {
return normalizePath(original);
}

@Unique
private static String normalizePath(String path) {
return path.replace('\\', '/');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.datagen;

import java.time.LocalDateTime;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

import net.minecraft.data.DataCache;

@Mixin(DataCache.class)
public abstract class DataCacheMixin {
// Lambda in write()V
@Redirect(method = "method_46571", at = @At(value = "INVOKE", target = "Ljava/time/LocalDateTime;now()Ljava/time/LocalDateTime;"))
private LocalDateTime constantTime() {
// Write a constant time to the .cache file to ensure datagen output is reproducible
return LocalDateTime.MIN;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"package": "net.fabricmc.fabric.mixin.datagen",
"compatibilityLevel": "JAVA_21",
"mixins": [
"DataCacheCachedDataMixin",
"DataCacheMixin",
"DataProviderMixin",
"TagBuilderMixin",
"TagProviderMixin",
Expand Down

0 comments on commit a22746d

Please sign in to comment.