Skip to content

Commit

Permalink
Start working on new registry remapper
Browse files Browse the repository at this point in the history
  • Loading branch information
thecatcore committed Mar 24, 2024
1 parent f482232 commit d949960
Show file tree
Hide file tree
Showing 13 changed files with 204 additions and 17 deletions.
2 changes: 1 addition & 1 deletion legacy-fabric-registry-sync-api-v1/common.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
])
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
minVersionExcluded=1.10.2
minVersionExcluded=1.8.9
maxVersionIncluded=1.12.2
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public abstract class class_2929Mixin<T> implements IdsHolder<T> {
@Nullable
public abstract T getById(int id);

@Shadow
public abstract void add(T value, int id);

@Override
public IdsHolder<T> fabric$new() {
return (IdsHolder<T>) new class_2929<>(256);
Expand All @@ -44,4 +47,9 @@ public abstract class class_2929Mixin<T> implements IdsHolder<T> {

return id;
}

@Override
public void fabric$setValue(T value, int id) {
add(value, id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,27 @@

@Mixin(SimpleRegistry.class)
public abstract class SimpleRegistryMixin<K, V> implements SyncedRegistryHolder<V>, SyncedRegistrable<V> {
// 1.8+
@Shadow
public abstract void add(int id, K identifier, V object);

// 1.9+
@Shadow
@Final
protected class_2929<V> 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);
Expand All @@ -49,4 +63,19 @@ public abstract class SimpleRegistryMixin<K, V> implements SyncedRegistryHolder<
public IdsHolder<V> fabric$getIdsHolder() {
return (IdsHolder<V>) 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);
}
}
Original file line number Diff line number Diff line change
@@ -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<T> extends SyncedRegistryHolder<T>, SyncedRegistrable<T> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
public interface RegistryHolder<T> extends Iterable<T> {
Identifier fabric$getId();
Event<RegistryEntryAddedCallback<T>> fabric$getEntryAddedCallback();
Event<RegistryBeforeAddCallback<T>> fabric$getBeforeAddedCallback();
Expand All @@ -31,4 +34,8 @@ public interface RegistryHolder<T> {

T fabric$getValue(Identifier id);
Identifier fabric$getId(T value);

default Stream<T> stream() {
return StreamSupport.stream(spliterator(), false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@

public interface SyncedRegistryHolder<T> extends RegistryHolder<T> {
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ public interface IdsHolder<T> extends Iterable<T> {
IdsHolder<T> fabric$new();

int fabric$nextId();

void fabric$setValue(T value, int id);
}
Original file line number Diff line number Diff line change
@@ -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<T> {
protected static final Logger LOGGER = Logger.get(LoggerImpl.API, "RegistryRemapper");
private final SyncedRegistry<T> registry;
private BiMap<Identifier, Integer> entryDump;
private BiMap<Identifier, Integer> missingMap = HashBiMap.create();

public RegistryRemapper(SyncedRegistry<T> 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<T> dumpIds = getDumpIds();
}

private IdsHolder<T> getDumpIds() {
IdsHolder<T> 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);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public abstract class IdListMixinV2<T> implements IdsHolder<T> {
@Shadow
public abstract Object fromId(int index);

@Shadow
public abstract void set(Object value, int index);

@Override
public IdsHolder<T> fabric$new() {
return (IdsHolder<T>) new IdList<>();
Expand All @@ -42,4 +45,9 @@ public abstract class IdListMixinV2<T> implements IdsHolder<T> {

return id;
}

@Override
public void fabric$setValue(T value, int id) {
set(value, id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,11 @@ public abstract class MutableRegistryMixin<K, V> implements RegistryHolder<V>, R

@Override
public Identifier fabric$getId(V value) {
if (map.containsValue(value)) {
for (Map.Entry<K, V> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<K, V> implements SyncedRegistryHolder<V> {
public abstract class SimpleRegistryMixinV2<K, V> implements SyncedRegistry<V> {
}

0 comments on commit d949960

Please sign in to comment.