Beispiel #1
0
        /// <summary>
        /// Loads the asset relative to another asset source. 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="relativeResource">Relative asset source</param>
        /// <param name="parameters">Loader parameters</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 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 LoadRelativeTo <T>(String assetName, IResource relativeResource, LoaderParameters parameters)
        {
            if (_loadedAssets == null)
            {
                throw new ObjectDisposedException("Content manager is disposed");
            }

            if (relativeResource == null)
            {
                throw new ArgumentNullException("Resource to use to locate relative resource cannot be null");
            }

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

            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.GetFullPathRelativeTo(assetName, relativeResource);
            IResource resource = null;

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

            //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(fullName);
            }

            Type targetType = typeof(T);
            //Find the appropiate resource loader
            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;
            return((T)asset);
        }