예제 #1
0
 public static void SetAssetLoader <T>(IAssetLoader <T> loader, bool storeInCache = true)
 {
     Loaders[typeof(T)] = new LoaderInfo(loader, storeInCache);
 }
예제 #2
0
        public T Load <T>(string assetName)
        {
            var type = typeof(T);

            assetName = assetName.Replace('\\', SeparatorSymbol);

            Dictionary <string, object> cache;

            if (_cache.TryGetValue(type, out cache))
            {
                object cached;
                if (cache.TryGetValue(assetName, out cached))
                {
                    // Found in cache
                    return((T)cached);
                }
            }

            LoaderInfo loaderBase;

            if (!Loaders.TryGetValue(type, out loaderBase))
            {
                // Try determine it using AssetLoader attribute
                var attr = type.FindAttribute <AssetLoaderAttribute>();
                if (attr == null)
                {
                    throw new Exception(string.Format("Unable to resolve AssetLoader for type {0}", type.Name));
                }

                // Create loader
                loaderBase = new LoaderInfo(Activator.CreateInstance(attr.AssetLoaderType),
                                            attr.StoreInCache);

                // Save in the _loaders
                Loaders[type] = loaderBase;
            }

            var loader = (IAssetLoader <T>)loaderBase.Loader;

            var baseFolder    = string.Empty;
            var assetFileName = assetName;

            var separatorIndex = assetName.LastIndexOf(SeparatorSymbol);

            if (separatorIndex != -1)
            {
                baseFolder    = assetName.Substring(0, separatorIndex);
                assetFileName = assetName.Substring(separatorIndex + 1);
            }

            var context = new AssetLoaderContext(this, baseFolder);
            var result  = loader.Load(context, assetFileName);

            if (loaderBase.StoreInCache)
            {
                // Store in cache
                if (cache == null)
                {
                    cache        = new Dictionary <string, object>();
                    _cache[type] = cache;
                }

                cache[assetName] = result;
            }

            return(result);
        }