This describes a resource format.
To implement creating, CanCreate and Create need to be overloaded. To implement loading, CanLoad, LoadMatch, and Load need to be overloaded; LoadTypesMutable should be filled or provided in the constructor (if CanLoad is true and no types are provided in the constructor, the PrimaryType is automatically added). To implement saving, CanSave, Save, and SaveCheck need to be overloaded; SaveTypesMutable should be filled or provided in the constructor (if CanSave is true and no types are provided in the constructor, the PrimaryType is automatically added).
Inheritance: PluginAsset
Exemplo n.º 1
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));
        }
Exemplo n.º 2
0
 /// <summary>Add a format to the collection of formats.</summary>
 /// <param name="format"></param>
 protected void AddFormat(AssetFormat format)
 {
     if (format == null)
     {
         throw new ArgumentNullException("format");
     }
     if (format.Plugin != Plugin || FormatsMutable.Contains(format))
     {
         throw new ArgumentException(format.Name + " cannot be added to " + Name + ".");
     }
     FormatsMutable.Add(format);
 }
Exemplo n.º 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;
            }));
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 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));
 }
Exemplo n.º 6
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));
 }
Exemplo n.º 7
0
 /// <summary>Add a format to the collection of formats.</summary>
 /// <param name="format"></param>
 protected void AddFormat(AssetFormat format)
 {
     if (format == null)
         throw new ArgumentNullException("format");
     if (format.Plugin != Plugin || FormatsMutable.Contains(format))
         throw new ArgumentException(format.Name + " cannot be added to " + Name + ".");
     FormatsMutable.Add(format);
 }