97 lines
3.5 KiB
Java
97 lines
3.5 KiB
Java
package com.esotericsoftware.kryo.serializers;
|
|
|
|
import c.e.a.a;
|
|
import com.esotericsoftware.kryo.Kryo;
|
|
import com.esotericsoftware.kryo.KryoException;
|
|
import com.esotericsoftware.kryo.io.Input;
|
|
import com.esotericsoftware.kryo.io.Output;
|
|
import com.esotericsoftware.kryo.serializers.FieldSerializer;
|
|
import java.lang.annotation.ElementType;
|
|
import java.lang.annotation.Retention;
|
|
import java.lang.annotation.RetentionPolicy;
|
|
import java.lang.annotation.Target;
|
|
public class VersionFieldSerializer<T> extends FieldSerializer<T> {
|
|
private boolean compatible;
|
|
private int[] fieldVersion;
|
|
private int typeVersion;
|
|
|
|
@Target({ElementType.FIELD})
|
|
@Retention(RetentionPolicy.RUNTIME)
|
|
public @interface Since {
|
|
int value() default 0;
|
|
}
|
|
|
|
public VersionFieldSerializer(Kryo kryo, Class cls) {
|
|
super(kryo, cls);
|
|
this.typeVersion = 0;
|
|
this.compatible = true;
|
|
initializeCachedFields();
|
|
}
|
|
|
|
public VersionFieldSerializer(Kryo kryo, Class cls, boolean z2) {
|
|
this(kryo, cls);
|
|
this.compatible = z2;
|
|
}
|
|
|
|
@Override // com.esotericsoftware.kryo.serializers.FieldSerializer
|
|
public void initializeCachedFields() {
|
|
FieldSerializer.CachedField[] fields = getFields();
|
|
this.fieldVersion = new int[fields.length];
|
|
int length = fields.length;
|
|
for (int i = 0; i < length; i++) {
|
|
Since since = (Since) fields[i].getField().getAnnotation(Since.class);
|
|
if (since != null) {
|
|
this.fieldVersion[i] = since.value();
|
|
this.typeVersion = Math.max(this.fieldVersion[i], this.typeVersion);
|
|
} else {
|
|
this.fieldVersion[i] = 0;
|
|
}
|
|
}
|
|
this.removedFields.clear();
|
|
a.C0053a aVar = a.a;
|
|
}
|
|
|
|
@Override // com.esotericsoftware.kryo.serializers.FieldSerializer, com.esotericsoftware.kryo.Serializer
|
|
public T read(Kryo kryo, Input input, Class<T> cls) {
|
|
T create = create(kryo, input, cls);
|
|
kryo.reference(create);
|
|
int readVarInt = input.readVarInt(true);
|
|
if (this.compatible || readVarInt == this.typeVersion) {
|
|
FieldSerializer.CachedField[] fields = getFields();
|
|
int length = fields.length;
|
|
for (int i = 0; i < length; i++) {
|
|
if (this.fieldVersion[i] > readVarInt) {
|
|
a.C0053a aVar = a.a;
|
|
} else {
|
|
fields[i].read(input, create);
|
|
}
|
|
}
|
|
return create;
|
|
}
|
|
StringBuilder M = c.d.b.a.a.M("Version not compatible: ", readVarInt, " <-> ");
|
|
M.append(this.typeVersion);
|
|
throw new KryoException(M.toString());
|
|
}
|
|
|
|
@Override // com.esotericsoftware.kryo.serializers.FieldSerializer
|
|
public void removeField(FieldSerializer.CachedField cachedField) {
|
|
super.removeField(cachedField);
|
|
initializeCachedFields();
|
|
}
|
|
|
|
@Override // com.esotericsoftware.kryo.serializers.FieldSerializer
|
|
public void removeField(String str) {
|
|
super.removeField(str);
|
|
initializeCachedFields();
|
|
}
|
|
|
|
@Override // com.esotericsoftware.kryo.serializers.FieldSerializer, com.esotericsoftware.kryo.Serializer
|
|
public void write(Kryo kryo, Output output, T t) {
|
|
FieldSerializer.CachedField[] fields = getFields();
|
|
output.writeVarInt(this.typeVersion, true);
|
|
for (FieldSerializer.CachedField cachedField : fields) {
|
|
cachedField.write(output, t);
|
|
}
|
|
}
|
|
}
|