-
Notifications
You must be signed in to change notification settings - Fork 417
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
[1.21.4] Refactor registry sync #4233
Draft
modmuss50
wants to merge
2
commits into
FabricMC:1.21.4
Choose a base branch
from
modmuss50:registry-sync-refactor
base: 1.21.4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+762
−254
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
198 changes: 198 additions & 0 deletions
198
.../client/java/net/fabricmc/fabric/impl/client/registry/sync/ClientRegistrySyncHandler.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,198 @@ | ||
/* | ||
* 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.ArrayList; | ||
import java.util.EnumSet; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.CompletionException; | ||
|
||
import it.unimi.dsi.fastutil.objects.Object2IntMap; | ||
import org.jetbrains.annotations.VisibleForTesting; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import net.minecraft.registry.Registries; | ||
import net.minecraft.registry.Registry; | ||
import net.minecraft.screen.ScreenTexts; | ||
import net.minecraft.text.MutableText; | ||
import net.minecraft.text.Text; | ||
import net.minecraft.util.Formatting; | ||
import net.minecraft.util.Identifier; | ||
import net.minecraft.util.thread.ThreadExecutor; | ||
|
||
import net.fabricmc.fabric.api.event.registry.RegistryAttribute; | ||
import net.fabricmc.fabric.impl.registry.sync.RegistrySyncManager; | ||
import net.fabricmc.fabric.impl.registry.sync.RemapException; | ||
import net.fabricmc.fabric.impl.registry.sync.RemappableRegistry; | ||
import net.fabricmc.fabric.impl.registry.sync.packet.RegistryPacketHandler; | ||
|
||
public final class ClientRegistrySyncHandler { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(ClientRegistrySyncHandler.class); | ||
|
||
private ClientRegistrySyncHandler() { | ||
} | ||
|
||
public static <T extends RegistryPacketHandler.RegistrySyncPayload> CompletableFuture<Boolean> receivePacket(ThreadExecutor<?> executor, RegistryPacketHandler<T> handler, T payload, boolean accept) { | ||
handler.receivePayload(payload); | ||
|
||
if (!handler.isPacketFinished()) { | ||
return CompletableFuture.completedFuture(false); | ||
} | ||
|
||
if (RegistrySyncManager.DEBUG) { | ||
String handlerName = handler.getClass().getSimpleName(); | ||
LOGGER.info("{} total packet: {}", handlerName, handler.getTotalPacketReceived()); | ||
LOGGER.info("{} raw size: {}", handlerName, handler.getRawBufSize()); | ||
LOGGER.info("{} deflated size: {}", handlerName, handler.getDeflatedBufSize()); | ||
} | ||
|
||
RegistryPacketHandler.SyncedPacketData data = handler.getSyncedPacketData(); | ||
|
||
if (!accept) { | ||
return CompletableFuture.completedFuture(true); | ||
} | ||
|
||
return executor.submit(() -> { | ||
if (data == null) { | ||
throw new CompletionException(new RemapException("Received null map in sync packet!")); | ||
} | ||
|
||
try { | ||
apply(data); | ||
return true; | ||
} catch (RemapException e) { | ||
throw new CompletionException(e); | ||
} | ||
}); | ||
} | ||
|
||
public static void apply(RegistryPacketHandler.SyncedPacketData data) throws RemapException { | ||
// First check that all of the data provided is valid before making any changes | ||
checkRemoteRemap(data); | ||
|
||
for (Map.Entry<Identifier, Object2IntMap<Identifier>> entry : data.idMap().entrySet()) { | ||
final Identifier registryId = entry.getKey(); | ||
|
||
Registry<?> registry = Registries.REGISTRIES.get(registryId); | ||
|
||
// Registry was not found on the client, is it optional? | ||
// If so we can just ignore it. | ||
// Otherwise we throw an exception and disconnect. | ||
if (registry == null) { | ||
if (isRegistryOptional(registryId, data)) { | ||
LOGGER.info("Received registry data for unknown optional registry: {}", registryId); | ||
continue; | ||
} else { | ||
throw new RemapException("Received registry data for unknown registry: " + registryId); | ||
} | ||
} | ||
|
||
if (registry instanceof RemappableRegistry remappableRegistry) { | ||
remappableRegistry.remap(entry.getValue(), RemappableRegistry.RemapMode.REMOTE); | ||
return; | ||
} | ||
|
||
throw new RemapException("Registry " + registryId + " is not remappable"); | ||
} | ||
} | ||
|
||
@VisibleForTesting | ||
public static void checkRemoteRemap(RegistryPacketHandler.SyncedPacketData data) throws RemapException { | ||
Map<Identifier, Object2IntMap<Identifier>> map = data.idMap(); | ||
Map<Identifier, List<Identifier>> missingEntries = new HashMap<>(); | ||
|
||
for (Identifier registryId : map.keySet()) { | ||
final Object2IntMap<Identifier> remoteRegistry = map.get(registryId); | ||
Registry<?> registry = Registries.REGISTRIES.get(registryId); | ||
|
||
if (registry == null) { | ||
if (!isRegistryOptional(registryId, data)) { | ||
LOGGER.error("Received unknown remote registry ({}) from server", registryId); | ||
|
||
// Registry was not found on the client, and is not optional. | ||
missingEntries.put(registryId, new ArrayList<>(remoteRegistry.keySet())); | ||
} | ||
|
||
continue; | ||
} | ||
|
||
for (Identifier remoteId : remoteRegistry.keySet()) { | ||
if (!registry.containsId(remoteId)) { | ||
// Found a registry entry from the server that is | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that is... (missing?) |
||
missingEntries.computeIfAbsent(registryId, i -> new ArrayList<>()).add(remoteId); | ||
} | ||
} | ||
} | ||
|
||
if (missingEntries.isEmpty()) { | ||
// All good :) | ||
return; | ||
} | ||
|
||
// Print out details to the log | ||
LOGGER.error("Received unknown remote registry entries from server"); | ||
|
||
for (Map.Entry<Identifier, List<Identifier>> entry : missingEntries.entrySet()) { | ||
for (Identifier identifier : entry.getValue()) { | ||
LOGGER.error("Registry entry ({}) is missing from local registry ({})", identifier, entry.getKey()); | ||
} | ||
} | ||
|
||
// Create a nice user friendly error message. | ||
MutableText text = Text.empty(); | ||
|
||
final int count = missingEntries.values().stream().mapToInt(List::size).sum(); | ||
|
||
if (count == 1) { | ||
text = text.append(Text.translatable("fabric-registry-sync-v0.unknown-remote.title.singular")); | ||
} else { | ||
text = text.append(Text.translatable("fabric-registry-sync-v0.unknown-remote.title.plural", count)); | ||
} | ||
|
||
text = text.append(Text.translatable("fabric-registry-sync-v0.unknown-remote.subtitle.1").formatted(Formatting.GREEN)); | ||
text = text.append(Text.translatable("fabric-registry-sync-v0.unknown-remote.subtitle.2")); | ||
|
||
final int toDisplay = 4; | ||
// Get the distinct missing namespaces | ||
final List<String> namespaces = missingEntries.values().stream() | ||
.flatMap(List::stream) | ||
.map(Identifier::getNamespace) | ||
.distinct() | ||
.sorted() | ||
.toList(); | ||
|
||
for (int i = 0; i < Math.min(namespaces.size(), toDisplay); i++) { | ||
text = text.append(Text.literal(namespaces.get(i)).formatted(Formatting.YELLOW)); | ||
text = text.append(ScreenTexts.LINE_BREAK); | ||
} | ||
|
||
if (namespaces.size() > toDisplay) { | ||
text = text.append(Text.translatable("fabric-registry-sync-v0.unknown-remote.footer", namespaces.size() - toDisplay)); | ||
} | ||
|
||
throw new RemapException(text); | ||
} | ||
|
||
private static boolean isRegistryOptional(Identifier registryId, RegistryPacketHandler.SyncedPacketData data) { | ||
EnumSet<RegistryAttribute> registryAttributes = data.attributes().get(registryId); | ||
return registryAttributes.contains(RegistryAttribute.OPTIONAL); | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this is duplicative, as
checkRemoteRemap
should've already thrown in this case?