Exemplo n.º 1
0
        /// <summary>
        /// Locates the specified asset and opens a stream to read it using the resource locator
        /// override. If UseDefaultContent is set to true and the resource cannot be found, an
        /// attempt to locate it from default content will be made.
        /// </summary>
        /// <param name="assetName">Asset name</param>
        /// <param name="locatorOverride">Locator to attempt to locate the asset with</param>
        /// <returns>Stream</returns>
        public Stream OpenStream(String assetName, IResourceLocator locatorOverride)
        {
            if (String.IsNullOrEmpty(assetName))
            {
                throw new NullReferenceException("Asset name is not valid");
            }

            IResource resource = null;

            //First try: Try to locate the resource with the override
            resource = locatorOverride.LocateResource(assetName);

            //Second try, if resource is not located then try the default content
            if (resource == null)
            {
                if (!_useDefault)
                {
                    throw new ResourceNotFoundException(String.Format("Resource {0} could not be found", assetName));
                }
                try {
                    return(Engine.RenderSystemProvider.DefaultContent.OpenStream(assetName));
                } catch (Exception e) {
                    throw new ResourceNotFoundException(String.Format("Resource {0} could not be found", assetName), e.InnerException);
                }
            }

            return(resource.OpenStream());
        }
 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
 {
     return(_resourceLocator.LocateResource((string)value));
 }
Exemplo n.º 3
0
        /// <summary>
        /// Loads the asset using an overloaded resource locator. If UseDefaultContent is set to true, and if the asset cannot be found,
        /// an attempt to load it from the default content manager will be made.
        /// </summary>
        /// <typeparam name="T">Type of resource</typeparam>
        /// <param name="assetName">Asset name</param>
        /// <param name="parameters">Loader parameters</param>
        /// <param name="locatorOverride">Resource locator to use</param>
        /// <returns>Loaded asset, if it exists, otherwise null</returns>
        /// <exception cref="System.ObjectDisposedException">Thrown if the manager has been disposed</exception>
        /// <exception cref="System.ArgumentNullException">Thrown if the asset name or locator is null</exception>
        /// <exception cref="System.InvalidCastException">Thrown if the asset to load does not match return type</exception>
        /// <exception cref="Tesla.Content.ResourceNotFoundException">Thrown if resource could not be located</exception>
        /// <exception cref="System.InvalidOperationException">Thrown if no suitable loader registered to the manager</exception>
        /// <exception cref="Tesla.Core.TeslaException">Thrown if loader parameters are not valid.</exception>
        public T Load <T>(String assetName, LoaderParameters parameters, IResourceLocator locatorOverride)
        {
            if (_loadedAssets == null)
            {
                throw new ObjectDisposedException("Content manager is disposed");
            }

            if (String.IsNullOrEmpty(assetName))
            {
                throw new ArgumentNullException("Asset name is not valid");
            }

            IResourceLocator oldLocator = _resourceLocator;

            if (locatorOverride == null)
            {
                throw new ArgumentNullException("locatorOverride", "Resource locator override cannot be null");
            }
            _resourceLocator = locatorOverride;

            if (parameters == null)
            {
                parameters = LoaderParameters.None;
            }

            if (!parameters.ValidateParameters())
            {
                throw new TeslaException(String.Format("Error importing resource, {0} parameters are invalid.", parameters.GetType()));
            }

            Object    asset;
            String    fullName = _resourceLocator.GetFullPath(assetName);
            IResource resource = null;

            //First try: get the full name and see if its been loaded, if not try to locate the resource
            if (!parameters.OverrideCache && _loadedAssets.TryGetValue(fullName, out asset))
            {
                if (asset is T)
                {
                    return((T)asset);
                }
                else
                {
                    throw new InvalidCastException("Load error: Asset to retrieve does not match specified asset type.");
                }
            }
            else
            {
                resource = _resourceLocator.LocateResource(assetName);
            }

            //Second try, Try the default content
            if (resource == null)
            {
                if (!_useDefault)
                {
                    throw new ResourceNotFoundException(String.Format("Resource {0} could not be found", assetName));
                }
                try {
                    return(Engine.RenderSystemProvider.DefaultContent.Load <T>(assetName, parameters));
                } catch (Exception e) {
                    throw new ResourceNotFoundException(String.Format("Resource {0} could not be found", assetName), e.InnerException);
                }
            }

            //Find the appropiate resource loader
            Type           targetType = typeof(T);
            ResourceLoader loader     = _resourceLoaders[resource.Extension, targetType];

            if (loader == null)
            {
                throw new InvalidOperationException(String.Format("Error loading resource, no suitable loader for that {0} extension found.", resource.Extension));
            }

            //Load the asset
            asset = loader.Load(resource, targetType, parameters);
            if (!(asset is T))
            {
                throw new InvalidCastException("Load error: Asset to retrieve does not match specified asset type.");
            }

            //Cache the asset and return
            _loadedAssets[fullName] = asset;
            _resourceLocator        = oldLocator;
            return((T)asset);
        }