perf[protocol]: 精简生成的CSharp协议

This commit is contained in:
jaysunxiao committed 2021-10-21 17:27:02 +08:00
1 parent d154dd8c12
commit 94f0a6113f
8 files changed
+897 -18

No files matched your search

@@ -17,6 +17,7 @@ import com.zfoo.protocol.registration.EnhanceUtils;
import com.zfoo.protocol.registration.field.ArrayField;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.registration.field.ObjectProtocolField;
import com.zfoo.protocol.serializer.enhance.EnhanceObjectProtocolSerializer;
import com.zfoo.protocol.util.StringUtils;
import java.lang.reflect.Field;
@@ -364,7 +365,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("buffer.writePacketArray({}, {})", objectStr, objectProtocolField.getProtocolId())).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("buffer.WritePacketArray<{}>({}, {});", getArrayClassSimpleName(arrayField), objectStr, objectProtocolField.getProtocolId())).append(LS);
builder.append(StringUtils.format("buffer.WritePacketArray<{}>({}, {});", EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(objectProtocolField.getProtocolId()), objectStr, objectProtocolField.getProtocolId())).append(LS);
break;
default:
flag = false;
@@ -397,6 +398,9 @@ public class CutDownArraySerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readBooleanArray()", array)).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadBooleanArray();", array)).append(LS);
break;
default:
flag = false;
}
@@ -412,6 +416,9 @@ public class CutDownArraySerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readBooleanArray()", array)).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadBooleanArray();", array)).append(LS);
break;
default:
flag = false;
}
@@ -427,6 +434,9 @@ public class CutDownArraySerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readByteArray()", array)).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadByteArray();", array)).append(LS);
break;
default:
flag = false;
}
@@ -442,6 +452,9 @@ public class CutDownArraySerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readByteArray()", array)).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadByteArray();", array)).append(LS);
break;
default:
flag = false;
}
@@ -457,6 +470,9 @@ public class CutDownArraySerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readShortArray()", array)).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadShortArray();", array)).append(LS);
break;
default:
flag = false;
}
@@ -472,6 +488,9 @@ public class CutDownArraySerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readShortArray()", array)).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadShortArray();", array)).append(LS);
break;
default:
flag = false;
}
@@ -487,6 +506,9 @@ public class CutDownArraySerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readIntArray()", array)).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadIntArray();", array)).append(LS);
break;
default:
flag = false;
}
@@ -502,6 +524,9 @@ public class CutDownArraySerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readIntArray()", array)).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadIntArray();", array)).append(LS);
break;
default:
flag = false;
}
@@ -517,6 +542,9 @@ public class CutDownArraySerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readLongArray()", array)).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadLongArray();", array)).append(LS);
break;
default:
flag = false;
}
@@ -532,6 +560,9 @@ public class CutDownArraySerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readLongArray()", array)).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadLongArray();", array)).append(LS);
break;
default:
flag = false;
}
@@ -547,6 +578,9 @@ public class CutDownArraySerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readFloatArray()", array)).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadFloatArray();", array)).append(LS);
break;
default:
flag = false;
}
@@ -562,6 +596,9 @@ public class CutDownArraySerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readFloatArray()", array)).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadFloatArray();", array)).append(LS);
break;
default:
flag = false;
}
@@ -577,6 +614,9 @@ public class CutDownArraySerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readDoubleArray()", array)).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadDoubleArray();", array)).append(LS);
break;
default:
flag = false;
}
@@ -592,6 +632,9 @@ public class CutDownArraySerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readDoubleArray()", array)).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadDoubleArray();", array)).append(LS);
break;
default:
flag = false;
}
@@ -607,6 +650,9 @@ public class CutDownArraySerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readStringArray()", array)).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadStringArray();", array)).append(LS);
break;
default:
flag = false;
}
@@ -622,6 +668,9 @@ public class CutDownArraySerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readCharArray()", array)).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadCharArray();", array)).append(LS);
break;
default:
flag = false;
}
@@ -637,6 +686,9 @@ public class CutDownArraySerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readCharArray()", array)).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadCharArray();", array)).append(LS);
break;
default:
flag = false;
}
@@ -653,7 +705,7 @@ public class CutDownArraySerializer implements ICutDownSerializer {
builder.append(StringUtils.format("var {} = buffer.readPacketArray({})", array, objectProtocolField.getProtocolId())).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadPacketArray<{}>({});", array, getArrayClassSimpleName(arrayField), objectProtocolField.getProtocolId())).append(LS);
builder.append(StringUtils.format("var {} = buffer.ReadPacketArray<{}>({});", array, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(objectProtocolField.getProtocolId()), objectProtocolField.getProtocolId())).append(LS);
break;
default:
flag = false;
@@ -676,8 +728,4 @@ public class CutDownArraySerializer implements ICutDownSerializer {
return arrayField.getField().getType().getComponentType().getCanonicalName().replaceFirst("java.lang.", StringUtils.EMPTY);
}
public String getArrayClassSimpleName(ArrayField arrayField) {
return arrayField.getField().getType().getComponentType().getSimpleName();
}
}
@@ -17,10 +17,10 @@ import com.zfoo.protocol.registration.EnhanceUtils;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.registration.field.ListField;
import com.zfoo.protocol.registration.field.ObjectProtocolField;
import com.zfoo.protocol.serializer.enhance.EnhanceObjectProtocolSerializer;
import com.zfoo.protocol.util.StringUtils;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import static com.zfoo.protocol.util.FileUtils.LS;
@@ -204,7 +204,7 @@ public class CutDownListSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("buffer.writePacketArray({}, {})", objectStr, objectProtocolField.getProtocolId())).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("buffer.WritePacketList<{}>({}, {});", getListClassSimpleName(listField), objectStr, objectProtocolField.getProtocolId())).append(LS);
builder.append(StringUtils.format("buffer.WritePacketList<>({}, {});", objectStr, objectProtocolField.getProtocolId())).append(LS);
break;
default:
flag = false;
@@ -382,7 +382,7 @@ public class CutDownListSerializer implements ICutDownSerializer {
builder.append(StringUtils.format("var {} = buffer.readPacketArray({})", list, objectProtocolField.getProtocolId())).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadPacketList<{}>({});", list, getListClassSimpleName(listField), objectProtocolField.getProtocolId())).append(LS);
builder.append(StringUtils.format("var {} = buffer.ReadPacketList<{}>({});", list, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(objectProtocolField.getProtocolId()), objectProtocolField.getProtocolId())).append(LS);
break;
default:
flag = false;
@@ -401,8 +401,4 @@ public class CutDownListSerializer implements ICutDownSerializer {
}
}
public String getListClassSimpleName(ListField listField) {
return ((Class<?>) ((ParameterizedType) listField.getType()).getActualTypeArguments()[0]).getSimpleName();
}
}
@@ -18,6 +18,7 @@ import com.zfoo.protocol.registration.field.BaseField;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.registration.field.MapField;
import com.zfoo.protocol.registration.field.ObjectProtocolField;
import com.zfoo.protocol.serializer.enhance.EnhanceObjectProtocolSerializer;
import com.zfoo.protocol.serializer.reflect.IntSerializer;
import com.zfoo.protocol.serializer.reflect.LongSerializer;
import com.zfoo.protocol.serializer.reflect.ObjectProtocolSerializer;
@@ -62,6 +63,9 @@ public class CutDownMapSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("buffer.writeIntIntMap({})", objectStr)).append(LS);
return true;
case CSharp:
builder.append(StringUtils.format("buffer.WriteIntIntMap({});", objectStr)).append(LS);
return true;
}
} else if (valueSerializer == LongSerializer.INSTANCE) {
switch (language) {
@@ -74,6 +78,9 @@ public class CutDownMapSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("buffer.writeIntLongMap({})", objectStr)).append(LS);
return true;
case CSharp:
builder.append(StringUtils.format("buffer.WriteIntLongMap({});", objectStr)).append(LS);
return true;
}
} else if (valueSerializer == StringSerializer.INSTANCE) {
switch (language) {
@@ -86,6 +93,9 @@ public class CutDownMapSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("buffer.writeIntStringMap({})", objectStr)).append(LS);
return true;
case CSharp:
builder.append(StringUtils.format("buffer.WriteIntStringMap({});", objectStr)).append(LS);
return true;
}
} else if (valueSerializer == ObjectProtocolSerializer.INSTANCE) {
switch (language) {
@@ -98,6 +108,9 @@ public class CutDownMapSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("buffer.writeIntPacketMap({}, {})", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS);
return true;
case CSharp:
builder.append(StringUtils.format("buffer.WriteIntPacketMap<>({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS);
return true;
}
}
} else if (keySerializer == LongSerializer.INSTANCE) {
@@ -112,6 +125,9 @@ public class CutDownMapSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("buffer.writeLongIntMap({})", objectStr)).append(LS);
return true;
case CSharp:
builder.append(StringUtils.format("buffer.WriteLongIntMap({});", objectStr)).append(LS);
return true;
}
} else if (valueSerializer == LongSerializer.INSTANCE) {
switch (language) {
@@ -124,6 +140,9 @@ public class CutDownMapSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("buffer.writeLongLongMap({}, {})", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS);
return true;
case CSharp:
builder.append(StringUtils.format("buffer.WriteLongLongMap({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS);
return true;
}
} else if (valueSerializer == StringSerializer.INSTANCE) {
switch (language) {
@@ -136,6 +155,9 @@ public class CutDownMapSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("buffer.writeLongStringMap({})", objectStr)).append(LS);
return true;
case CSharp:
builder.append(StringUtils.format("buffer.WriteLongStringMap({});", objectStr)).append(LS);
return true;
}
} else if (valueSerializer == ObjectProtocolSerializer.INSTANCE) {
switch (language) {
@@ -148,6 +170,9 @@ public class CutDownMapSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("buffer.writeLongPacketMap({}, {})", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS);
return true;
case CSharp:
builder.append(StringUtils.format("buffer.WriteLongPacketMap<>({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS);
return true;
}
}
} else if (keySerializer == StringSerializer.INSTANCE) {
@@ -162,6 +187,9 @@ public class CutDownMapSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("buffer.writeStringIntMap({})", objectStr)).append(LS);
return true;
case CSharp:
builder.append(StringUtils.format("buffer.WriteStringIntMap({});", objectStr)).append(LS);
return true;
}
} else if (valueSerializer == LongSerializer.INSTANCE) {
switch (language) {
@@ -174,6 +202,9 @@ public class CutDownMapSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("buffer.writeStringLongMap({})", objectStr)).append(LS);
return true;
case CSharp:
builder.append(StringUtils.format("buffer.WriteStringLongMap({});", objectStr)).append(LS);
return true;
}
} else if (valueSerializer == StringSerializer.INSTANCE) {
switch (language) {
@@ -186,6 +217,9 @@ public class CutDownMapSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("buffer.writeStringStringMap({})", objectStr)).append(LS);
return true;
case CSharp:
builder.append(StringUtils.format("buffer.WriteStringStringMap({});", objectStr)).append(LS);
return true;
}
} else if (valueSerializer == ObjectProtocolSerializer.INSTANCE) {
switch (language) {
@@ -198,6 +232,9 @@ public class CutDownMapSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("buffer.writeStringPacketMap({}, {})", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS);
return true;
case CSharp:
builder.append(StringUtils.format("buffer.WriteStringPacketMap<>({}, {});", objectStr, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS);
return true;
}
}
}
@@ -228,6 +265,9 @@ public class CutDownMapSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readIntIntMap()", map)).append(LS);
return map;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadIntIntMap();", map)).append(LS);
return map;
}
} else if (valueSerializer == LongSerializer.INSTANCE) {
switch (language) {
@@ -240,6 +280,9 @@ public class CutDownMapSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readIntLongMap()", map)).append(LS);
return map;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadIntLongMap();", map)).append(LS);
return map;
}
} else if (valueSerializer == StringSerializer.INSTANCE) {
switch (language) {
@@ -252,8 +295,12 @@ public class CutDownMapSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readIntStringMap()", map)).append(LS);
return map;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadIntStringMap();", map)).append(LS);
return map;
}
} else if (valueSerializer == ObjectProtocolSerializer.INSTANCE) {
var protocolId = ((ObjectProtocolField) valueRegistration).getProtocolId();
switch (language) {
case Enhance:
builder.append(StringUtils.format("Map {} = {}.readIntPacketMap($1, {});", map, EnhanceUtils.byteBufUtils, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(((ObjectProtocolField) valueRegistration).getProtocolId())));
@@ -264,6 +311,9 @@ public class CutDownMapSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readIntPacketMap({})", map, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS);
return map;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadIntPacket<{}>Map({});", map, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId), protocolId)).append(LS);
return map;
}
}
} else if (keySerializer == LongSerializer.INSTANCE) {
@@ -278,6 +328,9 @@ public class CutDownMapSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readLongIntMap()", map)).append(LS);
return map;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadLongIntMap();", map)).append(LS);
return map;
}
} else if (valueSerializer == LongSerializer.INSTANCE) {
switch (language) {
@@ -290,6 +343,9 @@ public class CutDownMapSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readLongLongMap()", map)).append(LS);
return map;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadLongLongMap();", map)).append(LS);
return map;
}
} else if (valueSerializer == StringSerializer.INSTANCE) {
switch (language) {
@@ -302,8 +358,12 @@ public class CutDownMapSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readLongStringMap()", map)).append(LS);
return map;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadLongStringMap();", map)).append(LS);
return map;
}
} else if (valueSerializer == ObjectProtocolSerializer.INSTANCE) {
var protocolId = ((ObjectProtocolField) valueRegistration).getProtocolId();
switch (language) {
case Enhance:
builder.append(StringUtils.format("Map {} = {}.readLongPacketMap($1, {});", map, EnhanceUtils.byteBufUtils, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(((ObjectProtocolField) valueRegistration).getProtocolId())));
@@ -314,6 +374,9 @@ public class CutDownMapSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readLongPacketMap({})", map, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS);
return map;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadLongPacketMap<{}>({});", map, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId), protocolId)).append(LS);
return map;
}
}
} else if (keySerializer == StringSerializer.INSTANCE) {
@@ -328,6 +391,9 @@ public class CutDownMapSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readStringIntMap()", map)).append(LS);
return map;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadStringIntMap();", map)).append(LS);
return map;
}
} else if (valueSerializer == LongSerializer.INSTANCE) {
switch (language) {
@@ -340,6 +406,9 @@ public class CutDownMapSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readStringLongMap()", map)).append(LS);
return map;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadStringLongMap();", map)).append(LS);
return map;
}
} else if (valueSerializer == StringSerializer.INSTANCE) {
switch (language) {
@@ -352,8 +421,12 @@ public class CutDownMapSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readStringStringMap()", map)).append(LS);
return map;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadStringStringMap();", map)).append(LS);
return map;
}
} else if (valueSerializer == ObjectProtocolSerializer.INSTANCE) {
var protocolId = ((ObjectProtocolField) valueRegistration).getProtocolId();
switch (language) {
case Enhance:
builder.append(StringUtils.format("Map {} = {}.readStringPacketMap($1, {});", map, EnhanceUtils.byteBufUtils, EnhanceUtils.getProtocolRegistrationFieldNameByProtocolId(((ObjectProtocolField) valueRegistration).getProtocolId())));
@@ -364,6 +437,9 @@ public class CutDownMapSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readStringPacketMap({})", map, ((ObjectProtocolField) valueRegistration).getProtocolId())).append(LS);
return map;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadStringPacketMap<{}>({});", map, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(protocolId), protocolId)).append(LS);
return map;
}
}
}
@@ -17,6 +17,7 @@ import com.zfoo.protocol.registration.EnhanceUtils;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.registration.field.ObjectProtocolField;
import com.zfoo.protocol.registration.field.SetField;
import com.zfoo.protocol.serializer.enhance.EnhanceObjectProtocolSerializer;
import com.zfoo.protocol.util.StringUtils;
import java.lang.reflect.Field;
@@ -53,6 +54,9 @@ public class CutDownSetSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("buffer.writeBooleanArray({})", objectStr)).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("buffer.WriteBooleanSet({});", objectStr)).append(LS);
break;
default:
flag = false;
}
@@ -68,6 +72,9 @@ public class CutDownSetSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("buffer.writeByteArray({})", objectStr)).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("buffer.WriteByteSet({});", objectStr)).append(LS);
break;
default:
flag = false;
}
@@ -83,6 +90,9 @@ public class CutDownSetSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("buffer.writeShortArray({})", objectStr)).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("buffer.WriteShortSet({});", objectStr)).append(LS);
break;
default:
flag = false;
}
@@ -98,6 +108,9 @@ public class CutDownSetSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("buffer.writeIntArray({})", objectStr)).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("buffer.WriteIntSet({});", objectStr)).append(LS);
break;
default:
flag = false;
}
@@ -113,6 +126,9 @@ public class CutDownSetSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("buffer.writeLongArray({})", objectStr)).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("buffer.WriteLongSet({});", objectStr)).append(LS);
break;
default:
flag = false;
}
@@ -128,6 +144,9 @@ public class CutDownSetSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("buffer.writeFloatArray({})", objectStr)).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("buffer.WriteFloatSet({});", objectStr)).append(LS);
break;
default:
flag = false;
}
@@ -143,6 +162,9 @@ public class CutDownSetSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("buffer.writeDoubleArray({})", objectStr)).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("buffer.WriteDoubleSet({});", objectStr)).append(LS);
break;
default:
flag = false;
}
@@ -158,6 +180,9 @@ public class CutDownSetSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("buffer.writeStringArray({})", objectStr)).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("buffer.WriteStringSet({});", objectStr)).append(LS);
break;
default:
flag = false;
}
@@ -176,6 +201,9 @@ public class CutDownSetSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("buffer.writePacketArray({}, {})", objectStr, objectProtocolField.getProtocolId())).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("buffer.WritePacketSet<>({}, {});", objectStr, objectProtocolField.getProtocolId())).append(LS);
break;
default:
flag = false;
}
@@ -205,6 +233,9 @@ public class CutDownSetSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readBooleanArray()", set)).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadBooleanSet();", set)).append(LS);
break;
default:
flag = false;
}
@@ -220,6 +251,9 @@ public class CutDownSetSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readByteArray()", set)).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadByteSet();", set)).append(LS);
break;
default:
flag = false;
}
@@ -235,6 +269,9 @@ public class CutDownSetSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readShortArray()", set)).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadShortSet();", set)).append(LS);
break;
default:
flag = false;
}
@@ -250,6 +287,9 @@ public class CutDownSetSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readIntArray()", set)).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadIntSet();", set)).append(LS);
break;
default:
flag = false;
}
@@ -265,6 +305,9 @@ public class CutDownSetSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readLongArray()", set)).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadLongSet();", set)).append(LS);
break;
default:
flag = false;
}
@@ -280,6 +323,9 @@ public class CutDownSetSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readFloatArray()", set)).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadFloatSet();", set)).append(LS);
break;
default:
flag = false;
}
@@ -295,6 +341,9 @@ public class CutDownSetSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readDoubleArray()", set)).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadDoubleSet();", set)).append(LS);
break;
default:
flag = false;
}
@@ -310,6 +359,9 @@ public class CutDownSetSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readStringArray()", set)).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadStringSet();", set)).append(LS);
break;
default:
flag = false;
}
@@ -327,6 +379,9 @@ public class CutDownSetSerializer implements ICutDownSerializer {
case GdScript:
builder.append(StringUtils.format("var {} = buffer.readPacketArray({})", set, objectProtocolField.getProtocolId())).append(LS);
break;
case CSharp:
builder.append(StringUtils.format("var {} = buffer.ReadPacketSet<{}>({});", set, EnhanceObjectProtocolSerializer.getProtocolClassSimpleName(objectProtocolField.getProtocolId()), objectProtocolField.getProtocolId())).append(LS);
break;
default:
flag = false;
}
@@ -343,4 +398,5 @@ public class CutDownSetSerializer implements ICutDownSerializer {
return null;
}
}
}
@@ -16,6 +16,8 @@ package com.zfoo.protocol.serializer.cs;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.registration.field.MapField;
import com.zfoo.protocol.serializer.CodeLanguage;
import com.zfoo.protocol.serializer.CutDownMapSerializer;
import com.zfoo.protocol.util.StringUtils;
import java.lang.reflect.Field;
@@ -30,9 +32,13 @@ public class CsMapSerializer implements ICsSerializer {
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GenerateProtocolFile.addTab(builder, deep);
if (CutDownMapSerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.CSharp)) {
return;
}
MapField mapField = (MapField) fieldRegistration;
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("if (({} == null) || ({}.Count == 0))", objectStr, objectStr)).append(LS);
GenerateProtocolFile.addTab(builder, deep);
builder.append("{").append(LS);
@@ -80,12 +86,17 @@ public class CsMapSerializer implements ICsSerializer {
@Override
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
GenerateProtocolFile.addTab(builder, deep);
var cutDown = CutDownMapSerializer.getInstance().readObject(builder, field, fieldRegistration, CodeLanguage.CSharp);
if (cutDown != null) {
return cutDown;
}
MapField mapField = (MapField) fieldRegistration;
String result = "result" + GenerateProtocolFile.index.getAndIncrement();
var typeName = GenerateCsUtils.toCsClassName(mapField.getType().toString());
GenerateProtocolFile.addTab(builder, deep);
String size = "size" + GenerateProtocolFile.index.getAndIncrement();
builder.append(StringUtils.format("int {} = buffer.ReadInt();", size)).append(LS);
@@ -16,6 +16,8 @@ package com.zfoo.protocol.serializer.cs;
import com.zfoo.protocol.generate.GenerateProtocolFile;
import com.zfoo.protocol.registration.field.IFieldRegistration;
import com.zfoo.protocol.registration.field.SetField;
import com.zfoo.protocol.serializer.CodeLanguage;
import com.zfoo.protocol.serializer.CutDownSetSerializer;
import com.zfoo.protocol.util.StringUtils;
import java.lang.reflect.Field;
@@ -30,9 +32,13 @@ public class CsSetSerializer implements ICsSerializer {
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GenerateProtocolFile.addTab(builder, deep);
if (CutDownSetSerializer.getInstance().writeObject(builder, objectStr, field, fieldRegistration, CodeLanguage.CSharp)) {
return;
}
SetField setField = (SetField) fieldRegistration;
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("if ({} == null)", objectStr)).append(LS);
GenerateProtocolFile.addTab(builder, deep);
builder.append("{").append(LS);
@@ -66,6 +72,12 @@ public class CsSetSerializer implements ICsSerializer {
@Override
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
GenerateProtocolFile.addTab(builder, deep);
var cutDown = CutDownSetSerializer.getInstance().readObject(builder, field, fieldRegistration, CodeLanguage.CSharp);
if (cutDown != null) {
return cutDown;
}
SetField setField = (SetField) fieldRegistration;
var result = "result" + GenerateProtocolFile.index.getAndIncrement();
@@ -73,7 +85,6 @@ public class CsSetSerializer implements ICsSerializer {
var i = "index" + GenerateProtocolFile.index.getAndIncrement();
var size = "size" + GenerateProtocolFile.index.getAndIncrement();
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("int {} = buffer.ReadInt();", size)).append(LS);
GenerateProtocolFile.addTab(builder, deep);
// unity里不支持HashSet的初始化大小
@@ -45,7 +45,11 @@ public class EnhanceObjectProtocolSerializer implements IEnhanceSerializer {
return result;
}
private String getProtocolClassCanonicalName(short protocolId) {
public static String getProtocolClassSimpleName(short protocolId) {
return ProtocolManager.getProtocol(protocolId).protocolConstructor().getDeclaringClass().getSimpleName();
}
public static String getProtocolClassCanonicalName(short protocolId) {
return ProtocolManager.getProtocol(protocolId).protocolConstructor().getDeclaringClass().getCanonicalName();
}
@@ -1188,5 +1188,682 @@ namespace CsProtocol.Buffer
}
return list;
}
public void WriteBooleanSet(HashSet<bool> set)
{
if ((set == null) || (set.Count == 0))
{
WriteInt(0);
}
else
{
WriteInt(set.Count);
foreach (var element in set)
{
WriteBool(element);
}
}
}
public HashSet<bool> ReadBooleanSet()
{
int size = ReadInt();
HashSet<bool> set = new HashSet<bool>(size);
if (size > 0)
{
for (int index = 0; index < size; index++)
{
set.Add(ReadBool());
}
}
return set;
}
public void WriteShortSet(HashSet<short> set)
{
if ((set == null) || (set.Count == 0))
{
WriteInt(0);
}
else
{
WriteInt(set.Count);
foreach (var element in set)
{
WriteShort(element);
}
}
}
public HashSet<short> ReadShortSet()
{
int size = ReadInt();
HashSet<short> set = new HashSet<short>(size);
if (size > 0)
{
for (int index = 0; index < size; index++)
{
set.Add(ReadShort());
}
}
return set;
}
public void WriteIntSet(HashSet<int> set)
{
if ((set == null) || (set.Count == 0))
{
WriteInt(0);
}
else
{
WriteInt(set.Count);
foreach (var element in set)
{
WriteInt(element);
}
}
}
public HashSet<int> ReadIntSet()
{
int size = ReadInt();
HashSet<int> set = new HashSet<int>(size);
if (size > 0)
{
for (int index = 0; index < size; index++)
{
set.Add(ReadInt());
}
}
return set;
}
public void WriteLongSet(HashSet<long> set)
{
if ((set == null) || (set.Count == 0))
{
WriteInt(0);
}
else
{
WriteInt(set.Count);
foreach (var element in set)
{
WriteLong(element);
}
}
}
public HashSet<long> ReadLongSet()
{
int size = ReadInt();
HashSet<long> set = new HashSet<long>(size);
if (size > 0)
{
for (int index = 0; index < size; index++)
{
set.Add(ReadLong());
}
}
return set;
}
public void WriteFloatSet(HashSet<float> set)
{
if ((set == null) || (set.Count == 0))
{
WriteInt(0);
}
else
{
WriteInt(set.Count);
foreach (var element in set)
{
WriteFloat(element);
}
}
}
public HashSet<float> ReadFloatSet()
{
int size = ReadInt();
HashSet<float> set = new HashSet<float>(size);
if (size > 0)
{
for (int index = 0; index < size; index++)
{
set.Add(ReadFloat());
}
}
return set;
}
public void WriteDoubleSet(HashSet<double> set)
{
if ((set == null) || (set.Count == 0))
{
WriteInt(0);
}
else
{
WriteInt(set.Count);
foreach (var element in set)
{
WriteDouble(element);
}
}
}
public HashSet<double> ReadDoubleSet()
{
int size = ReadInt();
HashSet<double> set = new HashSet<double>(size);
if (size > 0)
{
for (int index = 0; index < size; index++)
{
set.Add(ReadDouble());
}
}
return set;
}
public void WriteCharSet(HashSet<char> set)
{
if ((set == null) || (set.Count == 0))
{
WriteInt(0);
}
else
{
WriteInt(set.Count);
foreach (var element in set)
{
WriteChar(element);
}
}
}
public HashSet<char> ReadCharSet()
{
int size = ReadInt();
HashSet<char> set = new HashSet<char>(size);
if (size > 0)
{
for (int index = 0; index < size; index++)
{
set.Add(ReadChar());
}
}
return set;
}
public void WriteStringSet(HashSet<string> set)
{
if ((set == null) || (set.Count == 0))
{
WriteInt(0);
}
else
{
WriteInt(set.Count);
foreach (var element in set)
{
WriteString(element);
}
}
}
public HashSet<string> ReadStringSet()
{
int size = ReadInt();
HashSet<string> set = new HashSet<string>(size);
if (size > 0)
{
for (int index = 0; index < size; index++)
{
set.Add(ReadString());
}
}
return set;
}
public void WritePacketSet<T>(HashSet<T> set, short protocolId)
{
if ((set == null) || (set.Count == 0))
{
WriteInt(0);
}
else
{
IProtocolRegistration protocolRegistration = ProtocolManager.GetProtocol(protocolId);
WriteInt(set.Count);
foreach (var element in set)
{
protocolRegistration.Write(this, (IPacket) element);
}
}
}
public HashSet<T> ReadPacketSet<T>(short protocolId)
{
int size = ReadInt();
HashSet<T> set = new HashSet<T>(size);
if (size > 0)
{
IProtocolRegistration protocolRegistration = ProtocolManager.GetProtocol(protocolId);
for (int index = 0; index < size; index++)
{
set.Add((T) protocolRegistration.Read(this));
}
}
return set;
}
public void WriteIntIntMap(Dictionary<int, int> map)
{
if ((map == null) || (map.Count == 0))
{
WriteInt(0);
}
else
{
WriteInt(map.Count);
foreach (var element in map)
{
WriteInt(element.Key);
WriteInt(element.Value);
}
}
}
public Dictionary<int, int> ReadIntIntMap()
{
int size = ReadInt();
Dictionary<int, int> map = new Dictionary<int, int>(size);
if (size > 0)
{
for (int index = 0; index < size; index++)
{
var key = ReadInt();
var value = ReadInt();
map[key] = value;
}
}
return map;
}
public void WriteIntLongMap(Dictionary<int, long> map)
{
if ((map == null) || (map.Count == 0))
{
WriteInt(0);
}
else
{
WriteInt(map.Count);
foreach (var element in map)
{
WriteInt(element.Key);
WriteLong(element.Value);
}
}
}
public Dictionary<int, long> ReadIntLongMap()
{
int size = ReadInt();
Dictionary<int, long> map = new Dictionary<int, long>(size);
if (size > 0)
{
for (int index = 0; index < size; index++)
{
var key = ReadInt();
var value = ReadLong();
map[key] = value;
}
}
return map;
}
public void WriteIntStringMap(Dictionary<int, string> map)
{
if ((map == null) || (map.Count == 0))
{
WriteInt(0);
}
else
{
WriteInt(map.Count);
foreach (var element in map)
{
WriteInt(element.Key);
WriteString(element.Value);
}
}
}
public Dictionary<int, string> ReadIntStringMap()
{
int size = ReadInt();
Dictionary<int, string> map = new Dictionary<int, string>(size);
if (size > 0)
{
for (int index = 0; index < size; index++)
{
var key = ReadInt();
var value = ReadString();
map[key] = value;
}
}
return map;
}
public void WriteIntPacketMap<T>(Dictionary<int, T> map, short protocolId)
{
if ((map == null) || (map.Count == 0))
{
WriteInt(0);
}
else
{
IProtocolRegistration protocolRegistration = ProtocolManager.GetProtocol(protocolId);
WriteInt(map.Count);
foreach (var element in map)
{
WriteInt(element.Key);
protocolRegistration.Write(this, (IPacket) element.Value);
}
}
}
public Dictionary<int, T> ReadIntPacketMap<T>(short protocolId)
{
int size = ReadInt();
Dictionary<int, T> map = new Dictionary<int, T>(size);
if (size > 0)
{
IProtocolRegistration protocolRegistration = ProtocolManager.GetProtocol(protocolId);
for (int index = 0; index < size; index++)
{
var key = ReadInt();
var value = (T) protocolRegistration.Read(this);
map[key] = value;
}
}
return map;
}
public void WriteLongIntMap(Dictionary<long, int> map)
{
if ((map == null) || (map.Count == 0))
{
WriteInt(0);
}
else
{
WriteInt(map.Count);
foreach (var element in map)
{
WriteLong(element.Key);
WriteInt(element.Value);
}
}
}
public Dictionary<long, int> ReadLongIntMap()
{
int size = ReadInt();
Dictionary<long, int> map = new Dictionary<long, int>(size);
if (size > 0)
{
for (int index = 0; index < size; index++)
{
var key = ReadLong();
var value = ReadInt();
map[key] = value;
}
}
return map;
}
public void WriteLongLongMap(Dictionary<long, long> map)
{
if ((map == null) || (map.Count == 0))
{
WriteInt(0);
}
else
{
WriteInt(map.Count);
foreach (var element in map)
{
WriteLong(element.Key);
WriteLong(element.Value);
}
}
}
public Dictionary<long, long> ReadLongLongMap()
{
int size = ReadInt();
Dictionary<long, long> map = new Dictionary<long, long>(size);
if (size > 0)
{
for (int index = 0; index < size; index++)
{
var key = ReadLong();
var value = ReadLong();
map[key] = value;
}
}
return map;
}
public void WriteLongStringMap(Dictionary<long, string> map)
{
if ((map == null) || (map.Count == 0))
{
WriteInt(0);
}
else
{
WriteInt(map.Count);
foreach (var element in map)
{
WriteLong(element.Key);
WriteString(element.Value);
}
}
}
public Dictionary<long, string> ReadLongStringMap()
{
int size = ReadInt();
Dictionary<long, string> map = new Dictionary<long, string>(size);
if (size > 0)
{
for (int index = 0; index < size; index++)
{
var key = ReadLong();
var value = ReadString();
map[key] = value;
}
}
return map;
}
public void WriteLongPacketMap<T>(Dictionary<long, T> map, short protocolId)
{
if ((map == null) || (map.Count == 0))
{
WriteInt(0);
}
else
{
IProtocolRegistration protocolRegistration = ProtocolManager.GetProtocol(protocolId);
WriteInt(map.Count);
foreach (var element in map)
{
WriteLong(element.Key);
protocolRegistration.Write(this, (IPacket) element.Value);
}
}
}
public Dictionary<long, T> ReadLongPacketMap<T>(short protocolId)
{
int size = ReadInt();
Dictionary<long, T> map = new Dictionary<long, T>(size);
if (size > 0)
{
IProtocolRegistration protocolRegistration = ProtocolManager.GetProtocol(protocolId);
for (int index = 0; index < size; index++)
{
var key = ReadLong();
var value = (T) protocolRegistration.Read(this);
map[key] = value;
}
}
return map;
}
public void WriteStringIntMap(Dictionary<string, int> map)
{
if ((map == null) || (map.Count == 0))
{
WriteInt(0);
}
else
{
WriteInt(map.Count);
foreach (var element in map)
{
WriteString(element.Key);
WriteInt(element.Value);
}
}
}
public Dictionary<string, int> ReadStringIntMap()
{
int size = ReadInt();
Dictionary<string, int> map = new Dictionary<string, int>(size);
if (size > 0)
{
for (int index = 0; index < size; index++)
{
var key = ReadString();
var value = ReadInt();
map[key] = value;
}
}
return map;
}
public void WriteStringLongMap(Dictionary<string, long> map)
{
if ((map == null) || (map.Count == 0))
{
WriteInt(0);
}
else
{
WriteInt(map.Count);
foreach (var element in map)
{
WriteString(element.Key);
WriteLong(element.Value);
}
}
}
public Dictionary<string, long> ReadStringLongMap()
{
int size = ReadInt();
Dictionary<string, long> map = new Dictionary<string, long>(size);
if (size > 0)
{
for (int index = 0; index < size; index++)
{
var key = ReadString();
var value = ReadLong();
map[key] = value;
}
}
return map;
}
public void WriteStringStringMap(Dictionary<string, string> map)
{
if ((map == null) || (map.Count == 0))
{
WriteInt(0);
}
else
{
WriteInt(map.Count);
foreach (var element in map)
{
WriteString(element.Key);
WriteString(element.Value);
}
}
}
public Dictionary<string, string> ReadStringStringMap()
{
int size = ReadInt();
Dictionary<string, string> map = new Dictionary<string, string>(size);
if (size > 0)
{
for (int index = 0; index < size; index++)
{
var key = ReadString();
var value = ReadString();
map[key] = value;
}
}
return map;
}
public void WriteStringPacketMap<T>(Dictionary<string, T> map, short protocolId)
{
if ((map == null) || (map.Count == 0))
{
WriteInt(0);
}
else
{
IProtocolRegistration protocolRegistration = ProtocolManager.GetProtocol(protocolId);
WriteInt(map.Count);
foreach (var element in map)
{
WriteString(element.Key);
protocolRegistration.Write(this, (IPacket) element.Value);
}
}
}
public Dictionary<string, T> ReadStringPacketMap<T>(short protocolId)
{
int size = ReadInt();
Dictionary<string, T> map = new Dictionary<string, T>(size);
if (size > 0)
{
IProtocolRegistration protocolRegistration = ProtocolManager.GetProtocol(protocolId);
for (int index = 0; index < size; index++)
{
var key = ReadString();
var value = (T) protocolRegistration.Read(this);
map[key] = value;
}
}
return map;
}
}
}