Bart van Strien avatar Bart van Strien committed d8c70ac

Make love.sound.newDecoder accept FileData, and make File->read return FileData

Comments (0)

Files changed (7)

src/modules/filesystem/File.h

 #include "common/Object.h"
 #include "common/StringMap.h"
 #include "common/int.h"
+#include "FileData.h"
 
 namespace love
 {
 	 * @param size The number of bytes to attempt reading, or -1 for EOF.
 	 * @return A newly allocated Data object.
 	 **/
-	virtual Data *read(int64 size = ALL) = 0;
+	virtual FileData *read(int64 size = ALL) = 0;
 
 	/**
 	 * Reads data into the destination buffer.

src/modules/filesystem/physfs/File.cpp

 }
 
 
-Data *File::read(int64 size)
+FileData *File::read(int64 size)
 {
 	bool isOpen = (file != 0);
 

src/modules/filesystem/physfs/File.h

 	bool open(Mode mode);
 	bool close();
 	int64 getSize();
-	Data *read(int64 size = ALL);
+	FileData *read(int64 size = ALL);
 	int64 read(void *dst, int64 size);
 	bool write(const void *data, int64 size);
 	bool write(const Data *data, int64 size = ALL);

src/modules/sound/Sound.h

 	 * @param sampleRate Samples per second, or quality of the audio. 44100 is a good value.
 	 * @return A Decoder object on success, or zero if no decoder could be found.
 	 **/
-	virtual Decoder *newDecoder(filesystem::File *file, int bufferSize) = 0;
+	virtual Decoder *newDecoder(filesystem::FileData *file, int bufferSize) = 0;
 
 }; // Sound
 
 } // sound
 } // love
 
-#endif // LOVE_SOUND_SOUND_H
+#endif // LOVE_SOUND_SOUND_H

src/modules/sound/lullaby/Sound.cpp

 	return "love.sound.lullaby";
 }
 
-sound::Decoder *Sound::newDecoder(love::filesystem::File *file, int bufferSize)
+sound::Decoder *Sound::newDecoder(love::filesystem::FileData *data, int bufferSize)
 {
-	Data *data = file->read();
-	std::string ext = file->getExtension();
+	std::string ext = data->getExtension();
 
 	sound::Decoder *decoder = 0;
 
 
 	// else if (OtherDecoder::accept(ext))
 
-	data->release();
-
 	return decoder;
 }
 

src/modules/sound/lullaby/Sound.h

 	const char *getName() const;
 
 	/// @copydoc love::sound::Sound::newDecoder
-	sound::Decoder *newDecoder(love::filesystem::File *file, int bufferSize);
+	sound::Decoder *newDecoder(love::filesystem::FileData *file, int bufferSize);
 
 }; // Sound
 
 } // sound
 } // love
 
-#endif // LOVE_SOUND_LULLABY_SOUND_H
+#endif // LOVE_SOUND_LULLABY_SOUND_H

src/modules/sound/wrap_Sound.cpp

 	if (lua_isstring(L, 1))
 		luax_convobj(L, 1, "filesystem", "newFile");
 
-	love::filesystem::File *file = luax_checktype<love::filesystem::File>(L, 1, "File", FILESYSTEM_FILE_T);
+	love::filesystem::FileData *data;
+	if (luax_istype(L, 1, FILESYSTEM_FILE_T))
+	{
+		love::filesystem::File *file = luax_checktype<love::filesystem::File>(L, 1, "File", FILESYSTEM_FILE_T);
+		data = file->read();
+	}
+	else
+	{
+		data = luax_checktype<love::filesystem::FileData>(L, 1, "FileData", FILESYSTEM_FILE_DATA_T);
+		data->retain();
+	}
+
 	int bufferSize = luaL_optint(L, 2, Decoder::DEFAULT_BUFFER_SIZE);
 
 	try
 	{
-		Decoder *t = instance->newDecoder(file, bufferSize);
+		Decoder *t = instance->newDecoder(data, bufferSize);
+		data->release();
 		if (t == 0)
-			return luaL_error(L, "Extension \"%s\" not supported.", file->getExtension().c_str());
+			return luaL_error(L, "Extension \"%s\" not supported.", data->getExtension().c_str());
 		luax_newtype(L, "Decoder", SOUND_DECODER_T, (void *)t);
 	}
 	catch(love::Exception &e)
 	{
+		data->release();
 		return luaL_error(L, e.what());
 	}
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.