/// <summary>Get a string describing a match strength, which handles values in between like "Perfect-2" or "Strong+3".</summary> /// <param name="value"></param> /// <returns></returns> public static string MatchStrengthToString(LoadMatchStrength value) { if (value >= LoadMatchStrength.Perfect) { return(MatchStrengthToString(LoadMatchStrength.Perfect, value)); } if (value >= LoadMatchStrength.Comprehensive) { return(MatchStrengthToString(LoadMatchStrength.Comprehensive, value)); } if (value >= LoadMatchStrength.Strong) { return(MatchStrengthToString(LoadMatchStrength.Strong, value)); } if (value >= LoadMatchStrength.Medium) { return(MatchStrengthToString(LoadMatchStrength.Medium, value)); } if (value >= LoadMatchStrength.Weak) { return(MatchStrengthToString(LoadMatchStrength.Weak, value)); } if (value >= LoadMatchStrength.Generic) { return(MatchStrengthToString(LoadMatchStrength.Generic, value)); } if (value > LoadMatchStrength.None) { return(MatchStrengthToString(LoadMatchStrength.Fallback, value)); } return(MatchStrengthToString(LoadMatchStrength.None, value)); }
static InvalidDataException CreateConflictException(AssetLoader loader, LoadMatchStrength matchStrength, IList <AssetFormat> formats) { string text = string.Format("{3} {0} loaders matched {1} with strength {2}: {4}", typeof(AssetFormat).Name, loader.Name, MatchStrengthToString(matchStrength), formats.Count, string.Join(", ", (from i in formats select i.Name))); return(new InvalidDataException(text)); }
static string MatchStrengthToString(LoadMatchStrength reference, LoadMatchStrength value) { if (value < reference) { return(reference.ToString() + (int)(value - reference)); } else { return(reference + "+" + (int)(value - reference)); } }
/// <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); }
static string MatchStrengthToString(LoadMatchStrength reference, LoadMatchStrength value) { if (value < reference) return reference.ToString() + (int)(value - reference); else return reference + "+" + (int)(value - reference); }
static InvalidDataException CreateConflictException(AssetLoader loader, LoadMatchStrength matchStrength, IList<AssetFormat> formats) { string text = string.Format("{3} {0} loaders matched {1} with strength {2}: {4}", typeof(AssetFormat).Name, loader.Name, MatchStrengthToString(matchStrength), formats.Count, string.Join(", ", (from i in formats select i.Name))); return new InvalidDataException(text); }
/// <summary>Get a string describing a match strength, which handles values in between like "Perfect-2" or "Strong+3".</summary> /// <param name="value"></param> /// <returns></returns> public static string MatchStrengthToString(LoadMatchStrength value) { if (value >= LoadMatchStrength.Perfect) return MatchStrengthToString(LoadMatchStrength.Perfect, value); if (value >= LoadMatchStrength.Comprehensive) return MatchStrengthToString(LoadMatchStrength.Comprehensive, value); if (value >= LoadMatchStrength.Strong) return MatchStrengthToString(LoadMatchStrength.Strong, value); if (value >= LoadMatchStrength.Medium) return MatchStrengthToString(LoadMatchStrength.Medium, value); if (value >= LoadMatchStrength.Weak) return MatchStrengthToString(LoadMatchStrength.Weak, value); if (value >= LoadMatchStrength.Generic) return MatchStrengthToString(LoadMatchStrength.Generic, value); if (value > LoadMatchStrength.None) return MatchStrengthToString(LoadMatchStrength.Fallback, value); return MatchStrengthToString(LoadMatchStrength.None, value); }
/// <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; }