Beispiel #1
0
        public static void WriteComponent(ref Library.Writer writer, SLComponent com, Predicate <SLComponent> filter = null)
        {
            var comList = new Dictionary <int, SLComponent>();

            com.Traversal(comList);

            if (null != filter)
            {
                unsafe
                {
                    var tempList = stackalloc int[comList.Count];
                    var tempIdx  = 0;
                    var iter     = comList.GetEnumerator();
                    while (iter.MoveNext())
                    {
                        if (filter(iter.Current.Value))
                        {
                            tempList[tempIdx++] = iter.Current.Key;
                        }
                    }

                    while (tempIdx > 0)
                    {
                        comList.Remove(tempList[--tempIdx]);
                    }
                }
            }

            writer.WriteInt(comList.Count);
            var pairIter = comList.GetEnumerator();

            while (pairIter.MoveNext())
            {
                writer.WriteInt((int)pairIter.Current.Value.Type);
                writer.WriteInt(pairIter.Current.Key);
            }

            var ctxList = new Dictionary <int, object>();

            pairIter = comList.GetEnumerator();
            while (pairIter.MoveNext())
            {
                var com2 = pairIter.Current.Value;
                com2.OnSerialized(ref writer);
                if (null != com2.Context && Attribute.IsDefined(com2.Context.GetType(), typeof(SerializableAttribute)))
                {
                    ctxList.Add(pairIter.Current.Key, com2.Context);
                }
            }

            writer.WriteInt(ctxList.Count);
            if (ctxList.Count > 0)
            {
                var stream = new MemoryStream();
                Library.SerializeHelper.SerializeToMemory(ctxList, stream);
                writer.WriteStream(stream);
            }
        }