コード例 #1
0
        /// <summary>
        /// Sets Reference.Object, and updates loadedAssetByUrl collection.
        /// </summary>
        internal void SetAssetObject(Reference reference, object obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            if (reference.Object != null)
            {
                if (reference.Object != obj)
                {
                    throw new InvalidOperationException("SetAssetObject has already been called with a different object");
                }

                return;
            }

            var url = reference.Url;

            reference.Object = obj;

            lock (LoadedAssetUrls)
            {
                Reference previousReference;

                if (LoadedAssetUrls.TryGetValue(url, out previousReference))
                {
                    reference.Next = previousReference.Next;
                    reference.Prev = previousReference;

                    if (previousReference.Next != null)
                    {
                        previousReference.Next.Prev = reference;
                    }
                    previousReference.Next = reference;
                }
                else
                {
                    LoadedAssetUrls[url] = reference;
                }

                LoadedAssetReferences[obj] = reference;

                // TODO: Currently here so that reference.ObjectValue later keeps its Url.
                // Need some reorganization?
                AttachedReferenceManager.SetUrl(obj, reference.Url);
            }
        }
コード例 #2
0
        public void LifetimeShared()
        {
            var c1 = new C {
                I = 16
            };
            var c2 = new C {
                I = 18
            };

            c1.Child = new C {
                I = 32
            };
            c2.Child = c1.Child;

            AttachedReferenceManager.SetUrl(c1.Child, "cchild");

            var databaseProvider = CreateDatabaseProvider();
            var assetManager1    = new ContentManager(databaseProvider);
            var assetManager2    = new ContentManager(databaseProvider);

            assetManager1.Save("c1", c1);
            assetManager1.Save("c2", c2);

            var c1Copy      = assetManager2.Load <C>("c1");
            var c2Copy      = assetManager2.Load <C>("c2");
            var c1ChildCopy = assetManager2.Load <C>("cchild");

            assetManager2.Unload(c1Copy);

            // Check that everything is properly unloaded
            Assert.Equal(0, ((IReferencable)c1Copy).ReferenceCount);
            Assert.Equal(1, ((IReferencable)c2Copy).ReferenceCount);
            Assert.Equal(1, ((IReferencable)c1ChildCopy).ReferenceCount);

            assetManager2.Unload(c2Copy);

            // Check that everything is properly unloaded
            Assert.Equal(0, ((IReferencable)c2Copy).ReferenceCount);
            Assert.Equal(1, ((IReferencable)c1ChildCopy).ReferenceCount);

            assetManager2.Unload(c1ChildCopy);

            // Check that everything is properly unloaded
            Assert.Equal(0, ((IReferencable)c1ChildCopy).ReferenceCount);
        }
コード例 #3
0
        public void LifetimeShared()
        {
            var c1 = new C {
                I = 16
            };
            var c2 = new C {
                I = 18
            };

            c1.Child = new C {
                I = 32
            };
            c2.Child = c1.Child;

            AttachedReferenceManager.SetUrl(c1.Child, "cchild");

            var assetManager1 = new AssetManager();
            var assetManager2 = new AssetManager();

            assetManager1.Save("c1", c1);
            assetManager1.Save("c2", c2);

            var c1Copy      = assetManager2.Load <C>("c1");
            var c2Copy      = assetManager2.Load <C>("c2");
            var c1ChildCopy = assetManager2.Load <C>("cchild");

            assetManager2.Unload(c1Copy);

            // Check that everything is properly unloaded
            Assert.That(((IReferencable)c1Copy).ReferenceCount, Is.EqualTo(0));
            Assert.That(((IReferencable)c2Copy).ReferenceCount, Is.EqualTo(1));
            Assert.That(((IReferencable)c1ChildCopy).ReferenceCount, Is.EqualTo(1));

            assetManager2.Unload(c2Copy);

            // Check that everything is properly unloaded
            Assert.That(((IReferencable)c2Copy).ReferenceCount, Is.EqualTo(0));
            Assert.That(((IReferencable)c1ChildCopy).ReferenceCount, Is.EqualTo(1));

            assetManager2.Unload(c1ChildCopy);

            // Check that everything is properly unloaded
            Assert.That(((IReferencable)c1ChildCopy).ReferenceCount, Is.EqualTo(0));
        }