예제 #1
0
        static void TrimTestSub(bool trackFree)
        {
            const int size = 1000;

            var objs = new object[size];

            for (var i = 0; i < size / 2; i++)
            {
                if ((i % 3) == 0)
                {
                    objs[i] = i.ToString();
                }
            }

            var d = new DArray <object>(size, trackFree);

            for (var i = 0; i < size; i++)
            {
                if (objs[i] != null)
                {
                    d[i] = objs[i];
                }
            }

            d.Trim();

            // Make sure our data has not changed
            for (var i = 0; i < size; i++)
            {
                if (objs[i] != null)
                {
                    Assert.AreSame(objs[i], d[i], "TrackFree = " + trackFree);
                }
            }

            // Make sure the null slots are still null
            for (var i = 0; i < d.Length; i++)
            {
                Assert.AreSame(objs[i], d[i], "TrackFree = " + trackFree);
            }

            // Make sure that inserts first fill up the gaps, THEN expand
            var startLen = d.Length;
            var gaps     = startLen - d.Count;

            for (var i = 0; i < gaps; i++)
            {
                d.Insert(new object());
            }

            Assert.AreEqual(startLen, d.Length, "TrackFree = " + trackFree);

            // Make sure we start expanding now
            for (var i = 0; i < 10; i++)
            {
                var before = d.Length;
                d.Insert(new object());
                Assert.AreEqual(before + 1, d.Length, "TrackFree = " + trackFree);
            }
        }
예제 #2
0
        /// <summary>
        /// Creates a new <see cref="ActionDisplay"/> in this collection.
        /// </summary>
        /// <returns>The new <see cref="ActionDisplay"/>.</returns>
        public ActionDisplay CreateAction()
        {
            var id   = _items.NextFreeIndex();
            var item = new ActionDisplay(new ActionDisplayID(id));

            _items.Insert(id, item);
            return(item);
        }
 /// <summary>
 /// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1"/>.
 /// </summary>
 /// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
 /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/>
 /// is read-only.</exception>
 public void Add(IQuestDescription item)
 {
     if (_questDescriptions.CanGet(item.QuestID.GetRawValue()))
     {
         _questDescriptions[item.QuestID.GetRawValue()] = item;
     }
     else
     {
         _questDescriptions.Insert(item.QuestID.GetRawValue(), item);
     }
 }
예제 #4
0
        static void RemoveInsertTestSub(bool trackFree)
        {
            var d = new DArray <object>(trackFree);

            for (var i = 0; i < 10; i++)
            {
                d[i] = new object();
            }

            d.RemoveAt(0);
            d.RemoveAt(5);
            d.RemoveAt(6);
            d.RemoveAt(9);

            var usedIndices = new List <int>();

            for (var i = 0; i < 7; i++)
            {
                usedIndices.Add(d.Insert(new object()));
            }

            var expected = new int[] { 0, 5, 6, 9, 10, 11, 12 };

            Assert.AreEqual(usedIndices.Count(), expected.Length);

            foreach (var i in usedIndices)
            {
                Assert.IsTrue(expected.Contains(i), "TrackFree = " + trackFree);
            }
        }
        IMessageProcessor[] BuildMessageProcessors(object source)
        {
            const BindingFlags bindFlags =
                BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod |
                BindingFlags.Static;

            var mpdType  = typeof(MessageProcessorHandler);
            var atbType  = typeof(MessageHandlerAttribute);
            var voidType = typeof(void);

            var tmpProcessors = new DArray <IMessageProcessor>();

            // Search through all types in the Assembly
            var assemb = Assembly.GetAssembly(source.GetType());

            foreach (var type in assemb.GetTypes())
            {
                // Search through every method in the class
                foreach (var method in type.GetMethods(bindFlags))
                {
                    // Only accept a method if it returns a void
                    if (method.ReturnType != voidType)
                    {
                        continue;
                    }

                    // Get all of the MessageAttributes for the method (should only be one)
                    var atbs = (MessageHandlerAttribute[])method.GetCustomAttributes(atbType, true);
                    if (atbs.Length > 1)
                    {
                        const string errmsg = "Multiple MessageHandlerAttributes found for method `{0}`.";
                        Debug.Fail(string.Format(errmsg, method.Name));
                        throw new ArgumentException(string.Format(errmsg, method.Name), "source");
                    }

                    // Create the message processor for the method
                    foreach (var atb in atbs)
                    {
                        if (tmpProcessors.CanGet(atb.MsgID) && tmpProcessors[atb.MsgID] != null)
                        {
                            const string errmsg =
                                "A MessageHandlerAttribute with ID `{0}` already exists. Methods in question: {1} and {2}";
                            Debug.Fail(string.Format(errmsg, atb.MsgID, tmpProcessors[atb.MsgID].Call.Method, method));
                            throw new DuplicateKeyException(string.Format(errmsg, atb.MsgID, tmpProcessors[atb.MsgID].Call.Method,
                                                                          method));
                        }

                        var del = (MessageProcessorHandler)Delegate.CreateDelegate(mpdType, source, method);
                        Debug.Assert(del != null);
                        var msgProcessor = CreateMessageProcessor(atb, del);
                        tmpProcessors.Insert(atb.MsgID, msgProcessor);
                    }
                }
            }

            return(tmpProcessors.ToArray());
        }
예제 #6
0
        /// <summary>
        /// Loads all of the items.
        /// </summary>
        void LoadAll()
        {
            var ids = GetIDs();

            foreach (var id in ids)
            {
                var item = LoadItem(id);
                var i    = IDToInt(id);
                _items.Insert(i, item);

                if (log.IsDebugEnabled)
                {
                    log.DebugFormat("Loaded item `{0}` at index `{1}`.", item, i);
                }
            }

            _items.Trim();
        }
예제 #7
0
        IMessageProcessor[] BuildMessageProcessors(object source)
        {
            const BindingFlags bindFlags =
                BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod |
                BindingFlags.Static;

            var mpdType = typeof(MessageProcessorHandler);
            var atbType = typeof(MessageHandlerAttribute);
            var voidType = typeof(void);

            var tmpProcessors = new DArray<IMessageProcessor>();

            // Search through all types in the Assembly
            var assemb = Assembly.GetAssembly(source.GetType());
            foreach (var type in assemb.GetTypes())
            {
                // Search through every method in the class
                foreach (var method in type.GetMethods(bindFlags))
                {
                    // Only accept a method if it returns a void
                    if (method.ReturnType != voidType)
                        continue;

                    // Get all of the MessageAttributes for the method (should only be one)
                    var atbs = (MessageHandlerAttribute[])method.GetCustomAttributes(atbType, true);
                    if (atbs.Length > 1)
                    {
                        const string errmsg = "Multiple MessageHandlerAttributes found for method `{0}`.";
                        Debug.Fail(string.Format(errmsg, method.Name));
                        throw new ArgumentException(string.Format(errmsg, method.Name), "source");
                    }

                    // Create the message processor for the method
                    foreach (var atb in atbs)
                    {
                        if (tmpProcessors.CanGet(atb.MsgID) && tmpProcessors[atb.MsgID] != null)
                        {
                            const string errmsg =
                                "A MessageHandlerAttribute with ID `{0}` already exists. Methods in question: {1} and {2}";
                            Debug.Fail(string.Format(errmsg, atb.MsgID, tmpProcessors[atb.MsgID].Call.Method, method));
                            throw new DuplicateKeyException(string.Format(errmsg, atb.MsgID, tmpProcessors[atb.MsgID].Call.Method,
                                method));
                        }

                        var del = (MessageProcessorHandler)Delegate.CreateDelegate(mpdType, source, method);
                        Debug.Assert(del != null);
                        var msgProcessor = CreateMessageProcessor(atb, del);
                        tmpProcessors.Insert(atb.MsgID, msgProcessor);
                    }
                }
            }

            return tmpProcessors.ToArray();
        }
예제 #8
0
        static void TrimTestSub(bool trackFree)
        {
            const int size = 1000;

            var objs = new object[size];
            for (var i = 0; i < size / 2; i++)
            {
                if ((i % 3) == 0)
                    objs[i] = i.ToString();
            }

            var d = new DArray<object>(size, trackFree);
            for (var i = 0; i < size; i++)
            {
                if (objs[i] != null)
                    d[i] = objs[i];
            }

            d.Trim();

            // Make sure our data has not changed
            for (var i = 0; i < size; i++)
            {
                if (objs[i] != null)
                    Assert.AreSame(objs[i], d[i], "TrackFree = " + trackFree);
            }

            // Make sure the null slots are still null
            for (var i = 0; i < d.Length; i++)
            {
                Assert.AreSame(objs[i], d[i], "TrackFree = " + trackFree);
            }

            // Make sure that inserts first fill up the gaps, THEN expand
            var startLen = d.Length;
            var gaps = startLen - d.Count;
            for (var i = 0; i < gaps; i++)
            {
                d.Insert(new object());
            }

            Assert.AreEqual(startLen, d.Length, "TrackFree = " + trackFree);

            // Make sure we start expanding now
            for (var i = 0; i < 10; i++)
            {
                var before = d.Length;
                d.Insert(new object());
                Assert.AreEqual(before + 1, d.Length, "TrackFree = " + trackFree);
            }
        }
예제 #9
0
        static void RemoveInsertTestSub(bool trackFree)
        {
            var d = new DArray<object>(trackFree);

            for (var i = 0; i < 10; i++)
            {
                d[i] = new object();
            }

            d.RemoveAt(0);
            d.RemoveAt(5);
            d.RemoveAt(6);
            d.RemoveAt(9);

            var usedIndices = new List<int>();
            for (var i = 0; i < 7; i++)
            {
                usedIndices.Add(d.Insert(new object()));
            }

            var expected = new int[] { 0, 5, 6, 9, 10, 11, 12 };

            Assert.AreEqual(usedIndices.Count(), expected.Length);

            foreach (var i in usedIndices)
            {
                Assert.IsTrue(expected.Contains(i), "TrackFree = " + trackFree);
            }
        }