Пример #1
0
        public void TestWithPackage()
        {
            var inputs = new List <AssetItem>();

            var asset = new AssetObjectTest();

            var package = new Package();

            package.Assets.Add(new AssetItem("0", asset));
            var session = new PackageSession(package);

            for (int i = 0; i < 10; i++)
            {
                var newAsset = new AssetObjectTest()
                {
                    Id = asset.Id, Reference = new AssetReference(asset.Id, "bad")
                };
                inputs.Add(new AssetItem("0", newAsset));
            }

            // Tries to use existing ids
            var outputs = new List <AssetItem>();

            AssetCollision.Clean(null, inputs, outputs, AssetResolver.FromPackage(package), true, false);

            // Make sure we are generating exactly the same number of elements
            Assert.Equal(inputs.Count, outputs.Count);

            // Make sure that asset has been cloned
            Assert.NotEqual(inputs[0], outputs[0]);

            // First Id should not change
            Assert.NotEqual(inputs[0].Id, outputs[0].Id);

            // Make sure that all ids are different
            var ids = new HashSet <AssetId>(outputs.Select(item => item.Id));

            Assert.Equal(inputs.Count, ids.Count);

            // Make sure that all locations are different
            var locations = new HashSet <UFile>(outputs.Select(item => item.Location));

            Assert.Equal(inputs.Count, locations.Count);

            // Reference location "bad"should be fixed to "0_1" pointing to the first element
            foreach (var output in outputs)
            {
                // Make sure of none of the locations are using "0"
                Assert.NotEqual((UFile)"0", output.Location);

                var assetRef = ((AssetObjectTest)output.Asset).Reference;
                Assert.Equal("0 (2)", assetRef.Location);
                Assert.Equal(outputs[0].Id, assetRef.Id);
            }
        }
Пример #2
0
        public void TestSimpleNewGuids()
        {
            var inputs = new List <AssetItem>();

            var asset = new AssetObjectTest();

            for (int i = 0; i < 10; i++)
            {
                var newAsset = new AssetObjectTest()
                {
                    Id = asset.Id, Reference = new AssetReference(asset.Id, "bad")
                };
                inputs.Add(new AssetItem("0", newAsset));
            }

            // Force to use new ids
            var outputs = new List <AssetItem>();

            AssetCollision.Clean(null, inputs, outputs, new AssetResolver()
            {
                AlwaysCreateNewId = true
            }, true, false);

            // Make sure we are generating exactly the same number of elements
            Assert.Equal(inputs.Count, outputs.Count);

            // Make sure that asset has been cloned
            Assert.NotEqual(inputs[0], outputs[0]);

            // First Id should not change
            Assert.NotEqual(inputs[0].Id, outputs[0].Id);

            // Make sure that all ids are different
            var ids = new HashSet <AssetId>(outputs.Select(item => item.Id));

            Assert.Equal(inputs.Count, ids.Count);

            // Make sure that all locations are different
            var locations = new HashSet <UFile>(outputs.Select(item => item.Location));

            Assert.Equal(inputs.Count, locations.Count);

            // Reference location "bad"should be fixed to "0"
            foreach (var output in outputs)
            {
                var assetRef = ((AssetObjectTest)output.Asset).Reference;
                Assert.Equal("0", assetRef.Location);
                Assert.Equal(outputs[0].Id, assetRef.Id);
            }
        }
Пример #3
0
        public List <AssetViewModel> PasteAssets(List <AssetItem> assets, [CanBeNull] ProjectViewModel project)
        {
            var viewModels = new List <AssetViewModel>();

            // Don't touch the action stack in this case.
            if (assets.Count == 0)
            {
                return(viewModels);
            }

            var fixedAssets = new List <AssetItem>();

            using (var transaction = UndoRedoService.CreateTransaction())
            {
                // Clean collision by renaming pasted asset if an asset with the same name already exists in that location.
                AssetCollision.Clean(null, assets, fixedAssets, AssetResolver.FromPackage(Package), false, false);

                // Temporarily add the new asset to the package
                fixedAssets.ForEach(x => Package.Assets.Add(x));

                // Find which assets are referencing the pasted assets in order to fix the reference link.
                var assetsToFix = GetReferencers(Session.DependencyManager, Session, fixedAssets);

                // Remove temporarily added assets - they will be properly re-added with the correct action stack entry when creating the view model
                fixedAssets.ForEach(x => Package.Assets.Remove(x));

                // Create directories and view models, actually add assets to package.
                foreach (var asset in fixedAssets)
                {
                    var location       = asset.Location.GetFullDirectory() ?? "";
                    var assetDirectory = project == null?
                                         GetOrCreateAssetDirectory(location, true) :
                                             project.GetOrCreateProjectDirectory(location, true);

                    var assetViewModel = CreateAsset(assetDirectory, asset, true, null);
                    viewModels.Add(assetViewModel);
                }

                // Fix references in the assets that references what we pasted.
                // We wrap this operation in an action item so the action stack can properly re-execute it.
                var fixReferencesAction = new FixAssetReferenceOperation(assetsToFix, false, true);
                fixReferencesAction.FixAssetReferences();
                UndoRedoService.PushOperation(fixReferencesAction);

                UndoRedoService.SetName(transaction, "Paste assets");
            }
            return(viewModels);
        }
Пример #4
0
        public void ValidateAssets(bool alwaysGenerateNewAssetId = false)
        {
            if (TemporaryAssets.Count == 0)
            {
                return;
            }

            try
            {
                // Make sure we are suspending notifications before updating all assets
                Assets.SuspendCollectionChanged();

                Assets.Clear();

                // Get generated output items
                var outputItems = new AssetItemCollection();

                // Create a resolver from the package
                var resolver = AssetResolver.FromPackage(this);
                resolver.AlwaysCreateNewId = alwaysGenerateNewAssetId;

                // Clean assets
                AssetCollision.Clean(TemporaryAssets, outputItems, resolver, false);

                // Add them back to the package
                foreach (var item in outputItems)
                {
                    Assets.Add(item);
                }

                TemporaryAssets.Clear();
            }
            finally
            {
                // Restore notification on assets
                Assets.ResumeCollectionChanged();
            }
        }