Exemplo n.º 1
0
 public static void SetAssetLoader <T>(IAssetLoader <T> loader, bool storeInCache = true)
 {
     _loaders[typeof(T)] = new LoaderInfo(loader, storeInCache);
 }
Exemplo n.º 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);
        }