Exemplo n.º 1
0
        private static void ImportMaterials(List <AssetItem> assetReferences, Dictionary <string, MaterialAsset> materials)
        {
            if (materials != null)
            {
                var loadedTextures = assetReferences.Where(x => x.Asset is TextureAsset).ToList();

                foreach (var materialKeyValue in materials)
                {
                    AdjustForTransparency(materialKeyValue.Value);
                    var material = materialKeyValue.Value;

                    // patch texture name and ids
                    var materialAssetReferences = AssetReferenceAnalysis.Visit(material);
                    foreach (var materialAssetReferenceLink in materialAssetReferences)
                    {
                        var materialAssetReference = materialAssetReferenceLink.Reference as IContentReference;
                        if (materialAssetReference == null)
                        {
                            continue;
                        }

                        // texture location is #nameOfTheModel_#nameOfTheTexture at this point in the material
                        var foundTexture = loadedTextures.FirstOrDefault(x => x.Location == materialAssetReference.Location);
                        if (foundTexture != null)
                        {
                            materialAssetReferenceLink.UpdateReference(foundTexture.Id, foundTexture.Location);
                        }
                    }

                    var assetReference = new AssetItem(materialKeyValue.Key, material);
                    assetReferences.Add(assetReference);
                }
            }
        }
Exemplo n.º 2
0
        public sealed override bool Run(AssetTemplateGeneratorParameters parameters)
        {
            var assets = CreateAssets(parameters)?.ToList();

            if (assets == null)
            {
                parameters.Logger.Error("No asset created by the asset factory.");
                return(false);
            }

            // Step one: add assets to package with proper unique name
            var newAssets = new Dictionary <AssetId, AssetItem>();

            foreach (var asset in assets)
            {
                if (string.IsNullOrEmpty(asset.Location))
                {
                    throw new InvalidOperationException($"Asset returned by {nameof(CreateAssets)} has no location. Use {nameof(GenerateLocation)} to generate the location for each asset.");
                }

                // Ensure unicity of names amongst package
                var name = NamingHelper.ComputeNewName(asset.Location, x => parameters.Package.Assets.Find(x) != null, "{0}_{1}");
                var item = new AssetItem(name, asset.Asset);

                try
                {
                    parameters.Package.Assets.Add(item);
                    // Ensure the dirty flag is properly set to refresh the dependency manager
                    item.IsDirty = true;
                }
                catch (Exception ex)
                {
                    parameters.Logger.Error("Failed to create new asset from template.", ex);
                    return(false);
                }
                newAssets.Add(item.Id, item);
            }

            // Step two: fix references in the newly added assets
            foreach (var asset in newAssets)
            {
                var referencesToFix = AssetReferenceAnalysis.Visit(asset.Value);
                foreach (var assetReferenceLink in referencesToFix)
                {
                    var refToUpdate = assetReferenceLink.Reference as IReference;
                    if (refToUpdate == null)
                    {
                        continue;
                    }

                    AssetItem realItem;
                    // Look for the referenced asset in the new assets
                    if (newAssets.TryGetValue(refToUpdate.Id, out realItem))
                    {
                        assetReferenceLink.UpdateReference(realItem.Id, realItem.Location);
                    }
                    else
                    {
                        // If not found, try on the already existing assets
                        realItem = parameters.Package.Session.FindAsset(refToUpdate.Id);
                        assetReferenceLink.UpdateReference(realItem?.Id ?? AssetId.Empty, realItem?.Location);
                    }
                }
            }

            // Step three: complete creation and mark them as dirty
            foreach (var asset in newAssets.Values)
            {
                try
                {
                    PostAssetCreation(parameters, asset);
                    // Ensure the dirty flag is properly set to refresh the dependency manager
                    asset.IsDirty = true;
                }
                catch (Exception ex)
                {
                    parameters.Logger.Error("Failed to create new asset from template.", ex);
                    return(false);
                }
            }
            return(true);
        }