示例#1
0
        /// <inheritdoc />
        public void Handshake(byte[] bytes, int offset)
        {
            System.Type targetType = typeof(T);

            System.Type[] allTypes             = targetType.Assembly.GetTypes();
            System.Type[] namespaceSchemaTypes = Array.FindAll(allTypes, t => t.Namespace == targetType.Namespace &&
                                                               typeof(Schema.Schema).IsAssignableFrom(
                                                                   targetType));

            ColyseusReflection reflection = new ColyseusReflection();
            Iterator           it         = new Iterator {
                Offset = offset
            };

            reflection.Decode(bytes, it);

            for (int i = 0; i < reflection.types.Count; i++)
            {
                System.Type schemaType = Array.Find(namespaceSchemaTypes, t => CompareTypes(t, reflection.types[i]));

                if (schemaType == null)
                {
                    throw new Exception(
                              "Local schema mismatch from server. Use \"schema-codegen\" to generate up-to-date local definitions.");
                }

                ColyseusContext.GetInstance().SetTypeId(schemaType, reflection.types[i].id);
            }
        }
示例#2
0
        private static bool CompareTypes(System.Type schemaType, ReflectionType reflectionType)
        {
            FieldInfo[] fields          = schemaType.GetFields();
            int         typedFieldCount = 0;

            string fieldNames = "";

            for (int i = 0; i < fields.Length; i++)
            {
                fieldNames += fields[i].Name + ", ";
            }

            foreach (FieldInfo field in fields)
            {
                object[] typeAttributes = field.GetCustomAttributes(typeof(Type), true);

                if (typeAttributes.Length == 1)
                {
                    Type            typedField      = (Type)typeAttributes[0];
                    ReflectionField reflectionField = reflectionType.fields[typedField.Index];

                    if (
                        reflectionField == null ||
                        reflectionField.type.IndexOf(typedField.FieldType) != 0 ||
                        reflectionField.name != field.Name
                        )
                    {
                        return(false);
                    }

                    typedFieldCount++;
                }
            }

            // skip if number of Type'd fields doesn't match
            if (typedFieldCount != reflectionType.fields.Count)
            {
                return(false);
            }

            return(true);
        }