-
Notifications
You must be signed in to change notification settings - Fork 420
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make data generation .cache reproducible (#4259)
* 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
Showing
3 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
...tion-api-v1/src/main/java/net/fabricmc/fabric/mixin/datagen/DataCacheCachedDataMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('\\', '/'); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...ata-generation-api-v1/src/main/java/net/fabricmc/fabric/mixin/datagen/DataCacheMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters