Browse Source

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

Bart van Strien 13 years ago
parent
commit
33d7115887

+ 2 - 1
src/modules/filesystem/File.h

@@ -29,6 +29,7 @@
 #include "common/Object.h"
 #include "common/StringMap.h"
 #include "common/int.h"
+#include "FileData.h"
 
 namespace love
 {
@@ -93,7 +94,7 @@ public:
 	 * @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.

+ 1 - 1
src/modules/filesystem/physfs/File.cpp

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

+ 1 - 1
src/modules/filesystem/physfs/File.h

@@ -69,7 +69,7 @@ public:
 	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);

+ 2 - 2
src/modules/sound/Sound.h

@@ -74,11 +74,11 @@ public:
 	 * @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

+ 2 - 5
src/modules/sound/lullaby/Sound.cpp

@@ -51,10 +51,9 @@ const char *Sound::getName() const
 	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;
 
@@ -76,8 +75,6 @@ sound::Decoder *Sound::newDecoder(love::filesystem::File *file, int bufferSize)
 
 	// else if (OtherDecoder::accept(ext))
 
-	data->release();
-
 	return decoder;
 }
 

+ 2 - 2
src/modules/sound/lullaby/Sound.h

@@ -59,7 +59,7 @@ public:
 	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
 
@@ -67,4 +67,4 @@ public:
 } // sound
 } // love
 
-#endif // LOVE_SOUND_LULLABY_SOUND_H
+#endif // LOVE_SOUND_LULLABY_SOUND_H

+ 16 - 3
src/modules/sound/wrap_Sound.cpp

@@ -83,18 +83,31 @@ int w_newDecoder(lua_State *L)
 	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());
 	}