Ejemplo n.º 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ExternalReference"/> class.
 /// </summary>
 /// <param name="fileName">The reference file name.</param>
 /// <param name="loaderParams">Loader parameters specifying optional parameters for loading the resource.</param>
 public ExternalReference(String fileName, LoaderParameters loaderParams)
 {
     if (String.IsNullOrEmpty(fileName))
     {
         throw new ArgumentNullException("Reference file name cannot be null!");
     }
     _fileName = fileName;
     if (loaderParams == null)
     {
         _loaderParams = LoaderParameters.None;
     }
     else
     {
         _loaderParams = loaderParams;
     }
 }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Loads the resource.
 /// </summary>
 /// <param name="resource">Resource to load</param>
 /// <param name="type">Type of resource</param>
 /// <param name="parameters">Loader parameters</param>
 /// <returns>Loaded object</returns>
 public abstract Object Load(IResource resource, Type type, LoaderParameters parameters);
Ejemplo n.º 4
0
 /// <summary>
 /// Deserializes the object and populates it from the input.
 /// </summary>
 /// <param name="input">Savable input</param>
 public void Read(ISavableReader input)
 {
     _fileName     = input.ReadString();
     _loaderParams = input.ReadSavable <LoaderParameters>();
 }