private T ReadAsset <T>(string assetName, System.IO.Stream assetStream, Action <IDisposable> recordDisposableObject) { // GG EDIT removed code for loading raw assets like pngs // Load a XNB file ContentReader reader = new ContentReader(this, assetStream, this.graphicsDeviceService.GraphicsDevice); ContentTypeReaderManager typeManager = new ContentTypeReaderManager(reader); reader.TypeReaders = typeManager.LoadAssetReaders(reader); foreach (ContentTypeReader r in reader.TypeReaders) { r.Initialize(typeManager); } // we need to read a byte here for things to work out, not sure why byte dummy = reader.ReadByte(); System.Diagnostics.Debug.Assert(dummy == 0); // Get the 1-based index of the typereader we should use to start decoding with int index = reader.ReadByte(); ContentTypeReader contentReader = reader.TypeReaders[index - 1]; object result = reader.ReadObject <T>(contentReader); reader.Close(); assetStream.Close(); if (result == null) { throw new ContentLoadException("Could not load " + assetName + " asset!"); } // GG EDIT added IDisposable recording T tresult = (T)result; if (tresult is IDisposable) { if (recordDisposableObject == null) { // GG TODO: would call local method here } else { recordDisposableObject((IDisposable)tresult); } } return(tresult); }
public T Load <T>(string assetName) { string originalAssetName = assetName; object result = null; if (this.graphicsDeviceService == null) { this.graphicsDeviceService = serviceProvider.GetService(typeof(IGraphicsDeviceService)) as IGraphicsDeviceService; if (this.graphicsDeviceService == null) { throw new InvalidOperationException("No Graphics Device Service"); } } if (string.IsNullOrEmpty(assetName)) { throw new ArgumentException("assetname"); } if (!string.IsNullOrEmpty(_rootDirectory)) { assetName = _rootDirectory + Path.DirectorySeparatorChar + assetName; } // Check for windows-style directory separator character assetName = assetName.Replace('\\', Path.DirectorySeparatorChar); // Get the real file name if ((typeof(T) == typeof(Texture2D))) { assetName = Texture2DReader.Normalize(assetName); } if ((typeof(T) == typeof(SpriteFont))) { assetName = SpriteFontReader.Normalize(assetName); } if ((typeof(T) == typeof(Song))) { assetName = SongReader.Normalize(assetName); } if ((typeof(T) == typeof(SoundEffect))) { assetName = SoundEffectReader.Normalize(assetName); } if ((typeof(T) == typeof(Video))) { assetName = Video.Normalize(assetName); } if ((typeof(T) == typeof(Effect))) { assetName = Effect.Normalize(assetName); } if (string.IsNullOrEmpty(assetName)) { throw new ContentLoadException("Could not load " + originalAssetName + " asset!"); } if (Path.GetExtension(assetName).ToUpper() != ".XNB") { if ((typeof(T) == typeof(Texture2D))) { result = Texture2D.FromFile(graphicsDeviceService.GraphicsDevice, assetName); } if ((typeof(T) == typeof(SpriteFont))) { //result = new SpriteFont(Texture2D.FromFile(graphicsDeviceService.GraphicsDevice,assetName), null, null, null, 0, 0.0f, null, null); throw new NotImplementedException(); } if ((typeof(T) == typeof(Song))) { result = new Song(assetName); } if ((typeof(T) == typeof(SoundEffect))) { result = new SoundEffect(assetName); } if ((typeof(T) == typeof(Video))) { result = new Video(assetName); } } else { // Load a XNB file FileStream stream = new FileStream(assetName, FileMode.Open, FileAccess.Read, FileShare.Read); ContentReader reader = new ContentReader(this, stream, this.graphicsDeviceService.GraphicsDevice); ContentTypeReaderManager typeManager = new ContentTypeReaderManager(reader); reader.TypeReaders = typeManager.LoadAssetReaders(reader); foreach (ContentTypeReader r in reader.TypeReaders) { r.Initialize(typeManager); } // we need to read a byte here for things to work out, not sure why reader.ReadByte(); // Get the 1-based index of the typereader we should use to start decoding with int index = reader.ReadByte(); ContentTypeReader contentReader = reader.TypeReaders[index - 1]; result = reader.ReadObject <T>(contentReader); reader.Close(); stream.Close(); } if (result == null) { throw new ContentLoadException("Could not load " + originalAssetName + " asset!"); } return((T)result); }
public T Load <T>(string assetName) { string originalAssetName = assetName; object result = null; if (this.graphicsDeviceService == null) { this.graphicsDeviceService = serviceProvider.GetService(typeof(IGraphicsDeviceService)) as IGraphicsDeviceService; if (this.graphicsDeviceService == null) { throw new InvalidOperationException("No Graphics Device Service"); } } // Check for windows-style directory separator character //Lowercase assetName (monodroid specification all assests are lowercase) assetName = Path.Combine(_rootDirectory, assetName.Replace('\\', Path.DirectorySeparatorChar)).ToLower(); // Get the real file name if ((typeof(T) == typeof(Texture2D))) { assetName = Texture2DReader.Normalize(assetName); } else if ((typeof(T) == typeof(SpriteFont))) { assetName = SpriteFontReader.Normalize(assetName); } else if ((typeof(T) == typeof(Effect))) { assetName = Effect.Normalize(assetName); } /*else if ((typeof(T) == typeof(Song))) * { * assetName = SongReader.Normalize(assetName); * } * else if ((typeof(T) == typeof(SoundEffect))) * { * assetName = SoundEffectReader.Normalize(assetName); * } * else if ((typeof(T) == typeof(Video))) * { * assetName = Video.Normalize(assetName); * }*/ else { throw new NotSupportedException("Format not supported"); } if (string.IsNullOrEmpty(assetName)) { throw new ContentLoadException("Could not load " + originalAssetName + " asset!"); } if (Path.GetExtension(assetName).ToUpper() != ".XNB") { if ((typeof(T) == typeof(Texture2D))) { //Basically the same as Texture2D.FromFile but loading from the assets instead of a filePath Stream assetStream = File.Open(assetName, FileMode.Open, FileAccess.Read); Bitmap image = (Bitmap)Bitmap.FromStream(assetStream); ESImage theTexture = new ESImage(image, graphicsDeviceService.GraphicsDevice.PreferedFilter); result = new Texture2D(theTexture) { Name = Path.GetFileNameWithoutExtension(assetName) }; } if ((typeof(T) == typeof(SpriteFont))) { //result = new SpriteFont(Texture2D.FromFile(graphicsDeviceService.GraphicsDevice,assetName), null, null, null, 0, 0.0f, null, null); throw new NotImplementedException(); } /*if ((typeof(T) == typeof(Song))) * result = new Song(assetName); * if ((typeof(T) == typeof(SoundEffect))) * result = new SoundEffect(assetName); * if ((typeof(T) == typeof(Video))) * result = new Video(assetName);*/ } else { // Load a XNB file //Loads from Assets directory + /assetName Stream assetStream = File.Open(assetName, FileMode.Open, FileAccess.Read); ContentReader reader = new ContentReader(this, assetStream, this.graphicsDeviceService.GraphicsDevice); ContentTypeReaderManager typeManager = new ContentTypeReaderManager(reader); reader.TypeReaders = typeManager.LoadAssetReaders(reader); foreach (ContentTypeReader r in reader.TypeReaders) { r.Initialize(typeManager); } // we need to read a byte here for things to work out, not sure why reader.ReadByte(); // Get the 1-based index of the typereader we should use to start decoding with int index = reader.ReadByte(); ContentTypeReader contentReader = reader.TypeReaders[index - 1]; result = reader.ReadObject <T>(contentReader); reader.Close(); assetStream.Close(); } if (result == null) { throw new ContentLoadException("Could not load " + originalAssetName + " asset!"); } return((T)result); }
public T Load <T>(string assetName) { string originalAssetName = assetName; object result = null; if (this.graphicsDeviceService == null) { this.graphicsDeviceService = serviceProvider.GetService(typeof(IGraphicsDeviceService)) as IGraphicsDeviceService; if (this.graphicsDeviceService == null) { throw new InvalidOperationException("No Graphics Device Service"); } } assetName = Path.Combine(_rootDirectory, assetName.Replace('\\', Path.DirectorySeparatorChar)); // Get the real file name if ((typeof(T) == typeof(Texture2D))) { assetName = Texture2DReader.Normalize(assetName); } else if ((typeof(T) == typeof(SpriteFont))) { assetName = SpriteFontReader.Normalize(assetName); } else if ((typeof(T) == typeof(Effect))) { assetName = Effect.Normalize(assetName); } else if ((typeof(T) == typeof(Song))) { assetName = SongReader.Normalize(assetName); } else if ((typeof(T) == typeof(SoundEffect))) { assetName = SoundEffectReader.Normalize(assetName); } else if ((typeof(T) == typeof(Video))) { assetName = Video.Normalize(assetName); } else { throw new NotSupportedException("Format not supported"); } if (string.IsNullOrEmpty(assetName)) { throw new ContentLoadException("Could not load " + originalAssetName + " asset!"); } if (Path.GetExtension(assetName).ToUpper() != ".XNB") { if ((typeof(T) == typeof(Texture2D))) { //Basically the same as Texture2D.FromFile but loading from the assets instead of a filePath Stream assetStream = Game.contextInstance.Assets.Open(assetName); Bitmap image = BitmapFactory.DecodeStream(assetStream); //Bitmap image = BitmapFactory.DecodeFileDescriptor(Game.contextInstance.Assets.OpenFd(assetName).FileDescriptor); ESImage theTexture; if (GraphicsDevice.openGLESVersion == OpenTK.Graphics.GLContextVersion.Gles2_0) { theTexture = new ESImage(image, graphicsDeviceService.GraphicsDevice.PreferedFilterGL20); } else { theTexture = new ESImage(image, graphicsDeviceService.GraphicsDevice.PreferedFilterGL11); } result = new Texture2D(theTexture) { Name = Path.GetFileNameWithoutExtension(assetName) }; } if ((typeof(T) == typeof(SpriteFont))) { //result = new SpriteFont(Texture2D.FromFile(graphicsDeviceService.GraphicsDevice,assetName), null, null, null, 0, 0.0f, null, null); throw new NotImplementedException(); } if ((typeof(T) == typeof(Song))) { result = new Song(assetName); } if ((typeof(T) == typeof(SoundEffect))) { result = new SoundEffect(assetName); } if ((typeof(T) == typeof(Video))) { result = new Video(assetName); } } else { // Load a XNB file //Loads from Assets directory + /assetName Stream assetStream = Game.contextInstance.Assets.Open(assetName); ContentReader reader = new ContentReader(this, assetStream, this.graphicsDeviceService.GraphicsDevice); ContentTypeReaderManager typeManager = new ContentTypeReaderManager(reader); reader.TypeReaders = typeManager.LoadAssetReaders(reader); foreach (ContentTypeReader r in reader.TypeReaders) { r.Initialize(typeManager); } // we need to read a byte here for things to work out, not sure why reader.ReadByte(); // Get the 1-based index of the typereader we should use to start decoding with int index = reader.ReadByte(); ContentTypeReader contentReader = reader.TypeReaders[index - 1]; result = reader.ReadObject <T>(contentReader); reader.Close(); assetStream.Close(); } if (result == null) { throw new ContentLoadException("Could not load " + originalAssetName + " asset!"); } return((T)result); }