Exemple #1
0
        /// <summary>Find the best <see cref="AssetFormat"/> for loading an <see cref="Asset"/>.</summary>
        /// <param name="matchStrength"></param>
        /// <param name="loader"></param>
        /// <param name="formats"></param>
        /// <param name="resolveConflict"></param>
        /// <returns></returns>
        public static AssetFormat LoadMatchAsset(out LoadMatchStrength matchStrength, AssetLoader loader, IEnumerable <AssetFormat> formats, ResolveLoadConflictCallback resolveConflict = null)
        {
            if (loader == null)
            {
                throw new ArgumentNullException("context");
            }
            if (formats == null)
            {
                throw new ArgumentNullException("formats");
            }

            AssetFormat       bestFormat = null;
            LoadMatchStrength bestMatch  = LoadMatchStrength.None;
            bool bestConflict            = false;

            // Attempt to find the one best loader.
            foreach (AssetFormat format in formats)
            {
                if (!format.CanLoad || !format.IsEnabled)
                {
                    continue;
                }

                LoadMatchStrength match = format.LoadMatch(loader);
                loader.Reset();
                if (match <= LoadMatchStrength.None || match < bestMatch)
                {
                    continue;
                }

                if (match == bestMatch)
                {
                    bestConflict = true;
                }
                else
                {
                    bestFormat   = format;
                    bestMatch    = match;
                    bestConflict = false;
                }
            }

            matchStrength = bestMatch;

            // If a single best loader is found, return it.
            if (!bestConflict)
            {
                if (bestFormat == null)
                {
                    throw new InvalidDataException("No loader could be found for " + loader.Name + ".");
                }

                return(bestFormat);
            }

            // Otherwise there are multiple formats with equal match strengths; gather those together.
            List <AssetFormat> conflicts = new List <AssetFormat>();

            foreach (AssetFormat format in formats)
            {
                if (!format.CanLoad || !format.IsEnabled)
                {
                    continue;
                }

                loader.Reset();
                LoadMatchStrength match = format.LoadMatch(loader);
                if (match == bestMatch)
                {
                    conflicts.Add(format);
                }
            }

            // Attempt to resolve the conflict.
            bestFormat = null;
            if (resolveConflict != null)
            {
                bestFormat = resolveConflict(loader, conflicts, matchStrength);
            }

            // If no resolution is found, throw an exception.
            if (bestFormat == null)
            {
                throw CreateConflictException(loader, matchStrength, conflicts);
            }

            // A resolution was found, so return the best format.
            return(bestFormat);
        }
Exemple #2
0
        /// <summary>Load an <see cref="Asset"/>.</summary>
        /// <param name="loader"></param>
        /// <param name="formats"></param>
        /// <param name="resolveConflict"></param>
        /// <returns></returns>
        public static Asset LoadAsset(AssetLoader loader, IEnumerable <AssetFormat> formats, ResolveLoadConflictCallback resolveConflict = null)
        {
            LoadMatchStrength matchStrength;
            AssetFormat       format = LoadMatchAsset(out matchStrength, loader, formats, resolveConflict);

            if (loader.Context != null)
            {
                loader.Context.LoadErrors = loader.Errors;
            }
            return(format.Load(loader));
        }
Exemple #3
0
        /// <summary>Load an <see cref="Asset"/>.</summary>
        /// <param name="loader"></param>
        /// <param name="formats"></param>
        /// <param name="resolveConflict"></param>
        /// <param name="progress"></param>
        /// <param name="progressUpdateRate"></param>
        /// <returns></returns>
        public static Task <Asset> LoadAssetAsync(AssetLoader loader, IEnumerable <AssetFormat> formats, ResolveLoadConflictCallback resolveConflict = null, AssetLoaderProgressCallback progress = null, TimeSpan?progressUpdateRate = null)
        {
            TimeSpan          progressUpdateRateValue = progressUpdateRate.GetValueOrDefault(TimeSpan.FromSeconds(0.1));
            LoadMatchStrength matchStrength;
            AssetFormat       format = LoadMatchAsset(out matchStrength, loader, formats, resolveConflict);

            if (loader.Context != null)
            {
                loader.Context.LoadErrors = loader.Errors;
            }

            return(new Task <Asset>(() => {
                Asset asset = null;
                bool complete = false;

                Thread loadThread = new Thread(() => {
                    asset = format.Load(loader);
                    complete = true;
                });

                loadThread.Start();

                while (!complete)
                {
                    if (!loadThread.IsAlive)
                    {
                        throw new InvalidOperationException("The load operation failed.");
                    }
                    Thread.Sleep(progressUpdateRateValue);
                    if (progress != null)
                    {
                        progress.Invoke(loader);
                    }
                }

                return asset;
            }));
        }
Exemple #4
0
 /// <summary>Load an <see cref="Asset"/></summary>
 /// <param name="reader"></param>
 /// <param name="name"></param>
 /// <param name="fileManager">The <see cref="FileManager"/> to use. If <c>null</c> (the default), the system file manager is used.</param>
 /// <param name="context"></param>
 /// <param name="conflictResolver"></param>
 /// <returns></returns>
 public Asset Load(BinaryReader reader, string name, FileManager fileManager = null, Asset context = null, ResolveLoadConflictCallback conflictResolver = null)
 {
     return(AssetFormat.LoadAsset(new AssetLoader(this, reader, name, fileManager, context), AllEnabledFormats, conflictResolver));
 }
Exemple #5
0
 /// <summary>Load an <see cref="Asset"/> asynchronously.</summary>
 /// <param name="reader"></param>
 /// <param name="name"></param>
 /// <param name="fileManager">The <see cref="FileManager"/> to use. If <c>null</c> (the default), the system file manager is used.</param>
 /// <param name="context"></param>
 /// <param name="conflictResolver"></param>
 /// <param name="progressMonitor"></param>
 /// <param name="progressUpdateRate"></param>
 /// <returns></returns>
 public Task <Asset> LoadAsync(BinaryReader reader, string name, FileManager fileManager, Asset context = null, ResolveLoadConflictCallback conflictResolver = null, AssetLoaderProgressCallback progressMonitor = null, TimeSpan?progressUpdateRate = null)
 {
     return(AssetFormat.LoadAssetAsync(new AssetLoader(this, reader, name, fileManager, context), AllEnabledFormats, conflictResolver, progressMonitor, progressUpdateRate));
 }
Exemple #6
0
 /// <summary>Load an <see cref="Asset"/> asynchronously.</summary>
 /// <param name="stream"></param>
 /// <param name="name"></param>
 /// <param name="fileManager">The <see cref="FileManager"/> to use. If <c>null</c> (the default), the system file manager is used.</param>
 /// <param name="context"></param>
 /// <param name="byteOrder"></param>
 /// <param name="conflictResolver"></param>
 /// <param name="progressMonitor"></param>
 /// <param name="progressUpdateRate"></param>
 /// <returns></returns>
 public Task <Asset> LoadAsync(Stream stream, string name, FileManager fileManager = null, Asset context = null, ByteOrder byteOrder = ByteOrder.LittleEndian, ResolveLoadConflictCallback conflictResolver = null, AssetLoaderProgressCallback progressMonitor = null, TimeSpan?progressUpdateRate = null)
 {
     return(LoadAsync(BigEndianBinaryReader.Create(byteOrder, stream), name, fileManager, context, conflictResolver, progressMonitor, progressUpdateRate));
 }
Exemple #7
0
        /// <summary>Load an <see cref="Asset"/> from a file asynchronously.</summary>
        /// <param name="path"></param>
        /// <param name="fileManager">The <see cref="FileManager"/> to use. If <c>null</c> (the default), the system file manager is used.</param>
        /// <param name="context"></param>
        /// <param name="byteOrder"></param>
        /// <param name="conflictResolver"></param>
        /// <param name="progressMonitor"></param>
        /// <param name="progressUpdateRate"></param>
        /// <returns></returns>
        public Task <Asset> LoadFileAsync(string path, FileManager fileManager = null, Asset context = null, ByteOrder byteOrder = ByteOrder.LittleEndian, ResolveLoadConflictCallback conflictResolver = null, AssetLoaderProgressCallback progressMonitor = null, TimeSpan?progressUpdateRate = null)
        {
            if (fileManager == null)
            {
                fileManager = FileManager.System;
            }
            Stream stream = fileManager.OpenRead(path);

            return(LoadAsync(stream, path, fileManager, context, byteOrder, conflictResolver, progressMonitor, progressUpdateRate));
        }
        /// <summary>Find the best <see cref="AssetFormat"/> for loading an <see cref="Asset"/>.</summary>
        /// <param name="matchStrength"></param>
        /// <param name="loader"></param>
        /// <param name="formats"></param>
        /// <param name="resolveConflict"></param>
        /// <returns></returns>
        public static AssetFormat LoadMatchAsset(out LoadMatchStrength matchStrength, AssetLoader loader, IEnumerable<AssetFormat> formats, ResolveLoadConflictCallback resolveConflict = null)
        {
            if (loader == null)
                throw new ArgumentNullException("context");
            if (formats == null)
                throw new ArgumentNullException("formats");

            AssetFormat bestFormat = null;
            LoadMatchStrength bestMatch = LoadMatchStrength.None;
            bool bestConflict = false;

            // Attempt to find the one best loader.
            foreach (AssetFormat format in formats) {
                if (!format.CanLoad || !format.IsEnabled)
                    continue;

                LoadMatchStrength match = format.LoadMatch(loader);
                loader.Reset();
                if (match <= LoadMatchStrength.None || match < bestMatch)
                    continue;

                if (match == bestMatch)
                    bestConflict = true;
                else {
                    bestFormat = format;
                    bestMatch = match;
                    bestConflict = false;
                }
            }

            matchStrength = bestMatch;

            // If a single best loader is found, return it.
            if (!bestConflict) {
                if (bestFormat == null)
                    throw new InvalidDataException("No loader could be found for " + loader.Name + ".");

                return bestFormat;
            }

            // Otherwise there are multiple formats with equal match strengths; gather those together.
            List<AssetFormat> conflicts = new List<AssetFormat>();

            foreach (AssetFormat format in formats) {
                if (!format.CanLoad || !format.IsEnabled)
                    continue;

                loader.Reset();
                LoadMatchStrength match = format.LoadMatch(loader);
                if (match == bestMatch)
                    conflicts.Add(format);
            }

            // Attempt to resolve the conflict.
            bestFormat = null;
            if (resolveConflict != null)
                bestFormat = resolveConflict(loader, conflicts, matchStrength);

            // If no resolution is found, throw an exception.
            if (bestFormat == null)
                throw CreateConflictException(loader, matchStrength, conflicts);

            // A resolution was found, so return the best format.
            return bestFormat;
        }
        /// <summary>Load an <see cref="Asset"/>.</summary>
        /// <param name="loader"></param>
        /// <param name="formats"></param>
        /// <param name="resolveConflict"></param>
        /// <param name="progress"></param>
        /// <param name="progressUpdateRate"></param>
        /// <returns></returns>
        public static Task<Asset> LoadAssetAsync(AssetLoader loader, IEnumerable<AssetFormat> formats, ResolveLoadConflictCallback resolveConflict = null, AssetLoaderProgressCallback progress = null, TimeSpan? progressUpdateRate = null)
        {
            TimeSpan progressUpdateRateValue = progressUpdateRate.GetValueOrDefault(TimeSpan.FromSeconds(0.1));
            LoadMatchStrength matchStrength;
            AssetFormat format = LoadMatchAsset(out matchStrength, loader, formats, resolveConflict);
            if (loader.Context != null)
                loader.Context.LoadErrors = loader.Errors;

            return new Task<Asset>(() => {
                Asset asset = null;
                bool complete = false;

                Thread loadThread = new Thread(() => {
                    asset = format.Load(loader);
                    complete = true;
                });

                loadThread.Start();

                while (!complete) {
                    if (!loadThread.IsAlive)
                        throw new InvalidOperationException("The load operation failed.");
                    Thread.Sleep(progressUpdateRateValue);
                    if (progress != null)
                        progress.Invoke(loader);
                }

                return asset;
            });
        }
 /// <summary>Load an <see cref="Asset"/>.</summary>
 /// <param name="loader"></param>
 /// <param name="formats"></param>
 /// <param name="resolveConflict"></param>
 /// <returns></returns>
 public static Asset LoadAsset(AssetLoader loader, IEnumerable<AssetFormat> formats, ResolveLoadConflictCallback resolveConflict = null)
 {
     LoadMatchStrength matchStrength;
     AssetFormat format = LoadMatchAsset(out matchStrength, loader, formats, resolveConflict);
     if (loader.Context != null)
         loader.Context.LoadErrors = loader.Errors;
     return format.Load(loader);
 }