Example #1
0
        public void AddRemoveRegistryContents()
        {
            NetworkableIdRegistry registry = new NetworkableIdRegistry(typeof(DummyClass1));

            DummyClass1 obj1 = new DummyClass1();
            DummyClass1 obj2 = new DummyClass1();

            // Add a pair of items to registry
            Assert.DoesNotThrow(() => registry.Add(obj1));
            Assert.DoesNotThrow(() => registry.Add(obj2));

            // Once added, the item is assigned an ID, and translations to/from ID match
            int id1 = registry.ToId(obj1);

            Assert.That(registry.FromId(id1), Is.EqualTo(obj1));

            // Adding the same object twice is not allowed
            Assert.Throws <ArgumentException>(() => registry.Add(obj1));

            // Remove item from registry
            registry.Remove(obj1);

            // Once removed, the item is no longer available for lookup
            Assert.Throws <ArgumentException>(() => registry.ToId(obj1));

            // Multiple removal of the same object is not allowed
            Assert.Throws <ArgumentException>(() => registry.Remove(obj1));
        }
Example #2
0
    /// <summary>
    /// Translate ID->item. The item must already exist in the registry.
    /// If the item happens to be of an incompatible type (the user has effectively requested an unsupported cast), the implementation will assert.
    /// </summary>
    public static T FromId(int id)
    {
        Assert.IsNotNull(rootRegistry, "NetworkableId<" + typeof(T).Name + "> registry has not yet been initialized");
        object obj = rootRegistry.FromId(id);

        Assert.IsNotNull(obj, "Object with id " + id + " cannot be found in registry for root type " + rootRegistry.Type.Name);
        T result = obj as T;

        Assert.IsNotNull(result, "Object with id " + id + " is of type " + obj.GetType().Name + " -- this cannot be casted to type " + typeof(T).GetType().Name);
        return(result);
    }