public object ReadContent(IContentManager readerManager, ref ContentReaderParameters parameters) { parameters.KeepStreamOpen = false; var image = Image.Load(parameters.Stream); if (image != null) image.Name = parameters.AssetName; return image; }
private object LoadAssetWithDynamicContentReader <T>(string assetName, Stream stream, object options) { object result; var type = typeof(T); var parameters = new ContentReaderParameters { AssetName = assetName, AssetType = type, Stream = stream, Options = options }; try { IContentReader contentReader; lock (registeredContentReaders) { if (!registeredContentReaders.TryGetValue(type, out contentReader)) { // Use registered factories to handle a type lock (registeredContentReaderFactories) { foreach (var factory in registeredContentReaderFactories) { contentReader = factory.TryCreate(type); if (contentReader != null) { registeredContentReaders.Add(type, contentReader); break; } } } // Else tries to get a ContentReaderAttribute to resolve the type if (contentReader == null) { #if WIN8METRO var contentReaderAttribute = Utilities.GetCustomAttribute <ContentReaderAttribute>(type.GetTypeInfo(), true); #else var contentReaderAttribute = Utilities.GetCustomAttribute <ContentReaderAttribute>(type, true); #endif if (contentReaderAttribute != null) { contentReader = Activator.CreateInstance(contentReaderAttribute.ContentReaderType) as IContentReader; if (contentReader != null) { Readers.Add(typeof(T), contentReader); } } } } } if (contentReader == null) { throw new NotSupportedException(string.Format("Type [{0}] doesn't provide a ContentReaderAttribute, and there is no registered content reader/factory for it.", type.FullName)); } result = contentReader.ReadContent(this, ref parameters); if (result == null) { throw new NotSupportedException(string.Format("Registered ContentReader of type [{0}] fails to load content of type [{1}] from file [{2}].", contentReader.GetType(), type.FullName, assetName)); } } finally { // If we don't need to keep the stream open, then we can close it // and make sure that we will close the stream even if there is an exception. if (!parameters.KeepStreamOpen) { stream.Dispose(); } } return(result); }
private object LoadAssetWithDynamicContentReader <T>(string assetName, Stream stream, object options) { object result = null; long startPosition = stream.Position; var parameters = new ContentReaderParameters() { AssetName = assetName, AssetType = typeof(T), Stream = stream, Options = options }; try { // Else try to load using a dynamic content reader attribute #if WIN8METRO var contentReaderAttribute = Utilities.GetCustomAttribute <ContentReaderAttribute>(typeof(T).GetTypeInfo(), true); #else var contentReaderAttribute = Utilities.GetCustomAttribute <ContentReaderAttribute>(typeof(T), true); #endif if (contentReaderAttribute != null) { IContentReader contentReader = null; #if WIN8METRO var isReaderValid = typeof(IContentReader).GetTypeInfo().IsAssignableFrom(contentReaderAttribute.ContentReaderType.GetTypeInfo()); #else var isReaderValid = typeof(IContentReader).IsAssignableFrom(contentReaderAttribute.ContentReaderType); #endif if (!isReaderValid) { throw new NotSupportedException(string.Format("Invalid content reader type [{0}]. Expecting an instance of IContentReader", contentReaderAttribute.ContentReaderType.FullName)); } lock (registeredContentReaders) { foreach (var reader in registeredContentReaders) { if (contentReaderAttribute.ContentReaderType == reader.GetType()) { contentReader = reader; break; } } if (contentReader == null) { contentReader = (IContentReader)Activator.CreateInstance(contentReaderAttribute.ContentReaderType); // If this content reader has been used successfully, then we can register it. lock (registeredContentReaders) { registeredContentReaders.Add(contentReader); } } } // Rewind position every time we try to load an asset stream.Position = startPosition; result = contentReader.ReadContent(this, ref parameters); stream.Position = startPosition; } else { // Try to load from registered content readers List <IContentReader> readers; lock (registeredContentReaders) { readers = new List <IContentReader>(registeredContentReaders); } foreach (IContentReader registeredContentReader in readers) { // Rewind position every time we try to load an asset result = registeredContentReader.ReadContent(this, ref parameters); stream.Position = startPosition; if (result != null) { break; } } } if (result == null) { throw new NotSupportedException("Unable to load content"); } } finally { // If we don't need to keep the stream open, then we can close it // and make sure that we will close the stream even if there is an exception. if (!parameters.KeepStreamOpen) { stream.Dispose(); } } return(result); }
object IContentReader.ReadContent(IContentManager contentManagerArg, ref ContentReaderParameters parameters) { return serializer.Deserialize(parameters.Stream, parameters.AssetType); }
public object ReadContent(IContentManager readerManager, ref ContentReaderParameters parameters) { parameters.KeepStreamOpen = false; return EffectData.Load(parameters.Stream); }