Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement registry removal check #3258

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
/*
* 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.client.registry.sync;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why client only? I think it would make sense to save this file on the server, thus you get the warning when taking a server world to the client.

We decided not to show the error on the server, but maybe a warning log (but still try to load) could be helpful?


import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.stream.Collectors;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.mojang.datafixers.util.Pair;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.minecraft.block.Block;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.state.property.Property;
import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper;
import net.minecraft.util.Util;

import net.fabricmc.fabric.api.event.registry.RegistryAttribute;
import net.fabricmc.fabric.api.event.registry.RegistryAttributeHolder;

public final class RegistryRemovalChecker {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some unit tests would be great for this.

private static final int VERSION = 1;
public static final Logger LOGGER = LoggerFactory.getLogger(RegistryRemovalChecker.class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally we try to keep loggers private?

private static final Gson GSON = new Gson();
public static final String FILE_NAME = "fabric_registry_removal_check.json";
private static final boolean DISABLED = Boolean.getBoolean("fabric.registry.debug.disableRemovalCheck");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this field to the top, a small comment might be helpful to make it more visable?

private final Set<String> missingNamespaces;
private final Set<RegistryKey<?>> missingKeys;
private final Set<Pair<Block, String>> missingBlockStates = new HashSet<>();

@SuppressWarnings("unchecked, rawtypes")
public RegistryRemovalChecker(JsonObject root) {
JsonObject json = root.getAsJsonObject("entries");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This never checks the version, it should handle the case of the version being newer than supported. I imagine telling the user this.

Set<RegistryKey<?>> keys = new HashSet<>();

for (Map.Entry<String, JsonElement> registry : json.entrySet()) {
Identifier registryId = Identifier.tryParse(registry.getKey());

if (registryId == null) continue;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is an entry it doesnt understand (maybe from a future version) this warrents showing the backup screen IMO.


RegistryKey<? extends Registry<?>> registryRef = RegistryKey.ofRegistry(registryId);

for (Map.Entry<String, JsonElement> namespacedEntries : registry.getValue().getAsJsonObject().entrySet()) {
for (JsonElement entry : namespacedEntries.getValue().getAsJsonArray()) {
Identifier entryId = Identifier.of(namespacedEntries.getKey(), entry.getAsString());

if (entryId == null) continue;

keys.add(RegistryKey.of((RegistryKey) registryRef, entryId));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe getting a bit deeply nested here, could cleanup by moving the registry entries and blocksate stuff into their own methods.

}
}

Registry<?> clientRegistry = Registries.REGISTRIES.get(registryId);

if (clientRegistry != null) keys.removeAll(clientRegistry.getKeys());
}

for (Map.Entry<String, JsonElement> blockNsEntry : root.getAsJsonObject("blockStates").entrySet()) {
for (Map.Entry<String, JsonElement> blockEntry : blockNsEntry.getValue().getAsJsonObject().entrySet()) {
Identifier id = Identifier.of(blockNsEntry.getKey(), blockEntry.getKey());

if (id == null || !Registries.BLOCK.containsId(id)) continue;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that the missing block will be caught above, but it might still be good to include it in the missing states.


Block block = Registries.BLOCK.get(id);
Set<Pair<Block, String>> missing = new HashSet<>();

for (JsonElement state : blockEntry.getValue().getAsJsonArray()) {
if (block.getStateManager().getProperty(state.getAsString()) == null) {
missing.add(Pair.of(block, state.getAsString()));
}
}

if (!missing.isEmpty()) {
keys.add(RegistryKey.of(RegistryKeys.BLOCK, id));
this.missingBlockStates.addAll(missing);
}
}
}

this.missingKeys = Collections.unmodifiableSet(keys);
this.missingNamespaces = keys.stream().map(RegistryKey::getValue).map(Identifier::getNamespace).collect(Collectors.toUnmodifiableSet());
}

public Set<String> getMissingNamespaces() {
return missingNamespaces;
}

public Set<RegistryKey<?>> getMissingKeys() {
return missingKeys;
}

public Set<Pair<Block, String>> getMissingBlockStates() {
return missingBlockStates;
}

public static JsonObject serializeRegistries() {
JsonObject json = new JsonObject();
json.addProperty("version", VERSION);

// Sort the entries first
Map<String, Map<String, Set<String>>> entriesMap = new TreeMap<>();
Map<String, Map<String, Set<String>>> blockStatesMap = new TreeMap<>();

for (Registry<?> registry : Registries.REGISTRIES) {
if (registry.size() == 0 || !RegistryAttributeHolder.get(registry).hasAttribute(RegistryAttribute.REMOVAL_CHECKED)) continue;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should only write when modded, this file should not be rewritten if you load with only Fabric API.


Map<String, Set<String>> nsToEntries = new TreeMap<>();

for (Identifier id : registry.getIds()) {
if (!id.getNamespace().equals(Identifier.DEFAULT_NAMESPACE)) {
nsToEntries.computeIfAbsent(id.getNamespace(), ns -> new TreeSet<>()).add(id.getPath());
}
}

entriesMap.put(registry.getKey().getValue().toString(), nsToEntries);
}

for (Map.Entry<RegistryKey<Block>, Block> blockEntry : Registries.BLOCK.getEntrySet()) {
Identifier blockId = blockEntry.getKey().getValue();

if (
blockId.getNamespace().equals(Identifier.DEFAULT_NAMESPACE)
|| blockEntry.getValue().getStateManager().getProperties().isEmpty()
) {
continue;
}

Set<String> states = blockStatesMap.computeIfAbsent(blockId.getNamespace(), ns -> new TreeMap<>()).computeIfAbsent(blockId.getPath(), ns -> new TreeSet<>());

for (Property<?> property : blockEntry.getValue().getStateManager().getProperties()) {
states.add(property.getName());
}
}

JsonObject entries = new JsonObject();

for (Map.Entry<String, Map<String, Set<String>>> registry : entriesMap.entrySet()) {
JsonObject registryJson = new JsonObject();

for (Map.Entry<String, Set<String>> nsToEntries : registry.getValue().entrySet()) {
registryJson.add(nsToEntries.getKey(), Util.make(new JsonArray(), arr -> nsToEntries.getValue().forEach(arr::add)));
}

entries.add(registry.getKey(), registryJson);
}
Comment on lines +168 to +178
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this code not duplicate the code bellow? Could be moved to a method.


JsonObject blockStates = new JsonObject();

for (Map.Entry<String, Map<String, Set<String>>> registry : blockStatesMap.entrySet()) {
JsonObject registryJson = new JsonObject();

for (Map.Entry<String, Set<String>> nsToEntries : registry.getValue().entrySet()) {
registryJson.add(nsToEntries.getKey(), Util.make(new JsonArray(), arr -> nsToEntries.getValue().forEach(arr::add)));
}

entries.add(registry.getKey(), registryJson);
}

json.add("entries", entries);
json.add("blockStates", blockStates);
return json;
}

@Nullable
public static RegistryRemovalChecker runCheck(Path jsonFile) {
if (DISABLED) {
LOGGER.info("Registry removal check was disabled via system property.");
return null;
}

try {
if (Files.exists(jsonFile)) {
// Do not run removal check if the file is missing
JsonObject json = JsonHelper.deserialize(Files.newBufferedReader(jsonFile, StandardCharsets.UTF_8));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts on Gzip'ing the file? Will make it smaller, and also a lot less likely for someone to go and edit it without knowing what it does.

return new RegistryRemovalChecker(json);
}
} catch (IOException | JsonParseException e) {
LOGGER.warn("Could not read {}, registry removal check disabled", FILE_NAME, e);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it exists, but is invalid the warning should be shown imo.

}

return null;
}

public static void write(Path jsonFile) {
if (DISABLED) return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Should the system prop only stop the warning screen? I think it would still make sense to save it.


try {
JsonObject json = serializeRegistries();
Files.writeString(jsonFile, GSON.toJson(json), StandardCharsets.UTF_8);
} catch (IOException e) {
LOGGER.warn("Could not write {}", FILE_NAME, e);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let it crash, something has gone badly wrong if this fails to write.

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.client.registry.sync;

import java.util.Collection;
import java.util.stream.Collectors;

import net.minecraft.client.gui.screen.BackupPromptScreen;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.Text;

public final class RemovedRegistryEntryWarningScreen extends BackupPromptScreen {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be nice to include a screenshot in the PR.

private static final int DISPLAYED_NAMESPACES = 5;
private static final Text TITLE = Text.translatable("fabric-registry-sync-v0.screen.removed-registry.title");
private static final Text SUBTITLE = Text.translatable("fabric-registry-sync-v0.screen.removed-registry.subtitle.1")
.append(Text.translatable("fabric-registry-sync-v0.screen.removed-registry.subtitle.2"))
.append(Text.translatable("fabric-registry-sync-v0.screen.removed-registry.subtitle.3"))
.append(Text.translatable("fabric-registry-sync-v0.screen.removed-registry.subtitle.4"));

public RemovedRegistryEntryWarningScreen(Screen parent, Callback callback, Collection<String> namespaces) {
super(parent, callback, TITLE, getSubtitle(namespaces), false);
}

private static Text getSubtitle(Collection<String> namespaces) {
if (namespaces.size() <= DISPLAYED_NAMESPACES) {
return SUBTITLE.copy().append(String.join(", ", namespaces));
}

return SUBTITLE
.copy()
.append(namespaces.stream().limit(DISPLAYED_NAMESPACES).collect(Collectors.joining(", ")))
.append("\n")
.append(Text.translatable("fabric-registry-sync-v0.screen.removed-registry.footer", namespaces.size() - DISPLAYED_NAMESPACES));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.registry.sync.client;

import java.nio.file.Path;
import java.util.Optional;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import net.minecraft.client.gui.screen.world.CreateWorldScreen;
import net.minecraft.util.WorldSavePath;
import net.minecraft.world.level.storage.LevelStorage;

import net.fabricmc.fabric.impl.client.registry.sync.RegistryRemovalChecker;

@Mixin(CreateWorldScreen.class)
public class CreateWorldScreenMixin {
@Inject(method = "createSession", at = @At(value = "RETURN"))
private void createInitialRegistryFile(CallbackInfoReturnable<Optional<LevelStorage.Session>> cir) {
// There are multiple returns in this method. We want to target the first two.
// Since specifying 2 ordinals is impossible, just check whether the return value is what we want.
cir.getReturnValue().ifPresent(session -> {
Path jsonFile = session.getDirectory(WorldSavePath.ROOT).resolve(RegistryRemovalChecker.FILE_NAME);
RegistryRemovalChecker.write(jsonFile);
Comment on lines +40 to +41
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should backup the old file, maybe store 5 or so previous files. This will match the previous reg sync behaviour.

});
}
}
Loading
Loading