Skip to content

Commit

Permalink
Type registry is now a singleton :3
Browse files Browse the repository at this point in the history
  • Loading branch information
Desoroxxx committed Oct 25, 2024
1 parent 3aa419a commit c1f0666
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 68 deletions.
77 changes: 34 additions & 43 deletions src/main/java/com/paneedah/mwc/network/TypeRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,63 +12,54 @@
import com.paneedah.weaponlib.melee.PlayerMeleeInstance;
import com.paneedah.weaponlib.state.Permit;
import io.netty.buffer.ByteBuf;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.UUID;

import static com.paneedah.mwc.ProjectConstants.LOGGER;
import static com.paneedah.mwc.ProjectConstants.RED_LOGGER;

@NoArgsConstructor
public final class TypeRegistry {

@Getter private static final TypeRegistry INSTANCE = new TypeRegistry();

private final HashMap<String, Class<? extends ISerializable>> typeRegistry = new HashMap<>();
private static final HashMap<String, Class<? extends ISerializable>> typeRegistry = new HashMap<>();

static {
INSTANCE.register(PlayerItemInstance.class);
INSTANCE.register(PlayerWeaponInstance.class);
INSTANCE.register(PlayerMagazineInstance.class);
INSTANCE.register(PlayerMeleeInstance.class);
INSTANCE.register(PlayerGrenadeInstance.class);
INSTANCE.register(PlayerHandheldInstance.class);
INSTANCE.register(PlayerTabletInstance.class);

INSTANCE.register(WeaponState.class);
INSTANCE.register(MagazineState.class);
INSTANCE.register(MeleeState.class);
INSTANCE.register(GrenadeState.class);
INSTANCE.register(HandheldState.class);
INSTANCE.register(TabletState.class);

INSTANCE.register(Permit.class);
INSTANCE.register(WeaponAttachmentAspect.EnterAttachmentModePermit.class);
INSTANCE.register(WeaponAttachmentAspect.ExitAttachmentModePermit.class);
INSTANCE.register(WeaponAttachmentAspect.ChangeAttachmentPermit.class);
INSTANCE.register(MeleeAttachmentAspect.EnterAttachmentModePermit.class);
INSTANCE.register(MeleeAttachmentAspect.ExitAttachmentModePermit.class);
INSTANCE.register(MeleeAttachmentAspect.ChangeAttachmentPermit.class);
INSTANCE.register(WeaponReloadAspect.LoadPermit.class);
INSTANCE.register(WeaponReloadAspect.UnloadPermit.class);
INSTANCE.register(WeaponReloadAspect.CompoundPermit.class);
INSTANCE.register(MagazineReloadAspect.LoadPermit.class);
INSTANCE.register(MagazineReloadAspect.UnloadPermit.class);

INSTANCE.register(LightExposure.class);
INSTANCE.register(SpreadableExposure.class);
register(PlayerItemInstance.class);
register(PlayerWeaponInstance.class);
register(PlayerMagazineInstance.class);
register(PlayerMeleeInstance.class);
register(PlayerGrenadeInstance.class);
register(PlayerHandheldInstance.class);
register(PlayerTabletInstance.class);

register(WeaponState.class);
register(MagazineState.class);
register(MeleeState.class);
register(GrenadeState.class);
register(HandheldState.class);
register(TabletState.class);

register(Permit.class);
register(WeaponAttachmentAspect.EnterAttachmentModePermit.class);
register(WeaponAttachmentAspect.ExitAttachmentModePermit.class);
register(WeaponAttachmentAspect.ChangeAttachmentPermit.class);
register(MeleeAttachmentAspect.EnterAttachmentModePermit.class);
register(MeleeAttachmentAspect.ExitAttachmentModePermit.class);
register(MeleeAttachmentAspect.ChangeAttachmentPermit.class);
register(WeaponReloadAspect.LoadPermit.class);
register(WeaponReloadAspect.UnloadPermit.class);
register(WeaponReloadAspect.CompoundPermit.class);
register(MagazineReloadAspect.LoadPermit.class);
register(MagazineReloadAspect.UnloadPermit.class);

register(LightExposure.class);
register(SpreadableExposure.class);
}

private <T extends ISerializable> void register(Class<T> cls) {
private static <T extends ISerializable> void register(Class<T> cls) {
typeRegistry.put(cls.getName(), cls);
}

public <T extends ISerializable> void toBytes(final T object, final ByteBuf byteBuf) {
public static <T extends ISerializable> void toBytes(final T object, final ByteBuf byteBuf) {
final String className = object.getClass().getName();

if (!typeRegistry.containsKey(className)) {
Expand All @@ -87,7 +78,7 @@ public <T extends ISerializable> void toBytes(final T object, final ByteBuf byte
}
}

public <T extends ISerializable> T fromBytes(final ByteBuf byteBuf) {
public static <T extends ISerializable> T fromBytes(final ByteBuf byteBuf) {
Class<T> targetClass;

final byte[] classNameBytes = new byte[byteBuf.readByte()];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ public void fromBytes(final ByteBuf byteBuf) {
final int size = byteBuf.readInt();

for (int i = 0; i < size; i++)
exposures.add(TypeRegistry.getINSTANCE().fromBytes(byteBuf));
exposures.add(TypeRegistry.fromBytes(byteBuf));
}

@Override
public void toBytes(final ByteBuf byteBuf) {
byteBuf.writeInt(exposures.size());

for (Exposure exposure : exposures)
TypeRegistry.getINSTANCE().toBytes(exposure, byteBuf);
TypeRegistry.toBytes(exposure, byteBuf);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ public final class GrenadeMessage implements IMessage {

@Override
public void fromBytes(final ByteBuf byteBuf) {
this.instance = TypeRegistry.getINSTANCE().fromBytes(byteBuf);
this.instance = TypeRegistry.fromBytes(byteBuf);
this.activationTimestamp = byteBuf.readLong();
}

@Override
public void toBytes(final ByteBuf byteBuf) {
TypeRegistry.getINSTANCE().toBytes(instance, byteBuf);
TypeRegistry.toBytes(instance, byteBuf);
byteBuf.writeLong(activationTimestamp);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ public final class MeleeAttackMessage implements IMessage {

@Override
public void fromBytes(final ByteBuf byteBuf) {
instance = TypeRegistry.getINSTANCE().fromBytes(byteBuf);
instance = TypeRegistry.fromBytes(byteBuf);
entityId = byteBuf.readInt();
isHeavyAttack = byteBuf.readBoolean();
}

@Override
public void toBytes(final ByteBuf byteBuf) {
TypeRegistry.getINSTANCE().toBytes(instance, byteBuf);
TypeRegistry.toBytes(instance, byteBuf);
byteBuf.writeInt(entityId);
byteBuf.writeBoolean(isHeavyAttack);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,13 @@ public final class PermitMessage implements IMessage {

@Override
public void fromBytes(final ByteBuf byteBuf) {
final TypeRegistry typeRegistry = TypeRegistry.getINSTANCE();

playerItemInstance = typeRegistry.fromBytes(byteBuf);
permit = typeRegistry.fromBytes(byteBuf);
playerItemInstance = TypeRegistry.fromBytes(byteBuf);
permit = TypeRegistry.fromBytes(byteBuf);
}

@Override
public void toBytes(final ByteBuf byteBuf) {
final TypeRegistry typeRegistry = TypeRegistry.getINSTANCE();

typeRegistry.toBytes(playerItemInstance, byteBuf);
typeRegistry.toBytes(permit, byteBuf);
TypeRegistry.toBytes(playerItemInstance, byteBuf);
TypeRegistry.toBytes(permit, byteBuf);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void fromBytes(final ByteBuf byteBuf) {
removed = byteBuf.readBoolean();

if (!removed) {
spreadableExposure = TypeRegistry.getINSTANCE().fromBytes(byteBuf);
spreadableExposure = TypeRegistry.fromBytes(byteBuf);
}
}

Expand All @@ -33,7 +33,7 @@ public void toBytes(final ByteBuf byteBuf) {
byteBuf.writeBoolean(removed);

if (!removed) {
TypeRegistry.getINSTANCE().toBytes(spreadableExposure, byteBuf);
TypeRegistry.toBytes(spreadableExposure, byteBuf);
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/paneedah/weaponlib/PlayerItemInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void read(ByteBuf byteBuf) {

// state = WeaponState.DRAWING;

state = TypeRegistry.getINSTANCE().fromBytes(byteBuf);
state = TypeRegistry.fromBytes(byteBuf);
}

@Override
Expand All @@ -81,7 +81,7 @@ public void write(ByteBuf byteBuf) {

byteBuf.writeLong(updateId);

TypeRegistry.getINSTANCE().toBytes(state, byteBuf);
TypeRegistry.toBytes(state, byteBuf);
}

// ! This in the past was weirder, and I never really got how it worked,
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/paneedah/weaponlib/Tags.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static PlayerItemInstance<?> getInstance(ItemStack itemStack) {

byte[] bytes = itemStack.getTagCompound().getByteArray(INSTANCE_TAG);
if (bytes != null && bytes.length > 0) {
return TypeRegistry.getINSTANCE().fromBytes(Unpooled.wrappedBuffer(bytes));
return TypeRegistry.fromBytes(Unpooled.wrappedBuffer(bytes));
}
return null;
}
Expand All @@ -82,7 +82,7 @@ public static <T extends PlayerItemInstance<?>> T getInstance(ItemStack itemStac
byte[] bytes = itemStack.getTagCompound().getByteArray(INSTANCE_TAG);
if (bytes != null && bytes.length > 0) {
try {
return targetClass.cast(TypeRegistry.getINSTANCE().fromBytes(Unpooled.wrappedBuffer(bytes)));
return targetClass.cast(TypeRegistry.fromBytes(Unpooled.wrappedBuffer(bytes)));
} catch (RuntimeException e) {
return null;
}
Expand All @@ -97,7 +97,7 @@ public static void setInstance(ItemStack itemStack, PlayerItemInstance<?> instan

ByteBuf buf = Unpooled.buffer();
if (instance != null) {
TypeRegistry.getINSTANCE().toBytes(instance, buf);
TypeRegistry.toBytes(instance, buf);
NBTTagCompound tagCompound = itemStack.getTagCompound();
tagCompound.setByteArray(INSTANCE_TAG, buf.array());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public NBTBase writeNBT(Capability<ExposureContainer> capability, ExposureContai
NBTTagList exposureTags = new NBTTagList();
for (Exposure exposure : instance.getExposures().values()) {
ByteBuf buf = Unpooled.buffer();
TypeRegistry.getINSTANCE().toBytes(exposure, buf);
TypeRegistry.toBytes(exposure, buf);
exposureTags.appendTag(new NBTTagByteArray(buf.array()));
}
tagCompound.setTag(TAG_EXPOSURES, exposureTags);
Expand All @@ -100,7 +100,7 @@ public void readNBT(Capability<ExposureContainer> capability, ExposureContainer
for (int i = 0; i < exposureTags.tagCount(); i++) {
NBTTagByteArray byteArray = (NBTTagByteArray) exposureTags.get(i);
ByteBuf buf = Unpooled.wrappedBuffer(byteArray.getByteArray());
Exposure exposure = TypeRegistry.getINSTANCE().fromBytes(buf);
Exposure exposure = TypeRegistry.fromBytes(buf);
instance.getExposures().put(exposure.getClass(), exposure);
}
instance.setLastSyncTimestamp(tagCompound.getLong(TAG_LAST_SYNC));
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/paneedah/weaponlib/state/Permit.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ public void read(ByteBuf byteBuf) {
super.read(byteBuf);
timestamp = byteBuf.readLong();
status = Status.values()[byteBuf.readInt()];
state = TypeRegistry.getINSTANCE().fromBytes(byteBuf);
state = TypeRegistry.fromBytes(byteBuf);
}

@Override
public void write(ByteBuf byteBuf) {
super.write(byteBuf);
byteBuf.writeLong(timestamp);
byteBuf.writeInt(status.ordinal());
TypeRegistry.getINSTANCE().toBytes(state, byteBuf);
TypeRegistry.toBytes(state, byteBuf);
}

}

0 comments on commit c1f0666

Please sign in to comment.