public void Test_CreateTable_WithFilter() { var table = new TypeAliasTable(new[] { GetAssembly() }, t => t.Name.StartsWith("Alice") == false); Assert.AreEqual(null, table.GetType(1)); Assert.AreEqual(typeof(BobClass), table.GetType(2)); }
public void Test_CreateTable_WithFilter() { var table = new TypeAliasTable(new[] { GetAssembly() }, t => t.Name.StartsWith("Alice") == false); Assert.AreEqual(null, table.GetType(1)); Assert.AreEqual(typeof(BobClass), table.GetType(2)); }
public void Test_CreateTable_WithCustomResolveAutoAlias() { var table = new TypeAliasTable(new[] { GetAssembly() }, null, t => t.Name.Length); Assert.AreEqual(typeof(AliceClass), table.GetType(1)); Assert.AreEqual(typeof(EveClass), table.GetType(8)); Assert.AreEqual(null, table.GetType(483686294)); }
public void Test_CreateTable_WithCustomResolveAutoAlias() { var table = new TypeAliasTable(new[] { GetAssembly() }, null, t => t.Name.Length); Assert.AreEqual(typeof(AliceClass), table.GetType(1)); Assert.AreEqual(typeof(EveClass), table.GetType(8)); Assert.AreEqual(null, table.GetType(483686294)); }
private Type ReadType(Stream stream) { var firstByte = stream.ReadByte(); if ((firstByte & 0x80) == 0) { // use type alias var b1 = stream.ReadByte(); var b2 = stream.ReadByte(); var b3 = stream.ReadByte(); var typeAlias = ((int)(firstByte) << 24) | ((int)(b1) << 16) | ((int)(b2) << 8) | ((int)(b3)); return(_typeTable.GetType(typeAlias)); } else { // use type assembly qualified name stream.Position = stream.Position - 1; var length = stream.Read7BitEncodedInt(); length -= 0x80; var bytes = new byte[length]; stream.Read(bytes, 0, length); var typeName = Encoding.UTF8.GetString(bytes); return(Type.GetType(typeName)); } }
private static object Deserialize(Stream stream, int length) { // Read alias of type var aliasBuf = new byte[4]; stream.Read(aliasBuf, 0, aliasBuf.Length); var alias = BitConverter.ToInt32(aliasBuf, 0); var type = s_typeAliasTable.GetType(alias); // Read proto-buf stream of object return(s_typeModel.Deserialize(stream, null, type, length - aliasBuf.Length)); }
public static object ReadObject(BinaryReader reader, TypeAliasTable typeTable, TypeModel typeModel) { var length = reader.ReadInt32(); if (length > 0) { var typeAlias = reader.ReadInt32(); var type = typeTable.GetType(typeAlias); var obj = typeModel.Deserialize(reader.BaseStream, null, type, length - 4); return(obj); } else { return(null); } }
public void Test_CreateTable_WithCustomAssemblies() { var table = new TypeAliasTable(new[] { GetAssembly() }); Assert.AreEqual(typeof(AliceClass), table.GetType(1)); }
public void FromBytes(byte[] bytes, IChannelToClientZone channelToClientZone) { using (var stream = new MemoryStream(bytes)) using (var reader = new BinaryReader(stream)) { while (stream.Position < stream.Length) { var code = reader.ReadByte(); switch (code) { case 1: { var clientId = reader.ReadInt32(); var startTime = reader.ReadInt64(); var elapsedTime = reader.ReadInt64(); channelToClientZone.Init(clientId, new DateTime(startTime, DateTimeKind.Utc), new TimeSpan(elapsedTime)); break; } case 2: { var entityId = reader.ReadInt32(); var typeAlias = reader.ReadInt32(); var ownerId = reader.ReadInt32(); var flags = (EntityFlags)reader.ReadByte(); var payload = (ISpawnPayload)ProtobufStreamHelper.ReadObject(reader, TypeTable, TypeModel); channelToClientZone.Spawn(entityId, TypeTable.GetType(typeAlias), ownerId, flags, payload); break; } case 3: { var entityId = reader.ReadInt32(); channelToClientZone.Despawn(entityId); break; } case 4: { var entityId = reader.ReadInt32(); var payload = (IInvokePayload)ProtobufStreamHelper.ReadObject(reader, TypeTable, TypeModel); channelToClientZone.Invoke(entityId, payload); break; } case 5: { var entityId = reader.ReadInt32(); var payload = (IUpdateChangePayload)ProtobufStreamHelper.ReadObject(reader, TypeTable, TypeModel); channelToClientZone.UpdateChange(entityId, payload); break; } case 6: { var entityId = reader.ReadInt32(); var ownerId = reader.ReadInt32(); channelToClientZone.OwnershipChange(entityId, ownerId); break; } default: throw new Exception("Invalid code: " + code); } } } }
public void Test_CreateTable_WithCustomAssemblies() { var table = new TypeAliasTable(new[] { GetAssembly() }); Assert.AreEqual(typeof(AliceClass), table.GetType(1)); }