private string GetTypeNameFromXaml(string resourcePath, UnmanagedMemoryStream unmanagedStream)
        {
            if (unmanagedStream == null)
            {
                return(null);
            }

#if ENGINEONLYBUILD || JUSTASSEMBLY
            return(null);
#else
            bool      isBaml  = resourcePath.EndsWith(".baml", StringComparison.OrdinalIgnoreCase);
            XDocument xamlDoc = null;

            if (isBaml)
            {
                try
                {
                    unmanagedStream.Seek(0, SeekOrigin.Begin);
                    xamlDoc = BamlToXamlConverter.DecompileToDocument(unmanagedStream, assemblyResolver, assemblyPath);
                }
                catch
                {
                    isBaml = false;
                    unmanagedStream.Seek(0, SeekOrigin.Begin);
                }
            }

            string fullClassName = null;
            if (isBaml)
            {
                XAttribute classAttribute = xamlDoc.Root.Attribute(XName.Get("Class", "http://schemas.microsoft.com/winfx/2006/xaml"));
                if (classAttribute != null && classAttribute.Name != null)
                {
                    fullClassName = classAttribute.Value;
                }
            }

            return(fullClassName);
#endif
        }
        private string TryWriteBamlResource(string resourcePath, bool isBamlResource, UnmanagedMemoryStream unmanagedStream)
        {
            if (unmanagedStream == null)
            {
                return(null);
            }

            string resourceDir = Path.GetDirectoryName(resourcePath);

            if (!Directory.Exists(resourceDir))
            {
                Directory.CreateDirectory(resourceDir);
            }

            Stream sourceStream;
            string fullClassName = null;

#if ENGINEONLYBUILD || JUSTASSEMBLY
            sourceStream = unmanagedStream;
#else
            XDocument xamlDoc = null;

            bool exceptionOccurred = false;
            //TODO: refactor
            if (isBamlResource)
            {
                sourceStream = new MemoryStream();
                try
                {
                    unmanagedStream.Seek(0, SeekOrigin.Begin);
                    xamlDoc = BamlToXamlConverter.DecompileToDocument(unmanagedStream, currentAssemblyResolver, assemblyPath);
#if !NET35
                    xamlDoc.Save(sourceStream);
#else
                    xamlDoc.Save(new StreamWriter(sourceStream));
#endif
                }
                catch (Exception ex)
                {
                    exceptionOccurred = true;
                    unmanagedStream.Seek(0, SeekOrigin.Begin);
                    sourceStream = unmanagedStream;

                    OnExceptionThrown(ex);
                }
            }
            else
            {
                sourceStream = unmanagedStream;
            }

            if (isBamlResource && !exceptionOccurred)
            {
                fullClassName = Utilities.GetXamlTypeFullName(xamlDoc);
                if (fullClassName != null)
                {
                    xamlGeneratedFields.Add(fullClassName, Utilities.GetXamlGeneratedFields(xamlDoc));
                }
            }
#endif

            using (FileStream fileStream = new FileStream(resourcePath, FileMode.Create, FileAccess.Write))
            {
                using (sourceStream)
                {
                    sourceStream.Seek(0, SeekOrigin.Begin);
                    sourceStream.CopyTo(fileStream);
                }
            }

            return(fullClassName);
        }