Ejemplo n.º 1
0
        public static SLComponent BFS(SLComponent root, ComponentType t)
        {
            var visited = new HashSet <SLComponent>();
            var q       = new Queue <SLComponent>();

            q.Enqueue(root);
            visited.Add(root);
            while (q.Count > 0)
            {
                var here = q.Dequeue();
                if (here.Type == t)
                {
                    return(here);
                }

                var gIter = here.GetGroupEnumerator();
                while (gIter.MoveNext())
                {
                    var wrap  = gIter.Current;
                    var wIter = wrap.GetEnumerator();
                    while (wIter.MoveNext())
                    {
                        var there = wIter.Current;
                        if (!visited.Contains(there))
                        {
                            q.Enqueue(there);
                            visited.Add(there);
                        }
                    }
                }
            }

            return(null);
        }
Ejemplo n.º 2
0
            public async Task <SLComponent> LoadAsync(SLComponent root, string name)
            {
                var path = $"{name}.slc";

                if (!File.Exists(path))
                {
                    return(null);
                }

                using (var fs = File.OpenRead(path))
                {
                    var buffer = Library.Locator.BufferPool.GetBuffer(1 << 16);
                    Library.Logger.Assert(buffer.Length >= fs.Length, "overflow buffer");

                    var n = await fs.ReadAsync(buffer, 0, (int)fs.Length);

                    if (n == 0)
                    {
                        return(null);
                    }

                    var reader = new Library.Reader(buffer, n);
                    var com    = Serialization.ReadComponent(ref reader);
                    defaultLink(root, com);
                    return(com);
                }
            }
Ejemplo n.º 3
0
            public SLComponent Create(SLComponent root, ComponentType type, string key)
            {
                var com = Create(root, type);

                root.LinkTo(key, com);
                return(com);
            }
Ejemplo n.º 4
0
            private void defaultLink(SLComponent root, SLComponent com)
            {
                root.LinkTo(com);
                var ptr = SLPointer.Index(com._index);

                root.insert(ref ptr, com);
            }
Ejemplo n.º 5
0
            public SLComponent Create(SLComponent root, ComponentType type, int handle)
            {
                var com = Create(root, type);

                root.LinkTo(handle, com);
                return(com);
            }
Ejemplo n.º 6
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);
            }
        }
Ejemplo n.º 7
0
 public async Task SaveAsync(SLComponent com, string name)
 {
     using (var fs = File.OpenWrite($"{name}.slc"))
     {
         var writer = new Library.Writer(1 << 16);
         Serialization.WriteComponent(ref writer, com);
         await writer.FlushAsync(fs);
     }
 }
Ejemplo n.º 8
0
            public bool Destroy(SLComponent root, string key)
            {
                if (!root.TryFind(key, out SLWrapper wrapper))
                {
                    return(false);
                }

                Destroy(root, wrapper);
                return(true);
            }
Ejemplo n.º 9
0
            public bool Destroy(SLComponent root, int handle)
            {
                if (!root.TryFind(handle, out SLWrapper wrapper))
                {
                    return(false);
                }

                Destroy(root, wrapper);
                return(true);
            }
Ejemplo n.º 10
0
        public void OnDeserialized(ref Library.Reader reader, SLComponent streamingComponent)
        {
            var read = reader.ReadInt(out int t);

            type = (PointerType)t;
            if (type == PointerType.Text)
            {
                read &= reader.ReadString(out string text);
                this  = SLPointer.Text(text);
            }
        }
Ejemplo n.º 11
0
            public void Destroy(SLComponent root, SLComponent com)
            {
                if (null != root)
                {
                    unsafe
                    {
                        var serviceProp = root.Get <Property.Service>();
                        serviceProp->destroyCount++;
                    }
                }

                _pool.PutObject(com.onRelease());
            }
Ejemplo n.º 12
0
            public SLComponent Create(SLComponent root, ComponentType type)
            {
                var com = _pool.GetObject().onTake(type, _pool.GenerateIndex);

                if (null != root)
                {
                    defaultLink(root, com);
                    unsafe
                    {
                        var serviceProp = root.Get <Property.Service>();
                        serviceProp->createCount++;
                    }
                }

                return(com);
            }
Ejemplo n.º 13
0
 public static void BidLink(SLComponent lhs, SLComponent rhs)
 {
     lhs.LinkTo(rhs);
     rhs.LinkTo(lhs);
 }
Ejemplo n.º 14
0
 public void Regist(int key, SLComponent com)
 {
     _ints.Add(key, com);
 }
Ejemplo n.º 15
0
 public void Regist(object key, SLComponent com)
 {
     _refs.Add(key, com);
 }