From d949960805373b0e3b7caff877ce0f0e38e96697 Mon Sep 17 00:00:00 2001 From: Cat Core Date: Sun, 24 Mar 2024 17:26:03 +0100 Subject: [PATCH] Start working on new registry remapper --- .../common.gradle | 2 +- .../impl/registry/RegistryHelperImpl.java | 3 - .../1.12.2/gradle.properties | 2 +- .../mixin/registry/sync/class_2929Mixin.java | 8 ++ .../sync/versioned/SimpleRegistryMixin.java | 29 +++++ .../registry/v2/registry/SyncedRegistry.java | 24 ++++ .../v2/registry/holder/RegistryHolder.java | 9 +- .../registry/holder/SyncedRegistryHolder.java | 11 +- .../v2/registry/registrable/IdsHolder.java | 2 + .../impl/registry/RegistryRemapper.java | 105 ++++++++++++++++++ .../mixin/registry/sync/IdListMixinV2.java | 8 ++ .../registry/sync/MutableRegistryMixin.java | 13 +-- .../registry/sync/SimpleRegistryMixinV2.java | 5 +- 13 files changed, 204 insertions(+), 17 deletions(-) create mode 100644 legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/registry/SyncedRegistry.java create mode 100644 legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/impl/registry/RegistryRemapper.java diff --git a/legacy-fabric-registry-sync-api-v1/common.gradle b/legacy-fabric-registry-sync-api-v1/common.gradle index 4e74f3a5e..e139671fc 100644 --- a/legacy-fabric-registry-sync-api-v1/common.gradle +++ b/legacy-fabric-registry-sync-api-v1/common.gradle @@ -2,5 +2,5 @@ moduleDependencies(project, [ "legacy-fabric-api-base", "legacy-fabric-networking-api-v1", "legacy-fabric-resource-loader-v1", - "legacy-fabric-registry-sync-v2" + "legacy-fabric-registry-sync-api-v2" ]) diff --git a/legacy-fabric-registry-sync-api-v1/common/src/main/java/net/legacyfabric/fabric/impl/registry/RegistryHelperImpl.java b/legacy-fabric-registry-sync-api-v1/common/src/main/java/net/legacyfabric/fabric/impl/registry/RegistryHelperImpl.java index 4ad5ae3c0..659b5f9d2 100644 --- a/legacy-fabric-registry-sync-api-v1/common/src/main/java/net/legacyfabric/fabric/impl/registry/RegistryHelperImpl.java +++ b/legacy-fabric-registry-sync-api-v1/common/src/main/java/net/legacyfabric/fabric/impl/registry/RegistryHelperImpl.java @@ -31,8 +31,6 @@ import net.minecraft.enchantment.Enchantment; import net.minecraft.entity.Entity; import net.minecraft.entity.effect.StatusEffect; -import net.minecraft.item.BlockItem; -import net.minecraft.item.Item; import net.minecraft.world.biome.Biome; import net.fabricmc.api.EnvType; @@ -45,7 +43,6 @@ import net.legacyfabric.fabric.impl.client.registry.sync.ClientRegistryRemapper; import net.legacyfabric.fabric.impl.registry.sync.ServerRegistryRemapper; import net.legacyfabric.fabric.impl.registry.sync.compat.IdListCompat; -import net.legacyfabric.fabric.impl.registry.sync.compat.ItemCompat; import net.legacyfabric.fabric.impl.registry.sync.compat.RegistriesGetter; import net.legacyfabric.fabric.impl.registry.sync.compat.SimpleRegistryCompat; import net.legacyfabric.fabric.impl.registry.sync.remappers.RegistryRemapper; diff --git a/legacy-fabric-registry-sync-api-v2/1.12.2/gradle.properties b/legacy-fabric-registry-sync-api-v2/1.12.2/gradle.properties index a104a3a86..22c79abec 100644 --- a/legacy-fabric-registry-sync-api-v2/1.12.2/gradle.properties +++ b/legacy-fabric-registry-sync-api-v2/1.12.2/gradle.properties @@ -1,2 +1,2 @@ -minVersionExcluded=1.10.2 +minVersionExcluded=1.8.9 maxVersionIncluded=1.12.2 diff --git a/legacy-fabric-registry-sync-api-v2/1.12.2/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/class_2929Mixin.java b/legacy-fabric-registry-sync-api-v2/1.12.2/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/class_2929Mixin.java index 1f8020359..8ac0cfb7e 100644 --- a/legacy-fabric-registry-sync-api-v2/1.12.2/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/class_2929Mixin.java +++ b/legacy-fabric-registry-sync-api-v2/1.12.2/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/class_2929Mixin.java @@ -31,6 +31,9 @@ public abstract class class_2929Mixin implements IdsHolder { @Nullable public abstract T getById(int id); + @Shadow + public abstract void add(T value, int id); + @Override public IdsHolder fabric$new() { return (IdsHolder) new class_2929<>(256); @@ -44,4 +47,9 @@ public abstract class class_2929Mixin implements IdsHolder { return id; } + + @Override + public void fabric$setValue(T value, int id) { + add(value, id); + } } diff --git a/legacy-fabric-registry-sync-api-v2/1.12.2/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/versioned/SimpleRegistryMixin.java b/legacy-fabric-registry-sync-api-v2/1.12.2/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/versioned/SimpleRegistryMixin.java index 4b6b945c4..d04669d94 100644 --- a/legacy-fabric-registry-sync-api-v2/1.12.2/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/versioned/SimpleRegistryMixin.java +++ b/legacy-fabric-registry-sync-api-v2/1.12.2/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/versioned/SimpleRegistryMixin.java @@ -31,13 +31,27 @@ @Mixin(SimpleRegistry.class) public abstract class SimpleRegistryMixin implements SyncedRegistryHolder, SyncedRegistrable { + // 1.8+ @Shadow public abstract void add(int id, K identifier, V object); + // 1.9+ @Shadow @Final protected class_2929 field_13718; + // 1.8+ + @Shadow + public abstract K getIdentifier(Object par1); + + // 1.9+ + @Shadow + public abstract int getRawId(Object object); + + // 1.7+ + @Shadow + public abstract Object getByRawId(int index); + @Override public void fabric$register(int rawId, Identifier identifier, V value) { fabric$getBeforeAddedCallback().invoker().onEntryAdding(rawId, identifier, value); @@ -49,4 +63,19 @@ public abstract class SimpleRegistryMixin implements SyncedRegistryHolder< public IdsHolder fabric$getIdsHolder() { return (IdsHolder) field_13718; } + + @Override + public Identifier fabric$getId(V value) { + return new Identifier(getIdentifier(this.fabric$toKeyType(value))); + } + + @Override + public int fabric$getRawId(V value) { + return getRawId(value); + } + + @Override + public V fabric$getValue(int rawId) { + return (V) getByRawId(rawId); + } } diff --git a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/registry/SyncedRegistry.java b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/registry/SyncedRegistry.java new file mode 100644 index 000000000..fc27a1826 --- /dev/null +++ b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/registry/SyncedRegistry.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2020 - 2024 Legacy Fabric + * Copyright (c) 2016 - 2022 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.legacyfabric.fabric.api.registry.v2.registry; + +import net.legacyfabric.fabric.api.registry.v2.registry.holder.SyncedRegistryHolder; +import net.legacyfabric.fabric.api.registry.v2.registry.registrable.SyncedRegistrable; + +public interface SyncedRegistry extends SyncedRegistryHolder, SyncedRegistrable { +} diff --git a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/registry/holder/RegistryHolder.java b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/registry/holder/RegistryHolder.java index d72879eb4..d2c42b146 100644 --- a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/registry/holder/RegistryHolder.java +++ b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/registry/holder/RegistryHolder.java @@ -17,12 +17,15 @@ package net.legacyfabric.fabric.api.registry.v2.registry.holder; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; + import net.legacyfabric.fabric.api.event.Event; import net.legacyfabric.fabric.api.registry.v2.event.RegistryBeforeAddCallback; import net.legacyfabric.fabric.api.registry.v2.event.RegistryEntryAddedCallback; import net.legacyfabric.fabric.api.util.Identifier; -public interface RegistryHolder { +public interface RegistryHolder extends Iterable { Identifier fabric$getId(); Event> fabric$getEntryAddedCallback(); Event> fabric$getBeforeAddedCallback(); @@ -31,4 +34,8 @@ public interface RegistryHolder { T fabric$getValue(Identifier id); Identifier fabric$getId(T value); + + default Stream stream() { + return StreamSupport.stream(spliterator(), false); + } } diff --git a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/registry/holder/SyncedRegistryHolder.java b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/registry/holder/SyncedRegistryHolder.java index 2d0ac6853..04f762a71 100644 --- a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/registry/holder/SyncedRegistryHolder.java +++ b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/registry/holder/SyncedRegistryHolder.java @@ -21,7 +21,14 @@ public interface SyncedRegistryHolder extends RegistryHolder { int fabric$getRawId(T value); - int fabric$getRawId(Identifier identifier); + default int fabric$getRawId(Identifier identifier) { + T value = fabric$getValue(identifier); + return fabric$getRawId(value); + } + T fabric$getValue(int rawId); - Identifier fabric$getId(int rawId); + default Identifier fabric$getId(int rawId) { + T value = fabric$getValue(rawId); + return fabric$getId(value); + } } diff --git a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/registry/registrable/IdsHolder.java b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/registry/registrable/IdsHolder.java index 26b37016e..1afcb2cd3 100644 --- a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/registry/registrable/IdsHolder.java +++ b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/api/registry/v2/registry/registrable/IdsHolder.java @@ -21,4 +21,6 @@ public interface IdsHolder extends Iterable { IdsHolder fabric$new(); int fabric$nextId(); + + void fabric$setValue(T value, int id); } diff --git a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/impl/registry/RegistryRemapper.java b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/impl/registry/RegistryRemapper.java new file mode 100644 index 000000000..063d26e9e --- /dev/null +++ b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/impl/registry/RegistryRemapper.java @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2020 - 2024 Legacy Fabric + * Copyright (c) 2016 - 2022 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.legacyfabric.fabric.impl.registry; + +import java.util.stream.Collectors; + +import com.google.common.collect.BiMap; +import com.google.common.collect.HashBiMap; + +import net.minecraft.nbt.NbtCompound; + +import net.legacyfabric.fabric.api.logger.v1.Logger; +import net.legacyfabric.fabric.api.registry.v2.registry.SyncedRegistry; +import net.legacyfabric.fabric.api.registry.v2.registry.registrable.IdsHolder; +import net.legacyfabric.fabric.api.util.Identifier; +import net.legacyfabric.fabric.impl.logger.LoggerImpl; + +public class RegistryRemapper { + protected static final Logger LOGGER = Logger.get(LoggerImpl.API, "RegistryRemapper"); + private final SyncedRegistry registry; + private BiMap entryDump; + private BiMap missingMap = HashBiMap.create(); + + public RegistryRemapper(SyncedRegistry registry) { + this.registry = registry; + } + + public void dump() { + if (this.entryDump != null && !this.entryDump.isEmpty()) return; + + this.entryDump = HashBiMap.create(); + + LOGGER.debug("Dumping registry %s.", this.registry.fabric$getId()); + + this.registry.stream() + .collect(Collectors.toMap( + this.registry::fabric$getId, + this.registry::fabric$getRawId + )) + .entrySet() + .stream() + .filter(entry -> entry.getKey() != null && entry.getValue() != -1) + .forEach(entry -> { + T value = this.registry.fabric$getValue(entry.getKey()); + LOGGER.debug("[%s] %s %s %s", this.registry.fabric$getId(), entry.getKey(), entry.getValue(), value); + this.entryDump.put(entry.getKey(), entry.getValue()); + }); + } + + public NbtCompound toNbt() { + this.dump(); + + NbtCompound nbt = new NbtCompound(); + this.entryDump.forEach((key, value) -> nbt.putInt(key.toString(), value)); + return nbt; + } + + public void readNbt(NbtCompound tag) { + this.entryDump = HashBiMap.create(); + + for (String key : tag.getKeys()) { + Identifier identifier = new Identifier(key); + int id = tag.getInt(key); + this.entryDump.put(identifier, id); + } + } + + public void remap() { + LOGGER.info("Remapping registry %s", this.registry.fabric$getId()); + + this.dump(); + + IdsHolder dumpIds = getDumpIds(); + } + + private IdsHolder getDumpIds() { + IdsHolder ids = this.registry.fabric$getIdsHolder().fabric$new(); + + this.entryDump.forEach((id, rawId) -> { + T value = this.registry.fabric$getValue(id); + + if (value == null) { + LOGGER.warn("[%s] Missing entry %s with raw id %s", this.registry.fabric$getId(), id, rawId); + this.missingMap.put(id, rawId); + } else { + ids.fabric$setValue(value, rawId); + } + }); + } +} diff --git a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/IdListMixinV2.java b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/IdListMixinV2.java index 3d4f9fded..979fee942 100644 --- a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/IdListMixinV2.java +++ b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/IdListMixinV2.java @@ -29,6 +29,9 @@ public abstract class IdListMixinV2 implements IdsHolder { @Shadow public abstract Object fromId(int index); + @Shadow + public abstract void set(Object value, int index); + @Override public IdsHolder fabric$new() { return (IdsHolder) new IdList<>(); @@ -42,4 +45,9 @@ public abstract class IdListMixinV2 implements IdsHolder { return id; } + + @Override + public void fabric$setValue(T value, int id) { + set(value, id); + } } diff --git a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/MutableRegistryMixin.java b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/MutableRegistryMixin.java index db0c9ce68..f07a965a3 100644 --- a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/MutableRegistryMixin.java +++ b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/MutableRegistryMixin.java @@ -108,12 +108,11 @@ public abstract class MutableRegistryMixin implements RegistryHolder, R @Override public Identifier fabric$getId(V value) { - if (map.containsValue(value)) { - for (Map.Entry entry : map.entrySet()) { - if (entry.getValue() != null && entry.getValue().equals(value)) return new Identifier(entry.getKey()); - } - } - - return null; + return map.entrySet() + .stream() + .filter(entry -> entry.getValue().equals(value)) + .findFirst() + .map(entry -> new Identifier(entry.getKey())) + .orElse(null); } } diff --git a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/SimpleRegistryMixinV2.java b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/SimpleRegistryMixinV2.java index b9843a79b..be5d67321 100644 --- a/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/SimpleRegistryMixinV2.java +++ b/legacy-fabric-registry-sync-api-v2/common/src/main/java/net/legacyfabric/fabric/mixin/registry/sync/SimpleRegistryMixinV2.java @@ -21,8 +21,9 @@ import net.minecraft.util.registry.SimpleRegistry; -import net.legacyfabric.fabric.api.registry.v2.registry.holder.SyncedRegistryHolder; +import net.legacyfabric.fabric.api.registry.v2.registry.SyncedRegistry; + @Mixin(SimpleRegistry.class) -public abstract class SimpleRegistryMixinV2 implements SyncedRegistryHolder { +public abstract class SimpleRegistryMixinV2 implements SyncedRegistry { }