From 17cf7bba83db9df3cecf9e8a3cd80cfaf99a59d8 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Sat, 29 Jul 2017 23:09:45 -0300 Subject: [PATCH] Cleaned up love.graphics.newShader's wrapper code a bit. --HG-- branch : minor --- src/modules/filesystem/physfs/File.cpp | 2 +- src/modules/filesystem/wrap_Filesystem.cpp | 6 ++--- src/modules/graphics/wrap_Graphics.cpp | 27 +++++++++++----------- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/modules/filesystem/physfs/File.cpp b/src/modules/filesystem/physfs/File.cpp index 0b2f47a34..75492af83 100644 --- a/src/modules/filesystem/physfs/File.cpp +++ b/src/modules/filesystem/physfs/File.cpp @@ -190,7 +190,7 @@ bool File::write(const void *data, int64 size) // Manually flush the buffer in BUFFER_LINE mode if we find a newline. if (bufferMode == BUFFER_LINE && bufferSize > size) { - if (memchr(data, '\n', (size_t) size) != NULL) + if (memchr(data, '\n', (size_t) size) != nullptr) flush(); } diff --git a/src/modules/filesystem/wrap_Filesystem.cpp b/src/modules/filesystem/wrap_Filesystem.cpp index 3d5a9542d..d44df9943 100644 --- a/src/modules/filesystem/wrap_Filesystem.cpp +++ b/src/modules/filesystem/wrap_Filesystem.cpp @@ -366,7 +366,7 @@ int w_read(lua_State *L) const char *filename = luaL_checkstring(L, 1); int64 len = (int64) luaL_optinteger(L, 2, File::ALL); - Data *data = 0; + Data *data = nullptr; try { data = instance()->read(filename, len); @@ -376,7 +376,7 @@ int w_read(lua_State *L) return luax_ioError(L, "%s", e.what()); } - if (data == 0) + if (data == nullptr) return luax_ioError(L, "File could not be read."); // Push the string. @@ -395,7 +395,7 @@ static int w_write_or_append(lua_State *L, File::Mode mode) { const char *filename = luaL_checkstring(L, 1); - const char *input = 0; + const char *input = nullptr; size_t len = 0; if (luax_istype(L, 2, love::Data::type)) diff --git a/src/modules/graphics/wrap_Graphics.cpp b/src/modules/graphics/wrap_Graphics.cpp index a27ee25e1..489a7925f 100644 --- a/src/modules/graphics/wrap_Graphics.cpp +++ b/src/modules/graphics/wrap_Graphics.cpp @@ -24,6 +24,7 @@ #include "image/ImageData.h" #include "image/Image.h" #include "font/Rasterizer.h" +#include "filesystem/Filesystem.h" #include "filesystem/wrap_Filesystem.h" #include "video/VideoStream.h" #include "image/wrap_Image.h" @@ -1190,34 +1191,32 @@ static int w_getShaderSource(lua_State *L, int startidx, bool gles, Shader::Shad { luax_checkgraphicscreated(L); + auto fs = Module::getInstance(Module::M_FILESYSTEM); + // read any filepath arguments for (int i = startidx; i < startidx + 2; i++) { if (!lua_isstring(L, i)) continue; - // call love.filesystem.isFile(arg_i) - luax_getfunction(L, "filesystem", "isFile"); - lua_pushvalue(L, i); - lua_call(L, 1, 1); + size_t slen = 0; + const char *str = lua_tolstring(L, i, &slen); - bool isFile = luax_toboolean(L, -1); - lua_pop(L, 1); - - if (isFile) + if (fs != nullptr && fs->isFile(str)) { - luax_getfunction(L, "filesystem", "read"); - lua_pushvalue(L, i); - lua_call(L, 1, 1); + filesystem::FileData *fd = nullptr; + luax_catchexcept(L, [&](){ fd = fs->read(str); }); + + lua_pushlstring(L, (const char *) fd->getData(), fd->getSize()); + fd->release(); + lua_replace(L, i); } else { // Check if the argument looks like a filepath - we want a nicer // error for misspelled filepath arguments. - size_t slen = 0; - const char *str = lua_tolstring(L, i, &slen); - if (slen > 0 && slen < 256 && !strchr(str, '\n')) + if (slen > 0 && slen < 64 && !strchr(str, '\n')) { const char *ext = strchr(str, '.'); if (ext != nullptr && !strchr(ext, ';') && !strchr(ext, ' '))