/// <summary> /// 处理事件结点。 /// </summary> /// <param name="sender">事件源。</param> /// <param name="e">事件参数。</param> private void HandleEvent(object sender, T e) { bool noHandlerException = false; CFLinkedListRange <EventHandler <T> > range = default(CFLinkedListRange <EventHandler <T> >); if (m_EventHandlers.TryGetValue(e.Id, out range)) { LinkedListNode <EventHandler <T> > current = range.First; while (current != null && current != range.Terminal) { m_CachedNodes[e] = current.Next != range.Terminal ? current.Next : null; current.Value(sender, e); current = m_CachedNodes[e]; } m_CachedNodes.Remove(e); } else if (m_DefaultHandler != null) { m_DefaultHandler(sender, e); } else if ((m_EventPoolMode & EventPoolMode.AllowNoHandler) == 0) { noHandlerException = true; } ReferencePool.Release(e); if (noHandlerException) { throw new Exception(Utility.Text.Format("Event '{0}' not allow no handler.", e.Id.ToString())); } }
/// <summary> /// 获取事件处理函数的数量。 /// </summary> /// <param name="id">事件类型编号。</param> /// <returns>事件处理函数的数量。</returns> public int Count(int id) { CFLinkedListRange <EventHandler <T> > range = default(CFLinkedListRange <EventHandler <T> >); if (m_EventHandlers.TryGetValue(id, out range)) { return(range.Count); } return(0); }
/// <summary> /// 获取对象。 /// </summary> /// <param name="name">对象名称。</param> /// <returns>要获取的对象。</returns> public T Spawn(string name) { CFLinkedListRange <Object <T> > objectRange = default(CFLinkedListRange <Object <T> >); if (m_Objects.TryGetValue(name, out objectRange)) { foreach (Object <T> internalObject in objectRange) { if (m_AllowMultiSpawn || !internalObject.IsInUse) { return(internalObject.Spawn()); } } } return(null); }
private int GetEmptyBlockIndex() { CFLinkedListRange <int> lengthRange = default(CFLinkedListRange <int>); if (m_FreeBlockIndexes.TryGetValue(0, out lengthRange)) { int blockIndex = lengthRange.First.Value; m_FreeBlockIndexes.Remove(0, blockIndex); return(blockIndex); } if (m_BlockDatas.Count < m_HeaderData.MaxBlockCount) { int blockIndex = m_BlockDatas.Count; m_BlockDatas.Add(BlockData.Empty); WriteHeaderData(); return(blockIndex); } return(-1); }
private int AllocBlock(int length) { if (length <= 0) { return(GetEmptyBlockIndex()); } length = (int)GetUpBoundClusterOffset(length); int lengthFound = -1; CFLinkedListRange <int> lengthRange = default(CFLinkedListRange <int>); foreach (KeyValuePair <int, CFLinkedListRange <int> > i in m_FreeBlockIndexes) { if (i.Key < length) { continue; } if (lengthFound >= 0 && lengthFound < i.Key) { continue; } lengthFound = i.Key; lengthRange = i.Value; } if (lengthFound >= 0) { if (lengthFound > length && m_BlockDatas.Count >= m_HeaderData.MaxBlockCount) { return(-1); } int blockIndex = lengthRange.First.Value; m_FreeBlockIndexes.Remove(lengthFound, blockIndex); if (lengthFound > length) { BlockData blockData = m_BlockDatas[blockIndex]; m_BlockDatas[blockIndex] = new BlockData(blockData.ClusterIndex, length); WriteBlockData(blockIndex); int deltaLength = lengthFound - length; int anotherBlockIndex = GetEmptyBlockIndex(); m_BlockDatas[anotherBlockIndex] = new BlockData(blockData.ClusterIndex + GetUpBoundClusterCount(length), deltaLength); m_FreeBlockIndexes.Add(deltaLength, anotherBlockIndex); WriteBlockData(anotherBlockIndex); } return(blockIndex); } else { int blockIndex = GetEmptyBlockIndex(); if (blockIndex < 0) { return(-1); } long fileLength = m_Stream.Length; try { m_Stream.SetLength(fileLength + length); } catch { return(-1); } m_BlockDatas[blockIndex] = new BlockData(GetUpBoundClusterCount(fileLength), length); WriteBlockData(blockIndex); return(blockIndex); } }