コード例 #1
0
ファイル: Manager.cs プロジェクト: ryden/Protoserial
        private FieldData GetFieldForHash(NameHash hash, DictionaryEntry entry )
        {
            if ( hash.ActualHash == 0 )
            {
                if (hash.Name.Length == 0)
                    throw new UnknownTypeException("");

                foreach ( var field in entry.Fields )
                {
                    if (field.Hash.Name.Equals(hash.Name))
                        return field;
                }
            }
            else
            {
                foreach ( var field in entry.Fields )
                {
                    if (field.Hash.ActualHash == hash.ActualHash)
                        return field;
                }
            }

            return null;
        }
コード例 #2
0
ファイル: Manager.cs プロジェクト: ryden/Protoserial
        public void RegisterMessageType(Type type)
        {
            var attrs = type.GetCustomAttributes(true).OfType<Message>();
            if (attrs.Any())
            {
                // Get the hash from the type name, and check for collisions
                short hash = Crc16.Calc(type.Name);
                bool collided = false;
                foreach (var item in mTypes.Keys)
                {
                    var value = mTypes[item];
                    if (value.Hash.OriginalHash == hash)
                    {
                        collided = true;
                        value.Hash.ActualHash = 0;
                        break;
                    }
                }

                var entry = new DictionaryEntry
                {
                    Type = type,
                    Hash =
                        {
                            OriginalHash = hash,
                            ActualHash = collided ? (short) 0 : hash,
                            Name = type.Name
                        }
                };

                // Find all the type members and add them
                var fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy);
                LoadFields(fields, out entry.Fields);

                mTypes.Add(type, entry);
            }
            else
            {
                throw new Protoserial.NotAMessageException(type.Name);
            }
        }